xref: /onnv-gate/usr/src/cmd/cpc/common/cpustat.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 2005 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/types.h>
30*0Sstevel@tonic-gate #include <sys/processor.h>
31*0Sstevel@tonic-gate #include <sys/pset.h>
32*0Sstevel@tonic-gate #include <sys/lwp.h>
33*0Sstevel@tonic-gate #include <sys/priocntl.h>
34*0Sstevel@tonic-gate #include <sys/fxpriocntl.h>
35*0Sstevel@tonic-gate #include <time.h>
36*0Sstevel@tonic-gate #include <stdio.h>
37*0Sstevel@tonic-gate #include <stdlib.h>
38*0Sstevel@tonic-gate #include <inttypes.h>
39*0Sstevel@tonic-gate #include <unistd.h>
40*0Sstevel@tonic-gate #include <limits.h>
41*0Sstevel@tonic-gate #include <string.h>
42*0Sstevel@tonic-gate #include <strings.h>
43*0Sstevel@tonic-gate #include <thread.h>
44*0Sstevel@tonic-gate #include <errno.h>
45*0Sstevel@tonic-gate #include <libintl.h>
46*0Sstevel@tonic-gate #include <locale.h>
47*0Sstevel@tonic-gate #include <kstat.h>
48*0Sstevel@tonic-gate #include <synch.h>
49*0Sstevel@tonic-gate #include <libcpc.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include "cpucmds.h"
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate static struct options {
54*0Sstevel@tonic-gate 	int debug;
55*0Sstevel@tonic-gate 	int dotitle;
56*0Sstevel@tonic-gate 	int dohelp;
57*0Sstevel@tonic-gate 	int dotick;
58*0Sstevel@tonic-gate 	int dosoaker;
59*0Sstevel@tonic-gate 	int doperiod;
60*0Sstevel@tonic-gate 	char *pgmname;
61*0Sstevel@tonic-gate 	uint_t mseconds;
62*0Sstevel@tonic-gate 	uint_t nsamples;
63*0Sstevel@tonic-gate 	uint_t nsets;
64*0Sstevel@tonic-gate 	uint_t mseconds_rest;
65*0Sstevel@tonic-gate 	cpc_setgrp_t *master;
66*0Sstevel@tonic-gate } __options;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate  * States for soaker threads.
70*0Sstevel@tonic-gate  */
71*0Sstevel@tonic-gate #define	SOAK_PAUSE	0
72*0Sstevel@tonic-gate #define	SOAK_RUN	1
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate struct tstate {
75*0Sstevel@tonic-gate 	processorid_t	cpuid;
76*0Sstevel@tonic-gate 	int		chip_id;
77*0Sstevel@tonic-gate 	cpc_setgrp_t	*sgrp;
78*0Sstevel@tonic-gate 	int		status;
79*0Sstevel@tonic-gate 	thread_t	tid;
80*0Sstevel@tonic-gate 	int		soak_state;
81*0Sstevel@tonic-gate 	mutex_t		soak_lock;
82*0Sstevel@tonic-gate 	cond_t		soak_cv;
83*0Sstevel@tonic-gate };
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate static const struct options *opts = (const struct options *)&__options;
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate static cpc_t *cpc;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate struct tstate	*gstate;
90*0Sstevel@tonic-gate static int	ncpus;
91*0Sstevel@tonic-gate static int	max_chip_id;
92*0Sstevel@tonic-gate static int	*chip_designees;    /* cpuid of CPU which counts for phs chip */
93*0Sstevel@tonic-gate static int	smt = 0;	    /* If set, cpustat needs to be SMT-aware. */
94*0Sstevel@tonic-gate static pcinfo_t	fxinfo = { 0, "FX", NULL }; /* FX scheduler class info */
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*ARGSUSED*/
97*0Sstevel@tonic-gate static void
98*0Sstevel@tonic-gate cpustat_errfn(const char *fn, int subcode, const char *fmt, va_list ap)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", opts->pgmname);
101*0Sstevel@tonic-gate 	if (opts->debug)
102*0Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", fn);
103*0Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate static int cpustat(void);
107*0Sstevel@tonic-gate static int get_chipid(kstat_ctl_t *kc, processorid_t cpuid);
108*0Sstevel@tonic-gate static void *soaker(void *arg);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
112*0Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
113*0Sstevel@tonic-gate #endif
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate int
116*0Sstevel@tonic-gate main(int argc, char *argv[])
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	struct options	*opts = &__options;
119*0Sstevel@tonic-gate 	int		c, errcnt = 0, ret;
120*0Sstevel@tonic-gate 	cpc_setgrp_t	*sgrp;
121*0Sstevel@tonic-gate 	char		*errstr;
122*0Sstevel@tonic-gate 	double		period;
123*0Sstevel@tonic-gate 	char		*endp;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
126*0Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	if ((opts->pgmname = strrchr(argv[0], '/')) == NULL)
129*0Sstevel@tonic-gate 		opts->pgmname = argv[0];
130*0Sstevel@tonic-gate 	else
131*0Sstevel@tonic-gate 		opts->pgmname++;
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	if ((cpc = cpc_open(CPC_VER_CURRENT)) == NULL) {
134*0Sstevel@tonic-gate 		errstr = strerror(errno);
135*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: cannot access performance "
136*0Sstevel@tonic-gate 		    "counters - %s\n"), opts->pgmname, errstr);
137*0Sstevel@tonic-gate 		return (1);
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	(void) cpc_seterrhndlr(cpc, cpustat_errfn);
141*0Sstevel@tonic-gate 	strtoset_errfn = cpustat_errfn;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	/*
144*0Sstevel@tonic-gate 	 * Check to see if cpustat needs to be SMT-aware.
145*0Sstevel@tonic-gate 	 */
146*0Sstevel@tonic-gate 	smt = smt_limited_cpc_hw(cpc);
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	/*
149*0Sstevel@tonic-gate 	 * Establish some defaults
150*0Sstevel@tonic-gate 	 */
151*0Sstevel@tonic-gate 	opts->mseconds = 5000;
152*0Sstevel@tonic-gate 	opts->nsamples = UINT_MAX;
153*0Sstevel@tonic-gate 	opts->dotitle = 1;
154*0Sstevel@tonic-gate 	if ((opts->master = cpc_setgrp_new(cpc, smt)) == NULL) {
155*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of heap\n"),
156*0Sstevel@tonic-gate 		    opts->pgmname);
157*0Sstevel@tonic-gate 		return (1);
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Dc:hntsp:")) != EOF && errcnt == 0)
161*0Sstevel@tonic-gate 		switch (c) {
162*0Sstevel@tonic-gate 		case 'D':			/* enable debugging */
163*0Sstevel@tonic-gate 			opts->debug++;
164*0Sstevel@tonic-gate 			break;
165*0Sstevel@tonic-gate 		case 'c':			/* specify statistics */
166*0Sstevel@tonic-gate 			if ((sgrp = cpc_setgrp_newset(opts->master,
167*0Sstevel@tonic-gate 			    optarg, &errcnt)) != NULL)
168*0Sstevel@tonic-gate 				opts->master = sgrp;
169*0Sstevel@tonic-gate 			break;
170*0Sstevel@tonic-gate 		case 'n':			/* no titles */
171*0Sstevel@tonic-gate 			opts->dotitle = 0;
172*0Sstevel@tonic-gate 			break;
173*0Sstevel@tonic-gate 		case 'p':			/* periodic behavior */
174*0Sstevel@tonic-gate 			opts->doperiod = 1;
175*0Sstevel@tonic-gate 			period = strtod(optarg, &endp);
176*0Sstevel@tonic-gate 			if (*endp != '\0') {
177*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: invalid "
178*0Sstevel@tonic-gate 				    "parameter \"%s\"\n"), opts->pgmname,
179*0Sstevel@tonic-gate 				    optarg);
180*0Sstevel@tonic-gate 				errcnt++;
181*0Sstevel@tonic-gate 			}
182*0Sstevel@tonic-gate 			break;
183*0Sstevel@tonic-gate 		case 's':			/* run soaker thread */
184*0Sstevel@tonic-gate 			opts->dosoaker = 1;
185*0Sstevel@tonic-gate 			break;
186*0Sstevel@tonic-gate 		case 't':			/* print %tick */
187*0Sstevel@tonic-gate 			opts->dotick = 1;
188*0Sstevel@tonic-gate 			break;
189*0Sstevel@tonic-gate 		case 'h':			/* help */
190*0Sstevel@tonic-gate 			opts->dohelp = 1;
191*0Sstevel@tonic-gate 			break;
192*0Sstevel@tonic-gate 		case '?':
193*0Sstevel@tonic-gate 		default:
194*0Sstevel@tonic-gate 			errcnt++;
195*0Sstevel@tonic-gate 			break;
196*0Sstevel@tonic-gate 		}
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	switch (argc - optind) {
199*0Sstevel@tonic-gate 	case 0:
200*0Sstevel@tonic-gate 		break;
201*0Sstevel@tonic-gate 	case 2:
202*0Sstevel@tonic-gate 		opts->nsamples = strtol(argv[optind + 1], &endp, 10);
203*0Sstevel@tonic-gate 		if (*endp != '\0') {
204*0Sstevel@tonic-gate 			(void) fprintf(stderr,
205*0Sstevel@tonic-gate 			    gettext("%s: invalid argument \"%s\"\n"),
206*0Sstevel@tonic-gate 			    opts->pgmname, argv[optind + 1]);
207*0Sstevel@tonic-gate 			errcnt++;
208*0Sstevel@tonic-gate 			break;
209*0Sstevel@tonic-gate 		}
210*0Sstevel@tonic-gate 		/*FALLTHROUGH*/
211*0Sstevel@tonic-gate 	case 1:
212*0Sstevel@tonic-gate 		opts->mseconds = (uint_t)(strtod(argv[optind], &endp) * 1000.0);
213*0Sstevel@tonic-gate 		if (*endp != '\0') {
214*0Sstevel@tonic-gate 			(void) fprintf(stderr,
215*0Sstevel@tonic-gate 			    gettext("%s: invalid argument \"%s\"\n"),
216*0Sstevel@tonic-gate 			    opts->pgmname, argv[optind]);
217*0Sstevel@tonic-gate 			errcnt++;
218*0Sstevel@tonic-gate 		}
219*0Sstevel@tonic-gate 		break;
220*0Sstevel@tonic-gate 	default:
221*0Sstevel@tonic-gate 		errcnt++;
222*0Sstevel@tonic-gate 		break;
223*0Sstevel@tonic-gate 	}
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	if (opts->nsamples == 0 || opts->mseconds == 0)
226*0Sstevel@tonic-gate 		errcnt++;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	if (errcnt != 0 || opts->dohelp ||
229*0Sstevel@tonic-gate 	    (opts->nsets = cpc_setgrp_numsets(opts->master)) == 0) {
230*0Sstevel@tonic-gate 		(void) fprintf(opts->dohelp ? stdout : stderr, gettext(
231*0Sstevel@tonic-gate 		    "Usage:\n\t%s [-c events] [-p period] [-nstD] "
232*0Sstevel@tonic-gate 			"[interval [count]]\n\n"
233*0Sstevel@tonic-gate 		    "\t-c events specify processor events to be monitored\n"
234*0Sstevel@tonic-gate 		    "\t-n\t  suppress titles\n"
235*0Sstevel@tonic-gate 		    "\t-p period cycle through event list periodically\n"
236*0Sstevel@tonic-gate 		    "\t-s\t  run user soaker thread for system-only events\n"
237*0Sstevel@tonic-gate 		    "\t-t\t  include %s register\n"
238*0Sstevel@tonic-gate 		    "\t-D\t  enable debug mode\n"
239*0Sstevel@tonic-gate 		    "\t-h\t  print extended usage information\n\n"
240*0Sstevel@tonic-gate 		    "\tUse cputrack(1) to monitor per-process statistics.\n"),
241*0Sstevel@tonic-gate 		    opts->pgmname, CPC_TICKREG_NAME);
242*0Sstevel@tonic-gate 		if (opts->dohelp) {
243*0Sstevel@tonic-gate 			(void) putchar('\n');
244*0Sstevel@tonic-gate 			(void) capabilities(cpc, stdout);
245*0Sstevel@tonic-gate 			exit(0);
246*0Sstevel@tonic-gate 		}
247*0Sstevel@tonic-gate 		exit(2);
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	/*
251*0Sstevel@tonic-gate 	 * If the user requested periodic behavior, calculate the rest time
252*0Sstevel@tonic-gate 	 * between cycles.
253*0Sstevel@tonic-gate 	 */
254*0Sstevel@tonic-gate 	if (opts->doperiod) {
255*0Sstevel@tonic-gate 		opts->mseconds_rest = (uint_t)((period * 1000.0) -
256*0Sstevel@tonic-gate 		    (opts->mseconds * opts->nsets));
257*0Sstevel@tonic-gate 		if ((int)opts->mseconds_rest < 0)
258*0Sstevel@tonic-gate 			opts->mseconds_rest = 0;
259*0Sstevel@tonic-gate 		if (opts->nsamples != UINT_MAX)
260*0Sstevel@tonic-gate 			opts->nsamples *= opts->nsets;
261*0Sstevel@tonic-gate 	}
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	cpc_setgrp_reset(opts->master);
264*0Sstevel@tonic-gate 	(void) setvbuf(stdout, NULL, _IOLBF, 0);
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	/*
267*0Sstevel@tonic-gate 	 * If no system-mode only sets were created, no soaker threads will be
268*0Sstevel@tonic-gate 	 * needed.
269*0Sstevel@tonic-gate 	 */
270*0Sstevel@tonic-gate 	if (opts->dosoaker == 1 && cpc_setgrp_has_sysonly(opts->master) == 0)
271*0Sstevel@tonic-gate 		opts->dosoaker = 0;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	ret = cpustat();
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	(void) cpc_close(cpc);
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	return (ret);
278*0Sstevel@tonic-gate }
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate static void
281*0Sstevel@tonic-gate print_title(cpc_setgrp_t *sgrp)
282*0Sstevel@tonic-gate {
283*0Sstevel@tonic-gate 	(void) printf("%7s %3s %5s ", "time", "cpu", "event");
284*0Sstevel@tonic-gate 	if (opts->dotick)
285*0Sstevel@tonic-gate 		(void) printf("%9s ", CPC_TICKREG_NAME);
286*0Sstevel@tonic-gate 	(void) printf("%s\n", cpc_setgrp_gethdr(sgrp));
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate static void
290*0Sstevel@tonic-gate print_sample(processorid_t cpuid, cpc_buf_t *buf, int nreq, const char *setname,
291*0Sstevel@tonic-gate     int sibling)
292*0Sstevel@tonic-gate {
293*0Sstevel@tonic-gate 	char		line[1024];
294*0Sstevel@tonic-gate 	int		ccnt;
295*0Sstevel@tonic-gate 	int		i;
296*0Sstevel@tonic-gate 	uint64_t	val;
297*0Sstevel@tonic-gate 	uint64_t	tick;
298*0Sstevel@tonic-gate 	hrtime_t	hrtime;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	hrtime = cpc_buf_hrtime(cpc, buf);
301*0Sstevel@tonic-gate 	tick = cpc_buf_tick(cpc, buf);
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	ccnt = snprintf(line, sizeof (line), "%7.3f %3d %5s ",
304*0Sstevel@tonic-gate 	    mstimestamp(hrtime), (int)cpuid, "tick");
305*0Sstevel@tonic-gate 	if (opts->dotick)
306*0Sstevel@tonic-gate 		ccnt += snprintf(line + ccnt, sizeof (line) - ccnt,
307*0Sstevel@tonic-gate 		    "%9" PRId64 " ", tick);
308*0Sstevel@tonic-gate 	for (i = 0; i < nreq; i++) {
309*0Sstevel@tonic-gate 		(void) cpc_buf_get(cpc, buf, i, &val);
310*0Sstevel@tonic-gate 		ccnt += snprintf(line + ccnt, sizeof (line) - ccnt,
311*0Sstevel@tonic-gate 		    "%9" PRId64 " ", val);
312*0Sstevel@tonic-gate 	}
313*0Sstevel@tonic-gate 	if (opts->nsets > 1)
314*0Sstevel@tonic-gate 		ccnt += snprintf(line + ccnt, sizeof (line) - ccnt,
315*0Sstevel@tonic-gate 		    " # %s\n", setname);
316*0Sstevel@tonic-gate 	else
317*0Sstevel@tonic-gate 		ccnt += snprintf(line + ccnt, sizeof (line) - ccnt, "\n");
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	if (sibling) {
320*0Sstevel@tonic-gate 		/*
321*0Sstevel@tonic-gate 		 * This sample is being printed for a "sibling" CPU -- that is,
322*0Sstevel@tonic-gate 		 * a CPU which does not have its own CPC set bound. It is being
323*0Sstevel@tonic-gate 		 * measured via a set bound to another CPU sharing its physical
324*0Sstevel@tonic-gate 		 * processor.
325*0Sstevel@tonic-gate 		 */
326*0Sstevel@tonic-gate 		int designee = chip_designees[gstate[cpuid].chip_id];
327*0Sstevel@tonic-gate 		char *p;
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 		if ((p = strrchr(line, '#')) == NULL)
330*0Sstevel@tonic-gate 			p = strrchr(line, '\n');
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 		if (p != NULL) {
333*0Sstevel@tonic-gate 			*p = '\0';
334*0Sstevel@tonic-gate 			ccnt = strlen(line);
335*0Sstevel@tonic-gate 			ccnt += snprintf(line + ccnt, sizeof (line) - ccnt,
336*0Sstevel@tonic-gate 			    "# counter shared with CPU %d\n", designee);
337*0Sstevel@tonic-gate 		}
338*0Sstevel@tonic-gate 	}
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	if (ccnt > sizeof (line))
341*0Sstevel@tonic-gate 		ccnt = sizeof (line);
342*0Sstevel@tonic-gate 	if (ccnt > 0)
343*0Sstevel@tonic-gate 		(void) write(1, line, ccnt);
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	/*
346*0Sstevel@tonic-gate 	 * If this CPU is the chip designee for any other CPUs, print a line for
347*0Sstevel@tonic-gate 	 * them here.
348*0Sstevel@tonic-gate 	 */
349*0Sstevel@tonic-gate 	if (smt && (sibling == 0)) {
350*0Sstevel@tonic-gate 		for (i = 0; i < ncpus; i++) {
351*0Sstevel@tonic-gate 			if ((i != cpuid) && (gstate[i].cpuid != -1) &&
352*0Sstevel@tonic-gate 			    (chip_designees[gstate[i].chip_id] == cpuid))
353*0Sstevel@tonic-gate 				print_sample(i, buf, nreq, setname, 1);
354*0Sstevel@tonic-gate 		}
355*0Sstevel@tonic-gate 	}
356*0Sstevel@tonic-gate }
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate static void
359*0Sstevel@tonic-gate print_total(int ncpus, cpc_buf_t *buf, int nreq, const char *setname)
360*0Sstevel@tonic-gate {
361*0Sstevel@tonic-gate 	int		i;
362*0Sstevel@tonic-gate 	uint64_t	val;
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 	(void) printf("%7.3f %3d %5s ", mstimestamp(cpc_buf_hrtime(cpc, buf)),
365*0Sstevel@tonic-gate 	    ncpus, "total");
366*0Sstevel@tonic-gate 	if (opts->dotick)
367*0Sstevel@tonic-gate 		(void) printf("%9" PRId64 " ", cpc_buf_tick(cpc, buf));
368*0Sstevel@tonic-gate 	for (i = 0; i < nreq; i++) {
369*0Sstevel@tonic-gate 		(void) cpc_buf_get(cpc, buf, i, &val);
370*0Sstevel@tonic-gate 		(void) printf("%9" PRId64 " ", val);
371*0Sstevel@tonic-gate 	}
372*0Sstevel@tonic-gate 	if (opts->nsets > 1)
373*0Sstevel@tonic-gate 		(void) printf(" # %s", setname);
374*0Sstevel@tonic-gate 	(void) fputc('\n', stdout);
375*0Sstevel@tonic-gate }
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate #define	NSECS_PER_MSEC	1000000ll
378*0Sstevel@tonic-gate #define	NSECS_PER_SEC	1000000000ll
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate static void *
381*0Sstevel@tonic-gate gtick(void *arg)
382*0Sstevel@tonic-gate {
383*0Sstevel@tonic-gate 	struct tstate		*state = arg;
384*0Sstevel@tonic-gate 	char			*errstr;
385*0Sstevel@tonic-gate 	uint_t			nsamples;
386*0Sstevel@tonic-gate 	uint_t			sample_cnt = 1;
387*0Sstevel@tonic-gate 	hrtime_t		ht, htdelta, restdelta;
388*0Sstevel@tonic-gate 	cpc_setgrp_t		*sgrp = state->sgrp;
389*0Sstevel@tonic-gate 	cpc_set_t		*this = cpc_setgrp_getset(sgrp);
390*0Sstevel@tonic-gate 	const char		*name = cpc_setgrp_getname(sgrp);
391*0Sstevel@tonic-gate 	cpc_buf_t		**data1, **data2, **scratch;
392*0Sstevel@tonic-gate 	cpc_buf_t		*tmp;
393*0Sstevel@tonic-gate 	int			nreqs;
394*0Sstevel@tonic-gate 	thread_t		tid;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	htdelta = NSECS_PER_MSEC * opts->mseconds;
397*0Sstevel@tonic-gate 	restdelta = NSECS_PER_MSEC * opts->mseconds_rest;
398*0Sstevel@tonic-gate 	ht = gethrtime();
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	/*
401*0Sstevel@tonic-gate 	 * If this CPU is SMT, we run one gtick() thread per _physical_ CPU,
402*0Sstevel@tonic-gate 	 * instead of per cpu_t. The following check returns if it detects that
403*0Sstevel@tonic-gate 	 * this cpu_t has not been designated to do the counting for this
404*0Sstevel@tonic-gate 	 * physical CPU.
405*0Sstevel@tonic-gate 	 */
406*0Sstevel@tonic-gate 	if (smt && chip_designees[state->chip_id] != state->cpuid)
407*0Sstevel@tonic-gate 		return (NULL);
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	/*
410*0Sstevel@tonic-gate 	 * If we need to run a soaker thread on this CPU, start it here.
411*0Sstevel@tonic-gate 	 */
412*0Sstevel@tonic-gate 	if (opts->dosoaker) {
413*0Sstevel@tonic-gate 		if (cond_init(&state->soak_cv, USYNC_THREAD, NULL) != 0)
414*0Sstevel@tonic-gate 			goto bad;
415*0Sstevel@tonic-gate 		if (mutex_init(&state->soak_lock, USYNC_THREAD,
416*0Sstevel@tonic-gate 		    NULL) != 0)
417*0Sstevel@tonic-gate 			goto bad;
418*0Sstevel@tonic-gate 		(void) mutex_lock(&state->soak_lock);
419*0Sstevel@tonic-gate 		state->soak_state = SOAK_PAUSE;
420*0Sstevel@tonic-gate 		if (thr_create(NULL, 0, soaker, state, NULL, &tid) != 0)
421*0Sstevel@tonic-gate 			goto bad;
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 		while (state->soak_state == SOAK_PAUSE)
424*0Sstevel@tonic-gate 			(void) cond_wait(&state->soak_cv,
425*0Sstevel@tonic-gate 			    &state->soak_lock);
426*0Sstevel@tonic-gate 		(void) mutex_unlock(&state->soak_lock);
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 		/*
429*0Sstevel@tonic-gate 		 * If the soaker needs to pause for the first set, stop it now.
430*0Sstevel@tonic-gate 		 */
431*0Sstevel@tonic-gate 		if (cpc_setgrp_sysonly(sgrp) == 0) {
432*0Sstevel@tonic-gate 			(void) mutex_lock(&state->soak_lock);
433*0Sstevel@tonic-gate 			state->soak_state = SOAK_PAUSE;
434*0Sstevel@tonic-gate 			(void) mutex_unlock(&state->soak_lock);
435*0Sstevel@tonic-gate 		}
436*0Sstevel@tonic-gate 	}
437*0Sstevel@tonic-gate 	if (cpc_bind_cpu(cpc, state->cpuid, this, 0) == -1)
438*0Sstevel@tonic-gate 		goto bad;
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 	for (nsamples = opts->nsamples; nsamples; nsamples--, sample_cnt++) {
441*0Sstevel@tonic-gate 		hrtime_t htnow;
442*0Sstevel@tonic-gate 		struct timespec ts;
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 		nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 		ht += htdelta;
447*0Sstevel@tonic-gate 		htnow = gethrtime();
448*0Sstevel@tonic-gate 		if (ht <= htnow)
449*0Sstevel@tonic-gate 			continue;
450*0Sstevel@tonic-gate 		ts.tv_sec = (time_t)((ht - htnow) / NSECS_PER_SEC);
451*0Sstevel@tonic-gate 		ts.tv_nsec = (suseconds_t)((ht - htnow) % NSECS_PER_SEC);
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 		(void) nanosleep(&ts, NULL);
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 		if (opts->nsets == 1) {
456*0Sstevel@tonic-gate 			/*
457*0Sstevel@tonic-gate 			 * If we're dealing with one set, buffer usage is:
458*0Sstevel@tonic-gate 			 *
459*0Sstevel@tonic-gate 			 * data1 = most recent data snapshot
460*0Sstevel@tonic-gate 			 * data2 = previous data snapshot
461*0Sstevel@tonic-gate 			 * scratch = used for diffing data1 and data2
462*0Sstevel@tonic-gate 			 *
463*0Sstevel@tonic-gate 			 * Save the snapshot from the previous sample in data2
464*0Sstevel@tonic-gate 			 * before putting the current sample in data1.
465*0Sstevel@tonic-gate 			 */
466*0Sstevel@tonic-gate 			tmp = *data1;
467*0Sstevel@tonic-gate 			*data1 = *data2;
468*0Sstevel@tonic-gate 			*data2 = tmp;
469*0Sstevel@tonic-gate 			if (cpc_set_sample(cpc, this, *data1) != 0)
470*0Sstevel@tonic-gate 				goto bad;
471*0Sstevel@tonic-gate 			cpc_buf_sub(cpc, *scratch, *data1, *data2);
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 			print_sample(state->cpuid, *scratch, nreqs, name, 0);
474*0Sstevel@tonic-gate 		} else {
475*0Sstevel@tonic-gate 			/*
476*0Sstevel@tonic-gate 			 * More than one set is in use (multiple -c options
477*0Sstevel@tonic-gate 			 * given). Buffer usage in this case is:
478*0Sstevel@tonic-gate 			 *
479*0Sstevel@tonic-gate 			 * data1 = total counts for this set since program began
480*0Sstevel@tonic-gate 			 * data2 = unused
481*0Sstevel@tonic-gate 			 * scratch = most recent data snapshot
482*0Sstevel@tonic-gate 			 */
483*0Sstevel@tonic-gate 			name = cpc_setgrp_getname(sgrp);
484*0Sstevel@tonic-gate 			nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2,
485*0Sstevel@tonic-gate 			    &scratch);
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate 			if (cpc_set_sample(cpc, this, *scratch) != 0)
488*0Sstevel@tonic-gate 				goto bad;
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 			cpc_buf_add(cpc, *data1, *data1, *scratch);
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 			if (cpc_unbind(cpc, this) != 0)
493*0Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("%s: error "
494*0Sstevel@tonic-gate 				    "unbinding on cpu %d - %s\n"),
495*0Sstevel@tonic-gate 				    opts->pgmname, state->cpuid,
496*0Sstevel@tonic-gate 				    strerror(errno));
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 			this = cpc_setgrp_nextset(sgrp);
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 			print_sample(state->cpuid, *scratch, nreqs, name, 0);
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 			/*
503*0Sstevel@tonic-gate 			 * If periodic behavior was requested, rest here.
504*0Sstevel@tonic-gate 			 */
505*0Sstevel@tonic-gate 			if (opts->doperiod && opts->mseconds_rest > 0 &&
506*0Sstevel@tonic-gate 				(sample_cnt % opts->nsets) == 0) {
507*0Sstevel@tonic-gate 				/*
508*0Sstevel@tonic-gate 				 * Stop the soaker while the tool rests.
509*0Sstevel@tonic-gate 				 */
510*0Sstevel@tonic-gate 				if (opts->dosoaker) {
511*0Sstevel@tonic-gate 					(void) mutex_lock(&state->soak_lock);
512*0Sstevel@tonic-gate 					if (state->soak_state == SOAK_RUN)
513*0Sstevel@tonic-gate 						state->soak_state = SOAK_PAUSE;
514*0Sstevel@tonic-gate 					(void) mutex_unlock(&state->soak_lock);
515*0Sstevel@tonic-gate 				}
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 				htnow = gethrtime();
518*0Sstevel@tonic-gate 				ht += restdelta;
519*0Sstevel@tonic-gate 				ts.tv_sec = (time_t)((ht - htnow) /
520*0Sstevel@tonic-gate 				    NSECS_PER_SEC);
521*0Sstevel@tonic-gate 				ts.tv_nsec = (suseconds_t)((ht - htnow) %
522*0Sstevel@tonic-gate 				    NSECS_PER_SEC);
523*0Sstevel@tonic-gate 
524*0Sstevel@tonic-gate 				(void) nanosleep(&ts, NULL);
525*0Sstevel@tonic-gate 			}
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate 			/*
528*0Sstevel@tonic-gate 			 * Start or stop the soaker if needed.
529*0Sstevel@tonic-gate 			 */
530*0Sstevel@tonic-gate 			if (opts->dosoaker) {
531*0Sstevel@tonic-gate 				(void) mutex_lock(&state->soak_lock);
532*0Sstevel@tonic-gate 				if (cpc_setgrp_sysonly(sgrp) &&
533*0Sstevel@tonic-gate 				    state->soak_state == SOAK_PAUSE) {
534*0Sstevel@tonic-gate 					/*
535*0Sstevel@tonic-gate 					 * Soaker is paused but the next set is
536*0Sstevel@tonic-gate 					 * sysonly: start the soaker.
537*0Sstevel@tonic-gate 					 */
538*0Sstevel@tonic-gate 					state->soak_state = SOAK_RUN;
539*0Sstevel@tonic-gate 					(void) cond_signal(&state->soak_cv);
540*0Sstevel@tonic-gate 				} else if (cpc_setgrp_sysonly(sgrp) == 0 &&
541*0Sstevel@tonic-gate 				    state->soak_state == SOAK_RUN)
542*0Sstevel@tonic-gate 					/*
543*0Sstevel@tonic-gate 					 * Soaker is running but the next set
544*0Sstevel@tonic-gate 					 * counts user events: stop the soaker.
545*0Sstevel@tonic-gate 					 */
546*0Sstevel@tonic-gate 					state->soak_state = SOAK_PAUSE;
547*0Sstevel@tonic-gate 				(void) mutex_unlock(&state->soak_lock);
548*0Sstevel@tonic-gate 			}
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate 			if (cpc_bind_cpu(cpc, state->cpuid, this, 0) != 0)
551*0Sstevel@tonic-gate 				goto bad;
552*0Sstevel@tonic-gate 		}
553*0Sstevel@tonic-gate 	}
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 	if (cpc_unbind(cpc, this) != 0)
556*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: error unbinding on"
557*0Sstevel@tonic-gate 		    " cpu %d - %s\n"), opts->pgmname,
558*0Sstevel@tonic-gate 		    state->cpuid, strerror(errno));
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate 	/*
561*0Sstevel@tonic-gate 	 * We're done, so stop the soaker if needed.
562*0Sstevel@tonic-gate 	 */
563*0Sstevel@tonic-gate 	if (opts->dosoaker) {
564*0Sstevel@tonic-gate 		(void) mutex_lock(&state->soak_lock);
565*0Sstevel@tonic-gate 		if (state->soak_state == SOAK_RUN)
566*0Sstevel@tonic-gate 			state->soak_state = SOAK_PAUSE;
567*0Sstevel@tonic-gate 		(void) mutex_unlock(&state->soak_lock);
568*0Sstevel@tonic-gate 	}
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate 	return (NULL);
571*0Sstevel@tonic-gate bad:
572*0Sstevel@tonic-gate 	state->status = 3;
573*0Sstevel@tonic-gate 	errstr = strerror(errno);
574*0Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%s: cpu%d - %s\n"),
575*0Sstevel@tonic-gate 	    opts->pgmname, state->cpuid, errstr);
576*0Sstevel@tonic-gate 	return (NULL);
577*0Sstevel@tonic-gate }
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate static int
580*0Sstevel@tonic-gate cpustat(void)
581*0Sstevel@tonic-gate {
582*0Sstevel@tonic-gate 	cpc_setgrp_t	*accum;
583*0Sstevel@tonic-gate 	cpc_set_t	*start;
584*0Sstevel@tonic-gate 	int		c, i, retval;
585*0Sstevel@tonic-gate 	int		lwps = 0;
586*0Sstevel@tonic-gate 	psetid_t	mypset, cpupset;
587*0Sstevel@tonic-gate 	char		*errstr;
588*0Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
589*0Sstevel@tonic-gate 	int		nreqs;
590*0Sstevel@tonic-gate 	kstat_ctl_t	*kc;
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 	ncpus = (int)sysconf(_SC_NPROCESSORS_CONF);
593*0Sstevel@tonic-gate 	if ((gstate = calloc(ncpus, sizeof (*gstate))) == NULL) {
594*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
595*0Sstevel@tonic-gate 		    "%s: out of heap\n"), opts->pgmname);
596*0Sstevel@tonic-gate 		return (1);
597*0Sstevel@tonic-gate 	}
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 	max_chip_id = sysconf(_SC_CPUID_MAX);
600*0Sstevel@tonic-gate 	if ((chip_designees = malloc(max_chip_id * sizeof (int))) == NULL) {
601*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
602*0Sstevel@tonic-gate 			"%s: out of heap\n"), opts->pgmname);
603*0Sstevel@tonic-gate 		return (1);
604*0Sstevel@tonic-gate 	}
605*0Sstevel@tonic-gate 	for (i = 0; i < max_chip_id; i++)
606*0Sstevel@tonic-gate 		chip_designees[i] = -1;
607*0Sstevel@tonic-gate 
608*0Sstevel@tonic-gate 	if (smt) {
609*0Sstevel@tonic-gate 		if ((kc = kstat_open()) == NULL) {
610*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
611*0Sstevel@tonic-gate 				"%s: kstat_open() failed: %s\n"), opts->pgmname,
612*0Sstevel@tonic-gate 			    strerror(errno));
613*0Sstevel@tonic-gate 			    return (1);
614*0Sstevel@tonic-gate 		}
615*0Sstevel@tonic-gate 	}
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	if (opts->dosoaker)
618*0Sstevel@tonic-gate 		if (priocntl(0, 0, PC_GETCID, &fxinfo) == -1) {
619*0Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
620*0Sstevel@tonic-gate 				"%s: couldn't get FX scheduler class: %s\n"),
621*0Sstevel@tonic-gate 			    opts->pgmname, strerror(errno));
622*0Sstevel@tonic-gate 			return (1);
623*0Sstevel@tonic-gate 		}
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 	/*
626*0Sstevel@tonic-gate 	 * Only include processors that are participating in the system
627*0Sstevel@tonic-gate 	 */
628*0Sstevel@tonic-gate 	for (c = 0, i = 0; i < ncpus; c++) {
629*0Sstevel@tonic-gate 		switch (p_online(c, P_STATUS)) {
630*0Sstevel@tonic-gate 		case P_ONLINE:
631*0Sstevel@tonic-gate 		case P_NOINTR:
632*0Sstevel@tonic-gate 			if (smt) {
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate 				gstate[i].chip_id = get_chipid(kc, c);
635*0Sstevel@tonic-gate 				if (gstate[i].chip_id != -1 &&
636*0Sstevel@tonic-gate 				    chip_designees[gstate[i].chip_id] == -1)
637*0Sstevel@tonic-gate 					chip_designees[gstate[i].chip_id] = c;
638*0Sstevel@tonic-gate 			}
639*0Sstevel@tonic-gate 
640*0Sstevel@tonic-gate 			gstate[i++].cpuid = c;
641*0Sstevel@tonic-gate 			break;
642*0Sstevel@tonic-gate 		case P_OFFLINE:
643*0Sstevel@tonic-gate 		case P_POWEROFF:
644*0Sstevel@tonic-gate 		case P_FAULTED:
645*0Sstevel@tonic-gate 		case P_SPARE:
646*0Sstevel@tonic-gate 			gstate[i++].cpuid = -1;
647*0Sstevel@tonic-gate 			break;
648*0Sstevel@tonic-gate 		default:
649*0Sstevel@tonic-gate 			gstate[i++].cpuid = -1;
650*0Sstevel@tonic-gate 			(void) fprintf(stderr,
651*0Sstevel@tonic-gate 			    gettext("%s: cpu%d in unknown state\n"),
652*0Sstevel@tonic-gate 			    opts->pgmname, c);
653*0Sstevel@tonic-gate 			break;
654*0Sstevel@tonic-gate 		case -1:
655*0Sstevel@tonic-gate 			break;
656*0Sstevel@tonic-gate 		}
657*0Sstevel@tonic-gate 	}
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 	/*
660*0Sstevel@tonic-gate 	 * Examine the processor sets; if we're in one, only attempt
661*0Sstevel@tonic-gate 	 * to report on the set we're in.
662*0Sstevel@tonic-gate 	 */
663*0Sstevel@tonic-gate 	if (pset_bind(PS_QUERY, P_PID, P_MYID, &mypset) == -1) {
664*0Sstevel@tonic-gate 		errstr = strerror(errno);
665*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: pset_bind - %s\n"),
666*0Sstevel@tonic-gate 		    opts->pgmname, errstr);
667*0Sstevel@tonic-gate 	} else {
668*0Sstevel@tonic-gate 		for (i = 0; i < ncpus; i++) {
669*0Sstevel@tonic-gate 			struct tstate *this = &gstate[i];
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate 			if (this->cpuid == -1)
672*0Sstevel@tonic-gate 				continue;
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate 			if (pset_assign(PS_QUERY,
675*0Sstevel@tonic-gate 			    this->cpuid, &cpupset) == -1) {
676*0Sstevel@tonic-gate 				errstr = strerror(errno);
677*0Sstevel@tonic-gate 				(void) fprintf(stderr,
678*0Sstevel@tonic-gate 				    gettext("%s: pset_assign - %s\n"),
679*0Sstevel@tonic-gate 				    opts->pgmname, errstr);
680*0Sstevel@tonic-gate 				continue;
681*0Sstevel@tonic-gate 			}
682*0Sstevel@tonic-gate 
683*0Sstevel@tonic-gate 			if (mypset != cpupset)
684*0Sstevel@tonic-gate 				this->cpuid = -1;
685*0Sstevel@tonic-gate 		}
686*0Sstevel@tonic-gate 	}
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 	if (opts->dotitle)
689*0Sstevel@tonic-gate 		print_title(opts->master);
690*0Sstevel@tonic-gate 	zerotime();
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	for (i = 0; i < ncpus; i++) {
693*0Sstevel@tonic-gate 		struct tstate *this = &gstate[i];
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 		if (this->cpuid == -1)
696*0Sstevel@tonic-gate 			continue;
697*0Sstevel@tonic-gate 		this->sgrp = cpc_setgrp_clone(opts->master);
698*0Sstevel@tonic-gate 		if (this->sgrp == NULL) {
699*0Sstevel@tonic-gate 			this->cpuid = -1;
700*0Sstevel@tonic-gate 			continue;
701*0Sstevel@tonic-gate 		}
702*0Sstevel@tonic-gate 		if (thr_create(NULL, 0, gtick, this,
703*0Sstevel@tonic-gate 		    THR_BOUND|THR_NEW_LWP, &this->tid) == 0)
704*0Sstevel@tonic-gate 			lwps++;
705*0Sstevel@tonic-gate 		else {
706*0Sstevel@tonic-gate 			(void) fprintf(stderr,
707*0Sstevel@tonic-gate 			    gettext("%s: cannot create thread for cpu%d\n"),
708*0Sstevel@tonic-gate 			    opts->pgmname, this->cpuid);
709*0Sstevel@tonic-gate 			this->status = 4;
710*0Sstevel@tonic-gate 		}
711*0Sstevel@tonic-gate 	}
712*0Sstevel@tonic-gate 
713*0Sstevel@tonic-gate 	if (lwps != 0)
714*0Sstevel@tonic-gate 		for (i = 0; i < ncpus; i++)
715*0Sstevel@tonic-gate 			(void) thr_join(gstate[i].tid, NULL, NULL);
716*0Sstevel@tonic-gate 
717*0Sstevel@tonic-gate 	if ((accum = cpc_setgrp_clone(opts->master)) == NULL) {
718*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: out of heap\n"),
719*0Sstevel@tonic-gate 		    opts->pgmname);
720*0Sstevel@tonic-gate 		return (1);
721*0Sstevel@tonic-gate 	}
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	retval = 0;
724*0Sstevel@tonic-gate 	for (i = 0; i < ncpus; i++) {
725*0Sstevel@tonic-gate 		struct tstate *this = &gstate[i];
726*0Sstevel@tonic-gate 
727*0Sstevel@tonic-gate 		if (this->cpuid == -1)
728*0Sstevel@tonic-gate 			continue;
729*0Sstevel@tonic-gate 		cpc_setgrp_accum(accum, this->sgrp);
730*0Sstevel@tonic-gate 		cpc_setgrp_free(this->sgrp);
731*0Sstevel@tonic-gate 		this->sgrp = NULL;
732*0Sstevel@tonic-gate 		if (this->status != 0)
733*0Sstevel@tonic-gate 			retval = 1;
734*0Sstevel@tonic-gate 	}
735*0Sstevel@tonic-gate 
736*0Sstevel@tonic-gate 	cpc_setgrp_reset(accum);
737*0Sstevel@tonic-gate 	start = cpc_setgrp_getset(accum);
738*0Sstevel@tonic-gate 	do {
739*0Sstevel@tonic-gate 		nreqs = cpc_setgrp_getbufs(accum, &data1, &data2, &scratch);
740*0Sstevel@tonic-gate 		print_total(lwps, *data1, nreqs, cpc_setgrp_getname(accum));
741*0Sstevel@tonic-gate 	} while (cpc_setgrp_nextset(accum) != start);
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate 	cpc_setgrp_free(accum);
744*0Sstevel@tonic-gate 	accum = NULL;
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate 	free(gstate);
747*0Sstevel@tonic-gate 	return (retval);
748*0Sstevel@tonic-gate }
749*0Sstevel@tonic-gate 
750*0Sstevel@tonic-gate static int
751*0Sstevel@tonic-gate get_chipid(kstat_ctl_t *kc, processorid_t cpuid)
752*0Sstevel@tonic-gate {
753*0Sstevel@tonic-gate 	kstat_t		*ksp;
754*0Sstevel@tonic-gate 	kstat_named_t	*k;
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate 	if ((ksp = kstat_lookup(kc, "cpu_info", cpuid, NULL)) == NULL)
757*0Sstevel@tonic-gate 		return (-1);
758*0Sstevel@tonic-gate 
759*0Sstevel@tonic-gate 	if (kstat_read(kc, ksp, NULL) == -1) {
760*0Sstevel@tonic-gate 		(void) fprintf(stderr,
761*0Sstevel@tonic-gate 		    gettext("%s: kstat_read() failed for cpu %d: %s\n"),
762*0Sstevel@tonic-gate 		    opts->pgmname, cpuid, strerror(errno));
763*0Sstevel@tonic-gate 		return (-1);
764*0Sstevel@tonic-gate 	}
765*0Sstevel@tonic-gate 
766*0Sstevel@tonic-gate 	if ((k = (kstat_named_t *)kstat_data_lookup(ksp, "chip_id")) == NULL) {
767*0Sstevel@tonic-gate 		(void) fprintf(stderr,
768*0Sstevel@tonic-gate 		    gettext("%s: chip_id not found for cpu %d: %s\n"),
769*0Sstevel@tonic-gate 		    opts->pgmname, cpuid, strerror(errno));
770*0Sstevel@tonic-gate 		return (-1);
771*0Sstevel@tonic-gate 	}
772*0Sstevel@tonic-gate 
773*0Sstevel@tonic-gate 	return (k->value.i32);
774*0Sstevel@tonic-gate }
775*0Sstevel@tonic-gate 
776*0Sstevel@tonic-gate static void *
777*0Sstevel@tonic-gate soaker(void *arg)
778*0Sstevel@tonic-gate {
779*0Sstevel@tonic-gate 	struct tstate	*state = arg;
780*0Sstevel@tonic-gate 	pcparms_t	pcparms;
781*0Sstevel@tonic-gate 	fxparms_t	*fx = (fxparms_t *)pcparms.pc_clparms;
782*0Sstevel@tonic-gate 
783*0Sstevel@tonic-gate 	if (processor_bind(P_LWPID, P_MYID, state->cpuid, NULL) != 0)
784*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: couldn't bind soaker "
785*0Sstevel@tonic-gate 		    "thread to cpu%d: %s\n"), opts->pgmname, state->cpuid,
786*0Sstevel@tonic-gate 		    strerror(errno));
787*0Sstevel@tonic-gate 
788*0Sstevel@tonic-gate 	/*
789*0Sstevel@tonic-gate 	 * Put the soaker thread in the fixed priority (FX) class so it runs
790*0Sstevel@tonic-gate 	 * at the lowest possible global priority.
791*0Sstevel@tonic-gate 	 */
792*0Sstevel@tonic-gate 	pcparms.pc_cid = fxinfo.pc_cid;
793*0Sstevel@tonic-gate 	fx->fx_upri = 0;
794*0Sstevel@tonic-gate 	fx->fx_uprilim = 0;
795*0Sstevel@tonic-gate 	fx->fx_tqsecs = fx->fx_tqnsecs = FX_TQDEF;
796*0Sstevel@tonic-gate 
797*0Sstevel@tonic-gate 	if (priocntl(P_LWPID, P_MYID, PC_SETPARMS, &pcparms) != 0)
798*0Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: couldn't put soaker "
799*0Sstevel@tonic-gate 		    "thread in FX sched class: %s\n"), opts->pgmname,
800*0Sstevel@tonic-gate 		    strerror(errno));
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate 	/*
803*0Sstevel@tonic-gate 	 * Let the parent thread know we're ready to roll.
804*0Sstevel@tonic-gate 	 */
805*0Sstevel@tonic-gate 	(void) mutex_lock(&state->soak_lock);
806*0Sstevel@tonic-gate 	state->soak_state = SOAK_RUN;
807*0Sstevel@tonic-gate 	(void) cond_signal(&state->soak_cv);
808*0Sstevel@tonic-gate 	(void) mutex_unlock(&state->soak_lock);
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate 	for (;;) {
811*0Sstevel@tonic-gate spin:
812*0Sstevel@tonic-gate 		(void) mutex_lock(&state->soak_lock);
813*0Sstevel@tonic-gate 		if (state->soak_state == SOAK_RUN) {
814*0Sstevel@tonic-gate 			(void) mutex_unlock(&state->soak_lock);
815*0Sstevel@tonic-gate 			goto spin;
816*0Sstevel@tonic-gate 		}
817*0Sstevel@tonic-gate 
818*0Sstevel@tonic-gate 		while (state->soak_state == SOAK_PAUSE)
819*0Sstevel@tonic-gate 			(void) cond_wait(&state->soak_cv,
820*0Sstevel@tonic-gate 			    &state->soak_lock);
821*0Sstevel@tonic-gate 		(void) mutex_unlock(&state->soak_lock);
822*0Sstevel@tonic-gate 	}
823*0Sstevel@tonic-gate 
824*0Sstevel@tonic-gate 	/*NOTREACHED*/
825*0Sstevel@tonic-gate 	return (NULL);
826*0Sstevel@tonic-gate }
827