xref: /onnv-gate/usr/src/cmd/cpc/common/cputrack.c (revision 3732:c8e62d396494)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52235Skk112340  * Common Development and Distribution License (the "License").
62235Skk112340  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3732Sae112802  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/time.h>
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <inttypes.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <strings.h>
350Sstevel@tonic-gate #include <limits.h>
360Sstevel@tonic-gate #include <libintl.h>
370Sstevel@tonic-gate #include <locale.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <kstat.h>
400Sstevel@tonic-gate #include <libcpc.h>
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include "cpucmds.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static struct options {
450Sstevel@tonic-gate 	int debug;
460Sstevel@tonic-gate 	int verbose;
470Sstevel@tonic-gate 	int dotitle;
480Sstevel@tonic-gate 	int dohelp;
490Sstevel@tonic-gate 	int dotick;
500Sstevel@tonic-gate 	int cpuver;
510Sstevel@tonic-gate 	char *pgmname;
520Sstevel@tonic-gate 	uint_t mseconds;
530Sstevel@tonic-gate 	uint_t nsamples;
540Sstevel@tonic-gate 	uint_t nsets;
550Sstevel@tonic-gate 	cpc_setgrp_t *master;
560Sstevel@tonic-gate 	int followfork;
570Sstevel@tonic-gate 	int followexec;
580Sstevel@tonic-gate 	pid_t pid;
590Sstevel@tonic-gate 	FILE *log;
600Sstevel@tonic-gate } __options;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static const struct options *opts = (const struct options *)&__options;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static cpc_t *cpc;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*ARGSUSED*/
670Sstevel@tonic-gate static void
680Sstevel@tonic-gate cputrack_errfn(const char *fn, int subcode, const char *fmt, va_list ap)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: ", opts->pgmname);
710Sstevel@tonic-gate 	if (opts->debug)
720Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", fn);
730Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate static void
770Sstevel@tonic-gate cputrack_pctx_errfn(const char *fn, const char *fmt, va_list ap)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	cputrack_errfn(fn, -1, fmt, ap);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static int cputrack(int argc, char *argv[], int optind);
83*3732Sae112802 #if defined(__i386)
840Sstevel@tonic-gate static void p4_ht_error(void);
85*3732Sae112802 #endif
860Sstevel@tonic-gate 
870Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
880Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
890Sstevel@tonic-gate #endif
900Sstevel@tonic-gate 
910Sstevel@tonic-gate int
920Sstevel@tonic-gate main(int argc, char *argv[])
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	struct options *opts = &__options;
950Sstevel@tonic-gate 	int c, errcnt = 0;
960Sstevel@tonic-gate 	int nsamples;
970Sstevel@tonic-gate 	cpc_setgrp_t *sgrp;
980Sstevel@tonic-gate 	char *errstr;
990Sstevel@tonic-gate 	int ret;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1020Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if ((opts->pgmname = strrchr(argv[0], '/')) == NULL)
1050Sstevel@tonic-gate 		opts->pgmname = argv[0];
1060Sstevel@tonic-gate 	else
1070Sstevel@tonic-gate 		opts->pgmname++;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if ((cpc = cpc_open(CPC_VER_CURRENT)) == NULL) {
1100Sstevel@tonic-gate 		errstr = strerror(errno);
1110Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: cannot access performance "
1120Sstevel@tonic-gate 		    "counter library - %s\n"), opts->pgmname, errstr);
1130Sstevel@tonic-gate 		return (1);
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	(void) cpc_seterrhndlr(cpc, cputrack_errfn);
1170Sstevel@tonic-gate 	strtoset_errfn = cputrack_errfn;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	/*
1200Sstevel@tonic-gate 	 * Establish (non-zero) defaults
1210Sstevel@tonic-gate 	 */
1220Sstevel@tonic-gate 	opts->mseconds = 1000;
1230Sstevel@tonic-gate 	opts->dotitle = 1;
1240Sstevel@tonic-gate 	opts->log = stdout;
1250Sstevel@tonic-gate 	if ((opts->master = cpc_setgrp_new(cpc, 0)) == NULL) {
1260Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
1270Sstevel@tonic-gate 		    opts->pgmname);
1280Sstevel@tonic-gate 		exit(1);
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "T:N:Defhntvo:r:c:p:")) != EOF)
1320Sstevel@tonic-gate 		switch (c) {
1330Sstevel@tonic-gate 		case 'T':			/* sample time,	seconds */
1340Sstevel@tonic-gate 			opts->mseconds = (uint_t)(atof(optarg) * 1000.0);
1350Sstevel@tonic-gate 			break;
1360Sstevel@tonic-gate 		case 'N':			/* number of samples */
1370Sstevel@tonic-gate 			nsamples = atoi(optarg);
1380Sstevel@tonic-gate 			if (nsamples < 0)
1390Sstevel@tonic-gate 				errcnt++;
1400Sstevel@tonic-gate 			else
1410Sstevel@tonic-gate 				opts->nsamples = (uint_t)nsamples;
1420Sstevel@tonic-gate 			break;
1430Sstevel@tonic-gate 		case 'D':			/* enable debugging */
1440Sstevel@tonic-gate 			opts->debug++;
1450Sstevel@tonic-gate 			break;
1460Sstevel@tonic-gate 		case 'f':			/* follow fork */
1470Sstevel@tonic-gate 			opts->followfork++;
1480Sstevel@tonic-gate 			break;
1490Sstevel@tonic-gate 		case 'e':			/* follow exec */
1500Sstevel@tonic-gate 			opts->followexec++;
1510Sstevel@tonic-gate 			break;
1520Sstevel@tonic-gate 		case 'n':			/* no titles */
1530Sstevel@tonic-gate 			opts->dotitle = 0;
1540Sstevel@tonic-gate 			break;
1550Sstevel@tonic-gate 		case 't':			/* print %tick */
1560Sstevel@tonic-gate 			opts->dotick = 1;
1570Sstevel@tonic-gate 			break;
1580Sstevel@tonic-gate 		case 'v':
1590Sstevel@tonic-gate 			opts->verbose = 1;	/* more chatty */
1600Sstevel@tonic-gate 			break;
1610Sstevel@tonic-gate 		case 'o':
1620Sstevel@tonic-gate 			if (optarg == NULL) {
1630Sstevel@tonic-gate 				errcnt++;
1640Sstevel@tonic-gate 				break;
1650Sstevel@tonic-gate 			}
1660Sstevel@tonic-gate 			if ((opts->log = fopen(optarg, "w")) == NULL) {
1670Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
1680Sstevel@tonic-gate 				    "%s: cannot open '%s' for writing\n"),
1690Sstevel@tonic-gate 				    opts->pgmname, optarg);
1700Sstevel@tonic-gate 				return (1);
1710Sstevel@tonic-gate 			}
1720Sstevel@tonic-gate 			break;
1730Sstevel@tonic-gate 		case 'c':			/* specify statistics */
1740Sstevel@tonic-gate 			if ((sgrp = cpc_setgrp_newset(opts->master,
1750Sstevel@tonic-gate 			    optarg, &errcnt)) != NULL)
1760Sstevel@tonic-gate 				opts->master = sgrp;
1770Sstevel@tonic-gate 			break;
1780Sstevel@tonic-gate 		case 'p':			/* grab given pid */
1790Sstevel@tonic-gate 			if ((opts->pid = atoi(optarg)) <= 0)
1800Sstevel@tonic-gate 				errcnt++;
1810Sstevel@tonic-gate 			break;
1820Sstevel@tonic-gate 		case 'h':
1830Sstevel@tonic-gate 			opts->dohelp = 1;
1840Sstevel@tonic-gate 			break;
1850Sstevel@tonic-gate 		case '?':
1860Sstevel@tonic-gate 		default:
1870Sstevel@tonic-gate 			errcnt++;
1880Sstevel@tonic-gate 			break;
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	if (opts->nsamples == 0)
1920Sstevel@tonic-gate 		opts->nsamples = UINT_MAX;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	if (errcnt != 0 ||
1950Sstevel@tonic-gate 	    opts->dohelp ||
1960Sstevel@tonic-gate 	    (argc == optind && opts->pid == 0) ||
1970Sstevel@tonic-gate 	    (argc > optind && opts->pid != 0) ||
1980Sstevel@tonic-gate 	    (opts->nsets = cpc_setgrp_numsets(opts->master)) == 0) {
1990Sstevel@tonic-gate 		(void) fprintf(opts->dohelp ? stdout : stderr, gettext(
2000Sstevel@tonic-gate 		    "Usage:\n\t%s [-T secs] [-N count] [-Defhnv] [-o file]\n"
2010Sstevel@tonic-gate 		    "\t\t-c events [command [args] | -p pid]\n\n"
2020Sstevel@tonic-gate 		    "\t-T secs\t  seconds between samples, default 1\n"
2030Sstevel@tonic-gate 		    "\t-N count  number of samples, default unlimited\n"
2040Sstevel@tonic-gate 		    "\t-D\t  enable debug mode\n"
2050Sstevel@tonic-gate 		    "\t-e\t  follow exec(2), and execve(2)\n"
2060Sstevel@tonic-gate 		    "\t-f\t  follow fork(2), fork1(2), and vfork(2)\n"
2070Sstevel@tonic-gate 		    "\t-h\t  print extended usage information\n"
2080Sstevel@tonic-gate 		    "\t-n\t  suppress titles\n"
2090Sstevel@tonic-gate 		    "\t-t\t  include virtualized %s register\n"
2100Sstevel@tonic-gate 		    "\t-v\t  verbose mode\n"
2110Sstevel@tonic-gate 		    "\t-o file\t  write cpu statistics to this file\n"
2120Sstevel@tonic-gate 		    "\t-c events specify processor events to be monitored\n"
2130Sstevel@tonic-gate 		    "\t-p pid\t  pid of existing process to capture\n\n"
2140Sstevel@tonic-gate 		    "\tUse cpustat(1M) to monitor system-wide statistics.\n"),
2150Sstevel@tonic-gate 		    opts->pgmname, CPC_TICKREG_NAME);
2160Sstevel@tonic-gate 		if (opts->dohelp) {
2170Sstevel@tonic-gate 			(void) putchar('\n');
2180Sstevel@tonic-gate 			(void) capabilities(cpc, stdout);
2190Sstevel@tonic-gate 			exit(0);
2200Sstevel@tonic-gate 		}
2210Sstevel@tonic-gate 		exit(2);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	cpc_setgrp_reset(opts->master);
2250Sstevel@tonic-gate 	(void) setvbuf(opts->log, NULL, _IOLBF, 0);
2260Sstevel@tonic-gate 	ret = cputrack(argc, argv, optind);
2270Sstevel@tonic-gate 	(void) cpc_close(cpc);
2280Sstevel@tonic-gate 	return (ret);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate static void
2320Sstevel@tonic-gate print_title(cpc_setgrp_t *sgrp)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7s ", "time");
2350Sstevel@tonic-gate 	if (opts->followfork)
2360Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6s ", "pid");
2370Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3s %10s ", "lwp", "event");
2380Sstevel@tonic-gate 	if (opts->dotick)
2390Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9s ", CPC_TICKREG_NAME);
2400Sstevel@tonic-gate 	(void) fprintf(opts->log, "%s\n", cpc_setgrp_gethdr(sgrp));
2410Sstevel@tonic-gate 	(void) fflush(opts->log);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate static void
2450Sstevel@tonic-gate print_exec(float now, pid_t pid, char *name)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate 	if (name == NULL)
2480Sstevel@tonic-gate 		name = "(unknown)";
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7.3f ", now);
2510Sstevel@tonic-gate 	if (opts->followfork)
2520Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6d ", (int)pid);
2530Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3d %10s ", 1, "exec");
2540Sstevel@tonic-gate 	if (opts->dotick)
2550Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9s ", "");
2560Sstevel@tonic-gate 	(void) fprintf(opts->log, "%9s %9s # '%s'\n", "", "", name);
2570Sstevel@tonic-gate 	(void) fflush(opts->log);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate static void
2610Sstevel@tonic-gate print_fork(float now, pid_t newpid, id_t lwpid, pid_t oldpid)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7.3f ", now);
2640Sstevel@tonic-gate 	if (opts->followfork)
2650Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6d ", (int)oldpid);
2660Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3d %10s ", (int)lwpid, "fork");
2670Sstevel@tonic-gate 	if (opts->dotick)
2680Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9s ", "");
2690Sstevel@tonic-gate 	(void) fprintf(opts->log, "%9s %9s # %d\n", "", "", (int)newpid);
2700Sstevel@tonic-gate 	(void) fflush(opts->log);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate static void
2740Sstevel@tonic-gate print_sample(pid_t pid, id_t lwpid,
2750Sstevel@tonic-gate     char *pevent, cpc_buf_t *buf, int nreq, const char *evname)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	uint64_t	val;
2780Sstevel@tonic-gate 	int		i;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	(void) fprintf(opts->log, "%7.3f ",
2810Sstevel@tonic-gate 	    mstimestamp(cpc_buf_hrtime(cpc, buf)));
2820Sstevel@tonic-gate 	if (opts->followfork)
2830Sstevel@tonic-gate 		(void) fprintf(opts->log, "%6d ", (int)pid);
2840Sstevel@tonic-gate 	(void) fprintf(opts->log, "%3d %10s ", (int)lwpid, pevent);
2850Sstevel@tonic-gate 	if (opts->dotick)
2860Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9" PRId64 " ",
2870Sstevel@tonic-gate 		    cpc_buf_tick(cpc, buf));
2880Sstevel@tonic-gate 	for (i = 0; i < nreq; i++) {
2890Sstevel@tonic-gate 		(void) cpc_buf_get(cpc, buf, i, &val);
2900Sstevel@tonic-gate 		(void) fprintf(opts->log, "%9" PRId64 " ", val);
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 	if (opts->nsets > 1)
2930Sstevel@tonic-gate 		(void) fprintf(opts->log, " # %s\n", evname);
2940Sstevel@tonic-gate 	else
2950Sstevel@tonic-gate 		(void) fputc('\n', opts->log);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate struct pstate {
2990Sstevel@tonic-gate 	cpc_setgrp_t *accum;
3000Sstevel@tonic-gate 	cpc_setgrp_t **sgrps;
3010Sstevel@tonic-gate 	int maxlwpid;
3020Sstevel@tonic-gate };
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate static int
3050Sstevel@tonic-gate pinit_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	struct pstate *state = arg;
3080Sstevel@tonic-gate 	cpc_setgrp_t *sgrp;
3090Sstevel@tonic-gate 	cpc_set_t *set;
3100Sstevel@tonic-gate 	cpc_buf_t **data1, **data2, **scratch;
3110Sstevel@tonic-gate 	char *errstr;
3120Sstevel@tonic-gate 	int nreq;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if (state->maxlwpid < lwpid) {
3150Sstevel@tonic-gate 		state->sgrps = realloc(state->sgrps,
3160Sstevel@tonic-gate 		    lwpid * sizeof (state->sgrps));
3170Sstevel@tonic-gate 		if (state->sgrps == NULL) {
3180Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3190Sstevel@tonic-gate 			    "%6d: init_lwp: out of memory\n"), (int)pid);
3200Sstevel@tonic-gate 			return (-1);
3210Sstevel@tonic-gate 		}
3220Sstevel@tonic-gate 		while (state->maxlwpid < lwpid) {
3230Sstevel@tonic-gate 			state->sgrps[state->maxlwpid] = NULL;
3240Sstevel@tonic-gate 			state->maxlwpid++;
3250Sstevel@tonic-gate 		}
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if ((sgrp = state->sgrps[lwpid-1]) == NULL) {
3290Sstevel@tonic-gate 		if ((sgrp = cpc_setgrp_clone(opts->master)) == NULL) {
3300Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3310Sstevel@tonic-gate 			    "%6d: init_lwp: out of memory\n"), (int)pid);
3320Sstevel@tonic-gate 			return (-1);
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 		state->sgrps[lwpid-1] = sgrp;
3350Sstevel@tonic-gate 		set = cpc_setgrp_getset(sgrp);
3360Sstevel@tonic-gate 	} else {
3370Sstevel@tonic-gate 		cpc_setgrp_reset(sgrp);
3380Sstevel@tonic-gate 		set = cpc_setgrp_getset(sgrp);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (cpc_bind_pctx(cpc, pctx, lwpid, set, 0) != 0 ||
3440Sstevel@tonic-gate 	    cpc_set_sample(cpc, set, *data2) != 0) {
3450Sstevel@tonic-gate 		errstr = strerror(errno);
3460Sstevel@tonic-gate 		if (errno == EAGAIN)
3470Sstevel@tonic-gate 			(void) cpc_unbind(cpc, set);
348*3732Sae112802 #if defined(__i386)
3490Sstevel@tonic-gate 		if (errno == EACCES)
3500Sstevel@tonic-gate 			p4_ht_error();
3510Sstevel@tonic-gate 		else
352*3732Sae112802 #endif
3530Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3540Sstevel@tonic-gate 				"%6d: init_lwp: can't bind perf counters "
3550Sstevel@tonic-gate 				    "to lwp%d - %s\n"), (int)pid, (int)lwpid,
3560Sstevel@tonic-gate 			    errstr);
3570Sstevel@tonic-gate 		return (-1);
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	if (opts->verbose)
3610Sstevel@tonic-gate 		print_sample(pid, lwpid, "init_lwp",
3620Sstevel@tonic-gate 		    *data2, nreq, cpc_setgrp_getname(sgrp));
3630Sstevel@tonic-gate 	return (0);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*ARGSUSED*/
3670Sstevel@tonic-gate static int
3680Sstevel@tonic-gate pfini_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	struct pstate *state = arg;
3710Sstevel@tonic-gate 	cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
3720Sstevel@tonic-gate 	cpc_set_t *set;
3730Sstevel@tonic-gate 	char *errstr;
3740Sstevel@tonic-gate 	cpc_buf_t **data1, **data2, **scratch;
3750Sstevel@tonic-gate 	int nreq;
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 	set = cpc_setgrp_getset(sgrp);
3780Sstevel@tonic-gate 	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
3790Sstevel@tonic-gate 	if (cpc_set_sample(cpc, set, *data1) == 0) {
3800Sstevel@tonic-gate 		if (opts->verbose)
3810Sstevel@tonic-gate 			print_sample(pid, lwpid, "fini_lwp",
3820Sstevel@tonic-gate 			    *data1, nreq, cpc_setgrp_getname(sgrp));
3830Sstevel@tonic-gate 		cpc_setgrp_accum(state->accum, sgrp);
3840Sstevel@tonic-gate 		if (cpc_unbind(cpc, set) == 0)
3850Sstevel@tonic-gate 			return (0);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	switch (errno) {
3890Sstevel@tonic-gate 	case EAGAIN:
3900Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%6d: fini_lwp: "
3910Sstevel@tonic-gate 		    "lwp%d: perf counter contents invalidated\n"),
3920Sstevel@tonic-gate 		    (int)pid, (int)lwpid);
3930Sstevel@tonic-gate 		break;
3940Sstevel@tonic-gate 	default:
3950Sstevel@tonic-gate 		errstr = strerror(errno);
3960Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%6d: fini_lwp: "
3970Sstevel@tonic-gate 		    "lwp%d: can't access perf counters - %s\n"),
3980Sstevel@tonic-gate 		    (int)pid, (int)lwpid, errstr);
3990Sstevel@tonic-gate 		break;
4000Sstevel@tonic-gate 	}
4012235Skk112340 	return (-1);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate /*ARGSUSED*/
4050Sstevel@tonic-gate static int
4060Sstevel@tonic-gate plwp_create(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	cpc_setgrp_t	*sgrp = opts->master;
4090Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
4100Sstevel@tonic-gate 	int		nreq;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	print_sample(pid, lwpid, "lwp_create",
4150Sstevel@tonic-gate 	    *data1, nreq, cpc_setgrp_getname(sgrp));
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	return (0);
4180Sstevel@tonic-gate }
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate /*ARGSUSED*/
4210Sstevel@tonic-gate static int
4220Sstevel@tonic-gate plwp_exit(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate 	struct pstate	*state = arg;
4250Sstevel@tonic-gate 	cpc_setgrp_t	*sgrp = state->sgrps[lwpid-1];
4260Sstevel@tonic-gate 	cpc_set_t	*start;
4270Sstevel@tonic-gate 	int		nreq;
4280Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	start = cpc_setgrp_getset(sgrp);
4310Sstevel@tonic-gate 	do {
4320Sstevel@tonic-gate 		nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4330Sstevel@tonic-gate 		if (cpc_buf_hrtime(cpc, *data1) == 0)
4340Sstevel@tonic-gate 			continue;
4350Sstevel@tonic-gate 		print_sample(pid, lwpid, "lwp_exit",
4360Sstevel@tonic-gate 		    *data1, nreq, cpc_setgrp_getname(sgrp));
4370Sstevel@tonic-gate 	} while (cpc_setgrp_nextset(sgrp) != start);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	return (0);
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate /*ARGSUSED*/
4430Sstevel@tonic-gate static int
4440Sstevel@tonic-gate pexec(pctx_t *pctx, pid_t pid, id_t lwpid, char *name, void *arg)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	struct pstate	*state = arg;
4470Sstevel@tonic-gate 	float		now = 0.0;
4480Sstevel@tonic-gate 	cpc_set_t	*start;
4490Sstevel@tonic-gate 	int		nreq;
4500Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
4510Sstevel@tonic-gate 	hrtime_t	hrt;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	/*
4540Sstevel@tonic-gate 	 * Print the accumulated results from the previous program image
4550Sstevel@tonic-gate 	 */
4560Sstevel@tonic-gate 	cpc_setgrp_reset(state->accum);
4570Sstevel@tonic-gate 	start = cpc_setgrp_getset(state->accum);
4580Sstevel@tonic-gate 	do {
4590Sstevel@tonic-gate 		nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
4600Sstevel@tonic-gate 		    &scratch);
4610Sstevel@tonic-gate 		hrt = cpc_buf_hrtime(cpc, *data1);
4620Sstevel@tonic-gate 		if (hrt == 0)
4630Sstevel@tonic-gate 			continue;
4640Sstevel@tonic-gate 		print_sample(pid, lwpid, "exec",
4650Sstevel@tonic-gate 		    *data1, nreq, cpc_setgrp_getname(state->accum));
4660Sstevel@tonic-gate 		if (now < mstimestamp(hrt))
4670Sstevel@tonic-gate 			now = mstimestamp(hrt);
4680Sstevel@tonic-gate 	} while (cpc_setgrp_nextset(state->accum) != start);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	print_exec(now, pid, name);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (state->accum != NULL) {
4730Sstevel@tonic-gate 		cpc_setgrp_free(state->accum);
4740Sstevel@tonic-gate 		state->accum = NULL;
4750Sstevel@tonic-gate 	}
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 	if (opts->followexec) {
4780Sstevel@tonic-gate 		state->accum = cpc_setgrp_clone(opts->master);
4790Sstevel@tonic-gate 		return (0);
4800Sstevel@tonic-gate 	}
4810Sstevel@tonic-gate 	return (-1);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate /*ARGSUSED*/
4850Sstevel@tonic-gate static void
4860Sstevel@tonic-gate pexit(pctx_t *pctx, pid_t pid, id_t lwpid, int status, void *arg)
4870Sstevel@tonic-gate {
4880Sstevel@tonic-gate 	struct pstate	*state = arg;
4890Sstevel@tonic-gate 	cpc_set_t	*start;
4900Sstevel@tonic-gate 	int		nreq;
4910Sstevel@tonic-gate 	cpc_buf_t	**data1, **data2, **scratch;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	cpc_setgrp_reset(state->accum);
4940Sstevel@tonic-gate 	start = cpc_setgrp_getset(state->accum);
4950Sstevel@tonic-gate 	do {
4960Sstevel@tonic-gate 		nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
4970Sstevel@tonic-gate 		    &scratch);
4980Sstevel@tonic-gate 		if (cpc_buf_hrtime(cpc, *data1) == 0)
4990Sstevel@tonic-gate 			continue;
5000Sstevel@tonic-gate 		print_sample(pid, lwpid, "exit",
5010Sstevel@tonic-gate 		    *data1, nreq, cpc_setgrp_getname(state->accum));
5020Sstevel@tonic-gate 	} while (cpc_setgrp_nextset(state->accum) != start);
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 	cpc_setgrp_free(state->accum);
5050Sstevel@tonic-gate 	state->accum = NULL;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	for (lwpid = 1; lwpid < state->maxlwpid; lwpid++)
5080Sstevel@tonic-gate 		if (state->sgrps[lwpid-1] != NULL) {
5090Sstevel@tonic-gate 			cpc_setgrp_free(state->sgrps[lwpid-1]);
5100Sstevel@tonic-gate 			state->sgrps[lwpid-1] = NULL;
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 	free(state->sgrps);
5130Sstevel@tonic-gate 	state->sgrps = NULL;
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate static int
5170Sstevel@tonic-gate ptick(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate 	struct pstate *state = arg;
5200Sstevel@tonic-gate 	cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
5210Sstevel@tonic-gate 	cpc_set_t *this = cpc_setgrp_getset(sgrp);
5220Sstevel@tonic-gate 	const char *name = cpc_setgrp_getname(sgrp);
5230Sstevel@tonic-gate 	cpc_buf_t **data1, **data2, **scratch, *tmp;
5240Sstevel@tonic-gate 	char *errstr;
5250Sstevel@tonic-gate 	int nreqs;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	if (opts->nsets == 1) {
5300Sstevel@tonic-gate 		/*
5310Sstevel@tonic-gate 		 * If we're dealing with one set, buffer usage is:
5320Sstevel@tonic-gate 		 *
5330Sstevel@tonic-gate 		 * data1 = most recent data snapshot
5340Sstevel@tonic-gate 		 * data2 = previous data snapshot
5350Sstevel@tonic-gate 		 * scratch = used for diffing data1 and data2
5360Sstevel@tonic-gate 		 *
5370Sstevel@tonic-gate 		 * Save the snapshot from the previous sample in data2
5380Sstevel@tonic-gate 		 * before putting the current sample in data1.
5390Sstevel@tonic-gate 		 */
5400Sstevel@tonic-gate 		tmp = *data1;
5410Sstevel@tonic-gate 		*data1 = *data2;
5420Sstevel@tonic-gate 		*data2 = tmp;
5430Sstevel@tonic-gate 		if (cpc_set_sample(cpc, this, *data1) != 0)
5440Sstevel@tonic-gate 			goto broken;
5450Sstevel@tonic-gate 		cpc_buf_sub(cpc, *scratch, *data1, *data2);
5460Sstevel@tonic-gate 	} else {
5470Sstevel@tonic-gate 		cpc_set_t *next = cpc_setgrp_nextset(sgrp);
5480Sstevel@tonic-gate 		/*
5490Sstevel@tonic-gate 		 * If there is more than set in use, we will need to
5500Sstevel@tonic-gate 		 * unbind and re-bind on each go-around because each
5510Sstevel@tonic-gate 		 * time a counter is bound, it is preset to 0 (as it was
5520Sstevel@tonic-gate 		 * specified when the requests were added to the set).
5530Sstevel@tonic-gate 		 *
5540Sstevel@tonic-gate 		 * Buffer usage in this case is:
5550Sstevel@tonic-gate 		 *
5560Sstevel@tonic-gate 		 * data1 = total counts for this set since program began
5570Sstevel@tonic-gate 		 * data2 = unused
5580Sstevel@tonic-gate 		 * scratch = most recent data snapshot
5590Sstevel@tonic-gate 		 */
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 		if (cpc_set_sample(cpc, this, *scratch) != 0)
5620Sstevel@tonic-gate 			goto broken;
5630Sstevel@tonic-gate 		cpc_buf_add(cpc, *data1, *data1, *scratch);
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 		/*
5660Sstevel@tonic-gate 		 * No need to unbind the previous set, as binding another set
5670Sstevel@tonic-gate 		 * automatically unbinds the most recently bound set.
5680Sstevel@tonic-gate 		 */
5690Sstevel@tonic-gate 		if (cpc_bind_pctx(cpc, pctx, lwpid, next, 0) != 0)
5700Sstevel@tonic-gate 			goto broken;
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 	print_sample(pid, lwpid, "tick", *scratch, nreqs, name);
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	return (0);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate broken:
5770Sstevel@tonic-gate 	switch (errno) {
5780Sstevel@tonic-gate 	case EAGAIN:
5790Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5800Sstevel@tonic-gate 		    "%6d: tick: lwp%d: perf counter contents invalidated\n"),
5810Sstevel@tonic-gate 		    (int)pid, (int)lwpid);
5820Sstevel@tonic-gate 		break;
5830Sstevel@tonic-gate 	default:
5840Sstevel@tonic-gate 		errstr = strerror(errno);
5850Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5860Sstevel@tonic-gate 		    "%6d: tick: lwp%d: can't access perf counter - %s\n"),
5870Sstevel@tonic-gate 		    (int)pid, (int)lwpid, errstr);
5880Sstevel@tonic-gate 		break;
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 	(void) cpc_unbind(cpc, this);
5910Sstevel@tonic-gate 	return (-1);
5920Sstevel@tonic-gate }
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate /*
5950Sstevel@tonic-gate  * The system has just created a new address space that has a new pid.
5960Sstevel@tonic-gate  * We're running in a child of the controlling process, with a new
5970Sstevel@tonic-gate  * pctx handle already opened on the child of the original controlled process.
5980Sstevel@tonic-gate  */
5990Sstevel@tonic-gate static void
6000Sstevel@tonic-gate pfork(pctx_t *pctx, pid_t oldpid, pid_t pid, id_t lwpid, void *arg)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	struct pstate *state = arg;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	print_fork(mstimestamp(0), pid, lwpid, oldpid);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	if (!opts->followfork)
6070Sstevel@tonic-gate 		return;
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	if (pctx_set_events(pctx,
6100Sstevel@tonic-gate 	    PCTX_SYSC_EXEC_EVENT, pexec,
6110Sstevel@tonic-gate 	    PCTX_SYSC_FORK_EVENT, pfork,
6120Sstevel@tonic-gate 	    PCTX_SYSC_EXIT_EVENT, pexit,
6130Sstevel@tonic-gate 	    PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
6140Sstevel@tonic-gate 	    PCTX_INIT_LWP_EVENT, pinit_lwp,
6150Sstevel@tonic-gate 	    PCTX_FINI_LWP_EVENT, pfini_lwp,
6160Sstevel@tonic-gate 	    PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
6170Sstevel@tonic-gate 	    PCTX_NULL_EVENT) == 0) {
6180Sstevel@tonic-gate 		state->accum = cpc_setgrp_clone(opts->master);
6190Sstevel@tonic-gate 		(void) pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
6200Sstevel@tonic-gate 		if (state->accum) {
6210Sstevel@tonic-gate 			free(state->accum);
6220Sstevel@tonic-gate 			state->accum = NULL;
6230Sstevel@tonic-gate 		}
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate /*
6280Sstevel@tonic-gate  * Translate the incoming options into actions, and get the
6290Sstevel@tonic-gate  * tool and the process to control running.
6300Sstevel@tonic-gate  */
6310Sstevel@tonic-gate static int
6320Sstevel@tonic-gate cputrack(int argc, char *argv[], int optind)
6330Sstevel@tonic-gate {
6340Sstevel@tonic-gate 	struct pstate __state, *state = &__state;
6350Sstevel@tonic-gate 	pctx_t *pctx;
6360Sstevel@tonic-gate 	int err;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	bzero(state, sizeof (*state));
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	if (opts->pid == 0) {
6410Sstevel@tonic-gate 		if (argc <= optind) {
6420Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s\n",
6430Sstevel@tonic-gate 			    opts->pgmname,
6440Sstevel@tonic-gate 			    gettext("no program to start"));
6450Sstevel@tonic-gate 			return (1);
6460Sstevel@tonic-gate 		}
6470Sstevel@tonic-gate 		pctx = pctx_create(argv[optind],
6480Sstevel@tonic-gate 		    &argv[optind], state, 1, cputrack_pctx_errfn);
6490Sstevel@tonic-gate 		if (pctx == NULL) {
6500Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s '%s'\n",
6510Sstevel@tonic-gate 			    opts->pgmname,
6520Sstevel@tonic-gate 			    gettext("failed to start program"),
6530Sstevel@tonic-gate 			    argv[optind]);
6540Sstevel@tonic-gate 			return (1);
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 	} else {
6570Sstevel@tonic-gate 		pctx = pctx_capture(opts->pid, state, 1, cputrack_pctx_errfn);
6580Sstevel@tonic-gate 		if (pctx == NULL) {
6590Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: %s %d\n",
6600Sstevel@tonic-gate 			    opts->pgmname,
6610Sstevel@tonic-gate 			    gettext("failed to capture pid"),
6620Sstevel@tonic-gate 			    (int)opts->pid);
6630Sstevel@tonic-gate 			return (1);
6640Sstevel@tonic-gate 		}
6650Sstevel@tonic-gate 	}
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	err = pctx_set_events(pctx,
6680Sstevel@tonic-gate 	    PCTX_SYSC_EXEC_EVENT, pexec,
6690Sstevel@tonic-gate 	    PCTX_SYSC_FORK_EVENT, pfork,
6700Sstevel@tonic-gate 	    PCTX_SYSC_EXIT_EVENT, pexit,
6710Sstevel@tonic-gate 	    PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
6720Sstevel@tonic-gate 	    PCTX_INIT_LWP_EVENT, pinit_lwp,
6730Sstevel@tonic-gate 	    PCTX_FINI_LWP_EVENT, pfini_lwp,
6740Sstevel@tonic-gate 	    PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
6750Sstevel@tonic-gate 	    PCTX_NULL_EVENT);
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	if (err != 0) {
6780Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: %s\n",
6790Sstevel@tonic-gate 		    opts->pgmname,
6800Sstevel@tonic-gate 		    gettext("can't bind process context ops to process"));
6810Sstevel@tonic-gate 	} else {
6820Sstevel@tonic-gate 		if (opts->dotitle)
6830Sstevel@tonic-gate 			print_title(opts->master);
6840Sstevel@tonic-gate 		state->accum = cpc_setgrp_clone(opts->master);
6850Sstevel@tonic-gate 		zerotime();
6860Sstevel@tonic-gate 		err = pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
6870Sstevel@tonic-gate 		if (state->accum) {
6880Sstevel@tonic-gate 			cpc_setgrp_free(state->accum);
6890Sstevel@tonic-gate 			state->accum = NULL;
6900Sstevel@tonic-gate 		}
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate 	pctx_release(pctx);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	return (err != 0 ? 1 : 0);
6950Sstevel@tonic-gate }
6960Sstevel@tonic-gate 
697*3732Sae112802 #if defined(__i386)
698*3732Sae112802 
6990Sstevel@tonic-gate #define	OFFLINE_CMD	"/usr/sbin/psradm -f "
7000Sstevel@tonic-gate #define	BUFSIZE		5	/* enough for "n " where n is a cpuid */
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate /*
7030Sstevel@tonic-gate  * cpc_bind_pctx() failed with EACCES, which means the user must first offline
7040Sstevel@tonic-gate  * all but one logical processor on each physical processor. Print to stderr the
7050Sstevel@tonic-gate  * psradm command string to do this.
7060Sstevel@tonic-gate  */
7070Sstevel@tonic-gate static void
7080Sstevel@tonic-gate p4_ht_error(void)
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate 	kstat_ctl_t	*kc;
7110Sstevel@tonic-gate 	kstat_t		*ksp;
7120Sstevel@tonic-gate 	kstat_named_t	*k;
7130Sstevel@tonic-gate 	int		i;
7140Sstevel@tonic-gate 	int		max;
7150Sstevel@tonic-gate 	int		stat;
7160Sstevel@tonic-gate 	int		*designees;
7170Sstevel@tonic-gate 	int		*must_offline;
7180Sstevel@tonic-gate 	char		buf[BUFSIZE];
7190Sstevel@tonic-gate 	char		*cmd;
7200Sstevel@tonic-gate 	int		noffline = 0;
7210Sstevel@tonic-gate 	int		ndone = 0;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	(void) fprintf(stderr, "%s\n",
7240Sstevel@tonic-gate 	    gettext("Pentium 4 processors with HyperThreading present.\nOffline"
7250Sstevel@tonic-gate 		" all but one logical processor on each physical processor in"
7260Sstevel@tonic-gate 		" order to use\ncputrack.\n"));
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
7300Sstevel@tonic-gate 		return;
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	max = sysconf(_SC_CPUID_MAX);
7330Sstevel@tonic-gate 	if ((designees = malloc(max * sizeof (*designees))) == NULL) {
7340Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
7350Sstevel@tonic-gate 		    opts->pgmname);
7360Sstevel@tonic-gate 		exit(0);
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	if ((must_offline = malloc(max * sizeof (*designees))) == NULL) {
7400Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
7410Sstevel@tonic-gate 		    opts->pgmname);
7420Sstevel@tonic-gate 		exit(0);
7430Sstevel@tonic-gate 	}
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	for (i = 0; i < max; i++) {
7460Sstevel@tonic-gate 		designees[i] = -1;
7470Sstevel@tonic-gate 		must_offline[i] = 0;
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	for (i = 0; i < max; i++) {
7510Sstevel@tonic-gate 		stat = p_online(i, P_STATUS);
7520Sstevel@tonic-gate 		if (stat != P_ONLINE && stat != P_NOINTR)
7530Sstevel@tonic-gate 			continue;
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 		if ((ksp = kstat_lookup(kc, "cpu_info", i, NULL)) == NULL) {
7560Sstevel@tonic-gate 			free(designees);
7570Sstevel@tonic-gate 			free(must_offline);
7580Sstevel@tonic-gate 			return;
7590Sstevel@tonic-gate 		}
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 		if (kstat_read(kc, ksp, NULL) == -1) {
7620Sstevel@tonic-gate 			free(designees);
7630Sstevel@tonic-gate 			free(must_offline);
7640Sstevel@tonic-gate 			return;
7650Sstevel@tonic-gate 		}
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 		if ((k = (kstat_named_t *)kstat_data_lookup(ksp, "chip_id"))
7680Sstevel@tonic-gate 		    == NULL) {
7690Sstevel@tonic-gate 			free(designees);
7700Sstevel@tonic-gate 			free(must_offline);
7710Sstevel@tonic-gate 			return;
7720Sstevel@tonic-gate 		}
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 		if (designees[k->value.i32] == -1)
7750Sstevel@tonic-gate 			/*
7760Sstevel@tonic-gate 			 * This chip doesn't yet have a CPU designated to remain
7770Sstevel@tonic-gate 			 * online; let this one be it.
7780Sstevel@tonic-gate 			 */
7790Sstevel@tonic-gate 			designees[k->value.i32] = i;
7800Sstevel@tonic-gate 		else {
7810Sstevel@tonic-gate 			/*
7820Sstevel@tonic-gate 			 * This chip already has a designated CPU; this CPU must
7830Sstevel@tonic-gate 			 * go offline.
7840Sstevel@tonic-gate 			 */
7850Sstevel@tonic-gate 			must_offline[i] = 1;
7860Sstevel@tonic-gate 			noffline++;
7870Sstevel@tonic-gate 		}
7880Sstevel@tonic-gate 	}
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	/*
7910Sstevel@tonic-gate 	 * Now construct a string containing the command line used to offline
7920Sstevel@tonic-gate 	 * the appropriate processors.
7930Sstevel@tonic-gate 	 */
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	if ((cmd = malloc(strlen(OFFLINE_CMD) + (noffline * BUFSIZE) + 1))
7960Sstevel@tonic-gate 	    == NULL) {
7970Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no memory available\n"),
7980Sstevel@tonic-gate 		    opts->pgmname);
7990Sstevel@tonic-gate 		exit(0);
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	(void) strcpy(cmd, OFFLINE_CMD);
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	for (i = 0; i < max; i++) {
8050Sstevel@tonic-gate 		if (must_offline[i] == 0)
8060Sstevel@tonic-gate 			continue;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 		ndone++;
8090Sstevel@tonic-gate 		(void) snprintf(buf, BUFSIZE, "%d", i);
8100Sstevel@tonic-gate 		if (ndone < noffline)
8110Sstevel@tonic-gate 			(void) strcat(buf, " ");
8120Sstevel@tonic-gate 		(void) strcat(cmd, buf);
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	(void) fprintf(stderr, "%s:\n%s\n", gettext("The following command "
8160Sstevel@tonic-gate 	    "will configure the system appropriately"), cmd);
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate 	exit(1);
8190Sstevel@tonic-gate }
820*3732Sae112802 
821*3732Sae112802 #endif /* defined(__i386) */
822