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