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*b885580bSAlexander Kolbasov * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <sys/time.h>
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <inttypes.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <strings.h>
347c478bd9Sstevel@tonic-gate #include <limits.h>
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #include <locale.h>
377c478bd9Sstevel@tonic-gate #include <errno.h>
387c478bd9Sstevel@tonic-gate #include <kstat.h>
397c478bd9Sstevel@tonic-gate #include <libcpc.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include "cpucmds.h"
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate static struct options {
447c478bd9Sstevel@tonic-gate int debug;
457c478bd9Sstevel@tonic-gate int verbose;
467c478bd9Sstevel@tonic-gate int dotitle;
477c478bd9Sstevel@tonic-gate int dohelp;
487c478bd9Sstevel@tonic-gate int dotick;
497c478bd9Sstevel@tonic-gate int cpuver;
507c478bd9Sstevel@tonic-gate char *pgmname;
517c478bd9Sstevel@tonic-gate uint_t mseconds;
527c478bd9Sstevel@tonic-gate uint_t nsamples;
537c478bd9Sstevel@tonic-gate uint_t nsets;
547c478bd9Sstevel@tonic-gate cpc_setgrp_t *master;
557c478bd9Sstevel@tonic-gate int followfork;
567c478bd9Sstevel@tonic-gate int followexec;
577c478bd9Sstevel@tonic-gate pid_t pid;
587c478bd9Sstevel@tonic-gate FILE *log;
597c478bd9Sstevel@tonic-gate } __options;
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate static const struct options *opts = (const struct options *)&__options;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate static cpc_t *cpc;
647c478bd9Sstevel@tonic-gate
65*b885580bSAlexander Kolbasov /*
66*b885580bSAlexander Kolbasov * How many signals caught from terminal
67*b885580bSAlexander Kolbasov * We bail out as soon as possible when interrupt is set
68*b885580bSAlexander Kolbasov */
69*b885580bSAlexander Kolbasov static int interrupt = 0;
70*b885580bSAlexander Kolbasov
717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
727c478bd9Sstevel@tonic-gate static void
cputrack_errfn(const char * fn,int subcode,const char * fmt,va_list ap)737c478bd9Sstevel@tonic-gate cputrack_errfn(const char *fn, int subcode, const char *fmt, va_list ap)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", opts->pgmname);
767c478bd9Sstevel@tonic-gate if (opts->debug)
777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", fn);
787c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate static void
cputrack_pctx_errfn(const char * fn,const char * fmt,va_list ap)827c478bd9Sstevel@tonic-gate cputrack_pctx_errfn(const char *fn, const char *fmt, va_list ap)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate cputrack_errfn(fn, -1, fmt, ap);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate static int cputrack(int argc, char *argv[], int optind);
88*b885580bSAlexander Kolbasov static void intr(int);
89*b885580bSAlexander Kolbasov
908d4e547dSae112802 #if defined(__i386)
917c478bd9Sstevel@tonic-gate static void p4_ht_error(void);
928d4e547dSae112802 #endif
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
957c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
967c478bd9Sstevel@tonic-gate #endif
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])997c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate struct options *opts = &__options;
1027c478bd9Sstevel@tonic-gate int c, errcnt = 0;
1037c478bd9Sstevel@tonic-gate int nsamples;
1047c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp;
1057c478bd9Sstevel@tonic-gate char *errstr;
1067c478bd9Sstevel@tonic-gate int ret;
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1097c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate if ((opts->pgmname = strrchr(argv[0], '/')) == NULL)
1127c478bd9Sstevel@tonic-gate opts->pgmname = argv[0];
1137c478bd9Sstevel@tonic-gate else
1147c478bd9Sstevel@tonic-gate opts->pgmname++;
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate if ((cpc = cpc_open(CPC_VER_CURRENT)) == NULL) {
1177c478bd9Sstevel@tonic-gate errstr = strerror(errno);
1187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: cannot access performance "
1197c478bd9Sstevel@tonic-gate "counter library - %s\n"), opts->pgmname, errstr);
1207c478bd9Sstevel@tonic-gate return (1);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate (void) cpc_seterrhndlr(cpc, cputrack_errfn);
1247c478bd9Sstevel@tonic-gate strtoset_errfn = cputrack_errfn;
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate * Establish (non-zero) defaults
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate opts->mseconds = 1000;
1307c478bd9Sstevel@tonic-gate opts->dotitle = 1;
1317c478bd9Sstevel@tonic-gate opts->log = stdout;
1327c478bd9Sstevel@tonic-gate if ((opts->master = cpc_setgrp_new(cpc, 0)) == NULL) {
1337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"),
1347c478bd9Sstevel@tonic-gate opts->pgmname);
1357c478bd9Sstevel@tonic-gate exit(1);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "T:N:Defhntvo:r:c:p:")) != EOF)
1397c478bd9Sstevel@tonic-gate switch (c) {
1407c478bd9Sstevel@tonic-gate case 'T': /* sample time, seconds */
1417c478bd9Sstevel@tonic-gate opts->mseconds = (uint_t)(atof(optarg) * 1000.0);
1427c478bd9Sstevel@tonic-gate break;
1437c478bd9Sstevel@tonic-gate case 'N': /* number of samples */
1447c478bd9Sstevel@tonic-gate nsamples = atoi(optarg);
1457c478bd9Sstevel@tonic-gate if (nsamples < 0)
1467c478bd9Sstevel@tonic-gate errcnt++;
1477c478bd9Sstevel@tonic-gate else
1487c478bd9Sstevel@tonic-gate opts->nsamples = (uint_t)nsamples;
1497c478bd9Sstevel@tonic-gate break;
1507c478bd9Sstevel@tonic-gate case 'D': /* enable debugging */
1517c478bd9Sstevel@tonic-gate opts->debug++;
1527c478bd9Sstevel@tonic-gate break;
1537c478bd9Sstevel@tonic-gate case 'f': /* follow fork */
1547c478bd9Sstevel@tonic-gate opts->followfork++;
1557c478bd9Sstevel@tonic-gate break;
1567c478bd9Sstevel@tonic-gate case 'e': /* follow exec */
1577c478bd9Sstevel@tonic-gate opts->followexec++;
1587c478bd9Sstevel@tonic-gate break;
1597c478bd9Sstevel@tonic-gate case 'n': /* no titles */
1607c478bd9Sstevel@tonic-gate opts->dotitle = 0;
1617c478bd9Sstevel@tonic-gate break;
1627c478bd9Sstevel@tonic-gate case 't': /* print %tick */
1637c478bd9Sstevel@tonic-gate opts->dotick = 1;
1647c478bd9Sstevel@tonic-gate break;
1657c478bd9Sstevel@tonic-gate case 'v':
1667c478bd9Sstevel@tonic-gate opts->verbose = 1; /* more chatty */
1677c478bd9Sstevel@tonic-gate break;
1687c478bd9Sstevel@tonic-gate case 'o':
1697c478bd9Sstevel@tonic-gate if (optarg == NULL) {
1707c478bd9Sstevel@tonic-gate errcnt++;
1717c478bd9Sstevel@tonic-gate break;
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate if ((opts->log = fopen(optarg, "w")) == NULL) {
1747c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1757c478bd9Sstevel@tonic-gate "%s: cannot open '%s' for writing\n"),
1767c478bd9Sstevel@tonic-gate opts->pgmname, optarg);
1777c478bd9Sstevel@tonic-gate return (1);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate break;
1807c478bd9Sstevel@tonic-gate case 'c': /* specify statistics */
1817c478bd9Sstevel@tonic-gate if ((sgrp = cpc_setgrp_newset(opts->master,
1827c478bd9Sstevel@tonic-gate optarg, &errcnt)) != NULL)
1837c478bd9Sstevel@tonic-gate opts->master = sgrp;
1847c478bd9Sstevel@tonic-gate break;
1857c478bd9Sstevel@tonic-gate case 'p': /* grab given pid */
1867c478bd9Sstevel@tonic-gate if ((opts->pid = atoi(optarg)) <= 0)
1877c478bd9Sstevel@tonic-gate errcnt++;
1887c478bd9Sstevel@tonic-gate break;
1897c478bd9Sstevel@tonic-gate case 'h':
1907c478bd9Sstevel@tonic-gate opts->dohelp = 1;
1917c478bd9Sstevel@tonic-gate break;
1927c478bd9Sstevel@tonic-gate case '?':
1937c478bd9Sstevel@tonic-gate default:
1947c478bd9Sstevel@tonic-gate errcnt++;
1957c478bd9Sstevel@tonic-gate break;
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate if (opts->nsamples == 0)
1997c478bd9Sstevel@tonic-gate opts->nsamples = UINT_MAX;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate if (errcnt != 0 ||
2027c478bd9Sstevel@tonic-gate opts->dohelp ||
2037c478bd9Sstevel@tonic-gate (argc == optind && opts->pid == 0) ||
2047c478bd9Sstevel@tonic-gate (argc > optind && opts->pid != 0) ||
2057c478bd9Sstevel@tonic-gate (opts->nsets = cpc_setgrp_numsets(opts->master)) == 0) {
2067c478bd9Sstevel@tonic-gate (void) fprintf(opts->dohelp ? stdout : stderr, gettext(
2077c478bd9Sstevel@tonic-gate "Usage:\n\t%s [-T secs] [-N count] [-Defhnv] [-o file]\n"
2087c478bd9Sstevel@tonic-gate "\t\t-c events [command [args] | -p pid]\n\n"
2097c478bd9Sstevel@tonic-gate "\t-T secs\t seconds between samples, default 1\n"
2107c478bd9Sstevel@tonic-gate "\t-N count number of samples, default unlimited\n"
2117c478bd9Sstevel@tonic-gate "\t-D\t enable debug mode\n"
2127c478bd9Sstevel@tonic-gate "\t-e\t follow exec(2), and execve(2)\n"
2137c478bd9Sstevel@tonic-gate "\t-f\t follow fork(2), fork1(2), and vfork(2)\n"
2147c478bd9Sstevel@tonic-gate "\t-h\t print extended usage information\n"
2157c478bd9Sstevel@tonic-gate "\t-n\t suppress titles\n"
2167c478bd9Sstevel@tonic-gate "\t-t\t include virtualized %s register\n"
2177c478bd9Sstevel@tonic-gate "\t-v\t verbose mode\n"
2187c478bd9Sstevel@tonic-gate "\t-o file\t write cpu statistics to this file\n"
2197c478bd9Sstevel@tonic-gate "\t-c events specify processor events to be monitored\n"
2207c478bd9Sstevel@tonic-gate "\t-p pid\t pid of existing process to capture\n\n"
2217c478bd9Sstevel@tonic-gate "\tUse cpustat(1M) to monitor system-wide statistics.\n"),
2227c478bd9Sstevel@tonic-gate opts->pgmname, CPC_TICKREG_NAME);
2237c478bd9Sstevel@tonic-gate if (opts->dohelp) {
2247c478bd9Sstevel@tonic-gate (void) putchar('\n');
2257c478bd9Sstevel@tonic-gate (void) capabilities(cpc, stdout);
2267c478bd9Sstevel@tonic-gate exit(0);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate exit(2);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate
231*b885580bSAlexander Kolbasov /*
232*b885580bSAlexander Kolbasov * Catch signals from terminal, so they can be handled asynchronously
233*b885580bSAlexander Kolbasov * when we're ready instead of when we're not (;-)
234*b885580bSAlexander Kolbasov */
235*b885580bSAlexander Kolbasov if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
236*b885580bSAlexander Kolbasov (void) sigset(SIGHUP, intr);
237*b885580bSAlexander Kolbasov if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
238*b885580bSAlexander Kolbasov (void) sigset(SIGINT, intr);
239*b885580bSAlexander Kolbasov if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
240*b885580bSAlexander Kolbasov (void) sigset(SIGQUIT, intr);
241*b885580bSAlexander Kolbasov (void) sigset(SIGPIPE, intr);
242*b885580bSAlexander Kolbasov (void) sigset(SIGTERM, intr);
243*b885580bSAlexander Kolbasov
2447c478bd9Sstevel@tonic-gate cpc_setgrp_reset(opts->master);
2457c478bd9Sstevel@tonic-gate (void) setvbuf(opts->log, NULL, _IOLBF, 0);
2467c478bd9Sstevel@tonic-gate ret = cputrack(argc, argv, optind);
2477c478bd9Sstevel@tonic-gate (void) cpc_close(cpc);
2487c478bd9Sstevel@tonic-gate return (ret);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate static void
print_title(cpc_setgrp_t * sgrp)2527c478bd9Sstevel@tonic-gate print_title(cpc_setgrp_t *sgrp)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7s ", "time");
2557c478bd9Sstevel@tonic-gate if (opts->followfork)
2567c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6s ", "pid");
2577c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3s %10s ", "lwp", "event");
2587c478bd9Sstevel@tonic-gate if (opts->dotick)
2597c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", CPC_TICKREG_NAME);
2607c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%s\n", cpc_setgrp_gethdr(sgrp));
2617c478bd9Sstevel@tonic-gate (void) fflush(opts->log);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate
2647c478bd9Sstevel@tonic-gate static void
print_exec(float now,pid_t pid,char * name)2657c478bd9Sstevel@tonic-gate print_exec(float now, pid_t pid, char *name)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate if (name == NULL)
2687c478bd9Sstevel@tonic-gate name = "(unknown)";
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ", now);
2717c478bd9Sstevel@tonic-gate if (opts->followfork)
2727c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)pid);
2737c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", 1, "exec");
2747c478bd9Sstevel@tonic-gate if (opts->dotick)
2757c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", "");
2767c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s %9s # '%s'\n", "", "", name);
2777c478bd9Sstevel@tonic-gate (void) fflush(opts->log);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate static void
print_fork(float now,pid_t newpid,id_t lwpid,pid_t oldpid)2817c478bd9Sstevel@tonic-gate print_fork(float now, pid_t newpid, id_t lwpid, pid_t oldpid)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ", now);
2847c478bd9Sstevel@tonic-gate if (opts->followfork)
2857c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)oldpid);
2867c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", (int)lwpid, "fork");
2877c478bd9Sstevel@tonic-gate if (opts->dotick)
2887c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", "");
2897c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s %9s # %d\n", "", "", (int)newpid);
2907c478bd9Sstevel@tonic-gate (void) fflush(opts->log);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate static void
print_sample(pid_t pid,id_t lwpid,char * pevent,cpc_buf_t * buf,int nreq,const char * evname)2947c478bd9Sstevel@tonic-gate print_sample(pid_t pid, id_t lwpid,
2957c478bd9Sstevel@tonic-gate char *pevent, cpc_buf_t *buf, int nreq, const char *evname)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate uint64_t val;
2987c478bd9Sstevel@tonic-gate int i;
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ",
3017c478bd9Sstevel@tonic-gate mstimestamp(cpc_buf_hrtime(cpc, buf)));
3027c478bd9Sstevel@tonic-gate if (opts->followfork)
3037c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)pid);
3047c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", (int)lwpid, pevent);
3057c478bd9Sstevel@tonic-gate if (opts->dotick)
3067c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9" PRId64 " ",
3077c478bd9Sstevel@tonic-gate cpc_buf_tick(cpc, buf));
3087c478bd9Sstevel@tonic-gate for (i = 0; i < nreq; i++) {
3097c478bd9Sstevel@tonic-gate (void) cpc_buf_get(cpc, buf, i, &val);
3107c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9" PRId64 " ", val);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate if (opts->nsets > 1)
3137c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, " # %s\n", evname);
3147c478bd9Sstevel@tonic-gate else
3157c478bd9Sstevel@tonic-gate (void) fputc('\n', opts->log);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate struct pstate {
3197c478bd9Sstevel@tonic-gate cpc_setgrp_t *accum;
3207c478bd9Sstevel@tonic-gate cpc_setgrp_t **sgrps;
3217c478bd9Sstevel@tonic-gate int maxlwpid;
3227c478bd9Sstevel@tonic-gate };
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate static int
pinit_lwp(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)3257c478bd9Sstevel@tonic-gate pinit_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate struct pstate *state = arg;
3287c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp;
3297c478bd9Sstevel@tonic-gate cpc_set_t *set;
3307c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
3317c478bd9Sstevel@tonic-gate char *errstr;
3327c478bd9Sstevel@tonic-gate int nreq;
3337c478bd9Sstevel@tonic-gate
334*b885580bSAlexander Kolbasov if (interrupt)
335*b885580bSAlexander Kolbasov return (0);
336*b885580bSAlexander Kolbasov
3377c478bd9Sstevel@tonic-gate if (state->maxlwpid < lwpid) {
3387c478bd9Sstevel@tonic-gate state->sgrps = realloc(state->sgrps,
3397c478bd9Sstevel@tonic-gate lwpid * sizeof (state->sgrps));
3407c478bd9Sstevel@tonic-gate if (state->sgrps == NULL) {
3417c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3427c478bd9Sstevel@tonic-gate "%6d: init_lwp: out of memory\n"), (int)pid);
3437c478bd9Sstevel@tonic-gate return (-1);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate while (state->maxlwpid < lwpid) {
3467c478bd9Sstevel@tonic-gate state->sgrps[state->maxlwpid] = NULL;
3477c478bd9Sstevel@tonic-gate state->maxlwpid++;
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate if ((sgrp = state->sgrps[lwpid-1]) == NULL) {
3527c478bd9Sstevel@tonic-gate if ((sgrp = cpc_setgrp_clone(opts->master)) == NULL) {
3537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3547c478bd9Sstevel@tonic-gate "%6d: init_lwp: out of memory\n"), (int)pid);
3557c478bd9Sstevel@tonic-gate return (-1);
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate state->sgrps[lwpid-1] = sgrp;
3587c478bd9Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp);
3597c478bd9Sstevel@tonic-gate } else {
3607c478bd9Sstevel@tonic-gate cpc_setgrp_reset(sgrp);
3617c478bd9Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate if (cpc_bind_pctx(cpc, pctx, lwpid, set, 0) != 0 ||
3677c478bd9Sstevel@tonic-gate cpc_set_sample(cpc, set, *data2) != 0) {
3687c478bd9Sstevel@tonic-gate errstr = strerror(errno);
3697c478bd9Sstevel@tonic-gate if (errno == EAGAIN)
3707c478bd9Sstevel@tonic-gate (void) cpc_unbind(cpc, set);
3718d4e547dSae112802 #if defined(__i386)
3727c478bd9Sstevel@tonic-gate if (errno == EACCES)
3737c478bd9Sstevel@tonic-gate p4_ht_error();
3747c478bd9Sstevel@tonic-gate else
3758d4e547dSae112802 #endif
3767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3777c478bd9Sstevel@tonic-gate "%6d: init_lwp: can't bind perf counters "
3787c478bd9Sstevel@tonic-gate "to lwp%d - %s\n"), (int)pid, (int)lwpid,
3797c478bd9Sstevel@tonic-gate errstr);
3807c478bd9Sstevel@tonic-gate return (-1);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate if (opts->verbose)
3847c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "init_lwp",
3857c478bd9Sstevel@tonic-gate *data2, nreq, cpc_setgrp_getname(sgrp));
3867c478bd9Sstevel@tonic-gate return (0);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3907c478bd9Sstevel@tonic-gate static int
pfini_lwp(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)3917c478bd9Sstevel@tonic-gate pfini_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate struct pstate *state = arg;
3947c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
3957c478bd9Sstevel@tonic-gate cpc_set_t *set;
3967c478bd9Sstevel@tonic-gate char *errstr;
3977c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
3987c478bd9Sstevel@tonic-gate int nreq;
3997c478bd9Sstevel@tonic-gate
400*b885580bSAlexander Kolbasov if (interrupt)
401*b885580bSAlexander Kolbasov return (0);
402*b885580bSAlexander Kolbasov
4037c478bd9Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp);
4047c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4050185ddb4SKuriakose Kuruvilla if (cpc_set_sample(cpc, set, *scratch) == 0) {
4060185ddb4SKuriakose Kuruvilla if (opts->nsets == 1) {
4070185ddb4SKuriakose Kuruvilla /*
4080185ddb4SKuriakose Kuruvilla * When we only have one set of counts, the sample
4090185ddb4SKuriakose Kuruvilla * gives us the accumulated count.
4100185ddb4SKuriakose Kuruvilla */
4110185ddb4SKuriakose Kuruvilla *data1 = *scratch;
4120185ddb4SKuriakose Kuruvilla } else {
4130185ddb4SKuriakose Kuruvilla /*
4140185ddb4SKuriakose Kuruvilla * When we have more than one set of counts, the
4150185ddb4SKuriakose Kuruvilla * sample gives us the count for the latest sample
4160185ddb4SKuriakose Kuruvilla * period. *data1 contains the accumulated count but
4170185ddb4SKuriakose Kuruvilla * does not include the count for the latest sample
4180185ddb4SKuriakose Kuruvilla * period for this set of counters.
4190185ddb4SKuriakose Kuruvilla */
4200185ddb4SKuriakose Kuruvilla cpc_buf_add(cpc, *data1, *data1, *scratch);
4210185ddb4SKuriakose Kuruvilla }
4227c478bd9Sstevel@tonic-gate if (opts->verbose)
4237c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "fini_lwp",
4247c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp));
4257c478bd9Sstevel@tonic-gate cpc_setgrp_accum(state->accum, sgrp);
4267c478bd9Sstevel@tonic-gate if (cpc_unbind(cpc, set) == 0)
4277c478bd9Sstevel@tonic-gate return (0);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate
4307c478bd9Sstevel@tonic-gate switch (errno) {
4317c478bd9Sstevel@tonic-gate case EAGAIN:
4327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%6d: fini_lwp: "
4337c478bd9Sstevel@tonic-gate "lwp%d: perf counter contents invalidated\n"),
4347c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid);
4357c478bd9Sstevel@tonic-gate break;
4367c478bd9Sstevel@tonic-gate default:
4377c478bd9Sstevel@tonic-gate errstr = strerror(errno);
4387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%6d: fini_lwp: "
4397c478bd9Sstevel@tonic-gate "lwp%d: can't access perf counters - %s\n"),
4407c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid, errstr);
4417c478bd9Sstevel@tonic-gate break;
4427c478bd9Sstevel@tonic-gate }
4437c3666b4Skk112340 return (-1);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4477c478bd9Sstevel@tonic-gate static int
plwp_create(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)4487c478bd9Sstevel@tonic-gate plwp_create(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = opts->master;
4517c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
4527c478bd9Sstevel@tonic-gate int nreq;
4537c478bd9Sstevel@tonic-gate
454*b885580bSAlexander Kolbasov if (interrupt)
455*b885580bSAlexander Kolbasov return (0);
456*b885580bSAlexander Kolbasov
4577c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "lwp_create",
4607c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp));
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate return (0);
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4667c478bd9Sstevel@tonic-gate static int
plwp_exit(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)4677c478bd9Sstevel@tonic-gate plwp_exit(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate struct pstate *state = arg;
4707c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
4717c478bd9Sstevel@tonic-gate cpc_set_t *start;
4727c478bd9Sstevel@tonic-gate int nreq;
4737c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
4747c478bd9Sstevel@tonic-gate
475*b885580bSAlexander Kolbasov if (interrupt)
476*b885580bSAlexander Kolbasov return (0);
477*b885580bSAlexander Kolbasov
4787c478bd9Sstevel@tonic-gate start = cpc_setgrp_getset(sgrp);
4797c478bd9Sstevel@tonic-gate do {
4807c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4817c478bd9Sstevel@tonic-gate if (cpc_buf_hrtime(cpc, *data1) == 0)
4827c478bd9Sstevel@tonic-gate continue;
4837c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "lwp_exit",
4847c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp));
4857c478bd9Sstevel@tonic-gate } while (cpc_setgrp_nextset(sgrp) != start);
4867c478bd9Sstevel@tonic-gate
4877c478bd9Sstevel@tonic-gate return (0);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4917c478bd9Sstevel@tonic-gate static int
pexec(pctx_t * pctx,pid_t pid,id_t lwpid,char * name,void * arg)4927c478bd9Sstevel@tonic-gate pexec(pctx_t *pctx, pid_t pid, id_t lwpid, char *name, void *arg)
4937c478bd9Sstevel@tonic-gate {
4947c478bd9Sstevel@tonic-gate struct pstate *state = arg;
4957c478bd9Sstevel@tonic-gate float now = 0.0;
4967c478bd9Sstevel@tonic-gate cpc_set_t *start;
4977c478bd9Sstevel@tonic-gate int nreq;
4987c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
4997c478bd9Sstevel@tonic-gate hrtime_t hrt;
5007c478bd9Sstevel@tonic-gate
501*b885580bSAlexander Kolbasov if (interrupt)
502*b885580bSAlexander Kolbasov return (0);
503*b885580bSAlexander Kolbasov
5047c478bd9Sstevel@tonic-gate /*
5057c478bd9Sstevel@tonic-gate * Print the accumulated results from the previous program image
5067c478bd9Sstevel@tonic-gate */
5077c478bd9Sstevel@tonic-gate cpc_setgrp_reset(state->accum);
5087c478bd9Sstevel@tonic-gate start = cpc_setgrp_getset(state->accum);
5097c478bd9Sstevel@tonic-gate do {
5107c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
5117c478bd9Sstevel@tonic-gate &scratch);
5127c478bd9Sstevel@tonic-gate hrt = cpc_buf_hrtime(cpc, *data1);
5137c478bd9Sstevel@tonic-gate if (hrt == 0)
5147c478bd9Sstevel@tonic-gate continue;
5157c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "exec",
5167c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(state->accum));
5177c478bd9Sstevel@tonic-gate if (now < mstimestamp(hrt))
5187c478bd9Sstevel@tonic-gate now = mstimestamp(hrt);
5197c478bd9Sstevel@tonic-gate } while (cpc_setgrp_nextset(state->accum) != start);
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate print_exec(now, pid, name);
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate if (state->accum != NULL) {
5247c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->accum);
5257c478bd9Sstevel@tonic-gate state->accum = NULL;
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate if (opts->followexec) {
5297c478bd9Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master);
5307c478bd9Sstevel@tonic-gate return (0);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate return (-1);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate
5357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5367c478bd9Sstevel@tonic-gate static void
pexit(pctx_t * pctx,pid_t pid,id_t lwpid,int status,void * arg)5377c478bd9Sstevel@tonic-gate pexit(pctx_t *pctx, pid_t pid, id_t lwpid, int status, void *arg)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate struct pstate *state = arg;
5407c478bd9Sstevel@tonic-gate cpc_set_t *start;
5417c478bd9Sstevel@tonic-gate int nreq;
5427c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
5437c478bd9Sstevel@tonic-gate
544*b885580bSAlexander Kolbasov if (interrupt)
545*b885580bSAlexander Kolbasov return;
546*b885580bSAlexander Kolbasov
5477c478bd9Sstevel@tonic-gate cpc_setgrp_reset(state->accum);
5487c478bd9Sstevel@tonic-gate start = cpc_setgrp_getset(state->accum);
5497c478bd9Sstevel@tonic-gate do {
5507c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
5517c478bd9Sstevel@tonic-gate &scratch);
5527c478bd9Sstevel@tonic-gate if (cpc_buf_hrtime(cpc, *data1) == 0)
5537c478bd9Sstevel@tonic-gate continue;
5547c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "exit",
5557c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(state->accum));
5567c478bd9Sstevel@tonic-gate } while (cpc_setgrp_nextset(state->accum) != start);
5577c478bd9Sstevel@tonic-gate
5587c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->accum);
5597c478bd9Sstevel@tonic-gate state->accum = NULL;
5607c478bd9Sstevel@tonic-gate
5617c478bd9Sstevel@tonic-gate for (lwpid = 1; lwpid < state->maxlwpid; lwpid++)
5627c478bd9Sstevel@tonic-gate if (state->sgrps[lwpid-1] != NULL) {
5637c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->sgrps[lwpid-1]);
5647c478bd9Sstevel@tonic-gate state->sgrps[lwpid-1] = NULL;
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate free(state->sgrps);
5677c478bd9Sstevel@tonic-gate state->sgrps = NULL;
5687c478bd9Sstevel@tonic-gate }
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate static int
ptick(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)5717c478bd9Sstevel@tonic-gate ptick(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
5727c478bd9Sstevel@tonic-gate {
5737c478bd9Sstevel@tonic-gate struct pstate *state = arg;
5747c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
5757c478bd9Sstevel@tonic-gate cpc_set_t *this = cpc_setgrp_getset(sgrp);
5767c478bd9Sstevel@tonic-gate const char *name = cpc_setgrp_getname(sgrp);
5777c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch, *tmp;
5787c478bd9Sstevel@tonic-gate char *errstr;
5797c478bd9Sstevel@tonic-gate int nreqs;
5807c478bd9Sstevel@tonic-gate
581*b885580bSAlexander Kolbasov if (interrupt)
582*b885580bSAlexander Kolbasov return (0);
583*b885580bSAlexander Kolbasov
5847c478bd9Sstevel@tonic-gate nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
5857c478bd9Sstevel@tonic-gate
5867c478bd9Sstevel@tonic-gate if (opts->nsets == 1) {
5877c478bd9Sstevel@tonic-gate /*
5887c478bd9Sstevel@tonic-gate * If we're dealing with one set, buffer usage is:
5897c478bd9Sstevel@tonic-gate *
5907c478bd9Sstevel@tonic-gate * data1 = most recent data snapshot
5917c478bd9Sstevel@tonic-gate * data2 = previous data snapshot
5927c478bd9Sstevel@tonic-gate * scratch = used for diffing data1 and data2
5937c478bd9Sstevel@tonic-gate *
5947c478bd9Sstevel@tonic-gate * Save the snapshot from the previous sample in data2
5957c478bd9Sstevel@tonic-gate * before putting the current sample in data1.
5967c478bd9Sstevel@tonic-gate */
5977c478bd9Sstevel@tonic-gate tmp = *data1;
5987c478bd9Sstevel@tonic-gate *data1 = *data2;
5997c478bd9Sstevel@tonic-gate *data2 = tmp;
6007c478bd9Sstevel@tonic-gate if (cpc_set_sample(cpc, this, *data1) != 0)
6017c478bd9Sstevel@tonic-gate goto broken;
6027c478bd9Sstevel@tonic-gate cpc_buf_sub(cpc, *scratch, *data1, *data2);
6037c478bd9Sstevel@tonic-gate } else {
6047c478bd9Sstevel@tonic-gate cpc_set_t *next = cpc_setgrp_nextset(sgrp);
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate * If there is more than set in use, we will need to
6077c478bd9Sstevel@tonic-gate * unbind and re-bind on each go-around because each
6087c478bd9Sstevel@tonic-gate * time a counter is bound, it is preset to 0 (as it was
6097c478bd9Sstevel@tonic-gate * specified when the requests were added to the set).
6107c478bd9Sstevel@tonic-gate *
6117c478bd9Sstevel@tonic-gate * Buffer usage in this case is:
6127c478bd9Sstevel@tonic-gate *
6137c478bd9Sstevel@tonic-gate * data1 = total counts for this set since program began
6147c478bd9Sstevel@tonic-gate * data2 = unused
6157c478bd9Sstevel@tonic-gate * scratch = most recent data snapshot
6167c478bd9Sstevel@tonic-gate */
6177c478bd9Sstevel@tonic-gate
6187c478bd9Sstevel@tonic-gate if (cpc_set_sample(cpc, this, *scratch) != 0)
6197c478bd9Sstevel@tonic-gate goto broken;
6207c478bd9Sstevel@tonic-gate cpc_buf_add(cpc, *data1, *data1, *scratch);
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate /*
6237c478bd9Sstevel@tonic-gate * No need to unbind the previous set, as binding another set
6247c478bd9Sstevel@tonic-gate * automatically unbinds the most recently bound set.
6257c478bd9Sstevel@tonic-gate */
6267c478bd9Sstevel@tonic-gate if (cpc_bind_pctx(cpc, pctx, lwpid, next, 0) != 0)
6277c478bd9Sstevel@tonic-gate goto broken;
6287c478bd9Sstevel@tonic-gate }
6297c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "tick", *scratch, nreqs, name);
6307c478bd9Sstevel@tonic-gate
6317c478bd9Sstevel@tonic-gate return (0);
6327c478bd9Sstevel@tonic-gate
6337c478bd9Sstevel@tonic-gate broken:
6347c478bd9Sstevel@tonic-gate switch (errno) {
6357c478bd9Sstevel@tonic-gate case EAGAIN:
6367c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
6377c478bd9Sstevel@tonic-gate "%6d: tick: lwp%d: perf counter contents invalidated\n"),
6387c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid);
6397c478bd9Sstevel@tonic-gate break;
6407c478bd9Sstevel@tonic-gate default:
6417c478bd9Sstevel@tonic-gate errstr = strerror(errno);
6427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
6437c478bd9Sstevel@tonic-gate "%6d: tick: lwp%d: can't access perf counter - %s\n"),
6447c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid, errstr);
6457c478bd9Sstevel@tonic-gate break;
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate (void) cpc_unbind(cpc, this);
6487c478bd9Sstevel@tonic-gate return (-1);
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate * The system has just created a new address space that has a new pid.
6537c478bd9Sstevel@tonic-gate * We're running in a child of the controlling process, with a new
6547c478bd9Sstevel@tonic-gate * pctx handle already opened on the child of the original controlled process.
6557c478bd9Sstevel@tonic-gate */
6567c478bd9Sstevel@tonic-gate static void
pfork(pctx_t * pctx,pid_t oldpid,pid_t pid,id_t lwpid,void * arg)6577c478bd9Sstevel@tonic-gate pfork(pctx_t *pctx, pid_t oldpid, pid_t pid, id_t lwpid, void *arg)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate struct pstate *state = arg;
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate print_fork(mstimestamp(0), pid, lwpid, oldpid);
6627c478bd9Sstevel@tonic-gate
6637c478bd9Sstevel@tonic-gate if (!opts->followfork)
6647c478bd9Sstevel@tonic-gate return;
6657c478bd9Sstevel@tonic-gate
6667c478bd9Sstevel@tonic-gate if (pctx_set_events(pctx,
6677c478bd9Sstevel@tonic-gate PCTX_SYSC_EXEC_EVENT, pexec,
6687c478bd9Sstevel@tonic-gate PCTX_SYSC_FORK_EVENT, pfork,
6697c478bd9Sstevel@tonic-gate PCTX_SYSC_EXIT_EVENT, pexit,
6707c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
6717c478bd9Sstevel@tonic-gate PCTX_INIT_LWP_EVENT, pinit_lwp,
6727c478bd9Sstevel@tonic-gate PCTX_FINI_LWP_EVENT, pfini_lwp,
6737c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
6747c478bd9Sstevel@tonic-gate PCTX_NULL_EVENT) == 0) {
6757c478bd9Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master);
6767c478bd9Sstevel@tonic-gate (void) pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
6777c478bd9Sstevel@tonic-gate if (state->accum) {
6787c478bd9Sstevel@tonic-gate free(state->accum);
6797c478bd9Sstevel@tonic-gate state->accum = NULL;
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate
6847c478bd9Sstevel@tonic-gate /*
6857c478bd9Sstevel@tonic-gate * Translate the incoming options into actions, and get the
6867c478bd9Sstevel@tonic-gate * tool and the process to control running.
6877c478bd9Sstevel@tonic-gate */
6887c478bd9Sstevel@tonic-gate static int
cputrack(int argc,char * argv[],int optind)6897c478bd9Sstevel@tonic-gate cputrack(int argc, char *argv[], int optind)
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate struct pstate __state, *state = &__state;
6927c478bd9Sstevel@tonic-gate pctx_t *pctx;
6937c478bd9Sstevel@tonic-gate int err;
6947c478bd9Sstevel@tonic-gate
6957c478bd9Sstevel@tonic-gate bzero(state, sizeof (*state));
6967c478bd9Sstevel@tonic-gate
6977c478bd9Sstevel@tonic-gate if (opts->pid == 0) {
6987c478bd9Sstevel@tonic-gate if (argc <= optind) {
6997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n",
7007c478bd9Sstevel@tonic-gate opts->pgmname,
7017c478bd9Sstevel@tonic-gate gettext("no program to start"));
7027c478bd9Sstevel@tonic-gate return (1);
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate pctx = pctx_create(argv[optind],
7057c478bd9Sstevel@tonic-gate &argv[optind], state, 1, cputrack_pctx_errfn);
7067c478bd9Sstevel@tonic-gate if (pctx == NULL) {
7077c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s '%s'\n",
7087c478bd9Sstevel@tonic-gate opts->pgmname,
7097c478bd9Sstevel@tonic-gate gettext("failed to start program"),
7107c478bd9Sstevel@tonic-gate argv[optind]);
7117c478bd9Sstevel@tonic-gate return (1);
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate } else {
7147c478bd9Sstevel@tonic-gate pctx = pctx_capture(opts->pid, state, 1, cputrack_pctx_errfn);
7157c478bd9Sstevel@tonic-gate if (pctx == NULL) {
7167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s %d\n",
7177c478bd9Sstevel@tonic-gate opts->pgmname,
7187c478bd9Sstevel@tonic-gate gettext("failed to capture pid"),
7197c478bd9Sstevel@tonic-gate (int)opts->pid);
7207c478bd9Sstevel@tonic-gate return (1);
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate
7247c478bd9Sstevel@tonic-gate err = pctx_set_events(pctx,
7257c478bd9Sstevel@tonic-gate PCTX_SYSC_EXEC_EVENT, pexec,
7267c478bd9Sstevel@tonic-gate PCTX_SYSC_FORK_EVENT, pfork,
7277c478bd9Sstevel@tonic-gate PCTX_SYSC_EXIT_EVENT, pexit,
7287c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
7297c478bd9Sstevel@tonic-gate PCTX_INIT_LWP_EVENT, pinit_lwp,
7307c478bd9Sstevel@tonic-gate PCTX_FINI_LWP_EVENT, pfini_lwp,
7317c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
7327c478bd9Sstevel@tonic-gate PCTX_NULL_EVENT);
7337c478bd9Sstevel@tonic-gate
7347c478bd9Sstevel@tonic-gate if (err != 0) {
7357c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n",
7367c478bd9Sstevel@tonic-gate opts->pgmname,
7377c478bd9Sstevel@tonic-gate gettext("can't bind process context ops to process"));
7387c478bd9Sstevel@tonic-gate } else {
7397c478bd9Sstevel@tonic-gate if (opts->dotitle)
7407c478bd9Sstevel@tonic-gate print_title(opts->master);
7417c478bd9Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master);
7427c478bd9Sstevel@tonic-gate zerotime();
7437c478bd9Sstevel@tonic-gate err = pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
7447c478bd9Sstevel@tonic-gate if (state->accum) {
7457c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->accum);
7467c478bd9Sstevel@tonic-gate state->accum = NULL;
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate
7507c478bd9Sstevel@tonic-gate return (err != 0 ? 1 : 0);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate
7538d4e547dSae112802 #if defined(__i386)
7548d4e547dSae112802
7557c478bd9Sstevel@tonic-gate #define OFFLINE_CMD "/usr/sbin/psradm -f "
7567c478bd9Sstevel@tonic-gate #define BUFSIZE 5 /* enough for "n " where n is a cpuid */
7577c478bd9Sstevel@tonic-gate
7587c478bd9Sstevel@tonic-gate /*
7597c478bd9Sstevel@tonic-gate * cpc_bind_pctx() failed with EACCES, which means the user must first offline
7607c478bd9Sstevel@tonic-gate * all but one logical processor on each physical processor. Print to stderr the
7617c478bd9Sstevel@tonic-gate * psradm command string to do this.
7627c478bd9Sstevel@tonic-gate */
7637c478bd9Sstevel@tonic-gate static void
p4_ht_error(void)7647c478bd9Sstevel@tonic-gate p4_ht_error(void)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate kstat_ctl_t *kc;
7677c478bd9Sstevel@tonic-gate kstat_t *ksp;
7687c478bd9Sstevel@tonic-gate kstat_named_t *k;
7697c478bd9Sstevel@tonic-gate int i;
7707c478bd9Sstevel@tonic-gate int max;
7717c478bd9Sstevel@tonic-gate int stat;
7727c478bd9Sstevel@tonic-gate int *designees;
7737c478bd9Sstevel@tonic-gate int *must_offline;
7747c478bd9Sstevel@tonic-gate char buf[BUFSIZE];
7757c478bd9Sstevel@tonic-gate char *cmd;
7767c478bd9Sstevel@tonic-gate int noffline = 0;
7777c478bd9Sstevel@tonic-gate int ndone = 0;
7787c478bd9Sstevel@tonic-gate
7797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n",
7807c478bd9Sstevel@tonic-gate gettext("Pentium 4 processors with HyperThreading present.\nOffline"
7817c478bd9Sstevel@tonic-gate " all but one logical processor on each physical processor in"
7827c478bd9Sstevel@tonic-gate " order to use\ncputrack.\n"));
7837c478bd9Sstevel@tonic-gate
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate if ((kc = kstat_open()) == NULL)
7867c478bd9Sstevel@tonic-gate return;
7877c478bd9Sstevel@tonic-gate
7887c478bd9Sstevel@tonic-gate max = sysconf(_SC_CPUID_MAX);
7897c478bd9Sstevel@tonic-gate if ((designees = malloc(max * sizeof (*designees))) == NULL) {
7907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"),
7917c478bd9Sstevel@tonic-gate opts->pgmname);
7927c478bd9Sstevel@tonic-gate exit(0);
7937c478bd9Sstevel@tonic-gate }
7947c478bd9Sstevel@tonic-gate
7957c478bd9Sstevel@tonic-gate if ((must_offline = malloc(max * sizeof (*designees))) == NULL) {
7967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"),
7977c478bd9Sstevel@tonic-gate opts->pgmname);
7987c478bd9Sstevel@tonic-gate exit(0);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate
8017c478bd9Sstevel@tonic-gate for (i = 0; i < max; i++) {
8027c478bd9Sstevel@tonic-gate designees[i] = -1;
8037c478bd9Sstevel@tonic-gate must_offline[i] = 0;
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate
8067c478bd9Sstevel@tonic-gate for (i = 0; i < max; i++) {
8077c478bd9Sstevel@tonic-gate stat = p_online(i, P_STATUS);
8087c478bd9Sstevel@tonic-gate if (stat != P_ONLINE && stat != P_NOINTR)
8097c478bd9Sstevel@tonic-gate continue;
8107c478bd9Sstevel@tonic-gate
8117c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "cpu_info", i, NULL)) == NULL) {
8127c478bd9Sstevel@tonic-gate free(designees);
8137c478bd9Sstevel@tonic-gate free(must_offline);
8147c478bd9Sstevel@tonic-gate return;
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate
8177c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, NULL) == -1) {
8187c478bd9Sstevel@tonic-gate free(designees);
8197c478bd9Sstevel@tonic-gate free(must_offline);
8207c478bd9Sstevel@tonic-gate return;
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate
8237c478bd9Sstevel@tonic-gate if ((k = (kstat_named_t *)kstat_data_lookup(ksp, "chip_id"))
8247c478bd9Sstevel@tonic-gate == NULL) {
8257c478bd9Sstevel@tonic-gate free(designees);
8267c478bd9Sstevel@tonic-gate free(must_offline);
8277c478bd9Sstevel@tonic-gate return;
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate
8307c478bd9Sstevel@tonic-gate if (designees[k->value.i32] == -1)
8317c478bd9Sstevel@tonic-gate /*
8327c478bd9Sstevel@tonic-gate * This chip doesn't yet have a CPU designated to remain
8337c478bd9Sstevel@tonic-gate * online; let this one be it.
8347c478bd9Sstevel@tonic-gate */
8357c478bd9Sstevel@tonic-gate designees[k->value.i32] = i;
8367c478bd9Sstevel@tonic-gate else {
8377c478bd9Sstevel@tonic-gate /*
8387c478bd9Sstevel@tonic-gate * This chip already has a designated CPU; this CPU must
8397c478bd9Sstevel@tonic-gate * go offline.
8407c478bd9Sstevel@tonic-gate */
8417c478bd9Sstevel@tonic-gate must_offline[i] = 1;
8427c478bd9Sstevel@tonic-gate noffline++;
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate * Now construct a string containing the command line used to offline
8487c478bd9Sstevel@tonic-gate * the appropriate processors.
8497c478bd9Sstevel@tonic-gate */
8507c478bd9Sstevel@tonic-gate
8517c478bd9Sstevel@tonic-gate if ((cmd = malloc(strlen(OFFLINE_CMD) + (noffline * BUFSIZE) + 1))
8527c478bd9Sstevel@tonic-gate == NULL) {
8537c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"),
8547c478bd9Sstevel@tonic-gate opts->pgmname);
8557c478bd9Sstevel@tonic-gate exit(0);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate
8587c478bd9Sstevel@tonic-gate (void) strcpy(cmd, OFFLINE_CMD);
8597c478bd9Sstevel@tonic-gate
8607c478bd9Sstevel@tonic-gate for (i = 0; i < max; i++) {
8617c478bd9Sstevel@tonic-gate if (must_offline[i] == 0)
8627c478bd9Sstevel@tonic-gate continue;
8637c478bd9Sstevel@tonic-gate
8647c478bd9Sstevel@tonic-gate ndone++;
8657c478bd9Sstevel@tonic-gate (void) snprintf(buf, BUFSIZE, "%d", i);
8667c478bd9Sstevel@tonic-gate if (ndone < noffline)
8677c478bd9Sstevel@tonic-gate (void) strcat(buf, " ");
8687c478bd9Sstevel@tonic-gate (void) strcat(cmd, buf);
8697c478bd9Sstevel@tonic-gate }
8707c478bd9Sstevel@tonic-gate
8717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s:\n%s\n", gettext("The following command "
8727c478bd9Sstevel@tonic-gate "will configure the system appropriately"), cmd);
8737c478bd9Sstevel@tonic-gate
8747c478bd9Sstevel@tonic-gate exit(1);
8757c478bd9Sstevel@tonic-gate }
8768d4e547dSae112802
8778d4e547dSae112802 #endif /* defined(__i386) */
878*b885580bSAlexander Kolbasov
879*b885580bSAlexander Kolbasov /*ARGSUSED*/
880*b885580bSAlexander Kolbasov static void
intr(int sig)881*b885580bSAlexander Kolbasov intr(int sig)
882*b885580bSAlexander Kolbasov {
883*b885580bSAlexander Kolbasov interrupt++;
884*b885580bSAlexander Kolbasov if (cpc != NULL)
885*b885580bSAlexander Kolbasov cpc_terminate(cpc);
886*b885580bSAlexander Kolbasov }
887