xref: /titanic_50/usr/src/cmd/cpc/common/cputrack.c (revision 0185ddb4dc51321503424e4f04bd721a063fd5a2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c3666b4Skk112340  * Common Development and Distribution License (the "License").
67c3666b4Skk112340  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*0185ddb4SKuriakose Kuruvilla  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/time.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <inttypes.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <limits.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <locale.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <kstat.h>
397c478bd9Sstevel@tonic-gate #include <libcpc.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include "cpucmds.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static struct options {
447c478bd9Sstevel@tonic-gate 	int debug;
457c478bd9Sstevel@tonic-gate 	int verbose;
467c478bd9Sstevel@tonic-gate 	int dotitle;
477c478bd9Sstevel@tonic-gate 	int dohelp;
487c478bd9Sstevel@tonic-gate 	int dotick;
497c478bd9Sstevel@tonic-gate 	int cpuver;
507c478bd9Sstevel@tonic-gate 	char *pgmname;
517c478bd9Sstevel@tonic-gate 	uint_t mseconds;
527c478bd9Sstevel@tonic-gate 	uint_t nsamples;
537c478bd9Sstevel@tonic-gate 	uint_t nsets;
547c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *master;
557c478bd9Sstevel@tonic-gate 	int followfork;
567c478bd9Sstevel@tonic-gate 	int followexec;
577c478bd9Sstevel@tonic-gate 	pid_t pid;
587c478bd9Sstevel@tonic-gate 	FILE *log;
597c478bd9Sstevel@tonic-gate } __options;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static const struct options *opts = (const struct options *)&__options;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static cpc_t *cpc;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
667c478bd9Sstevel@tonic-gate static void
677c478bd9Sstevel@tonic-gate cputrack_errfn(const char *fn, int subcode, const char *fmt, va_list ap)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", opts->pgmname);
707c478bd9Sstevel@tonic-gate 	if (opts->debug)
717c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", fn);
727c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static void
767c478bd9Sstevel@tonic-gate cputrack_pctx_errfn(const char *fn, const char *fmt, va_list ap)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	cputrack_errfn(fn, -1, fmt, ap);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static int cputrack(int argc, char *argv[], int optind);
828d4e547dSae112802 #if defined(__i386)
837c478bd9Sstevel@tonic-gate static void p4_ht_error(void);
848d4e547dSae112802 #endif
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
877c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
887c478bd9Sstevel@tonic-gate #endif
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate int
917c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	struct options *opts = &__options;
947c478bd9Sstevel@tonic-gate 	int c, errcnt = 0;
957c478bd9Sstevel@tonic-gate 	int nsamples;
967c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *sgrp;
977c478bd9Sstevel@tonic-gate 	char *errstr;
987c478bd9Sstevel@tonic-gate 	int ret;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1017c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	if ((opts->pgmname = strrchr(argv[0], '/')) == NULL)
1047c478bd9Sstevel@tonic-gate 		opts->pgmname = argv[0];
1057c478bd9Sstevel@tonic-gate 	else
1067c478bd9Sstevel@tonic-gate 		opts->pgmname++;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if ((cpc = cpc_open(CPC_VER_CURRENT)) == NULL) {
1097c478bd9Sstevel@tonic-gate 		errstr = strerror(errno);
1107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: cannot access performance "
1117c478bd9Sstevel@tonic-gate 		    "counter library - %s\n"), opts->pgmname, errstr);
1127c478bd9Sstevel@tonic-gate 		return (1);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	(void) cpc_seterrhndlr(cpc, cputrack_errfn);
1167c478bd9Sstevel@tonic-gate 	strtoset_errfn = cputrack_errfn;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	/*
1197c478bd9Sstevel@tonic-gate 	 * Establish (non-zero) defaults
1207c478bd9Sstevel@tonic-gate 	 */
1217c478bd9Sstevel@tonic-gate 	opts->mseconds = 1000;
1227c478bd9Sstevel@tonic-gate 	opts->dotitle = 1;
1237c478bd9Sstevel@tonic-gate 	opts->log = stdout;
1247c478bd9Sstevel@tonic-gate 	if ((opts->master = cpc_setgrp_new(cpc, 0)) == NULL) {
1257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
1267c478bd9Sstevel@tonic-gate 		    opts->pgmname);
1277c478bd9Sstevel@tonic-gate 		exit(1);
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "T:N:Defhntvo:r:c:p:")) != EOF)
1317c478bd9Sstevel@tonic-gate 		switch (c) {
1327c478bd9Sstevel@tonic-gate 		case 'T':			/* sample time,	seconds */
1337c478bd9Sstevel@tonic-gate 			opts->mseconds = (uint_t)(atof(optarg) * 1000.0);
1347c478bd9Sstevel@tonic-gate 			break;
1357c478bd9Sstevel@tonic-gate 		case 'N':			/* number of samples */
1367c478bd9Sstevel@tonic-gate 			nsamples = atoi(optarg);
1377c478bd9Sstevel@tonic-gate 			if (nsamples < 0)
1387c478bd9Sstevel@tonic-gate 				errcnt++;
1397c478bd9Sstevel@tonic-gate 			else
1407c478bd9Sstevel@tonic-gate 				opts->nsamples = (uint_t)nsamples;
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 		case 'D':			/* enable debugging */
1437c478bd9Sstevel@tonic-gate 			opts->debug++;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		case 'f':			/* follow fork */
1467c478bd9Sstevel@tonic-gate 			opts->followfork++;
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		case 'e':			/* follow exec */
1497c478bd9Sstevel@tonic-gate 			opts->followexec++;
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 		case 'n':			/* no titles */
1527c478bd9Sstevel@tonic-gate 			opts->dotitle = 0;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		case 't':			/* print %tick */
1557c478bd9Sstevel@tonic-gate 			opts->dotick = 1;
1567c478bd9Sstevel@tonic-gate 			break;
1577c478bd9Sstevel@tonic-gate 		case 'v':
1587c478bd9Sstevel@tonic-gate 			opts->verbose = 1;	/* more chatty */
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 		case 'o':
1617c478bd9Sstevel@tonic-gate 			if (optarg == NULL) {
1627c478bd9Sstevel@tonic-gate 				errcnt++;
1637c478bd9Sstevel@tonic-gate 				break;
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 			if ((opts->log = fopen(optarg, "w")) == NULL) {
1667c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
1677c478bd9Sstevel@tonic-gate 				    "%s: cannot open '%s' for writing\n"),
1687c478bd9Sstevel@tonic-gate 				    opts->pgmname, optarg);
1697c478bd9Sstevel@tonic-gate 				return (1);
1707c478bd9Sstevel@tonic-gate 			}
1717c478bd9Sstevel@tonic-gate 			break;
1727c478bd9Sstevel@tonic-gate 		case 'c':			/* specify statistics */
1737c478bd9Sstevel@tonic-gate 			if ((sgrp = cpc_setgrp_newset(opts->master,
1747c478bd9Sstevel@tonic-gate 			    optarg, &errcnt)) != NULL)
1757c478bd9Sstevel@tonic-gate 				opts->master = sgrp;
1767c478bd9Sstevel@tonic-gate 			break;
1777c478bd9Sstevel@tonic-gate 		case 'p':			/* grab given pid */
1787c478bd9Sstevel@tonic-gate 			if ((opts->pid = atoi(optarg)) <= 0)
1797c478bd9Sstevel@tonic-gate 				errcnt++;
1807c478bd9Sstevel@tonic-gate 			break;
1817c478bd9Sstevel@tonic-gate 		case 'h':
1827c478bd9Sstevel@tonic-gate 			opts->dohelp = 1;
1837c478bd9Sstevel@tonic-gate 			break;
1847c478bd9Sstevel@tonic-gate 		case '?':
1857c478bd9Sstevel@tonic-gate 		default:
1867c478bd9Sstevel@tonic-gate 			errcnt++;
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (opts->nsamples == 0)
1917c478bd9Sstevel@tonic-gate 		opts->nsamples = UINT_MAX;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if (errcnt != 0 ||
1947c478bd9Sstevel@tonic-gate 	    opts->dohelp ||
1957c478bd9Sstevel@tonic-gate 	    (argc == optind && opts->pid == 0) ||
1967c478bd9Sstevel@tonic-gate 	    (argc > optind && opts->pid != 0) ||
1977c478bd9Sstevel@tonic-gate 	    (opts->nsets = cpc_setgrp_numsets(opts->master)) == 0) {
1987c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->dohelp ? stdout : stderr, gettext(
1997c478bd9Sstevel@tonic-gate 		    "Usage:\n\t%s [-T secs] [-N count] [-Defhnv] [-o file]\n"
2007c478bd9Sstevel@tonic-gate 		    "\t\t-c events [command [args] | -p pid]\n\n"
2017c478bd9Sstevel@tonic-gate 		    "\t-T secs\t  seconds between samples, default 1\n"
2027c478bd9Sstevel@tonic-gate 		    "\t-N count  number of samples, default unlimited\n"
2037c478bd9Sstevel@tonic-gate 		    "\t-D\t  enable debug mode\n"
2047c478bd9Sstevel@tonic-gate 		    "\t-e\t  follow exec(2), and execve(2)\n"
2057c478bd9Sstevel@tonic-gate 		    "\t-f\t  follow fork(2), fork1(2), and vfork(2)\n"
2067c478bd9Sstevel@tonic-gate 		    "\t-h\t  print extended usage information\n"
2077c478bd9Sstevel@tonic-gate 		    "\t-n\t  suppress titles\n"
2087c478bd9Sstevel@tonic-gate 		    "\t-t\t  include virtualized %s register\n"
2097c478bd9Sstevel@tonic-gate 		    "\t-v\t  verbose mode\n"
2107c478bd9Sstevel@tonic-gate 		    "\t-o file\t  write cpu statistics to this file\n"
2117c478bd9Sstevel@tonic-gate 		    "\t-c events specify processor events to be monitored\n"
2127c478bd9Sstevel@tonic-gate 		    "\t-p pid\t  pid of existing process to capture\n\n"
2137c478bd9Sstevel@tonic-gate 		    "\tUse cpustat(1M) to monitor system-wide statistics.\n"),
2147c478bd9Sstevel@tonic-gate 		    opts->pgmname, CPC_TICKREG_NAME);
2157c478bd9Sstevel@tonic-gate 		if (opts->dohelp) {
2167c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
2177c478bd9Sstevel@tonic-gate 			(void) capabilities(cpc, stdout);
2187c478bd9Sstevel@tonic-gate 			exit(0);
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 		exit(2);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	cpc_setgrp_reset(opts->master);
2247c478bd9Sstevel@tonic-gate 	(void) setvbuf(opts->log, NULL, _IOLBF, 0);
2257c478bd9Sstevel@tonic-gate 	ret = cputrack(argc, argv, optind);
2267c478bd9Sstevel@tonic-gate 	(void) cpc_close(cpc);
2277c478bd9Sstevel@tonic-gate 	return (ret);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static void
2317c478bd9Sstevel@tonic-gate print_title(cpc_setgrp_t *sgrp)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7s ", "time");
2347c478bd9Sstevel@tonic-gate 	if (opts->followfork)
2357c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6s ", "pid");
2367c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3s %10s ", "lwp", "event");
2377c478bd9Sstevel@tonic-gate 	if (opts->dotick)
2387c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9s ", CPC_TICKREG_NAME);
2397c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%s\n", cpc_setgrp_gethdr(sgrp));
2407c478bd9Sstevel@tonic-gate 	(void) fflush(opts->log);
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate static void
2447c478bd9Sstevel@tonic-gate print_exec(float now, pid_t pid, char *name)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	if (name == NULL)
2477c478bd9Sstevel@tonic-gate 		name = "(unknown)";
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7.3f ", now);
2507c478bd9Sstevel@tonic-gate 	if (opts->followfork)
2517c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6d ", (int)pid);
2527c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3d %10s ", 1, "exec");
2537c478bd9Sstevel@tonic-gate 	if (opts->dotick)
2547c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9s ", "");
2557c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%9s %9s # '%s'\n", "", "", name);
2567c478bd9Sstevel@tonic-gate 	(void) fflush(opts->log);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate static void
2607c478bd9Sstevel@tonic-gate print_fork(float now, pid_t newpid, id_t lwpid, pid_t oldpid)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7.3f ", now);
2637c478bd9Sstevel@tonic-gate 	if (opts->followfork)
2647c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6d ", (int)oldpid);
2657c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3d %10s ", (int)lwpid, "fork");
2667c478bd9Sstevel@tonic-gate 	if (opts->dotick)
2677c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9s ", "");
2687c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%9s %9s # %d\n", "", "", (int)newpid);
2697c478bd9Sstevel@tonic-gate 	(void) fflush(opts->log);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate static void
2737c478bd9Sstevel@tonic-gate print_sample(pid_t pid, id_t lwpid,
2747c478bd9Sstevel@tonic-gate     char *pevent, cpc_buf_t *buf, int nreq, const char *evname)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	uint64_t	val;
2777c478bd9Sstevel@tonic-gate 	int		i;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7.3f ",
2807c478bd9Sstevel@tonic-gate 	    mstimestamp(cpc_buf_hrtime(cpc, buf)));
2817c478bd9Sstevel@tonic-gate 	if (opts->followfork)
2827c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6d ", (int)pid);
2837c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3d %10s ", (int)lwpid, pevent);
2847c478bd9Sstevel@tonic-gate 	if (opts->dotick)
2857c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9" PRId64 " ",
2867c478bd9Sstevel@tonic-gate 		    cpc_buf_tick(cpc, buf));
2877c478bd9Sstevel@tonic-gate 	for (i = 0; i < nreq; i++) {
2887c478bd9Sstevel@tonic-gate 		(void) cpc_buf_get(cpc, buf, i, &val);
2897c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9" PRId64 " ", val);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	if (opts->nsets > 1)
2927c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, " # %s\n", evname);
2937c478bd9Sstevel@tonic-gate 	else
2947c478bd9Sstevel@tonic-gate 		(void) fputc('\n', opts->log);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate struct pstate {
2987c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *accum;
2997c478bd9Sstevel@tonic-gate 	cpc_setgrp_t **sgrps;
3007c478bd9Sstevel@tonic-gate 	int maxlwpid;
3017c478bd9Sstevel@tonic-gate };
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate static int
3047c478bd9Sstevel@tonic-gate pinit_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	struct pstate *state = arg;
3077c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *sgrp;
3087c478bd9Sstevel@tonic-gate 	cpc_set_t *set;
3097c478bd9Sstevel@tonic-gate 	cpc_buf_t **data1, **data2, **scratch;
3107c478bd9Sstevel@tonic-gate 	char *errstr;
3117c478bd9Sstevel@tonic-gate 	int nreq;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if (state->maxlwpid < lwpid) {
3147c478bd9Sstevel@tonic-gate 		state->sgrps = realloc(state->sgrps,
3157c478bd9Sstevel@tonic-gate 		    lwpid * sizeof (state->sgrps));
3167c478bd9Sstevel@tonic-gate 		if (state->sgrps == NULL) {
3177c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3187c478bd9Sstevel@tonic-gate 			    "%6d: init_lwp: out of memory\n"), (int)pid);
3197c478bd9Sstevel@tonic-gate 			return (-1);
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 		while (state->maxlwpid < lwpid) {
3227c478bd9Sstevel@tonic-gate 			state->sgrps[state->maxlwpid] = NULL;
3237c478bd9Sstevel@tonic-gate 			state->maxlwpid++;
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	if ((sgrp = state->sgrps[lwpid-1]) == NULL) {
3287c478bd9Sstevel@tonic-gate 		if ((sgrp = cpc_setgrp_clone(opts->master)) == NULL) {
3297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3307c478bd9Sstevel@tonic-gate 			    "%6d: init_lwp: out of memory\n"), (int)pid);
3317c478bd9Sstevel@tonic-gate 			return (-1);
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		state->sgrps[lwpid-1] = sgrp;
3347c478bd9Sstevel@tonic-gate 		set = cpc_setgrp_getset(sgrp);
3357c478bd9Sstevel@tonic-gate 	} else {
3367c478bd9Sstevel@tonic-gate 		cpc_setgrp_reset(sgrp);
3377c478bd9Sstevel@tonic-gate 		set = cpc_setgrp_getset(sgrp);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (cpc_bind_pctx(cpc, pctx, lwpid, set, 0) != 0 ||
3437c478bd9Sstevel@tonic-gate 	    cpc_set_sample(cpc, set, *data2) != 0) {
3447c478bd9Sstevel@tonic-gate 		errstr = strerror(errno);
3457c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN)
3467c478bd9Sstevel@tonic-gate 			(void) cpc_unbind(cpc, set);
3478d4e547dSae112802 #if defined(__i386)
3487c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
3497c478bd9Sstevel@tonic-gate 			p4_ht_error();
3507c478bd9Sstevel@tonic-gate 		else
3518d4e547dSae112802 #endif
3527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3537c478bd9Sstevel@tonic-gate 			    "%6d: init_lwp: can't bind perf counters "
3547c478bd9Sstevel@tonic-gate 			    "to lwp%d - %s\n"), (int)pid, (int)lwpid,
3557c478bd9Sstevel@tonic-gate 			    errstr);
3567c478bd9Sstevel@tonic-gate 		return (-1);
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if (opts->verbose)
3607c478bd9Sstevel@tonic-gate 		print_sample(pid, lwpid, "init_lwp",
3617c478bd9Sstevel@tonic-gate 		    *data2, nreq, cpc_setgrp_getname(sgrp));
3627c478bd9Sstevel@tonic-gate 	return (0);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3667c478bd9Sstevel@tonic-gate static int
3677c478bd9Sstevel@tonic-gate pfini_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
3687c478bd9Sstevel@tonic-gate {
3697c478bd9Sstevel@tonic-gate 	struct pstate *state = arg;
3707c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
3717c478bd9Sstevel@tonic-gate 	cpc_set_t *set;
3727c478bd9Sstevel@tonic-gate 	char *errstr;
3737c478bd9Sstevel@tonic-gate 	cpc_buf_t **data1, **data2, **scratch;
3747c478bd9Sstevel@tonic-gate 	int nreq;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	set = cpc_setgrp_getset(sgrp);
3777c478bd9Sstevel@tonic-gate 	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
378*0185ddb4SKuriakose Kuruvilla 	if (cpc_set_sample(cpc, set, *scratch) == 0) {
379*0185ddb4SKuriakose Kuruvilla 		if (opts->nsets == 1) {
380*0185ddb4SKuriakose Kuruvilla 			/*
381*0185ddb4SKuriakose Kuruvilla 			 * When we only have one set of counts, the sample
382*0185ddb4SKuriakose Kuruvilla 			 * gives us the accumulated count.
383*0185ddb4SKuriakose Kuruvilla 			 */
384*0185ddb4SKuriakose Kuruvilla 			*data1 = *scratch;
385*0185ddb4SKuriakose Kuruvilla 		} else {
386*0185ddb4SKuriakose Kuruvilla 			/*
387*0185ddb4SKuriakose Kuruvilla 			 * When we have more than one set of counts, the
388*0185ddb4SKuriakose Kuruvilla 			 * sample gives us the count for the latest sample
389*0185ddb4SKuriakose Kuruvilla 			 * period. *data1 contains the accumulated count but
390*0185ddb4SKuriakose Kuruvilla 			 * does not include the count for the latest sample
391*0185ddb4SKuriakose Kuruvilla 			 * period for this set of counters.
392*0185ddb4SKuriakose Kuruvilla 			 */
393*0185ddb4SKuriakose Kuruvilla 			cpc_buf_add(cpc, *data1, *data1, *scratch);
394*0185ddb4SKuriakose Kuruvilla 		}
3957c478bd9Sstevel@tonic-gate 		if (opts->verbose)
3967c478bd9Sstevel@tonic-gate 			print_sample(pid, lwpid, "fini_lwp",
3977c478bd9Sstevel@tonic-gate 			    *data1, nreq, cpc_setgrp_getname(sgrp));
3987c478bd9Sstevel@tonic-gate 		cpc_setgrp_accum(state->accum, sgrp);
3997c478bd9Sstevel@tonic-gate 		if (cpc_unbind(cpc, set) == 0)
4007c478bd9Sstevel@tonic-gate 			return (0);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	switch (errno) {
4047c478bd9Sstevel@tonic-gate 	case EAGAIN:
4057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%6d: fini_lwp: "
4067c478bd9Sstevel@tonic-gate 		    "lwp%d: perf counter contents invalidated\n"),
4077c478bd9Sstevel@tonic-gate 		    (int)pid, (int)lwpid);
4087c478bd9Sstevel@tonic-gate 		break;
4097c478bd9Sstevel@tonic-gate 	default:
4107c478bd9Sstevel@tonic-gate 		errstr = strerror(errno);
4117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%6d: fini_lwp: "
4127c478bd9Sstevel@tonic-gate 		    "lwp%d: can't access perf counters - %s\n"),
4137c478bd9Sstevel@tonic-gate 		    (int)pid, (int)lwpid, errstr);
4147c478bd9Sstevel@tonic-gate 		break;
4157c478bd9Sstevel@tonic-gate 	}
4167c3666b4Skk112340 	return (-1);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4207c478bd9Sstevel@tonic-gate static int
4217c478bd9Sstevel@tonic-gate plwp_create(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	cpc_setgrp_t	*sgrp = opts->master;
4247c478bd9Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
4257c478bd9Sstevel@tonic-gate 	int		nreq;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	print_sample(pid, lwpid, "lwp_create",
4307c478bd9Sstevel@tonic-gate 	    *data1, nreq, cpc_setgrp_getname(sgrp));
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	return (0);
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4367c478bd9Sstevel@tonic-gate static int
4377c478bd9Sstevel@tonic-gate plwp_exit(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	struct pstate	*state = arg;
4407c478bd9Sstevel@tonic-gate 	cpc_setgrp_t	*sgrp = state->sgrps[lwpid-1];
4417c478bd9Sstevel@tonic-gate 	cpc_set_t	*start;
4427c478bd9Sstevel@tonic-gate 	int		nreq;
4437c478bd9Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	start = cpc_setgrp_getset(sgrp);
4467c478bd9Sstevel@tonic-gate 	do {
4477c478bd9Sstevel@tonic-gate 		nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4487c478bd9Sstevel@tonic-gate 		if (cpc_buf_hrtime(cpc, *data1) == 0)
4497c478bd9Sstevel@tonic-gate 			continue;
4507c478bd9Sstevel@tonic-gate 		print_sample(pid, lwpid, "lwp_exit",
4517c478bd9Sstevel@tonic-gate 		    *data1, nreq, cpc_setgrp_getname(sgrp));
4527c478bd9Sstevel@tonic-gate 	} while (cpc_setgrp_nextset(sgrp) != start);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	return (0);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4587c478bd9Sstevel@tonic-gate static int
4597c478bd9Sstevel@tonic-gate pexec(pctx_t *pctx, pid_t pid, id_t lwpid, char *name, void *arg)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	struct pstate	*state = arg;
4627c478bd9Sstevel@tonic-gate 	float		now = 0.0;
4637c478bd9Sstevel@tonic-gate 	cpc_set_t	*start;
4647c478bd9Sstevel@tonic-gate 	int		nreq;
4657c478bd9Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
4667c478bd9Sstevel@tonic-gate 	hrtime_t	hrt;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	/*
4697c478bd9Sstevel@tonic-gate 	 * Print the accumulated results from the previous program image
4707c478bd9Sstevel@tonic-gate 	 */
4717c478bd9Sstevel@tonic-gate 	cpc_setgrp_reset(state->accum);
4727c478bd9Sstevel@tonic-gate 	start = cpc_setgrp_getset(state->accum);
4737c478bd9Sstevel@tonic-gate 	do {
4747c478bd9Sstevel@tonic-gate 		nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
4757c478bd9Sstevel@tonic-gate 		    &scratch);
4767c478bd9Sstevel@tonic-gate 		hrt = cpc_buf_hrtime(cpc, *data1);
4777c478bd9Sstevel@tonic-gate 		if (hrt == 0)
4787c478bd9Sstevel@tonic-gate 			continue;
4797c478bd9Sstevel@tonic-gate 		print_sample(pid, lwpid, "exec",
4807c478bd9Sstevel@tonic-gate 		    *data1, nreq, cpc_setgrp_getname(state->accum));
4817c478bd9Sstevel@tonic-gate 		if (now < mstimestamp(hrt))
4827c478bd9Sstevel@tonic-gate 			now = mstimestamp(hrt);
4837c478bd9Sstevel@tonic-gate 	} while (cpc_setgrp_nextset(state->accum) != start);
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	print_exec(now, pid, name);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	if (state->accum != NULL) {
4887c478bd9Sstevel@tonic-gate 		cpc_setgrp_free(state->accum);
4897c478bd9Sstevel@tonic-gate 		state->accum = NULL;
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if (opts->followexec) {
4937c478bd9Sstevel@tonic-gate 		state->accum = cpc_setgrp_clone(opts->master);
4947c478bd9Sstevel@tonic-gate 		return (0);
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 	return (-1);
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5007c478bd9Sstevel@tonic-gate static void
5017c478bd9Sstevel@tonic-gate pexit(pctx_t *pctx, pid_t pid, id_t lwpid, int status, void *arg)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	struct pstate	*state = arg;
5047c478bd9Sstevel@tonic-gate 	cpc_set_t	*start;
5057c478bd9Sstevel@tonic-gate 	int		nreq;
5067c478bd9Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	cpc_setgrp_reset(state->accum);
5097c478bd9Sstevel@tonic-gate 	start = cpc_setgrp_getset(state->accum);
5107c478bd9Sstevel@tonic-gate 	do {
5117c478bd9Sstevel@tonic-gate 		nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
5127c478bd9Sstevel@tonic-gate 		    &scratch);
5137c478bd9Sstevel@tonic-gate 		if (cpc_buf_hrtime(cpc, *data1) == 0)
5147c478bd9Sstevel@tonic-gate 			continue;
5157c478bd9Sstevel@tonic-gate 		print_sample(pid, lwpid, "exit",
5167c478bd9Sstevel@tonic-gate 		    *data1, nreq, cpc_setgrp_getname(state->accum));
5177c478bd9Sstevel@tonic-gate 	} while (cpc_setgrp_nextset(state->accum) != start);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	cpc_setgrp_free(state->accum);
5207c478bd9Sstevel@tonic-gate 	state->accum = NULL;
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	for (lwpid = 1; lwpid < state->maxlwpid; lwpid++)
5237c478bd9Sstevel@tonic-gate 		if (state->sgrps[lwpid-1] != NULL) {
5247c478bd9Sstevel@tonic-gate 			cpc_setgrp_free(state->sgrps[lwpid-1]);
5257c478bd9Sstevel@tonic-gate 			state->sgrps[lwpid-1] = NULL;
5267c478bd9Sstevel@tonic-gate 		}
5277c478bd9Sstevel@tonic-gate 	free(state->sgrps);
5287c478bd9Sstevel@tonic-gate 	state->sgrps = NULL;
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate static int
5327c478bd9Sstevel@tonic-gate ptick(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
5337c478bd9Sstevel@tonic-gate {
5347c478bd9Sstevel@tonic-gate 	struct pstate *state = arg;
5357c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
5367c478bd9Sstevel@tonic-gate 	cpc_set_t *this = cpc_setgrp_getset(sgrp);
5377c478bd9Sstevel@tonic-gate 	const char *name = cpc_setgrp_getname(sgrp);
5387c478bd9Sstevel@tonic-gate 	cpc_buf_t **data1, **data2, **scratch, *tmp;
5397c478bd9Sstevel@tonic-gate 	char *errstr;
5407c478bd9Sstevel@tonic-gate 	int nreqs;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (opts->nsets == 1) {
5457c478bd9Sstevel@tonic-gate 		/*
5467c478bd9Sstevel@tonic-gate 		 * If we're dealing with one set, buffer usage is:
5477c478bd9Sstevel@tonic-gate 		 *
5487c478bd9Sstevel@tonic-gate 		 * data1 = most recent data snapshot
5497c478bd9Sstevel@tonic-gate 		 * data2 = previous data snapshot
5507c478bd9Sstevel@tonic-gate 		 * scratch = used for diffing data1 and data2
5517c478bd9Sstevel@tonic-gate 		 *
5527c478bd9Sstevel@tonic-gate 		 * Save the snapshot from the previous sample in data2
5537c478bd9Sstevel@tonic-gate 		 * before putting the current sample in data1.
5547c478bd9Sstevel@tonic-gate 		 */
5557c478bd9Sstevel@tonic-gate 		tmp = *data1;
5567c478bd9Sstevel@tonic-gate 		*data1 = *data2;
5577c478bd9Sstevel@tonic-gate 		*data2 = tmp;
5587c478bd9Sstevel@tonic-gate 		if (cpc_set_sample(cpc, this, *data1) != 0)
5597c478bd9Sstevel@tonic-gate 			goto broken;
5607c478bd9Sstevel@tonic-gate 		cpc_buf_sub(cpc, *scratch, *data1, *data2);
5617c478bd9Sstevel@tonic-gate 	} else {
5627c478bd9Sstevel@tonic-gate 		cpc_set_t *next = cpc_setgrp_nextset(sgrp);
5637c478bd9Sstevel@tonic-gate 		/*
5647c478bd9Sstevel@tonic-gate 		 * If there is more than set in use, we will need to
5657c478bd9Sstevel@tonic-gate 		 * unbind and re-bind on each go-around because each
5667c478bd9Sstevel@tonic-gate 		 * time a counter is bound, it is preset to 0 (as it was
5677c478bd9Sstevel@tonic-gate 		 * specified when the requests were added to the set).
5687c478bd9Sstevel@tonic-gate 		 *
5697c478bd9Sstevel@tonic-gate 		 * Buffer usage in this case is:
5707c478bd9Sstevel@tonic-gate 		 *
5717c478bd9Sstevel@tonic-gate 		 * data1 = total counts for this set since program began
5727c478bd9Sstevel@tonic-gate 		 * data2 = unused
5737c478bd9Sstevel@tonic-gate 		 * scratch = most recent data snapshot
5747c478bd9Sstevel@tonic-gate 		 */
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		if (cpc_set_sample(cpc, this, *scratch) != 0)
5777c478bd9Sstevel@tonic-gate 			goto broken;
5787c478bd9Sstevel@tonic-gate 		cpc_buf_add(cpc, *data1, *data1, *scratch);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		/*
5817c478bd9Sstevel@tonic-gate 		 * No need to unbind the previous set, as binding another set
5827c478bd9Sstevel@tonic-gate 		 * automatically unbinds the most recently bound set.
5837c478bd9Sstevel@tonic-gate 		 */
5847c478bd9Sstevel@tonic-gate 		if (cpc_bind_pctx(cpc, pctx, lwpid, next, 0) != 0)
5857c478bd9Sstevel@tonic-gate 			goto broken;
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 	print_sample(pid, lwpid, "tick", *scratch, nreqs, name);
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	return (0);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate broken:
5927c478bd9Sstevel@tonic-gate 	switch (errno) {
5937c478bd9Sstevel@tonic-gate 	case EAGAIN:
5947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5957c478bd9Sstevel@tonic-gate 		    "%6d: tick: lwp%d: perf counter contents invalidated\n"),
5967c478bd9Sstevel@tonic-gate 		    (int)pid, (int)lwpid);
5977c478bd9Sstevel@tonic-gate 		break;
5987c478bd9Sstevel@tonic-gate 	default:
5997c478bd9Sstevel@tonic-gate 		errstr = strerror(errno);
6007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
6017c478bd9Sstevel@tonic-gate 		    "%6d: tick: lwp%d: can't access perf counter - %s\n"),
6027c478bd9Sstevel@tonic-gate 		    (int)pid, (int)lwpid, errstr);
6037c478bd9Sstevel@tonic-gate 		break;
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 	(void) cpc_unbind(cpc, this);
6067c478bd9Sstevel@tonic-gate 	return (-1);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate  * The system has just created a new address space that has a new pid.
6117c478bd9Sstevel@tonic-gate  * We're running in a child of the controlling process, with a new
6127c478bd9Sstevel@tonic-gate  * pctx handle already opened on the child of the original controlled process.
6137c478bd9Sstevel@tonic-gate  */
6147c478bd9Sstevel@tonic-gate static void
6157c478bd9Sstevel@tonic-gate pfork(pctx_t *pctx, pid_t oldpid, pid_t pid, id_t lwpid, void *arg)
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate 	struct pstate *state = arg;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	print_fork(mstimestamp(0), pid, lwpid, oldpid);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	if (!opts->followfork)
6227c478bd9Sstevel@tonic-gate 		return;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	if (pctx_set_events(pctx,
6257c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_EXEC_EVENT, pexec,
6267c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_FORK_EVENT, pfork,
6277c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_EXIT_EVENT, pexit,
6287c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
6297c478bd9Sstevel@tonic-gate 	    PCTX_INIT_LWP_EVENT, pinit_lwp,
6307c478bd9Sstevel@tonic-gate 	    PCTX_FINI_LWP_EVENT, pfini_lwp,
6317c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
6327c478bd9Sstevel@tonic-gate 	    PCTX_NULL_EVENT) == 0) {
6337c478bd9Sstevel@tonic-gate 		state->accum = cpc_setgrp_clone(opts->master);
6347c478bd9Sstevel@tonic-gate 		(void) pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
6357c478bd9Sstevel@tonic-gate 		if (state->accum) {
6367c478bd9Sstevel@tonic-gate 			free(state->accum);
6377c478bd9Sstevel@tonic-gate 			state->accum = NULL;
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate  * Translate the incoming options into actions, and get the
6447c478bd9Sstevel@tonic-gate  * tool and the process to control running.
6457c478bd9Sstevel@tonic-gate  */
6467c478bd9Sstevel@tonic-gate static int
6477c478bd9Sstevel@tonic-gate cputrack(int argc, char *argv[], int optind)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate 	struct pstate __state, *state = &__state;
6507c478bd9Sstevel@tonic-gate 	pctx_t *pctx;
6517c478bd9Sstevel@tonic-gate 	int err;
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	bzero(state, sizeof (*state));
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	if (opts->pid == 0) {
6567c478bd9Sstevel@tonic-gate 		if (argc <= optind) {
6577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s\n",
6587c478bd9Sstevel@tonic-gate 			    opts->pgmname,
6597c478bd9Sstevel@tonic-gate 			    gettext("no program to start"));
6607c478bd9Sstevel@tonic-gate 			return (1);
6617c478bd9Sstevel@tonic-gate 		}
6627c478bd9Sstevel@tonic-gate 		pctx = pctx_create(argv[optind],
6637c478bd9Sstevel@tonic-gate 		    &argv[optind], state, 1, cputrack_pctx_errfn);
6647c478bd9Sstevel@tonic-gate 		if (pctx == NULL) {
6657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s '%s'\n",
6667c478bd9Sstevel@tonic-gate 			    opts->pgmname,
6677c478bd9Sstevel@tonic-gate 			    gettext("failed to start program"),
6687c478bd9Sstevel@tonic-gate 			    argv[optind]);
6697c478bd9Sstevel@tonic-gate 			return (1);
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 	} else {
6727c478bd9Sstevel@tonic-gate 		pctx = pctx_capture(opts->pid, state, 1, cputrack_pctx_errfn);
6737c478bd9Sstevel@tonic-gate 		if (pctx == NULL) {
6747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s %d\n",
6757c478bd9Sstevel@tonic-gate 			    opts->pgmname,
6767c478bd9Sstevel@tonic-gate 			    gettext("failed to capture pid"),
6777c478bd9Sstevel@tonic-gate 			    (int)opts->pid);
6787c478bd9Sstevel@tonic-gate 			return (1);
6797c478bd9Sstevel@tonic-gate 		}
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	err = pctx_set_events(pctx,
6837c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_EXEC_EVENT, pexec,
6847c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_FORK_EVENT, pfork,
6857c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_EXIT_EVENT, pexit,
6867c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
6877c478bd9Sstevel@tonic-gate 	    PCTX_INIT_LWP_EVENT, pinit_lwp,
6887c478bd9Sstevel@tonic-gate 	    PCTX_FINI_LWP_EVENT, pfini_lwp,
6897c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
6907c478bd9Sstevel@tonic-gate 	    PCTX_NULL_EVENT);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (err != 0) {
6937c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s\n",
6947c478bd9Sstevel@tonic-gate 		    opts->pgmname,
6957c478bd9Sstevel@tonic-gate 		    gettext("can't bind process context ops to process"));
6967c478bd9Sstevel@tonic-gate 	} else {
6977c478bd9Sstevel@tonic-gate 		if (opts->dotitle)
6987c478bd9Sstevel@tonic-gate 			print_title(opts->master);
6997c478bd9Sstevel@tonic-gate 		state->accum = cpc_setgrp_clone(opts->master);
7007c478bd9Sstevel@tonic-gate 		zerotime();
7017c478bd9Sstevel@tonic-gate 		err = pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
7027c478bd9Sstevel@tonic-gate 		if (state->accum) {
7037c478bd9Sstevel@tonic-gate 			cpc_setgrp_free(state->accum);
7047c478bd9Sstevel@tonic-gate 			state->accum = NULL;
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 	pctx_release(pctx);
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	return (err != 0 ? 1 : 0);
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7128d4e547dSae112802 #if defined(__i386)
7138d4e547dSae112802 
7147c478bd9Sstevel@tonic-gate #define	OFFLINE_CMD	"/usr/sbin/psradm -f "
7157c478bd9Sstevel@tonic-gate #define	BUFSIZE		5	/* enough for "n " where n is a cpuid */
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate /*
7187c478bd9Sstevel@tonic-gate  * cpc_bind_pctx() failed with EACCES, which means the user must first offline
7197c478bd9Sstevel@tonic-gate  * all but one logical processor on each physical processor. Print to stderr the
7207c478bd9Sstevel@tonic-gate  * psradm command string to do this.
7217c478bd9Sstevel@tonic-gate  */
7227c478bd9Sstevel@tonic-gate static void
7237c478bd9Sstevel@tonic-gate p4_ht_error(void)
7247c478bd9Sstevel@tonic-gate {
7257c478bd9Sstevel@tonic-gate 	kstat_ctl_t	*kc;
7267c478bd9Sstevel@tonic-gate 	kstat_t		*ksp;
7277c478bd9Sstevel@tonic-gate 	kstat_named_t	*k;
7287c478bd9Sstevel@tonic-gate 	int		i;
7297c478bd9Sstevel@tonic-gate 	int		max;
7307c478bd9Sstevel@tonic-gate 	int		stat;
7317c478bd9Sstevel@tonic-gate 	int		*designees;
7327c478bd9Sstevel@tonic-gate 	int		*must_offline;
7337c478bd9Sstevel@tonic-gate 	char		buf[BUFSIZE];
7347c478bd9Sstevel@tonic-gate 	char		*cmd;
7357c478bd9Sstevel@tonic-gate 	int		noffline = 0;
7367c478bd9Sstevel@tonic-gate 	int		ndone = 0;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s\n",
7397c478bd9Sstevel@tonic-gate 	    gettext("Pentium 4 processors with HyperThreading present.\nOffline"
7407c478bd9Sstevel@tonic-gate 	    " all but one logical processor on each physical processor in"
7417c478bd9Sstevel@tonic-gate 	    " order to use\ncputrack.\n"));
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
7457c478bd9Sstevel@tonic-gate 		return;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	max = sysconf(_SC_CPUID_MAX);
7487c478bd9Sstevel@tonic-gate 	if ((designees = malloc(max * sizeof (*designees))) == NULL) {
7497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
7507c478bd9Sstevel@tonic-gate 		    opts->pgmname);
7517c478bd9Sstevel@tonic-gate 		exit(0);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if ((must_offline = malloc(max * sizeof (*designees))) == NULL) {
7557c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
7567c478bd9Sstevel@tonic-gate 		    opts->pgmname);
7577c478bd9Sstevel@tonic-gate 		exit(0);
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	for (i = 0; i < max; i++) {
7617c478bd9Sstevel@tonic-gate 		designees[i] = -1;
7627c478bd9Sstevel@tonic-gate 		must_offline[i] = 0;
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	for (i = 0; i < max; i++) {
7667c478bd9Sstevel@tonic-gate 		stat = p_online(i, P_STATUS);
7677c478bd9Sstevel@tonic-gate 		if (stat != P_ONLINE && stat != P_NOINTR)
7687c478bd9Sstevel@tonic-gate 			continue;
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 		if ((ksp = kstat_lookup(kc, "cpu_info", i, NULL)) == NULL) {
7717c478bd9Sstevel@tonic-gate 			free(designees);
7727c478bd9Sstevel@tonic-gate 			free(must_offline);
7737c478bd9Sstevel@tonic-gate 			return;
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		if (kstat_read(kc, ksp, NULL) == -1) {
7777c478bd9Sstevel@tonic-gate 			free(designees);
7787c478bd9Sstevel@tonic-gate 			free(must_offline);
7797c478bd9Sstevel@tonic-gate 			return;
7807c478bd9Sstevel@tonic-gate 		}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		if ((k = (kstat_named_t *)kstat_data_lookup(ksp, "chip_id"))
7837c478bd9Sstevel@tonic-gate 		    == NULL) {
7847c478bd9Sstevel@tonic-gate 			free(designees);
7857c478bd9Sstevel@tonic-gate 			free(must_offline);
7867c478bd9Sstevel@tonic-gate 			return;
7877c478bd9Sstevel@tonic-gate 		}
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		if (designees[k->value.i32] == -1)
7907c478bd9Sstevel@tonic-gate 			/*
7917c478bd9Sstevel@tonic-gate 			 * This chip doesn't yet have a CPU designated to remain
7927c478bd9Sstevel@tonic-gate 			 * online; let this one be it.
7937c478bd9Sstevel@tonic-gate 			 */
7947c478bd9Sstevel@tonic-gate 			designees[k->value.i32] = i;
7957c478bd9Sstevel@tonic-gate 		else {
7967c478bd9Sstevel@tonic-gate 			/*
7977c478bd9Sstevel@tonic-gate 			 * This chip already has a designated CPU; this CPU must
7987c478bd9Sstevel@tonic-gate 			 * go offline.
7997c478bd9Sstevel@tonic-gate 			 */
8007c478bd9Sstevel@tonic-gate 			must_offline[i] = 1;
8017c478bd9Sstevel@tonic-gate 			noffline++;
8027c478bd9Sstevel@tonic-gate 		}
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	/*
8067c478bd9Sstevel@tonic-gate 	 * Now construct a string containing the command line used to offline
8077c478bd9Sstevel@tonic-gate 	 * the appropriate processors.
8087c478bd9Sstevel@tonic-gate 	 */
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	if ((cmd = malloc(strlen(OFFLINE_CMD) + (noffline * BUFSIZE) + 1))
8117c478bd9Sstevel@tonic-gate 	    == NULL) {
8127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
8137c478bd9Sstevel@tonic-gate 		    opts->pgmname);
8147c478bd9Sstevel@tonic-gate 		exit(0);
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	(void) strcpy(cmd, OFFLINE_CMD);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	for (i = 0; i < max; i++) {
8207c478bd9Sstevel@tonic-gate 		if (must_offline[i] == 0)
8217c478bd9Sstevel@tonic-gate 			continue;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		ndone++;
8247c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, BUFSIZE, "%d", i);
8257c478bd9Sstevel@tonic-gate 		if (ndone < noffline)
8267c478bd9Sstevel@tonic-gate 			(void) strcat(buf, " ");
8277c478bd9Sstevel@tonic-gate 		(void) strcat(cmd, buf);
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\n%s\n", gettext("The following command "
8317c478bd9Sstevel@tonic-gate 	    "will configure the system appropriately"), cmd);
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	exit(1);
8347c478bd9Sstevel@tonic-gate }
8358d4e547dSae112802 
8368d4e547dSae112802 #endif /* defined(__i386) */
837