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