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