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*11389SAlexander.Kolbasov@Sun.COM * Copyright 2009 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
65*11389SAlexander.Kolbasov@Sun.COM /*
66*11389SAlexander.Kolbasov@Sun.COM * How many signals caught from terminal
67*11389SAlexander.Kolbasov@Sun.COM * We bail out as soon as possible when interrupt is set
68*11389SAlexander.Kolbasov@Sun.COM */
69*11389SAlexander.Kolbasov@Sun.COM static int interrupt = 0;
70*11389SAlexander.Kolbasov@Sun.COM
710Sstevel@tonic-gate /*ARGSUSED*/
720Sstevel@tonic-gate static void
cputrack_errfn(const char * fn,int subcode,const char * fmt,va_list ap)730Sstevel@tonic-gate cputrack_errfn(const char *fn, int subcode, const char *fmt, va_list ap)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", opts->pgmname);
760Sstevel@tonic-gate if (opts->debug)
770Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", fn);
780Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate static void
cputrack_pctx_errfn(const char * fn,const char * fmt,va_list ap)820Sstevel@tonic-gate cputrack_pctx_errfn(const char *fn, const char *fmt, va_list ap)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate cputrack_errfn(fn, -1, fmt, ap);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate
870Sstevel@tonic-gate static int cputrack(int argc, char *argv[], int optind);
88*11389SAlexander.Kolbasov@Sun.COM static void intr(int);
89*11389SAlexander.Kolbasov@Sun.COM
903732Sae112802 #if defined(__i386)
910Sstevel@tonic-gate static void p4_ht_error(void);
923732Sae112802 #endif
930Sstevel@tonic-gate
940Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
950Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
960Sstevel@tonic-gate #endif
970Sstevel@tonic-gate
980Sstevel@tonic-gate int
main(int argc,char * argv[])990Sstevel@tonic-gate main(int argc, char *argv[])
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate struct options *opts = &__options;
1020Sstevel@tonic-gate int c, errcnt = 0;
1030Sstevel@tonic-gate int nsamples;
1040Sstevel@tonic-gate cpc_setgrp_t *sgrp;
1050Sstevel@tonic-gate char *errstr;
1060Sstevel@tonic-gate int ret;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1090Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if ((opts->pgmname = strrchr(argv[0], '/')) == NULL)
1120Sstevel@tonic-gate opts->pgmname = argv[0];
1130Sstevel@tonic-gate else
1140Sstevel@tonic-gate opts->pgmname++;
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate if ((cpc = cpc_open(CPC_VER_CURRENT)) == NULL) {
1170Sstevel@tonic-gate errstr = strerror(errno);
1180Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: cannot access performance "
1190Sstevel@tonic-gate "counter library - %s\n"), opts->pgmname, errstr);
1200Sstevel@tonic-gate return (1);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate (void) cpc_seterrhndlr(cpc, cputrack_errfn);
1240Sstevel@tonic-gate strtoset_errfn = cputrack_errfn;
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate * Establish (non-zero) defaults
1280Sstevel@tonic-gate */
1290Sstevel@tonic-gate opts->mseconds = 1000;
1300Sstevel@tonic-gate opts->dotitle = 1;
1310Sstevel@tonic-gate opts->log = stdout;
1320Sstevel@tonic-gate if ((opts->master = cpc_setgrp_new(cpc, 0)) == NULL) {
1330Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"),
1340Sstevel@tonic-gate opts->pgmname);
1350Sstevel@tonic-gate exit(1);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate while ((c = getopt(argc, argv, "T:N:Defhntvo:r:c:p:")) != EOF)
1390Sstevel@tonic-gate switch (c) {
1400Sstevel@tonic-gate case 'T': /* sample time, seconds */
1410Sstevel@tonic-gate opts->mseconds = (uint_t)(atof(optarg) * 1000.0);
1420Sstevel@tonic-gate break;
1430Sstevel@tonic-gate case 'N': /* number of samples */
1440Sstevel@tonic-gate nsamples = atoi(optarg);
1450Sstevel@tonic-gate if (nsamples < 0)
1460Sstevel@tonic-gate errcnt++;
1470Sstevel@tonic-gate else
1480Sstevel@tonic-gate opts->nsamples = (uint_t)nsamples;
1490Sstevel@tonic-gate break;
1500Sstevel@tonic-gate case 'D': /* enable debugging */
1510Sstevel@tonic-gate opts->debug++;
1520Sstevel@tonic-gate break;
1530Sstevel@tonic-gate case 'f': /* follow fork */
1540Sstevel@tonic-gate opts->followfork++;
1550Sstevel@tonic-gate break;
1560Sstevel@tonic-gate case 'e': /* follow exec */
1570Sstevel@tonic-gate opts->followexec++;
1580Sstevel@tonic-gate break;
1590Sstevel@tonic-gate case 'n': /* no titles */
1600Sstevel@tonic-gate opts->dotitle = 0;
1610Sstevel@tonic-gate break;
1620Sstevel@tonic-gate case 't': /* print %tick */
1630Sstevel@tonic-gate opts->dotick = 1;
1640Sstevel@tonic-gate break;
1650Sstevel@tonic-gate case 'v':
1660Sstevel@tonic-gate opts->verbose = 1; /* more chatty */
1670Sstevel@tonic-gate break;
1680Sstevel@tonic-gate case 'o':
1690Sstevel@tonic-gate if (optarg == NULL) {
1700Sstevel@tonic-gate errcnt++;
1710Sstevel@tonic-gate break;
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate if ((opts->log = fopen(optarg, "w")) == NULL) {
1740Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1750Sstevel@tonic-gate "%s: cannot open '%s' for writing\n"),
1760Sstevel@tonic-gate opts->pgmname, optarg);
1770Sstevel@tonic-gate return (1);
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate break;
1800Sstevel@tonic-gate case 'c': /* specify statistics */
1810Sstevel@tonic-gate if ((sgrp = cpc_setgrp_newset(opts->master,
1820Sstevel@tonic-gate optarg, &errcnt)) != NULL)
1830Sstevel@tonic-gate opts->master = sgrp;
1840Sstevel@tonic-gate break;
1850Sstevel@tonic-gate case 'p': /* grab given pid */
1860Sstevel@tonic-gate if ((opts->pid = atoi(optarg)) <= 0)
1870Sstevel@tonic-gate errcnt++;
1880Sstevel@tonic-gate break;
1890Sstevel@tonic-gate case 'h':
1900Sstevel@tonic-gate opts->dohelp = 1;
1910Sstevel@tonic-gate break;
1920Sstevel@tonic-gate case '?':
1930Sstevel@tonic-gate default:
1940Sstevel@tonic-gate errcnt++;
1950Sstevel@tonic-gate break;
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate if (opts->nsamples == 0)
1990Sstevel@tonic-gate opts->nsamples = UINT_MAX;
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate if (errcnt != 0 ||
2020Sstevel@tonic-gate opts->dohelp ||
2030Sstevel@tonic-gate (argc == optind && opts->pid == 0) ||
2040Sstevel@tonic-gate (argc > optind && opts->pid != 0) ||
2050Sstevel@tonic-gate (opts->nsets = cpc_setgrp_numsets(opts->master)) == 0) {
2060Sstevel@tonic-gate (void) fprintf(opts->dohelp ? stdout : stderr, gettext(
2070Sstevel@tonic-gate "Usage:\n\t%s [-T secs] [-N count] [-Defhnv] [-o file]\n"
2080Sstevel@tonic-gate "\t\t-c events [command [args] | -p pid]\n\n"
2090Sstevel@tonic-gate "\t-T secs\t seconds between samples, default 1\n"
2100Sstevel@tonic-gate "\t-N count number of samples, default unlimited\n"
2110Sstevel@tonic-gate "\t-D\t enable debug mode\n"
2120Sstevel@tonic-gate "\t-e\t follow exec(2), and execve(2)\n"
2130Sstevel@tonic-gate "\t-f\t follow fork(2), fork1(2), and vfork(2)\n"
2140Sstevel@tonic-gate "\t-h\t print extended usage information\n"
2150Sstevel@tonic-gate "\t-n\t suppress titles\n"
2160Sstevel@tonic-gate "\t-t\t include virtualized %s register\n"
2170Sstevel@tonic-gate "\t-v\t verbose mode\n"
2180Sstevel@tonic-gate "\t-o file\t write cpu statistics to this file\n"
2190Sstevel@tonic-gate "\t-c events specify processor events to be monitored\n"
2200Sstevel@tonic-gate "\t-p pid\t pid of existing process to capture\n\n"
2210Sstevel@tonic-gate "\tUse cpustat(1M) to monitor system-wide statistics.\n"),
2220Sstevel@tonic-gate opts->pgmname, CPC_TICKREG_NAME);
2230Sstevel@tonic-gate if (opts->dohelp) {
2240Sstevel@tonic-gate (void) putchar('\n');
2250Sstevel@tonic-gate (void) capabilities(cpc, stdout);
2260Sstevel@tonic-gate exit(0);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate exit(2);
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate
231*11389SAlexander.Kolbasov@Sun.COM /*
232*11389SAlexander.Kolbasov@Sun.COM * Catch signals from terminal, so they can be handled asynchronously
233*11389SAlexander.Kolbasov@Sun.COM * when we're ready instead of when we're not (;-)
234*11389SAlexander.Kolbasov@Sun.COM */
235*11389SAlexander.Kolbasov@Sun.COM if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
236*11389SAlexander.Kolbasov@Sun.COM (void) sigset(SIGHUP, intr);
237*11389SAlexander.Kolbasov@Sun.COM if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
238*11389SAlexander.Kolbasov@Sun.COM (void) sigset(SIGINT, intr);
239*11389SAlexander.Kolbasov@Sun.COM if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
240*11389SAlexander.Kolbasov@Sun.COM (void) sigset(SIGQUIT, intr);
241*11389SAlexander.Kolbasov@Sun.COM (void) sigset(SIGPIPE, intr);
242*11389SAlexander.Kolbasov@Sun.COM (void) sigset(SIGTERM, intr);
243*11389SAlexander.Kolbasov@Sun.COM
2440Sstevel@tonic-gate cpc_setgrp_reset(opts->master);
2450Sstevel@tonic-gate (void) setvbuf(opts->log, NULL, _IOLBF, 0);
2460Sstevel@tonic-gate ret = cputrack(argc, argv, optind);
2470Sstevel@tonic-gate (void) cpc_close(cpc);
2480Sstevel@tonic-gate return (ret);
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate static void
print_title(cpc_setgrp_t * sgrp)2520Sstevel@tonic-gate print_title(cpc_setgrp_t *sgrp)
2530Sstevel@tonic-gate {
2540Sstevel@tonic-gate (void) fprintf(opts->log, "%7s ", "time");
2550Sstevel@tonic-gate if (opts->followfork)
2560Sstevel@tonic-gate (void) fprintf(opts->log, "%6s ", "pid");
2570Sstevel@tonic-gate (void) fprintf(opts->log, "%3s %10s ", "lwp", "event");
2580Sstevel@tonic-gate if (opts->dotick)
2590Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", CPC_TICKREG_NAME);
2600Sstevel@tonic-gate (void) fprintf(opts->log, "%s\n", cpc_setgrp_gethdr(sgrp));
2610Sstevel@tonic-gate (void) fflush(opts->log);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate static void
print_exec(float now,pid_t pid,char * name)2650Sstevel@tonic-gate print_exec(float now, pid_t pid, char *name)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate if (name == NULL)
2680Sstevel@tonic-gate name = "(unknown)";
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ", now);
2710Sstevel@tonic-gate if (opts->followfork)
2720Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)pid);
2730Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", 1, "exec");
2740Sstevel@tonic-gate if (opts->dotick)
2750Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", "");
2760Sstevel@tonic-gate (void) fprintf(opts->log, "%9s %9s # '%s'\n", "", "", name);
2770Sstevel@tonic-gate (void) fflush(opts->log);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate static void
print_fork(float now,pid_t newpid,id_t lwpid,pid_t oldpid)2810Sstevel@tonic-gate print_fork(float now, pid_t newpid, id_t lwpid, pid_t oldpid)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ", now);
2840Sstevel@tonic-gate if (opts->followfork)
2850Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)oldpid);
2860Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", (int)lwpid, "fork");
2870Sstevel@tonic-gate if (opts->dotick)
2880Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", "");
2890Sstevel@tonic-gate (void) fprintf(opts->log, "%9s %9s # %d\n", "", "", (int)newpid);
2900Sstevel@tonic-gate (void) fflush(opts->log);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate static void
print_sample(pid_t pid,id_t lwpid,char * pevent,cpc_buf_t * buf,int nreq,const char * evname)2940Sstevel@tonic-gate print_sample(pid_t pid, id_t lwpid,
2950Sstevel@tonic-gate char *pevent, cpc_buf_t *buf, int nreq, const char *evname)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate uint64_t val;
2980Sstevel@tonic-gate int i;
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ",
3010Sstevel@tonic-gate mstimestamp(cpc_buf_hrtime(cpc, buf)));
3020Sstevel@tonic-gate if (opts->followfork)
3030Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)pid);
3040Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", (int)lwpid, pevent);
3050Sstevel@tonic-gate if (opts->dotick)
3060Sstevel@tonic-gate (void) fprintf(opts->log, "%9" PRId64 " ",
3070Sstevel@tonic-gate cpc_buf_tick(cpc, buf));
3080Sstevel@tonic-gate for (i = 0; i < nreq; i++) {
3090Sstevel@tonic-gate (void) cpc_buf_get(cpc, buf, i, &val);
3100Sstevel@tonic-gate (void) fprintf(opts->log, "%9" PRId64 " ", val);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate if (opts->nsets > 1)
3130Sstevel@tonic-gate (void) fprintf(opts->log, " # %s\n", evname);
3140Sstevel@tonic-gate else
3150Sstevel@tonic-gate (void) fputc('\n', opts->log);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate struct pstate {
3190Sstevel@tonic-gate cpc_setgrp_t *accum;
3200Sstevel@tonic-gate cpc_setgrp_t **sgrps;
3210Sstevel@tonic-gate int maxlwpid;
3220Sstevel@tonic-gate };
3230Sstevel@tonic-gate
3240Sstevel@tonic-gate static int
pinit_lwp(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)3250Sstevel@tonic-gate pinit_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate struct pstate *state = arg;
3280Sstevel@tonic-gate cpc_setgrp_t *sgrp;
3290Sstevel@tonic-gate cpc_set_t *set;
3300Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
3310Sstevel@tonic-gate char *errstr;
3320Sstevel@tonic-gate int nreq;
3330Sstevel@tonic-gate
334*11389SAlexander.Kolbasov@Sun.COM if (interrupt)
335*11389SAlexander.Kolbasov@Sun.COM return (0);
336*11389SAlexander.Kolbasov@Sun.COM
3370Sstevel@tonic-gate if (state->maxlwpid < lwpid) {
3380Sstevel@tonic-gate state->sgrps = realloc(state->sgrps,
3390Sstevel@tonic-gate lwpid * sizeof (state->sgrps));
3400Sstevel@tonic-gate if (state->sgrps == NULL) {
3410Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3420Sstevel@tonic-gate "%6d: init_lwp: out of memory\n"), (int)pid);
3430Sstevel@tonic-gate return (-1);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate while (state->maxlwpid < lwpid) {
3460Sstevel@tonic-gate state->sgrps[state->maxlwpid] = NULL;
3470Sstevel@tonic-gate state->maxlwpid++;
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate if ((sgrp = state->sgrps[lwpid-1]) == NULL) {
3520Sstevel@tonic-gate if ((sgrp = cpc_setgrp_clone(opts->master)) == NULL) {
3530Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3540Sstevel@tonic-gate "%6d: init_lwp: out of memory\n"), (int)pid);
3550Sstevel@tonic-gate return (-1);
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate state->sgrps[lwpid-1] = sgrp;
3580Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp);
3590Sstevel@tonic-gate } else {
3600Sstevel@tonic-gate cpc_setgrp_reset(sgrp);
3610Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate
3640Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate if (cpc_bind_pctx(cpc, pctx, lwpid, set, 0) != 0 ||
3670Sstevel@tonic-gate cpc_set_sample(cpc, set, *data2) != 0) {
3680Sstevel@tonic-gate errstr = strerror(errno);
3690Sstevel@tonic-gate if (errno == EAGAIN)
3700Sstevel@tonic-gate (void) cpc_unbind(cpc, set);
3713732Sae112802 #if defined(__i386)
3720Sstevel@tonic-gate if (errno == EACCES)
3730Sstevel@tonic-gate p4_ht_error();
3740Sstevel@tonic-gate else
3753732Sae112802 #endif
3760Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3777990SKuriakose.Kuruvilla@Sun.COM "%6d: init_lwp: can't bind perf counters "
3787990SKuriakose.Kuruvilla@Sun.COM "to lwp%d - %s\n"), (int)pid, (int)lwpid,
3790Sstevel@tonic-gate errstr);
3800Sstevel@tonic-gate return (-1);
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate if (opts->verbose)
3840Sstevel@tonic-gate print_sample(pid, lwpid, "init_lwp",
3850Sstevel@tonic-gate *data2, nreq, cpc_setgrp_getname(sgrp));
3860Sstevel@tonic-gate return (0);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /*ARGSUSED*/
3900Sstevel@tonic-gate static int
pfini_lwp(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)3910Sstevel@tonic-gate pfini_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate struct pstate *state = arg;
3940Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
3950Sstevel@tonic-gate cpc_set_t *set;
3960Sstevel@tonic-gate char *errstr;
3970Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
3980Sstevel@tonic-gate int nreq;
3990Sstevel@tonic-gate
400*11389SAlexander.Kolbasov@Sun.COM if (interrupt)
401*11389SAlexander.Kolbasov@Sun.COM return (0);
402*11389SAlexander.Kolbasov@Sun.COM
4030Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp);
4040Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4057990SKuriakose.Kuruvilla@Sun.COM if (cpc_set_sample(cpc, set, *scratch) == 0) {
4067990SKuriakose.Kuruvilla@Sun.COM if (opts->nsets == 1) {
4077990SKuriakose.Kuruvilla@Sun.COM /*
4087990SKuriakose.Kuruvilla@Sun.COM * When we only have one set of counts, the sample
4097990SKuriakose.Kuruvilla@Sun.COM * gives us the accumulated count.
4107990SKuriakose.Kuruvilla@Sun.COM */
4117990SKuriakose.Kuruvilla@Sun.COM *data1 = *scratch;
4127990SKuriakose.Kuruvilla@Sun.COM } else {
4137990SKuriakose.Kuruvilla@Sun.COM /*
4147990SKuriakose.Kuruvilla@Sun.COM * When we have more than one set of counts, the
4157990SKuriakose.Kuruvilla@Sun.COM * sample gives us the count for the latest sample
4167990SKuriakose.Kuruvilla@Sun.COM * period. *data1 contains the accumulated count but
4177990SKuriakose.Kuruvilla@Sun.COM * does not include the count for the latest sample
4187990SKuriakose.Kuruvilla@Sun.COM * period for this set of counters.
4197990SKuriakose.Kuruvilla@Sun.COM */
4207990SKuriakose.Kuruvilla@Sun.COM cpc_buf_add(cpc, *data1, *data1, *scratch);
4217990SKuriakose.Kuruvilla@Sun.COM }
4220Sstevel@tonic-gate if (opts->verbose)
4230Sstevel@tonic-gate print_sample(pid, lwpid, "fini_lwp",
4240Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp));
4250Sstevel@tonic-gate cpc_setgrp_accum(state->accum, sgrp);
4260Sstevel@tonic-gate if (cpc_unbind(cpc, set) == 0)
4270Sstevel@tonic-gate return (0);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate switch (errno) {
4310Sstevel@tonic-gate case EAGAIN:
4320Sstevel@tonic-gate (void) fprintf(stderr, gettext("%6d: fini_lwp: "
4330Sstevel@tonic-gate "lwp%d: perf counter contents invalidated\n"),
4340Sstevel@tonic-gate (int)pid, (int)lwpid);
4350Sstevel@tonic-gate break;
4360Sstevel@tonic-gate default:
4370Sstevel@tonic-gate errstr = strerror(errno);
4380Sstevel@tonic-gate (void) fprintf(stderr, gettext("%6d: fini_lwp: "
4390Sstevel@tonic-gate "lwp%d: can't access perf counters - %s\n"),
4400Sstevel@tonic-gate (int)pid, (int)lwpid, errstr);
4410Sstevel@tonic-gate break;
4420Sstevel@tonic-gate }
4432235Skk112340 return (-1);
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate /*ARGSUSED*/
4470Sstevel@tonic-gate static int
plwp_create(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)4480Sstevel@tonic-gate plwp_create(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate cpc_setgrp_t *sgrp = opts->master;
4510Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
4520Sstevel@tonic-gate int nreq;
4530Sstevel@tonic-gate
454*11389SAlexander.Kolbasov@Sun.COM if (interrupt)
455*11389SAlexander.Kolbasov@Sun.COM return (0);
456*11389SAlexander.Kolbasov@Sun.COM
4570Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate print_sample(pid, lwpid, "lwp_create",
4600Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp));
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate return (0);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate /*ARGSUSED*/
4660Sstevel@tonic-gate static int
plwp_exit(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)4670Sstevel@tonic-gate plwp_exit(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate struct pstate *state = arg;
4700Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
4710Sstevel@tonic-gate cpc_set_t *start;
4720Sstevel@tonic-gate int nreq;
4730Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
4740Sstevel@tonic-gate
475*11389SAlexander.Kolbasov@Sun.COM if (interrupt)
476*11389SAlexander.Kolbasov@Sun.COM return (0);
477*11389SAlexander.Kolbasov@Sun.COM
4780Sstevel@tonic-gate start = cpc_setgrp_getset(sgrp);
4790Sstevel@tonic-gate do {
4800Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4810Sstevel@tonic-gate if (cpc_buf_hrtime(cpc, *data1) == 0)
4820Sstevel@tonic-gate continue;
4830Sstevel@tonic-gate print_sample(pid, lwpid, "lwp_exit",
4840Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp));
4850Sstevel@tonic-gate } while (cpc_setgrp_nextset(sgrp) != start);
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate return (0);
4880Sstevel@tonic-gate }
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate /*ARGSUSED*/
4910Sstevel@tonic-gate static int
pexec(pctx_t * pctx,pid_t pid,id_t lwpid,char * name,void * arg)4920Sstevel@tonic-gate pexec(pctx_t *pctx, pid_t pid, id_t lwpid, char *name, void *arg)
4930Sstevel@tonic-gate {
4940Sstevel@tonic-gate struct pstate *state = arg;
4950Sstevel@tonic-gate float now = 0.0;
4960Sstevel@tonic-gate cpc_set_t *start;
4970Sstevel@tonic-gate int nreq;
4980Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
4990Sstevel@tonic-gate hrtime_t hrt;
5000Sstevel@tonic-gate
501*11389SAlexander.Kolbasov@Sun.COM if (interrupt)
502*11389SAlexander.Kolbasov@Sun.COM return (0);
503*11389SAlexander.Kolbasov@Sun.COM
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate * Print the accumulated results from the previous program image
5060Sstevel@tonic-gate */
5070Sstevel@tonic-gate cpc_setgrp_reset(state->accum);
5080Sstevel@tonic-gate start = cpc_setgrp_getset(state->accum);
5090Sstevel@tonic-gate do {
5100Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
5110Sstevel@tonic-gate &scratch);
5120Sstevel@tonic-gate hrt = cpc_buf_hrtime(cpc, *data1);
5130Sstevel@tonic-gate if (hrt == 0)
5140Sstevel@tonic-gate continue;
5150Sstevel@tonic-gate print_sample(pid, lwpid, "exec",
5160Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(state->accum));
5170Sstevel@tonic-gate if (now < mstimestamp(hrt))
5180Sstevel@tonic-gate now = mstimestamp(hrt);
5190Sstevel@tonic-gate } while (cpc_setgrp_nextset(state->accum) != start);
5200Sstevel@tonic-gate
5210Sstevel@tonic-gate print_exec(now, pid, name);
5220Sstevel@tonic-gate
5230Sstevel@tonic-gate if (state->accum != NULL) {
5240Sstevel@tonic-gate cpc_setgrp_free(state->accum);
5250Sstevel@tonic-gate state->accum = NULL;
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate if (opts->followexec) {
5290Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master);
5300Sstevel@tonic-gate return (0);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate return (-1);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate /*ARGSUSED*/
5360Sstevel@tonic-gate static void
pexit(pctx_t * pctx,pid_t pid,id_t lwpid,int status,void * arg)5370Sstevel@tonic-gate pexit(pctx_t *pctx, pid_t pid, id_t lwpid, int status, void *arg)
5380Sstevel@tonic-gate {
5390Sstevel@tonic-gate struct pstate *state = arg;
5400Sstevel@tonic-gate cpc_set_t *start;
5410Sstevel@tonic-gate int nreq;
5420Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
5430Sstevel@tonic-gate
544*11389SAlexander.Kolbasov@Sun.COM if (interrupt)
545*11389SAlexander.Kolbasov@Sun.COM return;
546*11389SAlexander.Kolbasov@Sun.COM
5470Sstevel@tonic-gate cpc_setgrp_reset(state->accum);
5480Sstevel@tonic-gate start = cpc_setgrp_getset(state->accum);
5490Sstevel@tonic-gate do {
5500Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
5510Sstevel@tonic-gate &scratch);
5520Sstevel@tonic-gate if (cpc_buf_hrtime(cpc, *data1) == 0)
5530Sstevel@tonic-gate continue;
5540Sstevel@tonic-gate print_sample(pid, lwpid, "exit",
5550Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(state->accum));
5560Sstevel@tonic-gate } while (cpc_setgrp_nextset(state->accum) != start);
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate cpc_setgrp_free(state->accum);
5590Sstevel@tonic-gate state->accum = NULL;
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate for (lwpid = 1; lwpid < state->maxlwpid; lwpid++)
5620Sstevel@tonic-gate if (state->sgrps[lwpid-1] != NULL) {
5630Sstevel@tonic-gate cpc_setgrp_free(state->sgrps[lwpid-1]);
5640Sstevel@tonic-gate state->sgrps[lwpid-1] = NULL;
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate free(state->sgrps);
5670Sstevel@tonic-gate state->sgrps = NULL;
5680Sstevel@tonic-gate }
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate static int
ptick(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)5710Sstevel@tonic-gate ptick(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
5720Sstevel@tonic-gate {
5730Sstevel@tonic-gate struct pstate *state = arg;
5740Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
5750Sstevel@tonic-gate cpc_set_t *this = cpc_setgrp_getset(sgrp);
5760Sstevel@tonic-gate const char *name = cpc_setgrp_getname(sgrp);
5770Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch, *tmp;
5780Sstevel@tonic-gate char *errstr;
5790Sstevel@tonic-gate int nreqs;
5800Sstevel@tonic-gate
581*11389SAlexander.Kolbasov@Sun.COM if (interrupt)
582*11389SAlexander.Kolbasov@Sun.COM return (0);
583*11389SAlexander.Kolbasov@Sun.COM
5840Sstevel@tonic-gate nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate if (opts->nsets == 1) {
5870Sstevel@tonic-gate /*
5880Sstevel@tonic-gate * If we're dealing with one set, buffer usage is:
5890Sstevel@tonic-gate *
5900Sstevel@tonic-gate * data1 = most recent data snapshot
5910Sstevel@tonic-gate * data2 = previous data snapshot
5920Sstevel@tonic-gate * scratch = used for diffing data1 and data2
5930Sstevel@tonic-gate *
5940Sstevel@tonic-gate * Save the snapshot from the previous sample in data2
5950Sstevel@tonic-gate * before putting the current sample in data1.
5960Sstevel@tonic-gate */
5970Sstevel@tonic-gate tmp = *data1;
5980Sstevel@tonic-gate *data1 = *data2;
5990Sstevel@tonic-gate *data2 = tmp;
6000Sstevel@tonic-gate if (cpc_set_sample(cpc, this, *data1) != 0)
6010Sstevel@tonic-gate goto broken;
6020Sstevel@tonic-gate cpc_buf_sub(cpc, *scratch, *data1, *data2);
6030Sstevel@tonic-gate } else {
6040Sstevel@tonic-gate cpc_set_t *next = cpc_setgrp_nextset(sgrp);
6050Sstevel@tonic-gate /*
6060Sstevel@tonic-gate * If there is more than set in use, we will need to
6070Sstevel@tonic-gate * unbind and re-bind on each go-around because each
6080Sstevel@tonic-gate * time a counter is bound, it is preset to 0 (as it was
6090Sstevel@tonic-gate * specified when the requests were added to the set).
6100Sstevel@tonic-gate *
6110Sstevel@tonic-gate * Buffer usage in this case is:
6120Sstevel@tonic-gate *
6130Sstevel@tonic-gate * data1 = total counts for this set since program began
6140Sstevel@tonic-gate * data2 = unused
6150Sstevel@tonic-gate * scratch = most recent data snapshot
6160Sstevel@tonic-gate */
6170Sstevel@tonic-gate
6180Sstevel@tonic-gate if (cpc_set_sample(cpc, this, *scratch) != 0)
6190Sstevel@tonic-gate goto broken;
6200Sstevel@tonic-gate cpc_buf_add(cpc, *data1, *data1, *scratch);
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate /*
6230Sstevel@tonic-gate * No need to unbind the previous set, as binding another set
6240Sstevel@tonic-gate * automatically unbinds the most recently bound set.
6250Sstevel@tonic-gate */
6260Sstevel@tonic-gate if (cpc_bind_pctx(cpc, pctx, lwpid, next, 0) != 0)
6270Sstevel@tonic-gate goto broken;
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate print_sample(pid, lwpid, "tick", *scratch, nreqs, name);
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate return (0);
6320Sstevel@tonic-gate
6330Sstevel@tonic-gate broken:
6340Sstevel@tonic-gate switch (errno) {
6350Sstevel@tonic-gate case EAGAIN:
6360Sstevel@tonic-gate (void) fprintf(stderr, gettext(
6370Sstevel@tonic-gate "%6d: tick: lwp%d: perf counter contents invalidated\n"),
6380Sstevel@tonic-gate (int)pid, (int)lwpid);
6390Sstevel@tonic-gate break;
6400Sstevel@tonic-gate default:
6410Sstevel@tonic-gate errstr = strerror(errno);
6420Sstevel@tonic-gate (void) fprintf(stderr, gettext(
6430Sstevel@tonic-gate "%6d: tick: lwp%d: can't access perf counter - %s\n"),
6440Sstevel@tonic-gate (int)pid, (int)lwpid, errstr);
6450Sstevel@tonic-gate break;
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate (void) cpc_unbind(cpc, this);
6480Sstevel@tonic-gate return (-1);
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate * The system has just created a new address space that has a new pid.
6530Sstevel@tonic-gate * We're running in a child of the controlling process, with a new
6540Sstevel@tonic-gate * pctx handle already opened on the child of the original controlled process.
6550Sstevel@tonic-gate */
6560Sstevel@tonic-gate static void
pfork(pctx_t * pctx,pid_t oldpid,pid_t pid,id_t lwpid,void * arg)6570Sstevel@tonic-gate pfork(pctx_t *pctx, pid_t oldpid, pid_t pid, id_t lwpid, void *arg)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate struct pstate *state = arg;
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate print_fork(mstimestamp(0), pid, lwpid, oldpid);
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate if (!opts->followfork)
6640Sstevel@tonic-gate return;
6650Sstevel@tonic-gate
6660Sstevel@tonic-gate if (pctx_set_events(pctx,
6670Sstevel@tonic-gate PCTX_SYSC_EXEC_EVENT, pexec,
6680Sstevel@tonic-gate PCTX_SYSC_FORK_EVENT, pfork,
6690Sstevel@tonic-gate PCTX_SYSC_EXIT_EVENT, pexit,
6700Sstevel@tonic-gate PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
6710Sstevel@tonic-gate PCTX_INIT_LWP_EVENT, pinit_lwp,
6720Sstevel@tonic-gate PCTX_FINI_LWP_EVENT, pfini_lwp,
6730Sstevel@tonic-gate PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
6740Sstevel@tonic-gate PCTX_NULL_EVENT) == 0) {
6750Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master);
6760Sstevel@tonic-gate (void) pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
6770Sstevel@tonic-gate if (state->accum) {
6780Sstevel@tonic-gate free(state->accum);
6790Sstevel@tonic-gate state->accum = NULL;
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate /*
6850Sstevel@tonic-gate * Translate the incoming options into actions, and get the
6860Sstevel@tonic-gate * tool and the process to control running.
6870Sstevel@tonic-gate */
6880Sstevel@tonic-gate static int
cputrack(int argc,char * argv[],int optind)6890Sstevel@tonic-gate cputrack(int argc, char *argv[], int optind)
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate struct pstate __state, *state = &__state;
6920Sstevel@tonic-gate pctx_t *pctx;
6930Sstevel@tonic-gate int err;
6940Sstevel@tonic-gate
6950Sstevel@tonic-gate bzero(state, sizeof (*state));
6960Sstevel@tonic-gate
6970Sstevel@tonic-gate if (opts->pid == 0) {
6980Sstevel@tonic-gate if (argc <= optind) {
6990Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n",
7000Sstevel@tonic-gate opts->pgmname,
7010Sstevel@tonic-gate gettext("no program to start"));
7020Sstevel@tonic-gate return (1);
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate pctx = pctx_create(argv[optind],
7050Sstevel@tonic-gate &argv[optind], state, 1, cputrack_pctx_errfn);
7060Sstevel@tonic-gate if (pctx == NULL) {
7070Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s '%s'\n",
7080Sstevel@tonic-gate opts->pgmname,
7090Sstevel@tonic-gate gettext("failed to start program"),
7100Sstevel@tonic-gate argv[optind]);
7110Sstevel@tonic-gate return (1);
7120Sstevel@tonic-gate }
7130Sstevel@tonic-gate } else {
7140Sstevel@tonic-gate pctx = pctx_capture(opts->pid, state, 1, cputrack_pctx_errfn);
7150Sstevel@tonic-gate if (pctx == NULL) {
7160Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s %d\n",
7170Sstevel@tonic-gate opts->pgmname,
7180Sstevel@tonic-gate gettext("failed to capture pid"),
7190Sstevel@tonic-gate (int)opts->pid);
7200Sstevel@tonic-gate return (1);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate err = pctx_set_events(pctx,
7250Sstevel@tonic-gate PCTX_SYSC_EXEC_EVENT, pexec,
7260Sstevel@tonic-gate PCTX_SYSC_FORK_EVENT, pfork,
7270Sstevel@tonic-gate PCTX_SYSC_EXIT_EVENT, pexit,
7280Sstevel@tonic-gate PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
7290Sstevel@tonic-gate PCTX_INIT_LWP_EVENT, pinit_lwp,
7300Sstevel@tonic-gate PCTX_FINI_LWP_EVENT, pfini_lwp,
7310Sstevel@tonic-gate PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
7320Sstevel@tonic-gate PCTX_NULL_EVENT);
7330Sstevel@tonic-gate
7340Sstevel@tonic-gate if (err != 0) {
7350Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n",
7360Sstevel@tonic-gate opts->pgmname,
7370Sstevel@tonic-gate gettext("can't bind process context ops to process"));
7380Sstevel@tonic-gate } else {
7390Sstevel@tonic-gate if (opts->dotitle)
7400Sstevel@tonic-gate print_title(opts->master);
7410Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master);
7420Sstevel@tonic-gate zerotime();
7430Sstevel@tonic-gate err = pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
7440Sstevel@tonic-gate if (state->accum) {
7450Sstevel@tonic-gate cpc_setgrp_free(state->accum);
7460Sstevel@tonic-gate state->accum = NULL;
7470Sstevel@tonic-gate }
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate return (err != 0 ? 1 : 0);
7510Sstevel@tonic-gate }
7520Sstevel@tonic-gate
7533732Sae112802 #if defined(__i386)
7543732Sae112802
7550Sstevel@tonic-gate #define OFFLINE_CMD "/usr/sbin/psradm -f "
7560Sstevel@tonic-gate #define BUFSIZE 5 /* enough for "n " where n is a cpuid */
7570Sstevel@tonic-gate
7580Sstevel@tonic-gate /*
7590Sstevel@tonic-gate * cpc_bind_pctx() failed with EACCES, which means the user must first offline
7600Sstevel@tonic-gate * all but one logical processor on each physical processor. Print to stderr the
7610Sstevel@tonic-gate * psradm command string to do this.
7620Sstevel@tonic-gate */
7630Sstevel@tonic-gate static void
p4_ht_error(void)7640Sstevel@tonic-gate p4_ht_error(void)
7650Sstevel@tonic-gate {
7660Sstevel@tonic-gate kstat_ctl_t *kc;
7670Sstevel@tonic-gate kstat_t *ksp;
7680Sstevel@tonic-gate kstat_named_t *k;
7690Sstevel@tonic-gate int i;
7700Sstevel@tonic-gate int max;
7710Sstevel@tonic-gate int stat;
7720Sstevel@tonic-gate int *designees;
7730Sstevel@tonic-gate int *must_offline;
7740Sstevel@tonic-gate char buf[BUFSIZE];
7750Sstevel@tonic-gate char *cmd;
7760Sstevel@tonic-gate int noffline = 0;
7770Sstevel@tonic-gate int ndone = 0;
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate (void) fprintf(stderr, "%s\n",
7800Sstevel@tonic-gate gettext("Pentium 4 processors with HyperThreading present.\nOffline"
7817990SKuriakose.Kuruvilla@Sun.COM " all but one logical processor on each physical processor in"
7827990SKuriakose.Kuruvilla@Sun.COM " order to use\ncputrack.\n"));
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate if ((kc = kstat_open()) == NULL)
7860Sstevel@tonic-gate return;
7870Sstevel@tonic-gate
7880Sstevel@tonic-gate max = sysconf(_SC_CPUID_MAX);
7890Sstevel@tonic-gate if ((designees = malloc(max * sizeof (*designees))) == NULL) {
7900Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"),
7910Sstevel@tonic-gate opts->pgmname);
7920Sstevel@tonic-gate exit(0);
7930Sstevel@tonic-gate }
7940Sstevel@tonic-gate
7950Sstevel@tonic-gate if ((must_offline = malloc(max * sizeof (*designees))) == NULL) {
7960Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"),
7970Sstevel@tonic-gate opts->pgmname);
7980Sstevel@tonic-gate exit(0);
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate for (i = 0; i < max; i++) {
8020Sstevel@tonic-gate designees[i] = -1;
8030Sstevel@tonic-gate must_offline[i] = 0;
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate
8060Sstevel@tonic-gate for (i = 0; i < max; i++) {
8070Sstevel@tonic-gate stat = p_online(i, P_STATUS);
8080Sstevel@tonic-gate if (stat != P_ONLINE && stat != P_NOINTR)
8090Sstevel@tonic-gate continue;
8100Sstevel@tonic-gate
8110Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "cpu_info", i, NULL)) == NULL) {
8120Sstevel@tonic-gate free(designees);
8130Sstevel@tonic-gate free(must_offline);
8140Sstevel@tonic-gate return;
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate
8170Sstevel@tonic-gate if (kstat_read(kc, ksp, NULL) == -1) {
8180Sstevel@tonic-gate free(designees);
8190Sstevel@tonic-gate free(must_offline);
8200Sstevel@tonic-gate return;
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate if ((k = (kstat_named_t *)kstat_data_lookup(ksp, "chip_id"))
8240Sstevel@tonic-gate == NULL) {
8250Sstevel@tonic-gate free(designees);
8260Sstevel@tonic-gate free(must_offline);
8270Sstevel@tonic-gate return;
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate
8300Sstevel@tonic-gate if (designees[k->value.i32] == -1)
8310Sstevel@tonic-gate /*
8320Sstevel@tonic-gate * This chip doesn't yet have a CPU designated to remain
8330Sstevel@tonic-gate * online; let this one be it.
8340Sstevel@tonic-gate */
8350Sstevel@tonic-gate designees[k->value.i32] = i;
8360Sstevel@tonic-gate else {
8370Sstevel@tonic-gate /*
8380Sstevel@tonic-gate * This chip already has a designated CPU; this CPU must
8390Sstevel@tonic-gate * go offline.
8400Sstevel@tonic-gate */
8410Sstevel@tonic-gate must_offline[i] = 1;
8420Sstevel@tonic-gate noffline++;
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate
8460Sstevel@tonic-gate /*
8470Sstevel@tonic-gate * Now construct a string containing the command line used to offline
8480Sstevel@tonic-gate * the appropriate processors.
8490Sstevel@tonic-gate */
8500Sstevel@tonic-gate
8510Sstevel@tonic-gate if ((cmd = malloc(strlen(OFFLINE_CMD) + (noffline * BUFSIZE) + 1))
8520Sstevel@tonic-gate == NULL) {
8530Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"),
8540Sstevel@tonic-gate opts->pgmname);
8550Sstevel@tonic-gate exit(0);
8560Sstevel@tonic-gate }
8570Sstevel@tonic-gate
8580Sstevel@tonic-gate (void) strcpy(cmd, OFFLINE_CMD);
8590Sstevel@tonic-gate
8600Sstevel@tonic-gate for (i = 0; i < max; i++) {
8610Sstevel@tonic-gate if (must_offline[i] == 0)
8620Sstevel@tonic-gate continue;
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate ndone++;
8650Sstevel@tonic-gate (void) snprintf(buf, BUFSIZE, "%d", i);
8660Sstevel@tonic-gate if (ndone < noffline)
8670Sstevel@tonic-gate (void) strcat(buf, " ");
8680Sstevel@tonic-gate (void) strcat(cmd, buf);
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate
8710Sstevel@tonic-gate (void) fprintf(stderr, "%s:\n%s\n", gettext("The following command "
8720Sstevel@tonic-gate "will configure the system appropriately"), cmd);
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate exit(1);
8750Sstevel@tonic-gate }
8763732Sae112802
8773732Sae112802 #endif /* defined(__i386) */
878*11389SAlexander.Kolbasov@Sun.COM
879*11389SAlexander.Kolbasov@Sun.COM /*ARGSUSED*/
880*11389SAlexander.Kolbasov@Sun.COM static void
intr(int sig)881*11389SAlexander.Kolbasov@Sun.COM intr(int sig)
882*11389SAlexander.Kolbasov@Sun.COM {
883*11389SAlexander.Kolbasov@Sun.COM interrupt++;
884*11389SAlexander.Kolbasov@Sun.COM if (cpc != NULL)
885*11389SAlexander.Kolbasov@Sun.COM cpc_terminate(cpc);
886*11389SAlexander.Kolbasov@Sun.COM }
887