17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c3666b4Skk112340 * Common Development and Distribution License (the "License"). 6*7c3666b4Skk112340 * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*7c3666b4Skk112340 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/time.h> 297c478bd9Sstevel@tonic-gate #include <stdio.h> 307c478bd9Sstevel@tonic-gate #include <stdlib.h> 317c478bd9Sstevel@tonic-gate #include <inttypes.h> 327c478bd9Sstevel@tonic-gate #include <unistd.h> 337c478bd9Sstevel@tonic-gate #include <string.h> 347c478bd9Sstevel@tonic-gate #include <strings.h> 357c478bd9Sstevel@tonic-gate #include <limits.h> 367c478bd9Sstevel@tonic-gate #include <libintl.h> 377c478bd9Sstevel@tonic-gate #include <locale.h> 387c478bd9Sstevel@tonic-gate #include <errno.h> 397c478bd9Sstevel@tonic-gate #include <kstat.h> 407c478bd9Sstevel@tonic-gate #include <libcpc.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include "cpucmds.h" 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate static struct options { 457c478bd9Sstevel@tonic-gate int debug; 467c478bd9Sstevel@tonic-gate int verbose; 477c478bd9Sstevel@tonic-gate int dotitle; 487c478bd9Sstevel@tonic-gate int dohelp; 497c478bd9Sstevel@tonic-gate int dotick; 507c478bd9Sstevel@tonic-gate int cpuver; 517c478bd9Sstevel@tonic-gate char *pgmname; 527c478bd9Sstevel@tonic-gate uint_t mseconds; 537c478bd9Sstevel@tonic-gate uint_t nsamples; 547c478bd9Sstevel@tonic-gate uint_t nsets; 557c478bd9Sstevel@tonic-gate cpc_setgrp_t *master; 567c478bd9Sstevel@tonic-gate int followfork; 577c478bd9Sstevel@tonic-gate int followexec; 587c478bd9Sstevel@tonic-gate pid_t pid; 597c478bd9Sstevel@tonic-gate FILE *log; 607c478bd9Sstevel@tonic-gate } __options; 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate static const struct options *opts = (const struct options *)&__options; 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate static cpc_t *cpc; 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 677c478bd9Sstevel@tonic-gate static void 687c478bd9Sstevel@tonic-gate cputrack_errfn(const char *fn, int subcode, const char *fmt, va_list ap) 697c478bd9Sstevel@tonic-gate { 707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", opts->pgmname); 717c478bd9Sstevel@tonic-gate if (opts->debug) 727c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", fn); 737c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap); 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate static void 777c478bd9Sstevel@tonic-gate cputrack_pctx_errfn(const char *fn, const char *fmt, va_list ap) 787c478bd9Sstevel@tonic-gate { 797c478bd9Sstevel@tonic-gate cputrack_errfn(fn, -1, fmt, ap); 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate static int cputrack(int argc, char *argv[], int optind); 837c478bd9Sstevel@tonic-gate static void p4_ht_error(void); 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 867c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 877c478bd9Sstevel@tonic-gate #endif 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate int 907c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 917c478bd9Sstevel@tonic-gate { 927c478bd9Sstevel@tonic-gate struct options *opts = &__options; 937c478bd9Sstevel@tonic-gate int c, errcnt = 0; 947c478bd9Sstevel@tonic-gate int nsamples; 957c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp; 967c478bd9Sstevel@tonic-gate char *errstr; 977c478bd9Sstevel@tonic-gate int ret; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1007c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate if ((opts->pgmname = strrchr(argv[0], '/')) == NULL) 1037c478bd9Sstevel@tonic-gate opts->pgmname = argv[0]; 1047c478bd9Sstevel@tonic-gate else 1057c478bd9Sstevel@tonic-gate opts->pgmname++; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate if ((cpc = cpc_open(CPC_VER_CURRENT)) == NULL) { 1087c478bd9Sstevel@tonic-gate errstr = strerror(errno); 1097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: cannot access performance " 1107c478bd9Sstevel@tonic-gate "counter library - %s\n"), opts->pgmname, errstr); 1117c478bd9Sstevel@tonic-gate return (1); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate (void) cpc_seterrhndlr(cpc, cputrack_errfn); 1157c478bd9Sstevel@tonic-gate strtoset_errfn = cputrack_errfn; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * Establish (non-zero) defaults 1197c478bd9Sstevel@tonic-gate */ 1207c478bd9Sstevel@tonic-gate opts->mseconds = 1000; 1217c478bd9Sstevel@tonic-gate opts->dotitle = 1; 1227c478bd9Sstevel@tonic-gate opts->log = stdout; 1237c478bd9Sstevel@tonic-gate if ((opts->master = cpc_setgrp_new(cpc, 0)) == NULL) { 1247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"), 1257c478bd9Sstevel@tonic-gate opts->pgmname); 1267c478bd9Sstevel@tonic-gate exit(1); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "T:N:Defhntvo:r:c:p:")) != EOF) 1307c478bd9Sstevel@tonic-gate switch (c) { 1317c478bd9Sstevel@tonic-gate case 'T': /* sample time, seconds */ 1327c478bd9Sstevel@tonic-gate opts->mseconds = (uint_t)(atof(optarg) * 1000.0); 1337c478bd9Sstevel@tonic-gate break; 1347c478bd9Sstevel@tonic-gate case 'N': /* number of samples */ 1357c478bd9Sstevel@tonic-gate nsamples = atoi(optarg); 1367c478bd9Sstevel@tonic-gate if (nsamples < 0) 1377c478bd9Sstevel@tonic-gate errcnt++; 1387c478bd9Sstevel@tonic-gate else 1397c478bd9Sstevel@tonic-gate opts->nsamples = (uint_t)nsamples; 1407c478bd9Sstevel@tonic-gate break; 1417c478bd9Sstevel@tonic-gate case 'D': /* enable debugging */ 1427c478bd9Sstevel@tonic-gate opts->debug++; 1437c478bd9Sstevel@tonic-gate break; 1447c478bd9Sstevel@tonic-gate case 'f': /* follow fork */ 1457c478bd9Sstevel@tonic-gate opts->followfork++; 1467c478bd9Sstevel@tonic-gate break; 1477c478bd9Sstevel@tonic-gate case 'e': /* follow exec */ 1487c478bd9Sstevel@tonic-gate opts->followexec++; 1497c478bd9Sstevel@tonic-gate break; 1507c478bd9Sstevel@tonic-gate case 'n': /* no titles */ 1517c478bd9Sstevel@tonic-gate opts->dotitle = 0; 1527c478bd9Sstevel@tonic-gate break; 1537c478bd9Sstevel@tonic-gate case 't': /* print %tick */ 1547c478bd9Sstevel@tonic-gate opts->dotick = 1; 1557c478bd9Sstevel@tonic-gate break; 1567c478bd9Sstevel@tonic-gate case 'v': 1577c478bd9Sstevel@tonic-gate opts->verbose = 1; /* more chatty */ 1587c478bd9Sstevel@tonic-gate break; 1597c478bd9Sstevel@tonic-gate case 'o': 1607c478bd9Sstevel@tonic-gate if (optarg == NULL) { 1617c478bd9Sstevel@tonic-gate errcnt++; 1627c478bd9Sstevel@tonic-gate break; 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate if ((opts->log = fopen(optarg, "w")) == NULL) { 1657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 1667c478bd9Sstevel@tonic-gate "%s: cannot open '%s' for writing\n"), 1677c478bd9Sstevel@tonic-gate opts->pgmname, optarg); 1687c478bd9Sstevel@tonic-gate return (1); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate break; 1717c478bd9Sstevel@tonic-gate case 'c': /* specify statistics */ 1727c478bd9Sstevel@tonic-gate if ((sgrp = cpc_setgrp_newset(opts->master, 1737c478bd9Sstevel@tonic-gate optarg, &errcnt)) != NULL) 1747c478bd9Sstevel@tonic-gate opts->master = sgrp; 1757c478bd9Sstevel@tonic-gate break; 1767c478bd9Sstevel@tonic-gate case 'p': /* grab given pid */ 1777c478bd9Sstevel@tonic-gate if ((opts->pid = atoi(optarg)) <= 0) 1787c478bd9Sstevel@tonic-gate errcnt++; 1797c478bd9Sstevel@tonic-gate break; 1807c478bd9Sstevel@tonic-gate case 'h': 1817c478bd9Sstevel@tonic-gate opts->dohelp = 1; 1827c478bd9Sstevel@tonic-gate break; 1837c478bd9Sstevel@tonic-gate case '?': 1847c478bd9Sstevel@tonic-gate default: 1857c478bd9Sstevel@tonic-gate errcnt++; 1867c478bd9Sstevel@tonic-gate break; 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate if (opts->nsamples == 0) 1907c478bd9Sstevel@tonic-gate opts->nsamples = UINT_MAX; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate if (errcnt != 0 || 1937c478bd9Sstevel@tonic-gate opts->dohelp || 1947c478bd9Sstevel@tonic-gate (argc == optind && opts->pid == 0) || 1957c478bd9Sstevel@tonic-gate (argc > optind && opts->pid != 0) || 1967c478bd9Sstevel@tonic-gate (opts->nsets = cpc_setgrp_numsets(opts->master)) == 0) { 1977c478bd9Sstevel@tonic-gate (void) fprintf(opts->dohelp ? stdout : stderr, gettext( 1987c478bd9Sstevel@tonic-gate "Usage:\n\t%s [-T secs] [-N count] [-Defhnv] [-o file]\n" 1997c478bd9Sstevel@tonic-gate "\t\t-c events [command [args] | -p pid]\n\n" 2007c478bd9Sstevel@tonic-gate "\t-T secs\t seconds between samples, default 1\n" 2017c478bd9Sstevel@tonic-gate "\t-N count number of samples, default unlimited\n" 2027c478bd9Sstevel@tonic-gate "\t-D\t enable debug mode\n" 2037c478bd9Sstevel@tonic-gate "\t-e\t follow exec(2), and execve(2)\n" 2047c478bd9Sstevel@tonic-gate "\t-f\t follow fork(2), fork1(2), and vfork(2)\n" 2057c478bd9Sstevel@tonic-gate "\t-h\t print extended usage information\n" 2067c478bd9Sstevel@tonic-gate "\t-n\t suppress titles\n" 2077c478bd9Sstevel@tonic-gate "\t-t\t include virtualized %s register\n" 2087c478bd9Sstevel@tonic-gate "\t-v\t verbose mode\n" 2097c478bd9Sstevel@tonic-gate "\t-o file\t write cpu statistics to this file\n" 2107c478bd9Sstevel@tonic-gate "\t-c events specify processor events to be monitored\n" 2117c478bd9Sstevel@tonic-gate "\t-p pid\t pid of existing process to capture\n\n" 2127c478bd9Sstevel@tonic-gate "\tUse cpustat(1M) to monitor system-wide statistics.\n"), 2137c478bd9Sstevel@tonic-gate opts->pgmname, CPC_TICKREG_NAME); 2147c478bd9Sstevel@tonic-gate if (opts->dohelp) { 2157c478bd9Sstevel@tonic-gate (void) putchar('\n'); 2167c478bd9Sstevel@tonic-gate (void) capabilities(cpc, stdout); 2177c478bd9Sstevel@tonic-gate exit(0); 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate exit(2); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate cpc_setgrp_reset(opts->master); 2237c478bd9Sstevel@tonic-gate (void) setvbuf(opts->log, NULL, _IOLBF, 0); 2247c478bd9Sstevel@tonic-gate ret = cputrack(argc, argv, optind); 2257c478bd9Sstevel@tonic-gate (void) cpc_close(cpc); 2267c478bd9Sstevel@tonic-gate return (ret); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate static void 2307c478bd9Sstevel@tonic-gate print_title(cpc_setgrp_t *sgrp) 2317c478bd9Sstevel@tonic-gate { 2327c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7s ", "time"); 2337c478bd9Sstevel@tonic-gate if (opts->followfork) 2347c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6s ", "pid"); 2357c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3s %10s ", "lwp", "event"); 2367c478bd9Sstevel@tonic-gate if (opts->dotick) 2377c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", CPC_TICKREG_NAME); 2387c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%s\n", cpc_setgrp_gethdr(sgrp)); 2397c478bd9Sstevel@tonic-gate (void) fflush(opts->log); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate static void 2437c478bd9Sstevel@tonic-gate print_exec(float now, pid_t pid, char *name) 2447c478bd9Sstevel@tonic-gate { 2457c478bd9Sstevel@tonic-gate if (name == NULL) 2467c478bd9Sstevel@tonic-gate name = "(unknown)"; 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ", now); 2497c478bd9Sstevel@tonic-gate if (opts->followfork) 2507c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)pid); 2517c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", 1, "exec"); 2527c478bd9Sstevel@tonic-gate if (opts->dotick) 2537c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", ""); 2547c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s %9s # '%s'\n", "", "", name); 2557c478bd9Sstevel@tonic-gate (void) fflush(opts->log); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate static void 2597c478bd9Sstevel@tonic-gate print_fork(float now, pid_t newpid, id_t lwpid, pid_t oldpid) 2607c478bd9Sstevel@tonic-gate { 2617c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ", now); 2627c478bd9Sstevel@tonic-gate if (opts->followfork) 2637c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)oldpid); 2647c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", (int)lwpid, "fork"); 2657c478bd9Sstevel@tonic-gate if (opts->dotick) 2667c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", ""); 2677c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s %9s # %d\n", "", "", (int)newpid); 2687c478bd9Sstevel@tonic-gate (void) fflush(opts->log); 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate static void 2727c478bd9Sstevel@tonic-gate print_sample(pid_t pid, id_t lwpid, 2737c478bd9Sstevel@tonic-gate char *pevent, cpc_buf_t *buf, int nreq, const char *evname) 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate uint64_t val; 2767c478bd9Sstevel@tonic-gate int i; 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ", 2797c478bd9Sstevel@tonic-gate mstimestamp(cpc_buf_hrtime(cpc, buf))); 2807c478bd9Sstevel@tonic-gate if (opts->followfork) 2817c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)pid); 2827c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", (int)lwpid, pevent); 2837c478bd9Sstevel@tonic-gate if (opts->dotick) 2847c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9" PRId64 " ", 2857c478bd9Sstevel@tonic-gate cpc_buf_tick(cpc, buf)); 2867c478bd9Sstevel@tonic-gate for (i = 0; i < nreq; i++) { 2877c478bd9Sstevel@tonic-gate (void) cpc_buf_get(cpc, buf, i, &val); 2887c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9" PRId64 " ", val); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate if (opts->nsets > 1) 2917c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, " # %s\n", evname); 2927c478bd9Sstevel@tonic-gate else 2937c478bd9Sstevel@tonic-gate (void) fputc('\n', opts->log); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate struct pstate { 2977c478bd9Sstevel@tonic-gate cpc_setgrp_t *accum; 2987c478bd9Sstevel@tonic-gate cpc_setgrp_t **sgrps; 2997c478bd9Sstevel@tonic-gate int maxlwpid; 3007c478bd9Sstevel@tonic-gate }; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate static int 3037c478bd9Sstevel@tonic-gate pinit_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg) 3047c478bd9Sstevel@tonic-gate { 3057c478bd9Sstevel@tonic-gate struct pstate *state = arg; 3067c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp; 3077c478bd9Sstevel@tonic-gate cpc_set_t *set; 3087c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch; 3097c478bd9Sstevel@tonic-gate char *errstr; 3107c478bd9Sstevel@tonic-gate int nreq; 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate if (state->maxlwpid < lwpid) { 3137c478bd9Sstevel@tonic-gate state->sgrps = realloc(state->sgrps, 3147c478bd9Sstevel@tonic-gate lwpid * sizeof (state->sgrps)); 3157c478bd9Sstevel@tonic-gate if (state->sgrps == NULL) { 3167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 3177c478bd9Sstevel@tonic-gate "%6d: init_lwp: out of memory\n"), (int)pid); 3187c478bd9Sstevel@tonic-gate return (-1); 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate while (state->maxlwpid < lwpid) { 3217c478bd9Sstevel@tonic-gate state->sgrps[state->maxlwpid] = NULL; 3227c478bd9Sstevel@tonic-gate state->maxlwpid++; 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate } 3257c478bd9Sstevel@tonic-gate 3267c478bd9Sstevel@tonic-gate if ((sgrp = state->sgrps[lwpid-1]) == NULL) { 3277c478bd9Sstevel@tonic-gate if ((sgrp = cpc_setgrp_clone(opts->master)) == NULL) { 3287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 3297c478bd9Sstevel@tonic-gate "%6d: init_lwp: out of memory\n"), (int)pid); 3307c478bd9Sstevel@tonic-gate return (-1); 3317c478bd9Sstevel@tonic-gate } 3327c478bd9Sstevel@tonic-gate state->sgrps[lwpid-1] = sgrp; 3337c478bd9Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp); 3347c478bd9Sstevel@tonic-gate } else { 3357c478bd9Sstevel@tonic-gate cpc_setgrp_reset(sgrp); 3367c478bd9Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp); 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch); 3407c478bd9Sstevel@tonic-gate 3417c478bd9Sstevel@tonic-gate if (cpc_bind_pctx(cpc, pctx, lwpid, set, 0) != 0 || 3427c478bd9Sstevel@tonic-gate cpc_set_sample(cpc, set, *data2) != 0) { 3437c478bd9Sstevel@tonic-gate errstr = strerror(errno); 3447c478bd9Sstevel@tonic-gate if (errno == EAGAIN) 3457c478bd9Sstevel@tonic-gate (void) cpc_unbind(cpc, set); 3467c478bd9Sstevel@tonic-gate if (errno == EACCES) 3477c478bd9Sstevel@tonic-gate p4_ht_error(); 3487c478bd9Sstevel@tonic-gate else 3497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 3507c478bd9Sstevel@tonic-gate "%6d: init_lwp: can't bind perf counters " 3517c478bd9Sstevel@tonic-gate "to lwp%d - %s\n"), (int)pid, (int)lwpid, 3527c478bd9Sstevel@tonic-gate errstr); 3537c478bd9Sstevel@tonic-gate return (-1); 3547c478bd9Sstevel@tonic-gate } 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate if (opts->verbose) 3577c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "init_lwp", 3587c478bd9Sstevel@tonic-gate *data2, nreq, cpc_setgrp_getname(sgrp)); 3597c478bd9Sstevel@tonic-gate return (0); 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3637c478bd9Sstevel@tonic-gate static int 3647c478bd9Sstevel@tonic-gate pfini_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg) 3657c478bd9Sstevel@tonic-gate { 3667c478bd9Sstevel@tonic-gate struct pstate *state = arg; 3677c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1]; 3687c478bd9Sstevel@tonic-gate cpc_set_t *set; 3697c478bd9Sstevel@tonic-gate char *errstr; 3707c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch; 3717c478bd9Sstevel@tonic-gate int nreq; 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp); 3747c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch); 3757c478bd9Sstevel@tonic-gate if (cpc_set_sample(cpc, set, *data1) == 0) { 3767c478bd9Sstevel@tonic-gate if (opts->verbose) 3777c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "fini_lwp", 3787c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp)); 3797c478bd9Sstevel@tonic-gate cpc_setgrp_accum(state->accum, sgrp); 3807c478bd9Sstevel@tonic-gate if (cpc_unbind(cpc, set) == 0) 3817c478bd9Sstevel@tonic-gate return (0); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate switch (errno) { 3857c478bd9Sstevel@tonic-gate case EAGAIN: 3867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%6d: fini_lwp: " 3877c478bd9Sstevel@tonic-gate "lwp%d: perf counter contents invalidated\n"), 3887c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid); 3897c478bd9Sstevel@tonic-gate break; 3907c478bd9Sstevel@tonic-gate default: 3917c478bd9Sstevel@tonic-gate errstr = strerror(errno); 3927c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%6d: fini_lwp: " 3937c478bd9Sstevel@tonic-gate "lwp%d: can't access perf counters - %s\n"), 3947c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid, errstr); 3957c478bd9Sstevel@tonic-gate break; 3967c478bd9Sstevel@tonic-gate } 397*7c3666b4Skk112340 return (-1); 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4017c478bd9Sstevel@tonic-gate static int 4027c478bd9Sstevel@tonic-gate plwp_create(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg) 4037c478bd9Sstevel@tonic-gate { 4047c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = opts->master; 4057c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch; 4067c478bd9Sstevel@tonic-gate int nreq; 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch); 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "lwp_create", 4117c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp)); 4127c478bd9Sstevel@tonic-gate 4137c478bd9Sstevel@tonic-gate return (0); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4177c478bd9Sstevel@tonic-gate static int 4187c478bd9Sstevel@tonic-gate plwp_exit(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg) 4197c478bd9Sstevel@tonic-gate { 4207c478bd9Sstevel@tonic-gate struct pstate *state = arg; 4217c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1]; 4227c478bd9Sstevel@tonic-gate cpc_set_t *start; 4237c478bd9Sstevel@tonic-gate int nreq; 4247c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch; 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate start = cpc_setgrp_getset(sgrp); 4277c478bd9Sstevel@tonic-gate do { 4287c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch); 4297c478bd9Sstevel@tonic-gate if (cpc_buf_hrtime(cpc, *data1) == 0) 4307c478bd9Sstevel@tonic-gate continue; 4317c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "lwp_exit", 4327c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp)); 4337c478bd9Sstevel@tonic-gate } while (cpc_setgrp_nextset(sgrp) != start); 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate return (0); 4367c478bd9Sstevel@tonic-gate } 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4397c478bd9Sstevel@tonic-gate static int 4407c478bd9Sstevel@tonic-gate pexec(pctx_t *pctx, pid_t pid, id_t lwpid, char *name, void *arg) 4417c478bd9Sstevel@tonic-gate { 4427c478bd9Sstevel@tonic-gate struct pstate *state = arg; 4437c478bd9Sstevel@tonic-gate float now = 0.0; 4447c478bd9Sstevel@tonic-gate cpc_set_t *start; 4457c478bd9Sstevel@tonic-gate int nreq; 4467c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch; 4477c478bd9Sstevel@tonic-gate hrtime_t hrt; 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate /* 4507c478bd9Sstevel@tonic-gate * Print the accumulated results from the previous program image 4517c478bd9Sstevel@tonic-gate */ 4527c478bd9Sstevel@tonic-gate cpc_setgrp_reset(state->accum); 4537c478bd9Sstevel@tonic-gate start = cpc_setgrp_getset(state->accum); 4547c478bd9Sstevel@tonic-gate do { 4557c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2, 4567c478bd9Sstevel@tonic-gate &scratch); 4577c478bd9Sstevel@tonic-gate hrt = cpc_buf_hrtime(cpc, *data1); 4587c478bd9Sstevel@tonic-gate if (hrt == 0) 4597c478bd9Sstevel@tonic-gate continue; 4607c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "exec", 4617c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(state->accum)); 4627c478bd9Sstevel@tonic-gate if (now < mstimestamp(hrt)) 4637c478bd9Sstevel@tonic-gate now = mstimestamp(hrt); 4647c478bd9Sstevel@tonic-gate } while (cpc_setgrp_nextset(state->accum) != start); 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate print_exec(now, pid, name); 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate if (state->accum != NULL) { 4697c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->accum); 4707c478bd9Sstevel@tonic-gate state->accum = NULL; 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate if (opts->followexec) { 4747c478bd9Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master); 4757c478bd9Sstevel@tonic-gate return (0); 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate return (-1); 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4817c478bd9Sstevel@tonic-gate static void 4827c478bd9Sstevel@tonic-gate pexit(pctx_t *pctx, pid_t pid, id_t lwpid, int status, void *arg) 4837c478bd9Sstevel@tonic-gate { 4847c478bd9Sstevel@tonic-gate struct pstate *state = arg; 4857c478bd9Sstevel@tonic-gate cpc_set_t *start; 4867c478bd9Sstevel@tonic-gate int nreq; 4877c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch; 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate cpc_setgrp_reset(state->accum); 4907c478bd9Sstevel@tonic-gate start = cpc_setgrp_getset(state->accum); 4917c478bd9Sstevel@tonic-gate do { 4927c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2, 4937c478bd9Sstevel@tonic-gate &scratch); 4947c478bd9Sstevel@tonic-gate if (cpc_buf_hrtime(cpc, *data1) == 0) 4957c478bd9Sstevel@tonic-gate continue; 4967c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "exit", 4977c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(state->accum)); 4987c478bd9Sstevel@tonic-gate } while (cpc_setgrp_nextset(state->accum) != start); 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->accum); 5017c478bd9Sstevel@tonic-gate state->accum = NULL; 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate for (lwpid = 1; lwpid < state->maxlwpid; lwpid++) 5047c478bd9Sstevel@tonic-gate if (state->sgrps[lwpid-1] != NULL) { 5057c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->sgrps[lwpid-1]); 5067c478bd9Sstevel@tonic-gate state->sgrps[lwpid-1] = NULL; 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate free(state->sgrps); 5097c478bd9Sstevel@tonic-gate state->sgrps = NULL; 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate static int 5137c478bd9Sstevel@tonic-gate ptick(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg) 5147c478bd9Sstevel@tonic-gate { 5157c478bd9Sstevel@tonic-gate struct pstate *state = arg; 5167c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1]; 5177c478bd9Sstevel@tonic-gate cpc_set_t *this = cpc_setgrp_getset(sgrp); 5187c478bd9Sstevel@tonic-gate const char *name = cpc_setgrp_getname(sgrp); 5197c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch, *tmp; 5207c478bd9Sstevel@tonic-gate char *errstr; 5217c478bd9Sstevel@tonic-gate int nreqs; 5227c478bd9Sstevel@tonic-gate 5237c478bd9Sstevel@tonic-gate nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch); 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate if (opts->nsets == 1) { 5267c478bd9Sstevel@tonic-gate /* 5277c478bd9Sstevel@tonic-gate * If we're dealing with one set, buffer usage is: 5287c478bd9Sstevel@tonic-gate * 5297c478bd9Sstevel@tonic-gate * data1 = most recent data snapshot 5307c478bd9Sstevel@tonic-gate * data2 = previous data snapshot 5317c478bd9Sstevel@tonic-gate * scratch = used for diffing data1 and data2 5327c478bd9Sstevel@tonic-gate * 5337c478bd9Sstevel@tonic-gate * Save the snapshot from the previous sample in data2 5347c478bd9Sstevel@tonic-gate * before putting the current sample in data1. 5357c478bd9Sstevel@tonic-gate */ 5367c478bd9Sstevel@tonic-gate tmp = *data1; 5377c478bd9Sstevel@tonic-gate *data1 = *data2; 5387c478bd9Sstevel@tonic-gate *data2 = tmp; 5397c478bd9Sstevel@tonic-gate if (cpc_set_sample(cpc, this, *data1) != 0) 5407c478bd9Sstevel@tonic-gate goto broken; 5417c478bd9Sstevel@tonic-gate cpc_buf_sub(cpc, *scratch, *data1, *data2); 5427c478bd9Sstevel@tonic-gate } else { 5437c478bd9Sstevel@tonic-gate cpc_set_t *next = cpc_setgrp_nextset(sgrp); 5447c478bd9Sstevel@tonic-gate /* 5457c478bd9Sstevel@tonic-gate * If there is more than set in use, we will need to 5467c478bd9Sstevel@tonic-gate * unbind and re-bind on each go-around because each 5477c478bd9Sstevel@tonic-gate * time a counter is bound, it is preset to 0 (as it was 5487c478bd9Sstevel@tonic-gate * specified when the requests were added to the set). 5497c478bd9Sstevel@tonic-gate * 5507c478bd9Sstevel@tonic-gate * Buffer usage in this case is: 5517c478bd9Sstevel@tonic-gate * 5527c478bd9Sstevel@tonic-gate * data1 = total counts for this set since program began 5537c478bd9Sstevel@tonic-gate * data2 = unused 5547c478bd9Sstevel@tonic-gate * scratch = most recent data snapshot 5557c478bd9Sstevel@tonic-gate */ 5567c478bd9Sstevel@tonic-gate 5577c478bd9Sstevel@tonic-gate if (cpc_set_sample(cpc, this, *scratch) != 0) 5587c478bd9Sstevel@tonic-gate goto broken; 5597c478bd9Sstevel@tonic-gate cpc_buf_add(cpc, *data1, *data1, *scratch); 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate /* 5627c478bd9Sstevel@tonic-gate * No need to unbind the previous set, as binding another set 5637c478bd9Sstevel@tonic-gate * automatically unbinds the most recently bound set. 5647c478bd9Sstevel@tonic-gate */ 5657c478bd9Sstevel@tonic-gate if (cpc_bind_pctx(cpc, pctx, lwpid, next, 0) != 0) 5667c478bd9Sstevel@tonic-gate goto broken; 5677c478bd9Sstevel@tonic-gate } 5687c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "tick", *scratch, nreqs, name); 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate return (0); 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate broken: 5737c478bd9Sstevel@tonic-gate switch (errno) { 5747c478bd9Sstevel@tonic-gate case EAGAIN: 5757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5767c478bd9Sstevel@tonic-gate "%6d: tick: lwp%d: perf counter contents invalidated\n"), 5777c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid); 5787c478bd9Sstevel@tonic-gate break; 5797c478bd9Sstevel@tonic-gate default: 5807c478bd9Sstevel@tonic-gate errstr = strerror(errno); 5817c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 5827c478bd9Sstevel@tonic-gate "%6d: tick: lwp%d: can't access perf counter - %s\n"), 5837c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid, errstr); 5847c478bd9Sstevel@tonic-gate break; 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate (void) cpc_unbind(cpc, this); 5877c478bd9Sstevel@tonic-gate return (-1); 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate /* 5917c478bd9Sstevel@tonic-gate * The system has just created a new address space that has a new pid. 5927c478bd9Sstevel@tonic-gate * We're running in a child of the controlling process, with a new 5937c478bd9Sstevel@tonic-gate * pctx handle already opened on the child of the original controlled process. 5947c478bd9Sstevel@tonic-gate */ 5957c478bd9Sstevel@tonic-gate static void 5967c478bd9Sstevel@tonic-gate pfork(pctx_t *pctx, pid_t oldpid, pid_t pid, id_t lwpid, void *arg) 5977c478bd9Sstevel@tonic-gate { 5987c478bd9Sstevel@tonic-gate struct pstate *state = arg; 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate print_fork(mstimestamp(0), pid, lwpid, oldpid); 6017c478bd9Sstevel@tonic-gate 6027c478bd9Sstevel@tonic-gate if (!opts->followfork) 6037c478bd9Sstevel@tonic-gate return; 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate if (pctx_set_events(pctx, 6067c478bd9Sstevel@tonic-gate PCTX_SYSC_EXEC_EVENT, pexec, 6077c478bd9Sstevel@tonic-gate PCTX_SYSC_FORK_EVENT, pfork, 6087c478bd9Sstevel@tonic-gate PCTX_SYSC_EXIT_EVENT, pexit, 6097c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_CREATE_EVENT, plwp_create, 6107c478bd9Sstevel@tonic-gate PCTX_INIT_LWP_EVENT, pinit_lwp, 6117c478bd9Sstevel@tonic-gate PCTX_FINI_LWP_EVENT, pfini_lwp, 6127c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit, 6137c478bd9Sstevel@tonic-gate PCTX_NULL_EVENT) == 0) { 6147c478bd9Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master); 6157c478bd9Sstevel@tonic-gate (void) pctx_run(pctx, opts->mseconds, opts->nsamples, ptick); 6167c478bd9Sstevel@tonic-gate if (state->accum) { 6177c478bd9Sstevel@tonic-gate free(state->accum); 6187c478bd9Sstevel@tonic-gate state->accum = NULL; 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate } 6217c478bd9Sstevel@tonic-gate } 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate /* 6247c478bd9Sstevel@tonic-gate * Translate the incoming options into actions, and get the 6257c478bd9Sstevel@tonic-gate * tool and the process to control running. 6267c478bd9Sstevel@tonic-gate */ 6277c478bd9Sstevel@tonic-gate static int 6287c478bd9Sstevel@tonic-gate cputrack(int argc, char *argv[], int optind) 6297c478bd9Sstevel@tonic-gate { 6307c478bd9Sstevel@tonic-gate struct pstate __state, *state = &__state; 6317c478bd9Sstevel@tonic-gate pctx_t *pctx; 6327c478bd9Sstevel@tonic-gate int err; 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate bzero(state, sizeof (*state)); 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate if (opts->pid == 0) { 6377c478bd9Sstevel@tonic-gate if (argc <= optind) { 6387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", 6397c478bd9Sstevel@tonic-gate opts->pgmname, 6407c478bd9Sstevel@tonic-gate gettext("no program to start")); 6417c478bd9Sstevel@tonic-gate return (1); 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate pctx = pctx_create(argv[optind], 6447c478bd9Sstevel@tonic-gate &argv[optind], state, 1, cputrack_pctx_errfn); 6457c478bd9Sstevel@tonic-gate if (pctx == NULL) { 6467c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s '%s'\n", 6477c478bd9Sstevel@tonic-gate opts->pgmname, 6487c478bd9Sstevel@tonic-gate gettext("failed to start program"), 6497c478bd9Sstevel@tonic-gate argv[optind]); 6507c478bd9Sstevel@tonic-gate return (1); 6517c478bd9Sstevel@tonic-gate } 6527c478bd9Sstevel@tonic-gate } else { 6537c478bd9Sstevel@tonic-gate pctx = pctx_capture(opts->pid, state, 1, cputrack_pctx_errfn); 6547c478bd9Sstevel@tonic-gate if (pctx == NULL) { 6557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s %d\n", 6567c478bd9Sstevel@tonic-gate opts->pgmname, 6577c478bd9Sstevel@tonic-gate gettext("failed to capture pid"), 6587c478bd9Sstevel@tonic-gate (int)opts->pid); 6597c478bd9Sstevel@tonic-gate return (1); 6607c478bd9Sstevel@tonic-gate } 6617c478bd9Sstevel@tonic-gate } 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate err = pctx_set_events(pctx, 6647c478bd9Sstevel@tonic-gate PCTX_SYSC_EXEC_EVENT, pexec, 6657c478bd9Sstevel@tonic-gate PCTX_SYSC_FORK_EVENT, pfork, 6667c478bd9Sstevel@tonic-gate PCTX_SYSC_EXIT_EVENT, pexit, 6677c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_CREATE_EVENT, plwp_create, 6687c478bd9Sstevel@tonic-gate PCTX_INIT_LWP_EVENT, pinit_lwp, 6697c478bd9Sstevel@tonic-gate PCTX_FINI_LWP_EVENT, pfini_lwp, 6707c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit, 6717c478bd9Sstevel@tonic-gate PCTX_NULL_EVENT); 6727c478bd9Sstevel@tonic-gate 6737c478bd9Sstevel@tonic-gate if (err != 0) { 6747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n", 6757c478bd9Sstevel@tonic-gate opts->pgmname, 6767c478bd9Sstevel@tonic-gate gettext("can't bind process context ops to process")); 6777c478bd9Sstevel@tonic-gate } else { 6787c478bd9Sstevel@tonic-gate if (opts->dotitle) 6797c478bd9Sstevel@tonic-gate print_title(opts->master); 6807c478bd9Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master); 6817c478bd9Sstevel@tonic-gate zerotime(); 6827c478bd9Sstevel@tonic-gate err = pctx_run(pctx, opts->mseconds, opts->nsamples, ptick); 6837c478bd9Sstevel@tonic-gate if (state->accum) { 6847c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->accum); 6857c478bd9Sstevel@tonic-gate state->accum = NULL; 6867c478bd9Sstevel@tonic-gate } 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate pctx_release(pctx); 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate return (err != 0 ? 1 : 0); 6917c478bd9Sstevel@tonic-gate } 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate #define OFFLINE_CMD "/usr/sbin/psradm -f " 6947c478bd9Sstevel@tonic-gate #define BUFSIZE 5 /* enough for "n " where n is a cpuid */ 6957c478bd9Sstevel@tonic-gate 6967c478bd9Sstevel@tonic-gate /* 6977c478bd9Sstevel@tonic-gate * cpc_bind_pctx() failed with EACCES, which means the user must first offline 6987c478bd9Sstevel@tonic-gate * all but one logical processor on each physical processor. Print to stderr the 6997c478bd9Sstevel@tonic-gate * psradm command string to do this. 7007c478bd9Sstevel@tonic-gate */ 7017c478bd9Sstevel@tonic-gate static void 7027c478bd9Sstevel@tonic-gate p4_ht_error(void) 7037c478bd9Sstevel@tonic-gate { 7047c478bd9Sstevel@tonic-gate kstat_ctl_t *kc; 7057c478bd9Sstevel@tonic-gate kstat_t *ksp; 7067c478bd9Sstevel@tonic-gate kstat_named_t *k; 7077c478bd9Sstevel@tonic-gate int i; 7087c478bd9Sstevel@tonic-gate int max; 7097c478bd9Sstevel@tonic-gate int stat; 7107c478bd9Sstevel@tonic-gate int *designees; 7117c478bd9Sstevel@tonic-gate int *must_offline; 7127c478bd9Sstevel@tonic-gate char buf[BUFSIZE]; 7137c478bd9Sstevel@tonic-gate char *cmd; 7147c478bd9Sstevel@tonic-gate int noffline = 0; 7157c478bd9Sstevel@tonic-gate int ndone = 0; 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", 7187c478bd9Sstevel@tonic-gate gettext("Pentium 4 processors with HyperThreading present.\nOffline" 7197c478bd9Sstevel@tonic-gate " all but one logical processor on each physical processor in" 7207c478bd9Sstevel@tonic-gate " order to use\ncputrack.\n")); 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate if ((kc = kstat_open()) == NULL) 7247c478bd9Sstevel@tonic-gate return; 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate max = sysconf(_SC_CPUID_MAX); 7277c478bd9Sstevel@tonic-gate if ((designees = malloc(max * sizeof (*designees))) == NULL) { 7287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"), 7297c478bd9Sstevel@tonic-gate opts->pgmname); 7307c478bd9Sstevel@tonic-gate exit(0); 7317c478bd9Sstevel@tonic-gate } 7327c478bd9Sstevel@tonic-gate 7337c478bd9Sstevel@tonic-gate if ((must_offline = malloc(max * sizeof (*designees))) == NULL) { 7347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"), 7357c478bd9Sstevel@tonic-gate opts->pgmname); 7367c478bd9Sstevel@tonic-gate exit(0); 7377c478bd9Sstevel@tonic-gate } 7387c478bd9Sstevel@tonic-gate 7397c478bd9Sstevel@tonic-gate for (i = 0; i < max; i++) { 7407c478bd9Sstevel@tonic-gate designees[i] = -1; 7417c478bd9Sstevel@tonic-gate must_offline[i] = 0; 7427c478bd9Sstevel@tonic-gate } 7437c478bd9Sstevel@tonic-gate 7447c478bd9Sstevel@tonic-gate for (i = 0; i < max; i++) { 7457c478bd9Sstevel@tonic-gate stat = p_online(i, P_STATUS); 7467c478bd9Sstevel@tonic-gate if (stat != P_ONLINE && stat != P_NOINTR) 7477c478bd9Sstevel@tonic-gate continue; 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "cpu_info", i, NULL)) == NULL) { 7507c478bd9Sstevel@tonic-gate free(designees); 7517c478bd9Sstevel@tonic-gate free(must_offline); 7527c478bd9Sstevel@tonic-gate return; 7537c478bd9Sstevel@tonic-gate } 7547c478bd9Sstevel@tonic-gate 7557c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, NULL) == -1) { 7567c478bd9Sstevel@tonic-gate free(designees); 7577c478bd9Sstevel@tonic-gate free(must_offline); 7587c478bd9Sstevel@tonic-gate return; 7597c478bd9Sstevel@tonic-gate } 7607c478bd9Sstevel@tonic-gate 7617c478bd9Sstevel@tonic-gate if ((k = (kstat_named_t *)kstat_data_lookup(ksp, "chip_id")) 7627c478bd9Sstevel@tonic-gate == NULL) { 7637c478bd9Sstevel@tonic-gate free(designees); 7647c478bd9Sstevel@tonic-gate free(must_offline); 7657c478bd9Sstevel@tonic-gate return; 7667c478bd9Sstevel@tonic-gate } 7677c478bd9Sstevel@tonic-gate 7687c478bd9Sstevel@tonic-gate if (designees[k->value.i32] == -1) 7697c478bd9Sstevel@tonic-gate /* 7707c478bd9Sstevel@tonic-gate * This chip doesn't yet have a CPU designated to remain 7717c478bd9Sstevel@tonic-gate * online; let this one be it. 7727c478bd9Sstevel@tonic-gate */ 7737c478bd9Sstevel@tonic-gate designees[k->value.i32] = i; 7747c478bd9Sstevel@tonic-gate else { 7757c478bd9Sstevel@tonic-gate /* 7767c478bd9Sstevel@tonic-gate * This chip already has a designated CPU; this CPU must 7777c478bd9Sstevel@tonic-gate * go offline. 7787c478bd9Sstevel@tonic-gate */ 7797c478bd9Sstevel@tonic-gate must_offline[i] = 1; 7807c478bd9Sstevel@tonic-gate noffline++; 7817c478bd9Sstevel@tonic-gate } 7827c478bd9Sstevel@tonic-gate } 7837c478bd9Sstevel@tonic-gate 7847c478bd9Sstevel@tonic-gate /* 7857c478bd9Sstevel@tonic-gate * Now construct a string containing the command line used to offline 7867c478bd9Sstevel@tonic-gate * the appropriate processors. 7877c478bd9Sstevel@tonic-gate */ 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate if ((cmd = malloc(strlen(OFFLINE_CMD) + (noffline * BUFSIZE) + 1)) 7907c478bd9Sstevel@tonic-gate == NULL) { 7917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"), 7927c478bd9Sstevel@tonic-gate opts->pgmname); 7937c478bd9Sstevel@tonic-gate exit(0); 7947c478bd9Sstevel@tonic-gate } 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate (void) strcpy(cmd, OFFLINE_CMD); 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate for (i = 0; i < max; i++) { 7997c478bd9Sstevel@tonic-gate if (must_offline[i] == 0) 8007c478bd9Sstevel@tonic-gate continue; 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate ndone++; 8037c478bd9Sstevel@tonic-gate (void) snprintf(buf, BUFSIZE, "%d", i); 8047c478bd9Sstevel@tonic-gate if (ndone < noffline) 8057c478bd9Sstevel@tonic-gate (void) strcat(buf, " "); 8067c478bd9Sstevel@tonic-gate (void) strcat(cmd, buf); 8077c478bd9Sstevel@tonic-gate } 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s:\n%s\n", gettext("The following command " 8107c478bd9Sstevel@tonic-gate "will configure the system appropriately"), cmd); 8117c478bd9Sstevel@tonic-gate 8127c478bd9Sstevel@tonic-gate exit(1); 8137c478bd9Sstevel@tonic-gate } 814