xref: /titanic_50/usr/src/cmd/cpc/common/cputrack.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*7c478bd9Sstevel@tonic-gate #include <inttypes.h>
33*7c478bd9Sstevel@tonic-gate #include <unistd.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <strings.h>
36*7c478bd9Sstevel@tonic-gate #include <limits.h>
37*7c478bd9Sstevel@tonic-gate #include <libintl.h>
38*7c478bd9Sstevel@tonic-gate #include <locale.h>
39*7c478bd9Sstevel@tonic-gate #include <errno.h>
40*7c478bd9Sstevel@tonic-gate #include <kstat.h>
41*7c478bd9Sstevel@tonic-gate #include <libcpc.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #include "cpucmds.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static struct options {
46*7c478bd9Sstevel@tonic-gate 	int debug;
47*7c478bd9Sstevel@tonic-gate 	int verbose;
48*7c478bd9Sstevel@tonic-gate 	int dotitle;
49*7c478bd9Sstevel@tonic-gate 	int dohelp;
50*7c478bd9Sstevel@tonic-gate 	int dotick;
51*7c478bd9Sstevel@tonic-gate 	int cpuver;
52*7c478bd9Sstevel@tonic-gate 	char *pgmname;
53*7c478bd9Sstevel@tonic-gate 	uint_t mseconds;
54*7c478bd9Sstevel@tonic-gate 	uint_t nsamples;
55*7c478bd9Sstevel@tonic-gate 	uint_t nsets;
56*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *master;
57*7c478bd9Sstevel@tonic-gate 	int followfork;
58*7c478bd9Sstevel@tonic-gate 	int followexec;
59*7c478bd9Sstevel@tonic-gate 	pid_t pid;
60*7c478bd9Sstevel@tonic-gate 	FILE *log;
61*7c478bd9Sstevel@tonic-gate } __options;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate static const struct options *opts = (const struct options *)&__options;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate static cpc_t *cpc;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
68*7c478bd9Sstevel@tonic-gate static void
69*7c478bd9Sstevel@tonic-gate cputrack_errfn(const char *fn, int subcode, const char *fmt, va_list ap)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", opts->pgmname);
72*7c478bd9Sstevel@tonic-gate 	if (opts->debug)
73*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", fn);
74*7c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
75*7c478bd9Sstevel@tonic-gate }
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static void
78*7c478bd9Sstevel@tonic-gate cputrack_pctx_errfn(const char *fn, const char *fmt, va_list ap)
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate 	cputrack_errfn(fn, -1, fmt, ap);
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate static int cputrack(int argc, char *argv[], int optind);
84*7c478bd9Sstevel@tonic-gate static void p4_ht_error(void);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
87*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
88*7c478bd9Sstevel@tonic-gate #endif
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate int
91*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate 	struct options *opts = &__options;
94*7c478bd9Sstevel@tonic-gate 	int c, errcnt = 0;
95*7c478bd9Sstevel@tonic-gate 	int nsamples;
96*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *sgrp;
97*7c478bd9Sstevel@tonic-gate 	char *errstr;
98*7c478bd9Sstevel@tonic-gate 	int ret;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
101*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	if ((opts->pgmname = strrchr(argv[0], '/')) == NULL)
104*7c478bd9Sstevel@tonic-gate 		opts->pgmname = argv[0];
105*7c478bd9Sstevel@tonic-gate 	else
106*7c478bd9Sstevel@tonic-gate 		opts->pgmname++;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	if ((cpc = cpc_open(CPC_VER_CURRENT)) == NULL) {
109*7c478bd9Sstevel@tonic-gate 		errstr = strerror(errno);
110*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: cannot access performance "
111*7c478bd9Sstevel@tonic-gate 		    "counter library - %s\n"), opts->pgmname, errstr);
112*7c478bd9Sstevel@tonic-gate 		return (1);
113*7c478bd9Sstevel@tonic-gate 	}
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 	(void) cpc_seterrhndlr(cpc, cputrack_errfn);
116*7c478bd9Sstevel@tonic-gate 	strtoset_errfn = cputrack_errfn;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	/*
119*7c478bd9Sstevel@tonic-gate 	 * Establish (non-zero) defaults
120*7c478bd9Sstevel@tonic-gate 	 */
121*7c478bd9Sstevel@tonic-gate 	opts->mseconds = 1000;
122*7c478bd9Sstevel@tonic-gate 	opts->dotitle = 1;
123*7c478bd9Sstevel@tonic-gate 	opts->log = stdout;
124*7c478bd9Sstevel@tonic-gate 	if ((opts->master = cpc_setgrp_new(cpc, 0)) == NULL) {
125*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
126*7c478bd9Sstevel@tonic-gate 		    opts->pgmname);
127*7c478bd9Sstevel@tonic-gate 		exit(1);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "T:N:Defhntvo:r:c:p:")) != EOF)
131*7c478bd9Sstevel@tonic-gate 		switch (c) {
132*7c478bd9Sstevel@tonic-gate 		case 'T':			/* sample time,	seconds */
133*7c478bd9Sstevel@tonic-gate 			opts->mseconds = (uint_t)(atof(optarg) * 1000.0);
134*7c478bd9Sstevel@tonic-gate 			break;
135*7c478bd9Sstevel@tonic-gate 		case 'N':			/* number of samples */
136*7c478bd9Sstevel@tonic-gate 			nsamples = atoi(optarg);
137*7c478bd9Sstevel@tonic-gate 			if (nsamples < 0)
138*7c478bd9Sstevel@tonic-gate 				errcnt++;
139*7c478bd9Sstevel@tonic-gate 			else
140*7c478bd9Sstevel@tonic-gate 				opts->nsamples = (uint_t)nsamples;
141*7c478bd9Sstevel@tonic-gate 			break;
142*7c478bd9Sstevel@tonic-gate 		case 'D':			/* enable debugging */
143*7c478bd9Sstevel@tonic-gate 			opts->debug++;
144*7c478bd9Sstevel@tonic-gate 			break;
145*7c478bd9Sstevel@tonic-gate 		case 'f':			/* follow fork */
146*7c478bd9Sstevel@tonic-gate 			opts->followfork++;
147*7c478bd9Sstevel@tonic-gate 			break;
148*7c478bd9Sstevel@tonic-gate 		case 'e':			/* follow exec */
149*7c478bd9Sstevel@tonic-gate 			opts->followexec++;
150*7c478bd9Sstevel@tonic-gate 			break;
151*7c478bd9Sstevel@tonic-gate 		case 'n':			/* no titles */
152*7c478bd9Sstevel@tonic-gate 			opts->dotitle = 0;
153*7c478bd9Sstevel@tonic-gate 			break;
154*7c478bd9Sstevel@tonic-gate 		case 't':			/* print %tick */
155*7c478bd9Sstevel@tonic-gate 			opts->dotick = 1;
156*7c478bd9Sstevel@tonic-gate 			break;
157*7c478bd9Sstevel@tonic-gate 		case 'v':
158*7c478bd9Sstevel@tonic-gate 			opts->verbose = 1;	/* more chatty */
159*7c478bd9Sstevel@tonic-gate 			break;
160*7c478bd9Sstevel@tonic-gate 		case 'o':
161*7c478bd9Sstevel@tonic-gate 			if (optarg == NULL) {
162*7c478bd9Sstevel@tonic-gate 				errcnt++;
163*7c478bd9Sstevel@tonic-gate 				break;
164*7c478bd9Sstevel@tonic-gate 			}
165*7c478bd9Sstevel@tonic-gate 			if ((opts->log = fopen(optarg, "w")) == NULL) {
166*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
167*7c478bd9Sstevel@tonic-gate 				    "%s: cannot open '%s' for writing\n"),
168*7c478bd9Sstevel@tonic-gate 				    opts->pgmname, optarg);
169*7c478bd9Sstevel@tonic-gate 				return (1);
170*7c478bd9Sstevel@tonic-gate 			}
171*7c478bd9Sstevel@tonic-gate 			break;
172*7c478bd9Sstevel@tonic-gate 		case 'c':			/* specify statistics */
173*7c478bd9Sstevel@tonic-gate 			if ((sgrp = cpc_setgrp_newset(opts->master,
174*7c478bd9Sstevel@tonic-gate 			    optarg, &errcnt)) != NULL)
175*7c478bd9Sstevel@tonic-gate 				opts->master = sgrp;
176*7c478bd9Sstevel@tonic-gate 			break;
177*7c478bd9Sstevel@tonic-gate 		case 'p':			/* grab given pid */
178*7c478bd9Sstevel@tonic-gate 			if ((opts->pid = atoi(optarg)) <= 0)
179*7c478bd9Sstevel@tonic-gate 				errcnt++;
180*7c478bd9Sstevel@tonic-gate 			break;
181*7c478bd9Sstevel@tonic-gate 		case 'h':
182*7c478bd9Sstevel@tonic-gate 			opts->dohelp = 1;
183*7c478bd9Sstevel@tonic-gate 			break;
184*7c478bd9Sstevel@tonic-gate 		case '?':
185*7c478bd9Sstevel@tonic-gate 		default:
186*7c478bd9Sstevel@tonic-gate 			errcnt++;
187*7c478bd9Sstevel@tonic-gate 			break;
188*7c478bd9Sstevel@tonic-gate 		}
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	if (opts->nsamples == 0)
191*7c478bd9Sstevel@tonic-gate 		opts->nsamples = UINT_MAX;
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	if (errcnt != 0 ||
194*7c478bd9Sstevel@tonic-gate 	    opts->dohelp ||
195*7c478bd9Sstevel@tonic-gate 	    (argc == optind && opts->pid == 0) ||
196*7c478bd9Sstevel@tonic-gate 	    (argc > optind && opts->pid != 0) ||
197*7c478bd9Sstevel@tonic-gate 	    (opts->nsets = cpc_setgrp_numsets(opts->master)) == 0) {
198*7c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->dohelp ? stdout : stderr, gettext(
199*7c478bd9Sstevel@tonic-gate 		    "Usage:\n\t%s [-T secs] [-N count] [-Defhnv] [-o file]\n"
200*7c478bd9Sstevel@tonic-gate 		    "\t\t-c events [command [args] | -p pid]\n\n"
201*7c478bd9Sstevel@tonic-gate 		    "\t-T secs\t  seconds between samples, default 1\n"
202*7c478bd9Sstevel@tonic-gate 		    "\t-N count  number of samples, default unlimited\n"
203*7c478bd9Sstevel@tonic-gate 		    "\t-D\t  enable debug mode\n"
204*7c478bd9Sstevel@tonic-gate 		    "\t-e\t  follow exec(2), and execve(2)\n"
205*7c478bd9Sstevel@tonic-gate 		    "\t-f\t  follow fork(2), fork1(2), and vfork(2)\n"
206*7c478bd9Sstevel@tonic-gate 		    "\t-h\t  print extended usage information\n"
207*7c478bd9Sstevel@tonic-gate 		    "\t-n\t  suppress titles\n"
208*7c478bd9Sstevel@tonic-gate 		    "\t-t\t  include virtualized %s register\n"
209*7c478bd9Sstevel@tonic-gate 		    "\t-v\t  verbose mode\n"
210*7c478bd9Sstevel@tonic-gate 		    "\t-o file\t  write cpu statistics to this file\n"
211*7c478bd9Sstevel@tonic-gate 		    "\t-c events specify processor events to be monitored\n"
212*7c478bd9Sstevel@tonic-gate 		    "\t-p pid\t  pid of existing process to capture\n\n"
213*7c478bd9Sstevel@tonic-gate 		    "\tUse cpustat(1M) to monitor system-wide statistics.\n"),
214*7c478bd9Sstevel@tonic-gate 		    opts->pgmname, CPC_TICKREG_NAME);
215*7c478bd9Sstevel@tonic-gate 		if (opts->dohelp) {
216*7c478bd9Sstevel@tonic-gate 			(void) putchar('\n');
217*7c478bd9Sstevel@tonic-gate 			(void) capabilities(cpc, stdout);
218*7c478bd9Sstevel@tonic-gate 			exit(0);
219*7c478bd9Sstevel@tonic-gate 		}
220*7c478bd9Sstevel@tonic-gate 		exit(2);
221*7c478bd9Sstevel@tonic-gate 	}
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_reset(opts->master);
224*7c478bd9Sstevel@tonic-gate 	(void) setvbuf(opts->log, NULL, _IOLBF, 0);
225*7c478bd9Sstevel@tonic-gate 	ret = cputrack(argc, argv, optind);
226*7c478bd9Sstevel@tonic-gate 	(void) cpc_close(cpc);
227*7c478bd9Sstevel@tonic-gate 	return (ret);
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate static void
231*7c478bd9Sstevel@tonic-gate print_title(cpc_setgrp_t *sgrp)
232*7c478bd9Sstevel@tonic-gate {
233*7c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7s ", "time");
234*7c478bd9Sstevel@tonic-gate 	if (opts->followfork)
235*7c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6s ", "pid");
236*7c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3s %10s ", "lwp", "event");
237*7c478bd9Sstevel@tonic-gate 	if (opts->dotick)
238*7c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9s ", CPC_TICKREG_NAME);
239*7c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%s\n", cpc_setgrp_gethdr(sgrp));
240*7c478bd9Sstevel@tonic-gate 	(void) fflush(opts->log);
241*7c478bd9Sstevel@tonic-gate }
242*7c478bd9Sstevel@tonic-gate 
243*7c478bd9Sstevel@tonic-gate static void
244*7c478bd9Sstevel@tonic-gate print_exec(float now, pid_t pid, char *name)
245*7c478bd9Sstevel@tonic-gate {
246*7c478bd9Sstevel@tonic-gate 	if (name == NULL)
247*7c478bd9Sstevel@tonic-gate 		name = "(unknown)";
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7.3f ", now);
250*7c478bd9Sstevel@tonic-gate 	if (opts->followfork)
251*7c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6d ", (int)pid);
252*7c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3d %10s ", 1, "exec");
253*7c478bd9Sstevel@tonic-gate 	if (opts->dotick)
254*7c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9s ", "");
255*7c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%9s %9s # '%s'\n", "", "", name);
256*7c478bd9Sstevel@tonic-gate 	(void) fflush(opts->log);
257*7c478bd9Sstevel@tonic-gate }
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate static void
260*7c478bd9Sstevel@tonic-gate print_fork(float now, pid_t newpid, id_t lwpid, pid_t oldpid)
261*7c478bd9Sstevel@tonic-gate {
262*7c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7.3f ", now);
263*7c478bd9Sstevel@tonic-gate 	if (opts->followfork)
264*7c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6d ", (int)oldpid);
265*7c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3d %10s ", (int)lwpid, "fork");
266*7c478bd9Sstevel@tonic-gate 	if (opts->dotick)
267*7c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9s ", "");
268*7c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%9s %9s # %d\n", "", "", (int)newpid);
269*7c478bd9Sstevel@tonic-gate 	(void) fflush(opts->log);
270*7c478bd9Sstevel@tonic-gate }
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate static void
273*7c478bd9Sstevel@tonic-gate print_sample(pid_t pid, id_t lwpid,
274*7c478bd9Sstevel@tonic-gate     char *pevent, cpc_buf_t *buf, int nreq, const char *evname)
275*7c478bd9Sstevel@tonic-gate {
276*7c478bd9Sstevel@tonic-gate 	uint64_t	val;
277*7c478bd9Sstevel@tonic-gate 	int		i;
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7.3f ",
280*7c478bd9Sstevel@tonic-gate 	    mstimestamp(cpc_buf_hrtime(cpc, buf)));
281*7c478bd9Sstevel@tonic-gate 	if (opts->followfork)
282*7c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6d ", (int)pid);
283*7c478bd9Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3d %10s ", (int)lwpid, pevent);
284*7c478bd9Sstevel@tonic-gate 	if (opts->dotick)
285*7c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9" PRId64 " ",
286*7c478bd9Sstevel@tonic-gate 		    cpc_buf_tick(cpc, buf));
287*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < nreq; i++) {
288*7c478bd9Sstevel@tonic-gate 		(void) cpc_buf_get(cpc, buf, i, &val);
289*7c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9" PRId64 " ", val);
290*7c478bd9Sstevel@tonic-gate 	}
291*7c478bd9Sstevel@tonic-gate 	if (opts->nsets > 1)
292*7c478bd9Sstevel@tonic-gate 		(void) fprintf(opts->log, " # %s\n", evname);
293*7c478bd9Sstevel@tonic-gate 	else
294*7c478bd9Sstevel@tonic-gate 		(void) fputc('\n', opts->log);
295*7c478bd9Sstevel@tonic-gate }
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate struct pstate {
298*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *accum;
299*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_t **sgrps;
300*7c478bd9Sstevel@tonic-gate 	int maxlwpid;
301*7c478bd9Sstevel@tonic-gate };
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate static int
304*7c478bd9Sstevel@tonic-gate pinit_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
305*7c478bd9Sstevel@tonic-gate {
306*7c478bd9Sstevel@tonic-gate 	struct pstate *state = arg;
307*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *sgrp;
308*7c478bd9Sstevel@tonic-gate 	cpc_set_t *set;
309*7c478bd9Sstevel@tonic-gate 	cpc_buf_t **data1, **data2, **scratch;
310*7c478bd9Sstevel@tonic-gate 	char *errstr;
311*7c478bd9Sstevel@tonic-gate 	int nreq;
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 	if (state->maxlwpid < lwpid) {
314*7c478bd9Sstevel@tonic-gate 		state->sgrps = realloc(state->sgrps,
315*7c478bd9Sstevel@tonic-gate 		    lwpid * sizeof (state->sgrps));
316*7c478bd9Sstevel@tonic-gate 		if (state->sgrps == NULL) {
317*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
318*7c478bd9Sstevel@tonic-gate 			    "%6d: init_lwp: out of memory\n"), (int)pid);
319*7c478bd9Sstevel@tonic-gate 			return (-1);
320*7c478bd9Sstevel@tonic-gate 		}
321*7c478bd9Sstevel@tonic-gate 		while (state->maxlwpid < lwpid) {
322*7c478bd9Sstevel@tonic-gate 			state->sgrps[state->maxlwpid] = NULL;
323*7c478bd9Sstevel@tonic-gate 			state->maxlwpid++;
324*7c478bd9Sstevel@tonic-gate 		}
325*7c478bd9Sstevel@tonic-gate 	}
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 	if ((sgrp = state->sgrps[lwpid-1]) == NULL) {
328*7c478bd9Sstevel@tonic-gate 		if ((sgrp = cpc_setgrp_clone(opts->master)) == NULL) {
329*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
330*7c478bd9Sstevel@tonic-gate 			    "%6d: init_lwp: out of memory\n"), (int)pid);
331*7c478bd9Sstevel@tonic-gate 			return (-1);
332*7c478bd9Sstevel@tonic-gate 		}
333*7c478bd9Sstevel@tonic-gate 		state->sgrps[lwpid-1] = sgrp;
334*7c478bd9Sstevel@tonic-gate 		set = cpc_setgrp_getset(sgrp);
335*7c478bd9Sstevel@tonic-gate 	} else {
336*7c478bd9Sstevel@tonic-gate 		cpc_setgrp_reset(sgrp);
337*7c478bd9Sstevel@tonic-gate 		set = cpc_setgrp_getset(sgrp);
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	if (cpc_bind_pctx(cpc, pctx, lwpid, set, 0) != 0 ||
343*7c478bd9Sstevel@tonic-gate 	    cpc_set_sample(cpc, set, *data2) != 0) {
344*7c478bd9Sstevel@tonic-gate 		errstr = strerror(errno);
345*7c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN)
346*7c478bd9Sstevel@tonic-gate 			(void) cpc_unbind(cpc, set);
347*7c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
348*7c478bd9Sstevel@tonic-gate 			p4_ht_error();
349*7c478bd9Sstevel@tonic-gate 		else
350*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
351*7c478bd9Sstevel@tonic-gate 				"%6d: init_lwp: can't bind perf counters "
352*7c478bd9Sstevel@tonic-gate 				    "to lwp%d - %s\n"), (int)pid, (int)lwpid,
353*7c478bd9Sstevel@tonic-gate 			    errstr);
354*7c478bd9Sstevel@tonic-gate 		return (-1);
355*7c478bd9Sstevel@tonic-gate 	}
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	if (opts->verbose)
358*7c478bd9Sstevel@tonic-gate 		print_sample(pid, lwpid, "init_lwp",
359*7c478bd9Sstevel@tonic-gate 		    *data2, nreq, cpc_setgrp_getname(sgrp));
360*7c478bd9Sstevel@tonic-gate 	return (0);
361*7c478bd9Sstevel@tonic-gate }
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
364*7c478bd9Sstevel@tonic-gate static int
365*7c478bd9Sstevel@tonic-gate pfini_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
366*7c478bd9Sstevel@tonic-gate {
367*7c478bd9Sstevel@tonic-gate 	struct pstate *state = arg;
368*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
369*7c478bd9Sstevel@tonic-gate 	cpc_set_t *set;
370*7c478bd9Sstevel@tonic-gate 	char *errstr;
371*7c478bd9Sstevel@tonic-gate 	cpc_buf_t **data1, **data2, **scratch;
372*7c478bd9Sstevel@tonic-gate 	int nreq;
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	set = cpc_setgrp_getset(sgrp);
375*7c478bd9Sstevel@tonic-gate 	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
376*7c478bd9Sstevel@tonic-gate 	if (cpc_set_sample(cpc, set, *data1) == 0) {
377*7c478bd9Sstevel@tonic-gate 		if (opts->verbose)
378*7c478bd9Sstevel@tonic-gate 			print_sample(pid, lwpid, "fini_lwp",
379*7c478bd9Sstevel@tonic-gate 			    *data1, nreq, cpc_setgrp_getname(sgrp));
380*7c478bd9Sstevel@tonic-gate 		cpc_setgrp_accum(state->accum, sgrp);
381*7c478bd9Sstevel@tonic-gate 		if (cpc_unbind(cpc, set) == 0)
382*7c478bd9Sstevel@tonic-gate 			return (0);
383*7c478bd9Sstevel@tonic-gate 	}
384*7c478bd9Sstevel@tonic-gate 
385*7c478bd9Sstevel@tonic-gate 	switch (errno) {
386*7c478bd9Sstevel@tonic-gate 	case EAGAIN:
387*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%6d: fini_lwp: "
388*7c478bd9Sstevel@tonic-gate 		    "lwp%d: perf counter contents invalidated\n"),
389*7c478bd9Sstevel@tonic-gate 		    (int)pid, (int)lwpid);
390*7c478bd9Sstevel@tonic-gate 		break;
391*7c478bd9Sstevel@tonic-gate 	default:
392*7c478bd9Sstevel@tonic-gate 		errstr = strerror(errno);
393*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%6d: fini_lwp: "
394*7c478bd9Sstevel@tonic-gate 		    "lwp%d: can't access perf counters - %s\n"),
395*7c478bd9Sstevel@tonic-gate 		    (int)pid, (int)lwpid, errstr);
396*7c478bd9Sstevel@tonic-gate 		break;
397*7c478bd9Sstevel@tonic-gate 	}
398*7c478bd9Sstevel@tonic-gate 	return (0);
399*7c478bd9Sstevel@tonic-gate }
400*7c478bd9Sstevel@tonic-gate 
401*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
402*7c478bd9Sstevel@tonic-gate static int
403*7c478bd9Sstevel@tonic-gate plwp_create(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
404*7c478bd9Sstevel@tonic-gate {
405*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_t	*sgrp = opts->master;
406*7c478bd9Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
407*7c478bd9Sstevel@tonic-gate 	int		nreq;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	print_sample(pid, lwpid, "lwp_create",
412*7c478bd9Sstevel@tonic-gate 	    *data1, nreq, cpc_setgrp_getname(sgrp));
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	return (0);
415*7c478bd9Sstevel@tonic-gate }
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
418*7c478bd9Sstevel@tonic-gate static int
419*7c478bd9Sstevel@tonic-gate plwp_exit(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
420*7c478bd9Sstevel@tonic-gate {
421*7c478bd9Sstevel@tonic-gate 	struct pstate	*state = arg;
422*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_t	*sgrp = state->sgrps[lwpid-1];
423*7c478bd9Sstevel@tonic-gate 	cpc_set_t	*start;
424*7c478bd9Sstevel@tonic-gate 	int		nreq;
425*7c478bd9Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	start = cpc_setgrp_getset(sgrp);
428*7c478bd9Sstevel@tonic-gate 	do {
429*7c478bd9Sstevel@tonic-gate 		nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
430*7c478bd9Sstevel@tonic-gate 		if (cpc_buf_hrtime(cpc, *data1) == 0)
431*7c478bd9Sstevel@tonic-gate 			continue;
432*7c478bd9Sstevel@tonic-gate 		print_sample(pid, lwpid, "lwp_exit",
433*7c478bd9Sstevel@tonic-gate 		    *data1, nreq, cpc_setgrp_getname(sgrp));
434*7c478bd9Sstevel@tonic-gate 	} while (cpc_setgrp_nextset(sgrp) != start);
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate 	return (0);
437*7c478bd9Sstevel@tonic-gate }
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
440*7c478bd9Sstevel@tonic-gate static int
441*7c478bd9Sstevel@tonic-gate pexec(pctx_t *pctx, pid_t pid, id_t lwpid, char *name, void *arg)
442*7c478bd9Sstevel@tonic-gate {
443*7c478bd9Sstevel@tonic-gate 	struct pstate	*state = arg;
444*7c478bd9Sstevel@tonic-gate 	float		now = 0.0;
445*7c478bd9Sstevel@tonic-gate 	cpc_set_t	*start;
446*7c478bd9Sstevel@tonic-gate 	int		nreq;
447*7c478bd9Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
448*7c478bd9Sstevel@tonic-gate 	hrtime_t	hrt;
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	/*
451*7c478bd9Sstevel@tonic-gate 	 * Print the accumulated results from the previous program image
452*7c478bd9Sstevel@tonic-gate 	 */
453*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_reset(state->accum);
454*7c478bd9Sstevel@tonic-gate 	start = cpc_setgrp_getset(state->accum);
455*7c478bd9Sstevel@tonic-gate 	do {
456*7c478bd9Sstevel@tonic-gate 		nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
457*7c478bd9Sstevel@tonic-gate 		    &scratch);
458*7c478bd9Sstevel@tonic-gate 		hrt = cpc_buf_hrtime(cpc, *data1);
459*7c478bd9Sstevel@tonic-gate 		if (hrt == 0)
460*7c478bd9Sstevel@tonic-gate 			continue;
461*7c478bd9Sstevel@tonic-gate 		print_sample(pid, lwpid, "exec",
462*7c478bd9Sstevel@tonic-gate 		    *data1, nreq, cpc_setgrp_getname(state->accum));
463*7c478bd9Sstevel@tonic-gate 		if (now < mstimestamp(hrt))
464*7c478bd9Sstevel@tonic-gate 			now = mstimestamp(hrt);
465*7c478bd9Sstevel@tonic-gate 	} while (cpc_setgrp_nextset(state->accum) != start);
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	print_exec(now, pid, name);
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 	if (state->accum != NULL) {
470*7c478bd9Sstevel@tonic-gate 		cpc_setgrp_free(state->accum);
471*7c478bd9Sstevel@tonic-gate 		state->accum = NULL;
472*7c478bd9Sstevel@tonic-gate 	}
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 	if (opts->followexec) {
475*7c478bd9Sstevel@tonic-gate 		state->accum = cpc_setgrp_clone(opts->master);
476*7c478bd9Sstevel@tonic-gate 		return (0);
477*7c478bd9Sstevel@tonic-gate 	}
478*7c478bd9Sstevel@tonic-gate 	return (-1);
479*7c478bd9Sstevel@tonic-gate }
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
482*7c478bd9Sstevel@tonic-gate static void
483*7c478bd9Sstevel@tonic-gate pexit(pctx_t *pctx, pid_t pid, id_t lwpid, int status, void *arg)
484*7c478bd9Sstevel@tonic-gate {
485*7c478bd9Sstevel@tonic-gate 	struct pstate	*state = arg;
486*7c478bd9Sstevel@tonic-gate 	cpc_set_t	*start;
487*7c478bd9Sstevel@tonic-gate 	int		nreq;
488*7c478bd9Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
489*7c478bd9Sstevel@tonic-gate 
490*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_reset(state->accum);
491*7c478bd9Sstevel@tonic-gate 	start = cpc_setgrp_getset(state->accum);
492*7c478bd9Sstevel@tonic-gate 	do {
493*7c478bd9Sstevel@tonic-gate 		nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
494*7c478bd9Sstevel@tonic-gate 		    &scratch);
495*7c478bd9Sstevel@tonic-gate 		if (cpc_buf_hrtime(cpc, *data1) == 0)
496*7c478bd9Sstevel@tonic-gate 			continue;
497*7c478bd9Sstevel@tonic-gate 		print_sample(pid, lwpid, "exit",
498*7c478bd9Sstevel@tonic-gate 		    *data1, nreq, cpc_setgrp_getname(state->accum));
499*7c478bd9Sstevel@tonic-gate 	} while (cpc_setgrp_nextset(state->accum) != start);
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_free(state->accum);
502*7c478bd9Sstevel@tonic-gate 	state->accum = NULL;
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate 	for (lwpid = 1; lwpid < state->maxlwpid; lwpid++)
505*7c478bd9Sstevel@tonic-gate 		if (state->sgrps[lwpid-1] != NULL) {
506*7c478bd9Sstevel@tonic-gate 			cpc_setgrp_free(state->sgrps[lwpid-1]);
507*7c478bd9Sstevel@tonic-gate 			state->sgrps[lwpid-1] = NULL;
508*7c478bd9Sstevel@tonic-gate 		}
509*7c478bd9Sstevel@tonic-gate 	free(state->sgrps);
510*7c478bd9Sstevel@tonic-gate 	state->sgrps = NULL;
511*7c478bd9Sstevel@tonic-gate }
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate static int
514*7c478bd9Sstevel@tonic-gate ptick(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
515*7c478bd9Sstevel@tonic-gate {
516*7c478bd9Sstevel@tonic-gate 	struct pstate *state = arg;
517*7c478bd9Sstevel@tonic-gate 	cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
518*7c478bd9Sstevel@tonic-gate 	cpc_set_t *this = cpc_setgrp_getset(sgrp);
519*7c478bd9Sstevel@tonic-gate 	const char *name = cpc_setgrp_getname(sgrp);
520*7c478bd9Sstevel@tonic-gate 	cpc_buf_t **data1, **data2, **scratch, *tmp;
521*7c478bd9Sstevel@tonic-gate 	char *errstr;
522*7c478bd9Sstevel@tonic-gate 	int nreqs;
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 	nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate 	if (opts->nsets == 1) {
527*7c478bd9Sstevel@tonic-gate 		/*
528*7c478bd9Sstevel@tonic-gate 		 * If we're dealing with one set, buffer usage is:
529*7c478bd9Sstevel@tonic-gate 		 *
530*7c478bd9Sstevel@tonic-gate 		 * data1 = most recent data snapshot
531*7c478bd9Sstevel@tonic-gate 		 * data2 = previous data snapshot
532*7c478bd9Sstevel@tonic-gate 		 * scratch = used for diffing data1 and data2
533*7c478bd9Sstevel@tonic-gate 		 *
534*7c478bd9Sstevel@tonic-gate 		 * Save the snapshot from the previous sample in data2
535*7c478bd9Sstevel@tonic-gate 		 * before putting the current sample in data1.
536*7c478bd9Sstevel@tonic-gate 		 */
537*7c478bd9Sstevel@tonic-gate 		tmp = *data1;
538*7c478bd9Sstevel@tonic-gate 		*data1 = *data2;
539*7c478bd9Sstevel@tonic-gate 		*data2 = tmp;
540*7c478bd9Sstevel@tonic-gate 		if (cpc_set_sample(cpc, this, *data1) != 0)
541*7c478bd9Sstevel@tonic-gate 			goto broken;
542*7c478bd9Sstevel@tonic-gate 		cpc_buf_sub(cpc, *scratch, *data1, *data2);
543*7c478bd9Sstevel@tonic-gate 	} else {
544*7c478bd9Sstevel@tonic-gate 		cpc_set_t *next = cpc_setgrp_nextset(sgrp);
545*7c478bd9Sstevel@tonic-gate 		/*
546*7c478bd9Sstevel@tonic-gate 		 * If there is more than set in use, we will need to
547*7c478bd9Sstevel@tonic-gate 		 * unbind and re-bind on each go-around because each
548*7c478bd9Sstevel@tonic-gate 		 * time a counter is bound, it is preset to 0 (as it was
549*7c478bd9Sstevel@tonic-gate 		 * specified when the requests were added to the set).
550*7c478bd9Sstevel@tonic-gate 		 *
551*7c478bd9Sstevel@tonic-gate 		 * Buffer usage in this case is:
552*7c478bd9Sstevel@tonic-gate 		 *
553*7c478bd9Sstevel@tonic-gate 		 * data1 = total counts for this set since program began
554*7c478bd9Sstevel@tonic-gate 		 * data2 = unused
555*7c478bd9Sstevel@tonic-gate 		 * scratch = most recent data snapshot
556*7c478bd9Sstevel@tonic-gate 		 */
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 		if (cpc_set_sample(cpc, this, *scratch) != 0)
559*7c478bd9Sstevel@tonic-gate 			goto broken;
560*7c478bd9Sstevel@tonic-gate 		cpc_buf_add(cpc, *data1, *data1, *scratch);
561*7c478bd9Sstevel@tonic-gate 
562*7c478bd9Sstevel@tonic-gate 		/*
563*7c478bd9Sstevel@tonic-gate 		 * No need to unbind the previous set, as binding another set
564*7c478bd9Sstevel@tonic-gate 		 * automatically unbinds the most recently bound set.
565*7c478bd9Sstevel@tonic-gate 		 */
566*7c478bd9Sstevel@tonic-gate 		if (cpc_bind_pctx(cpc, pctx, lwpid, next, 0) != 0)
567*7c478bd9Sstevel@tonic-gate 			goto broken;
568*7c478bd9Sstevel@tonic-gate 	}
569*7c478bd9Sstevel@tonic-gate 	print_sample(pid, lwpid, "tick", *scratch, nreqs, name);
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate 	return (0);
572*7c478bd9Sstevel@tonic-gate 
573*7c478bd9Sstevel@tonic-gate broken:
574*7c478bd9Sstevel@tonic-gate 	switch (errno) {
575*7c478bd9Sstevel@tonic-gate 	case EAGAIN:
576*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
577*7c478bd9Sstevel@tonic-gate 		    "%6d: tick: lwp%d: perf counter contents invalidated\n"),
578*7c478bd9Sstevel@tonic-gate 		    (int)pid, (int)lwpid);
579*7c478bd9Sstevel@tonic-gate 		break;
580*7c478bd9Sstevel@tonic-gate 	default:
581*7c478bd9Sstevel@tonic-gate 		errstr = strerror(errno);
582*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
583*7c478bd9Sstevel@tonic-gate 		    "%6d: tick: lwp%d: can't access perf counter - %s\n"),
584*7c478bd9Sstevel@tonic-gate 		    (int)pid, (int)lwpid, errstr);
585*7c478bd9Sstevel@tonic-gate 		break;
586*7c478bd9Sstevel@tonic-gate 	}
587*7c478bd9Sstevel@tonic-gate 	(void) cpc_unbind(cpc, this);
588*7c478bd9Sstevel@tonic-gate 	return (-1);
589*7c478bd9Sstevel@tonic-gate }
590*7c478bd9Sstevel@tonic-gate 
591*7c478bd9Sstevel@tonic-gate /*
592*7c478bd9Sstevel@tonic-gate  * The system has just created a new address space that has a new pid.
593*7c478bd9Sstevel@tonic-gate  * We're running in a child of the controlling process, with a new
594*7c478bd9Sstevel@tonic-gate  * pctx handle already opened on the child of the original controlled process.
595*7c478bd9Sstevel@tonic-gate  */
596*7c478bd9Sstevel@tonic-gate static void
597*7c478bd9Sstevel@tonic-gate pfork(pctx_t *pctx, pid_t oldpid, pid_t pid, id_t lwpid, void *arg)
598*7c478bd9Sstevel@tonic-gate {
599*7c478bd9Sstevel@tonic-gate 	struct pstate *state = arg;
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate 	print_fork(mstimestamp(0), pid, lwpid, oldpid);
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate 	if (!opts->followfork)
604*7c478bd9Sstevel@tonic-gate 		return;
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate 	if (pctx_set_events(pctx,
607*7c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_EXEC_EVENT, pexec,
608*7c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_FORK_EVENT, pfork,
609*7c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_EXIT_EVENT, pexit,
610*7c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
611*7c478bd9Sstevel@tonic-gate 	    PCTX_INIT_LWP_EVENT, pinit_lwp,
612*7c478bd9Sstevel@tonic-gate 	    PCTX_FINI_LWP_EVENT, pfini_lwp,
613*7c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
614*7c478bd9Sstevel@tonic-gate 	    PCTX_NULL_EVENT) == 0) {
615*7c478bd9Sstevel@tonic-gate 		state->accum = cpc_setgrp_clone(opts->master);
616*7c478bd9Sstevel@tonic-gate 		(void) pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
617*7c478bd9Sstevel@tonic-gate 		if (state->accum) {
618*7c478bd9Sstevel@tonic-gate 			free(state->accum);
619*7c478bd9Sstevel@tonic-gate 			state->accum = NULL;
620*7c478bd9Sstevel@tonic-gate 		}
621*7c478bd9Sstevel@tonic-gate 	}
622*7c478bd9Sstevel@tonic-gate }
623*7c478bd9Sstevel@tonic-gate 
624*7c478bd9Sstevel@tonic-gate /*
625*7c478bd9Sstevel@tonic-gate  * Translate the incoming options into actions, and get the
626*7c478bd9Sstevel@tonic-gate  * tool and the process to control running.
627*7c478bd9Sstevel@tonic-gate  */
628*7c478bd9Sstevel@tonic-gate static int
629*7c478bd9Sstevel@tonic-gate cputrack(int argc, char *argv[], int optind)
630*7c478bd9Sstevel@tonic-gate {
631*7c478bd9Sstevel@tonic-gate 	struct pstate __state, *state = &__state;
632*7c478bd9Sstevel@tonic-gate 	pctx_t *pctx;
633*7c478bd9Sstevel@tonic-gate 	int err;
634*7c478bd9Sstevel@tonic-gate 
635*7c478bd9Sstevel@tonic-gate 	bzero(state, sizeof (*state));
636*7c478bd9Sstevel@tonic-gate 
637*7c478bd9Sstevel@tonic-gate 	if (opts->pid == 0) {
638*7c478bd9Sstevel@tonic-gate 		if (argc <= optind) {
639*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s\n",
640*7c478bd9Sstevel@tonic-gate 			    opts->pgmname,
641*7c478bd9Sstevel@tonic-gate 			    gettext("no program to start"));
642*7c478bd9Sstevel@tonic-gate 			return (1);
643*7c478bd9Sstevel@tonic-gate 		}
644*7c478bd9Sstevel@tonic-gate 		pctx = pctx_create(argv[optind],
645*7c478bd9Sstevel@tonic-gate 		    &argv[optind], state, 1, cputrack_pctx_errfn);
646*7c478bd9Sstevel@tonic-gate 		if (pctx == NULL) {
647*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s '%s'\n",
648*7c478bd9Sstevel@tonic-gate 			    opts->pgmname,
649*7c478bd9Sstevel@tonic-gate 			    gettext("failed to start program"),
650*7c478bd9Sstevel@tonic-gate 			    argv[optind]);
651*7c478bd9Sstevel@tonic-gate 			return (1);
652*7c478bd9Sstevel@tonic-gate 		}
653*7c478bd9Sstevel@tonic-gate 	} else {
654*7c478bd9Sstevel@tonic-gate 		pctx = pctx_capture(opts->pid, state, 1, cputrack_pctx_errfn);
655*7c478bd9Sstevel@tonic-gate 		if (pctx == NULL) {
656*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s %d\n",
657*7c478bd9Sstevel@tonic-gate 			    opts->pgmname,
658*7c478bd9Sstevel@tonic-gate 			    gettext("failed to capture pid"),
659*7c478bd9Sstevel@tonic-gate 			    (int)opts->pid);
660*7c478bd9Sstevel@tonic-gate 			return (1);
661*7c478bd9Sstevel@tonic-gate 		}
662*7c478bd9Sstevel@tonic-gate 	}
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate 	err = pctx_set_events(pctx,
665*7c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_EXEC_EVENT, pexec,
666*7c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_FORK_EVENT, pfork,
667*7c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_EXIT_EVENT, pexit,
668*7c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
669*7c478bd9Sstevel@tonic-gate 	    PCTX_INIT_LWP_EVENT, pinit_lwp,
670*7c478bd9Sstevel@tonic-gate 	    PCTX_FINI_LWP_EVENT, pfini_lwp,
671*7c478bd9Sstevel@tonic-gate 	    PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
672*7c478bd9Sstevel@tonic-gate 	    PCTX_NULL_EVENT);
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate 	if (err != 0) {
675*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s\n",
676*7c478bd9Sstevel@tonic-gate 		    opts->pgmname,
677*7c478bd9Sstevel@tonic-gate 		    gettext("can't bind process context ops to process"));
678*7c478bd9Sstevel@tonic-gate 	} else {
679*7c478bd9Sstevel@tonic-gate 		if (opts->dotitle)
680*7c478bd9Sstevel@tonic-gate 			print_title(opts->master);
681*7c478bd9Sstevel@tonic-gate 		state->accum = cpc_setgrp_clone(opts->master);
682*7c478bd9Sstevel@tonic-gate 		zerotime();
683*7c478bd9Sstevel@tonic-gate 		err = pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
684*7c478bd9Sstevel@tonic-gate 		if (state->accum) {
685*7c478bd9Sstevel@tonic-gate 			cpc_setgrp_free(state->accum);
686*7c478bd9Sstevel@tonic-gate 			state->accum = NULL;
687*7c478bd9Sstevel@tonic-gate 		}
688*7c478bd9Sstevel@tonic-gate 	}
689*7c478bd9Sstevel@tonic-gate 	pctx_release(pctx);
690*7c478bd9Sstevel@tonic-gate 
691*7c478bd9Sstevel@tonic-gate 	return (err != 0 ? 1 : 0);
692*7c478bd9Sstevel@tonic-gate }
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate #define	OFFLINE_CMD	"/usr/sbin/psradm -f "
695*7c478bd9Sstevel@tonic-gate #define	BUFSIZE		5	/* enough for "n " where n is a cpuid */
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate /*
698*7c478bd9Sstevel@tonic-gate  * cpc_bind_pctx() failed with EACCES, which means the user must first offline
699*7c478bd9Sstevel@tonic-gate  * all but one logical processor on each physical processor. Print to stderr the
700*7c478bd9Sstevel@tonic-gate  * psradm command string to do this.
701*7c478bd9Sstevel@tonic-gate  */
702*7c478bd9Sstevel@tonic-gate static void
703*7c478bd9Sstevel@tonic-gate p4_ht_error(void)
704*7c478bd9Sstevel@tonic-gate {
705*7c478bd9Sstevel@tonic-gate 	kstat_ctl_t	*kc;
706*7c478bd9Sstevel@tonic-gate 	kstat_t		*ksp;
707*7c478bd9Sstevel@tonic-gate 	kstat_named_t	*k;
708*7c478bd9Sstevel@tonic-gate 	int		i;
709*7c478bd9Sstevel@tonic-gate 	int		max;
710*7c478bd9Sstevel@tonic-gate 	int		stat;
711*7c478bd9Sstevel@tonic-gate 	int		*designees;
712*7c478bd9Sstevel@tonic-gate 	int		*must_offline;
713*7c478bd9Sstevel@tonic-gate 	char		buf[BUFSIZE];
714*7c478bd9Sstevel@tonic-gate 	char		*cmd;
715*7c478bd9Sstevel@tonic-gate 	int		noffline = 0;
716*7c478bd9Sstevel@tonic-gate 	int		ndone = 0;
717*7c478bd9Sstevel@tonic-gate 
718*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s\n",
719*7c478bd9Sstevel@tonic-gate 	    gettext("Pentium 4 processors with HyperThreading present.\nOffline"
720*7c478bd9Sstevel@tonic-gate 		" all but one logical processor on each physical processor in"
721*7c478bd9Sstevel@tonic-gate 		" order to use\ncputrack.\n"));
722*7c478bd9Sstevel@tonic-gate 
723*7c478bd9Sstevel@tonic-gate 
724*7c478bd9Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
725*7c478bd9Sstevel@tonic-gate 		return;
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate 	max = sysconf(_SC_CPUID_MAX);
728*7c478bd9Sstevel@tonic-gate 	if ((designees = malloc(max * sizeof (*designees))) == NULL) {
729*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
730*7c478bd9Sstevel@tonic-gate 		    opts->pgmname);
731*7c478bd9Sstevel@tonic-gate 		exit(0);
732*7c478bd9Sstevel@tonic-gate 	}
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate 	if ((must_offline = malloc(max * sizeof (*designees))) == NULL) {
735*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
736*7c478bd9Sstevel@tonic-gate 		    opts->pgmname);
737*7c478bd9Sstevel@tonic-gate 		exit(0);
738*7c478bd9Sstevel@tonic-gate 	}
739*7c478bd9Sstevel@tonic-gate 
740*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < max; i++) {
741*7c478bd9Sstevel@tonic-gate 		designees[i] = -1;
742*7c478bd9Sstevel@tonic-gate 		must_offline[i] = 0;
743*7c478bd9Sstevel@tonic-gate 	}
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < max; i++) {
746*7c478bd9Sstevel@tonic-gate 		stat = p_online(i, P_STATUS);
747*7c478bd9Sstevel@tonic-gate 		if (stat != P_ONLINE && stat != P_NOINTR)
748*7c478bd9Sstevel@tonic-gate 			continue;
749*7c478bd9Sstevel@tonic-gate 
750*7c478bd9Sstevel@tonic-gate 		if ((ksp = kstat_lookup(kc, "cpu_info", i, NULL)) == NULL) {
751*7c478bd9Sstevel@tonic-gate 			free(designees);
752*7c478bd9Sstevel@tonic-gate 			free(must_offline);
753*7c478bd9Sstevel@tonic-gate 			return;
754*7c478bd9Sstevel@tonic-gate 		}
755*7c478bd9Sstevel@tonic-gate 
756*7c478bd9Sstevel@tonic-gate 		if (kstat_read(kc, ksp, NULL) == -1) {
757*7c478bd9Sstevel@tonic-gate 			free(designees);
758*7c478bd9Sstevel@tonic-gate 			free(must_offline);
759*7c478bd9Sstevel@tonic-gate 			return;
760*7c478bd9Sstevel@tonic-gate 		}
761*7c478bd9Sstevel@tonic-gate 
762*7c478bd9Sstevel@tonic-gate 		if ((k = (kstat_named_t *)kstat_data_lookup(ksp, "chip_id"))
763*7c478bd9Sstevel@tonic-gate 		    == NULL) {
764*7c478bd9Sstevel@tonic-gate 			free(designees);
765*7c478bd9Sstevel@tonic-gate 			free(must_offline);
766*7c478bd9Sstevel@tonic-gate 			return;
767*7c478bd9Sstevel@tonic-gate 		}
768*7c478bd9Sstevel@tonic-gate 
769*7c478bd9Sstevel@tonic-gate 		if (designees[k->value.i32] == -1)
770*7c478bd9Sstevel@tonic-gate 			/*
771*7c478bd9Sstevel@tonic-gate 			 * This chip doesn't yet have a CPU designated to remain
772*7c478bd9Sstevel@tonic-gate 			 * online; let this one be it.
773*7c478bd9Sstevel@tonic-gate 			 */
774*7c478bd9Sstevel@tonic-gate 			designees[k->value.i32] = i;
775*7c478bd9Sstevel@tonic-gate 		else {
776*7c478bd9Sstevel@tonic-gate 			/*
777*7c478bd9Sstevel@tonic-gate 			 * This chip already has a designated CPU; this CPU must
778*7c478bd9Sstevel@tonic-gate 			 * go offline.
779*7c478bd9Sstevel@tonic-gate 			 */
780*7c478bd9Sstevel@tonic-gate 			must_offline[i] = 1;
781*7c478bd9Sstevel@tonic-gate 			noffline++;
782*7c478bd9Sstevel@tonic-gate 		}
783*7c478bd9Sstevel@tonic-gate 	}
784*7c478bd9Sstevel@tonic-gate 
785*7c478bd9Sstevel@tonic-gate 	/*
786*7c478bd9Sstevel@tonic-gate 	 * Now construct a string containing the command line used to offline
787*7c478bd9Sstevel@tonic-gate 	 * the appropriate processors.
788*7c478bd9Sstevel@tonic-gate 	 */
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate 	if ((cmd = malloc(strlen(OFFLINE_CMD) + (noffline * BUFSIZE) + 1))
791*7c478bd9Sstevel@tonic-gate 	    == NULL) {
792*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
793*7c478bd9Sstevel@tonic-gate 		    opts->pgmname);
794*7c478bd9Sstevel@tonic-gate 		exit(0);
795*7c478bd9Sstevel@tonic-gate 	}
796*7c478bd9Sstevel@tonic-gate 
797*7c478bd9Sstevel@tonic-gate 	(void) strcpy(cmd, OFFLINE_CMD);
798*7c478bd9Sstevel@tonic-gate 
799*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < max; i++) {
800*7c478bd9Sstevel@tonic-gate 		if (must_offline[i] == 0)
801*7c478bd9Sstevel@tonic-gate 			continue;
802*7c478bd9Sstevel@tonic-gate 
803*7c478bd9Sstevel@tonic-gate 		ndone++;
804*7c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, BUFSIZE, "%d", i);
805*7c478bd9Sstevel@tonic-gate 		if (ndone < noffline)
806*7c478bd9Sstevel@tonic-gate 			(void) strcat(buf, " ");
807*7c478bd9Sstevel@tonic-gate 		(void) strcat(cmd, buf);
808*7c478bd9Sstevel@tonic-gate 	}
809*7c478bd9Sstevel@tonic-gate 
810*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\n%s\n", gettext("The following command "
811*7c478bd9Sstevel@tonic-gate 	    "will configure the system appropriately"), cmd);
812*7c478bd9Sstevel@tonic-gate 
813*7c478bd9Sstevel@tonic-gate 	exit(1);
814*7c478bd9Sstevel@tonic-gate }
815