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
57c3666b4Skk * Common Development and Distribution License (the "License").
67c3666b4Skk * 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 /*
22b885580bSAlexander 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
65b885580bSAlexander Kolbasov /*
66b885580bSAlexander Kolbasov * How many signals caught from terminal
67b885580bSAlexander Kolbasov * We bail out as soon as possible when interrupt is set
68b885580bSAlexander Kolbasov */
69b885580bSAlexander Kolbasov static int interrupt = 0;
70b885580bSAlexander 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);
88b885580bSAlexander Kolbasov static void intr(int);
89b885580bSAlexander Kolbasov
907c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
917c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])957c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate struct options *opts = &__options;
987c478bd9Sstevel@tonic-gate int c, errcnt = 0;
997c478bd9Sstevel@tonic-gate int nsamples;
1007c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp;
1017c478bd9Sstevel@tonic-gate char *errstr;
1027c478bd9Sstevel@tonic-gate int ret;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1057c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate if ((opts->pgmname = strrchr(argv[0], '/')) == NULL)
1087c478bd9Sstevel@tonic-gate opts->pgmname = argv[0];
1097c478bd9Sstevel@tonic-gate else
1107c478bd9Sstevel@tonic-gate opts->pgmname++;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate if ((cpc = cpc_open(CPC_VER_CURRENT)) == NULL) {
1137c478bd9Sstevel@tonic-gate errstr = strerror(errno);
1147c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: cannot access performance "
1157c478bd9Sstevel@tonic-gate "counter library - %s\n"), opts->pgmname, errstr);
1167c478bd9Sstevel@tonic-gate return (1);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate (void) cpc_seterrhndlr(cpc, cputrack_errfn);
1207c478bd9Sstevel@tonic-gate strtoset_errfn = cputrack_errfn;
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate * Establish (non-zero) defaults
1247c478bd9Sstevel@tonic-gate */
1257c478bd9Sstevel@tonic-gate opts->mseconds = 1000;
1267c478bd9Sstevel@tonic-gate opts->dotitle = 1;
1277c478bd9Sstevel@tonic-gate opts->log = stdout;
1287c478bd9Sstevel@tonic-gate if ((opts->master = cpc_setgrp_new(cpc, 0)) == NULL) {
1297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%s: no memory available\n"),
1307c478bd9Sstevel@tonic-gate opts->pgmname);
1317c478bd9Sstevel@tonic-gate exit(1);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "T:N:Defhntvo:r:c:p:")) != EOF)
1357c478bd9Sstevel@tonic-gate switch (c) {
1367c478bd9Sstevel@tonic-gate case 'T': /* sample time, seconds */
1377c478bd9Sstevel@tonic-gate opts->mseconds = (uint_t)(atof(optarg) * 1000.0);
1387c478bd9Sstevel@tonic-gate break;
1397c478bd9Sstevel@tonic-gate case 'N': /* number of samples */
1407c478bd9Sstevel@tonic-gate nsamples = atoi(optarg);
1417c478bd9Sstevel@tonic-gate if (nsamples < 0)
1427c478bd9Sstevel@tonic-gate errcnt++;
1437c478bd9Sstevel@tonic-gate else
1447c478bd9Sstevel@tonic-gate opts->nsamples = (uint_t)nsamples;
1457c478bd9Sstevel@tonic-gate break;
1467c478bd9Sstevel@tonic-gate case 'D': /* enable debugging */
1477c478bd9Sstevel@tonic-gate opts->debug++;
1487c478bd9Sstevel@tonic-gate break;
1497c478bd9Sstevel@tonic-gate case 'f': /* follow fork */
1507c478bd9Sstevel@tonic-gate opts->followfork++;
1517c478bd9Sstevel@tonic-gate break;
1527c478bd9Sstevel@tonic-gate case 'e': /* follow exec */
1537c478bd9Sstevel@tonic-gate opts->followexec++;
1547c478bd9Sstevel@tonic-gate break;
1557c478bd9Sstevel@tonic-gate case 'n': /* no titles */
1567c478bd9Sstevel@tonic-gate opts->dotitle = 0;
1577c478bd9Sstevel@tonic-gate break;
1587c478bd9Sstevel@tonic-gate case 't': /* print %tick */
1597c478bd9Sstevel@tonic-gate opts->dotick = 1;
1607c478bd9Sstevel@tonic-gate break;
1617c478bd9Sstevel@tonic-gate case 'v':
1627c478bd9Sstevel@tonic-gate opts->verbose = 1; /* more chatty */
1637c478bd9Sstevel@tonic-gate break;
1647c478bd9Sstevel@tonic-gate case 'o':
1657c478bd9Sstevel@tonic-gate if (optarg == NULL) {
1667c478bd9Sstevel@tonic-gate errcnt++;
1677c478bd9Sstevel@tonic-gate break;
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate if ((opts->log = fopen(optarg, "w")) == NULL) {
1707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1717c478bd9Sstevel@tonic-gate "%s: cannot open '%s' for writing\n"),
1727c478bd9Sstevel@tonic-gate opts->pgmname, optarg);
1737c478bd9Sstevel@tonic-gate return (1);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate break;
1767c478bd9Sstevel@tonic-gate case 'c': /* specify statistics */
1777c478bd9Sstevel@tonic-gate if ((sgrp = cpc_setgrp_newset(opts->master,
1787c478bd9Sstevel@tonic-gate optarg, &errcnt)) != NULL)
1797c478bd9Sstevel@tonic-gate opts->master = sgrp;
1807c478bd9Sstevel@tonic-gate break;
1817c478bd9Sstevel@tonic-gate case 'p': /* grab given pid */
1827c478bd9Sstevel@tonic-gate if ((opts->pid = atoi(optarg)) <= 0)
1837c478bd9Sstevel@tonic-gate errcnt++;
1847c478bd9Sstevel@tonic-gate break;
1857c478bd9Sstevel@tonic-gate case 'h':
1867c478bd9Sstevel@tonic-gate opts->dohelp = 1;
1877c478bd9Sstevel@tonic-gate break;
1887c478bd9Sstevel@tonic-gate case '?':
1897c478bd9Sstevel@tonic-gate default:
1907c478bd9Sstevel@tonic-gate errcnt++;
1917c478bd9Sstevel@tonic-gate break;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate if (opts->nsamples == 0)
1957c478bd9Sstevel@tonic-gate opts->nsamples = UINT_MAX;
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate if (errcnt != 0 ||
1987c478bd9Sstevel@tonic-gate opts->dohelp ||
1997c478bd9Sstevel@tonic-gate (argc == optind && opts->pid == 0) ||
2007c478bd9Sstevel@tonic-gate (argc > optind && opts->pid != 0) ||
2017c478bd9Sstevel@tonic-gate (opts->nsets = cpc_setgrp_numsets(opts->master)) == 0) {
2027c478bd9Sstevel@tonic-gate (void) fprintf(opts->dohelp ? stdout : stderr, gettext(
2037c478bd9Sstevel@tonic-gate "Usage:\n\t%s [-T secs] [-N count] [-Defhnv] [-o file]\n"
2047c478bd9Sstevel@tonic-gate "\t\t-c events [command [args] | -p pid]\n\n"
2057c478bd9Sstevel@tonic-gate "\t-T secs\t seconds between samples, default 1\n"
2067c478bd9Sstevel@tonic-gate "\t-N count number of samples, default unlimited\n"
2077c478bd9Sstevel@tonic-gate "\t-D\t enable debug mode\n"
2087c478bd9Sstevel@tonic-gate "\t-e\t follow exec(2), and execve(2)\n"
2097c478bd9Sstevel@tonic-gate "\t-f\t follow fork(2), fork1(2), and vfork(2)\n"
2107c478bd9Sstevel@tonic-gate "\t-h\t print extended usage information\n"
2117c478bd9Sstevel@tonic-gate "\t-n\t suppress titles\n"
2127c478bd9Sstevel@tonic-gate "\t-t\t include virtualized %s register\n"
2137c478bd9Sstevel@tonic-gate "\t-v\t verbose mode\n"
2147c478bd9Sstevel@tonic-gate "\t-o file\t write cpu statistics to this file\n"
2157c478bd9Sstevel@tonic-gate "\t-c events specify processor events to be monitored\n"
2167c478bd9Sstevel@tonic-gate "\t-p pid\t pid of existing process to capture\n\n"
217bbf21555SRichard Lowe "\tUse cpustat(8) to monitor system-wide statistics.\n"),
2187c478bd9Sstevel@tonic-gate opts->pgmname, CPC_TICKREG_NAME);
2197c478bd9Sstevel@tonic-gate if (opts->dohelp) {
2207c478bd9Sstevel@tonic-gate (void) putchar('\n');
2217c478bd9Sstevel@tonic-gate (void) capabilities(cpc, stdout);
2227c478bd9Sstevel@tonic-gate exit(0);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate exit(2);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
227b885580bSAlexander Kolbasov /*
228b885580bSAlexander Kolbasov * Catch signals from terminal, so they can be handled asynchronously
229b885580bSAlexander Kolbasov * when we're ready instead of when we're not (;-)
230b885580bSAlexander Kolbasov */
231b885580bSAlexander Kolbasov if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
232b885580bSAlexander Kolbasov (void) sigset(SIGHUP, intr);
233b885580bSAlexander Kolbasov if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
234b885580bSAlexander Kolbasov (void) sigset(SIGINT, intr);
235b885580bSAlexander Kolbasov if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
236b885580bSAlexander Kolbasov (void) sigset(SIGQUIT, intr);
237b885580bSAlexander Kolbasov (void) sigset(SIGPIPE, intr);
238b885580bSAlexander Kolbasov (void) sigset(SIGTERM, intr);
239b885580bSAlexander Kolbasov
2407c478bd9Sstevel@tonic-gate cpc_setgrp_reset(opts->master);
2417c478bd9Sstevel@tonic-gate (void) setvbuf(opts->log, NULL, _IOLBF, 0);
2427c478bd9Sstevel@tonic-gate ret = cputrack(argc, argv, optind);
2437c478bd9Sstevel@tonic-gate (void) cpc_close(cpc);
2447c478bd9Sstevel@tonic-gate return (ret);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate static void
print_title(cpc_setgrp_t * sgrp)2487c478bd9Sstevel@tonic-gate print_title(cpc_setgrp_t *sgrp)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7s ", "time");
2517c478bd9Sstevel@tonic-gate if (opts->followfork)
2527c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6s ", "pid");
2537c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3s %10s ", "lwp", "event");
2547c478bd9Sstevel@tonic-gate if (opts->dotick)
2557c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", CPC_TICKREG_NAME);
2567c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%s\n", cpc_setgrp_gethdr(sgrp));
2577c478bd9Sstevel@tonic-gate (void) fflush(opts->log);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate static void
print_exec(float now,pid_t pid,char * name)2617c478bd9Sstevel@tonic-gate print_exec(float now, pid_t pid, char *name)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate if (name == NULL)
2647c478bd9Sstevel@tonic-gate name = "(unknown)";
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ", now);
2677c478bd9Sstevel@tonic-gate if (opts->followfork)
2687c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)pid);
2697c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", 1, "exec");
2707c478bd9Sstevel@tonic-gate if (opts->dotick)
2717c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", "");
2727c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s %9s # '%s'\n", "", "", name);
2737c478bd9Sstevel@tonic-gate (void) fflush(opts->log);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate static void
print_fork(float now,pid_t newpid,id_t lwpid,pid_t oldpid)2777c478bd9Sstevel@tonic-gate print_fork(float now, pid_t newpid, id_t lwpid, pid_t oldpid)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ", now);
2807c478bd9Sstevel@tonic-gate if (opts->followfork)
2817c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)oldpid);
2827c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", (int)lwpid, "fork");
2837c478bd9Sstevel@tonic-gate if (opts->dotick)
2847c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s ", "");
2857c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9s %9s # %d\n", "", "", (int)newpid);
2867c478bd9Sstevel@tonic-gate (void) fflush(opts->log);
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate static void
print_sample(pid_t pid,id_t lwpid,char * pevent,cpc_buf_t * buf,int nreq,const char * evname)2907c478bd9Sstevel@tonic-gate print_sample(pid_t pid, id_t lwpid,
2917c478bd9Sstevel@tonic-gate char *pevent, cpc_buf_t *buf, int nreq, const char *evname)
2927c478bd9Sstevel@tonic-gate {
2937c478bd9Sstevel@tonic-gate uint64_t val;
2947c478bd9Sstevel@tonic-gate int i;
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%7.3f ",
2977c478bd9Sstevel@tonic-gate mstimestamp(cpc_buf_hrtime(cpc, buf)));
2987c478bd9Sstevel@tonic-gate if (opts->followfork)
2997c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%6d ", (int)pid);
3007c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%3d %10s ", (int)lwpid, pevent);
3017c478bd9Sstevel@tonic-gate if (opts->dotick)
3027c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9" PRId64 " ",
3037c478bd9Sstevel@tonic-gate cpc_buf_tick(cpc, buf));
3047c478bd9Sstevel@tonic-gate for (i = 0; i < nreq; i++) {
3057c478bd9Sstevel@tonic-gate (void) cpc_buf_get(cpc, buf, i, &val);
3067c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, "%9" PRId64 " ", val);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate if (opts->nsets > 1)
3097c478bd9Sstevel@tonic-gate (void) fprintf(opts->log, " # %s\n", evname);
3107c478bd9Sstevel@tonic-gate else
3117c478bd9Sstevel@tonic-gate (void) fputc('\n', opts->log);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate struct pstate {
3157c478bd9Sstevel@tonic-gate cpc_setgrp_t *accum;
3167c478bd9Sstevel@tonic-gate cpc_setgrp_t **sgrps;
3177c478bd9Sstevel@tonic-gate int maxlwpid;
3187c478bd9Sstevel@tonic-gate };
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate static int
pinit_lwp(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)3217c478bd9Sstevel@tonic-gate pinit_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate struct pstate *state = arg;
3247c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp;
3257c478bd9Sstevel@tonic-gate cpc_set_t *set;
3267c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
3277c478bd9Sstevel@tonic-gate char *errstr;
3287c478bd9Sstevel@tonic-gate int nreq;
3297c478bd9Sstevel@tonic-gate
330b885580bSAlexander Kolbasov if (interrupt)
331b885580bSAlexander Kolbasov return (0);
332b885580bSAlexander Kolbasov
3337c478bd9Sstevel@tonic-gate if (state->maxlwpid < lwpid) {
3347c478bd9Sstevel@tonic-gate state->sgrps = realloc(state->sgrps,
3357c478bd9Sstevel@tonic-gate lwpid * sizeof (state->sgrps));
3367c478bd9Sstevel@tonic-gate if (state->sgrps == NULL) {
3377c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3387c478bd9Sstevel@tonic-gate "%6d: init_lwp: out of memory\n"), (int)pid);
3397c478bd9Sstevel@tonic-gate return (-1);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate while (state->maxlwpid < lwpid) {
3427c478bd9Sstevel@tonic-gate state->sgrps[state->maxlwpid] = NULL;
3437c478bd9Sstevel@tonic-gate state->maxlwpid++;
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate if ((sgrp = state->sgrps[lwpid-1]) == NULL) {
3487c478bd9Sstevel@tonic-gate if ((sgrp = cpc_setgrp_clone(opts->master)) == NULL) {
3497c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3507c478bd9Sstevel@tonic-gate "%6d: init_lwp: out of memory\n"), (int)pid);
3517c478bd9Sstevel@tonic-gate return (-1);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate state->sgrps[lwpid-1] = sgrp;
3547c478bd9Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp);
3557c478bd9Sstevel@tonic-gate } else {
3567c478bd9Sstevel@tonic-gate cpc_setgrp_reset(sgrp);
3577c478bd9Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
3617c478bd9Sstevel@tonic-gate
3627c478bd9Sstevel@tonic-gate if (cpc_bind_pctx(cpc, pctx, lwpid, set, 0) != 0 ||
3637c478bd9Sstevel@tonic-gate cpc_set_sample(cpc, set, *data2) != 0) {
3647c478bd9Sstevel@tonic-gate errstr = strerror(errno);
365*12fb9219SRichard Lowe if (errno == EAGAIN) {
3667c478bd9Sstevel@tonic-gate (void) cpc_unbind(cpc, set);
367*12fb9219SRichard Lowe }
368*12fb9219SRichard Lowe
369*12fb9219SRichard Lowe (void) fprintf(stderr, gettext(
370*12fb9219SRichard Lowe "%6d: init_lwp: can't bind perf counters "
371*12fb9219SRichard Lowe "to lwp%d - %s\n"), (int)pid, (int)lwpid, errstr);
3727c478bd9Sstevel@tonic-gate return (-1);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate if (opts->verbose)
3767c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "init_lwp",
3777c478bd9Sstevel@tonic-gate *data2, nreq, cpc_setgrp_getname(sgrp));
3787c478bd9Sstevel@tonic-gate return (0);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3827c478bd9Sstevel@tonic-gate static int
pfini_lwp(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)3837c478bd9Sstevel@tonic-gate pfini_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate struct pstate *state = arg;
3867c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
3877c478bd9Sstevel@tonic-gate cpc_set_t *set;
3887c478bd9Sstevel@tonic-gate char *errstr;
3897c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
3907c478bd9Sstevel@tonic-gate int nreq;
3917c478bd9Sstevel@tonic-gate
392b885580bSAlexander Kolbasov if (interrupt)
393b885580bSAlexander Kolbasov return (0);
394b885580bSAlexander Kolbasov
3957c478bd9Sstevel@tonic-gate set = cpc_setgrp_getset(sgrp);
3967c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
3970185ddb4SKuriakose Kuruvilla if (cpc_set_sample(cpc, set, *scratch) == 0) {
3980185ddb4SKuriakose Kuruvilla if (opts->nsets == 1) {
3990185ddb4SKuriakose Kuruvilla /*
4000185ddb4SKuriakose Kuruvilla * When we only have one set of counts, the sample
4010185ddb4SKuriakose Kuruvilla * gives us the accumulated count.
4020185ddb4SKuriakose Kuruvilla */
4030185ddb4SKuriakose Kuruvilla *data1 = *scratch;
4040185ddb4SKuriakose Kuruvilla } else {
4050185ddb4SKuriakose Kuruvilla /*
4060185ddb4SKuriakose Kuruvilla * When we have more than one set of counts, the
4070185ddb4SKuriakose Kuruvilla * sample gives us the count for the latest sample
4080185ddb4SKuriakose Kuruvilla * period. *data1 contains the accumulated count but
4090185ddb4SKuriakose Kuruvilla * does not include the count for the latest sample
4100185ddb4SKuriakose Kuruvilla * period for this set of counters.
4110185ddb4SKuriakose Kuruvilla */
4120185ddb4SKuriakose Kuruvilla cpc_buf_add(cpc, *data1, *data1, *scratch);
4130185ddb4SKuriakose Kuruvilla }
4147c478bd9Sstevel@tonic-gate if (opts->verbose)
4157c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "fini_lwp",
4167c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp));
4177c478bd9Sstevel@tonic-gate cpc_setgrp_accum(state->accum, sgrp);
4187c478bd9Sstevel@tonic-gate if (cpc_unbind(cpc, set) == 0)
4197c478bd9Sstevel@tonic-gate return (0);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate switch (errno) {
4237c478bd9Sstevel@tonic-gate case EAGAIN:
4247c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%6d: fini_lwp: "
4257c478bd9Sstevel@tonic-gate "lwp%d: perf counter contents invalidated\n"),
4267c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid);
4277c478bd9Sstevel@tonic-gate break;
4287c478bd9Sstevel@tonic-gate default:
4297c478bd9Sstevel@tonic-gate errstr = strerror(errno);
4307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("%6d: fini_lwp: "
4317c478bd9Sstevel@tonic-gate "lwp%d: can't access perf counters - %s\n"),
4327c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid, errstr);
4337c478bd9Sstevel@tonic-gate break;
4347c478bd9Sstevel@tonic-gate }
4357c3666b4Skk return (-1);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4397c478bd9Sstevel@tonic-gate static int
plwp_create(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)4407c478bd9Sstevel@tonic-gate plwp_create(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = opts->master;
4437c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
4447c478bd9Sstevel@tonic-gate int nreq;
4457c478bd9Sstevel@tonic-gate
446b885580bSAlexander Kolbasov if (interrupt)
447b885580bSAlexander Kolbasov return (0);
448b885580bSAlexander Kolbasov
4497c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "lwp_create",
4527c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp));
4537c478bd9Sstevel@tonic-gate
4547c478bd9Sstevel@tonic-gate return (0);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4587c478bd9Sstevel@tonic-gate static int
plwp_exit(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)4597c478bd9Sstevel@tonic-gate plwp_exit(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate struct pstate *state = arg;
4627c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
4637c478bd9Sstevel@tonic-gate cpc_set_t *start;
4647c478bd9Sstevel@tonic-gate int nreq;
4657c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
4667c478bd9Sstevel@tonic-gate
467b885580bSAlexander Kolbasov if (interrupt)
468b885580bSAlexander Kolbasov return (0);
469b885580bSAlexander Kolbasov
4707c478bd9Sstevel@tonic-gate start = cpc_setgrp_getset(sgrp);
4717c478bd9Sstevel@tonic-gate do {
4727c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
4737c478bd9Sstevel@tonic-gate if (cpc_buf_hrtime(cpc, *data1) == 0)
4747c478bd9Sstevel@tonic-gate continue;
4757c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "lwp_exit",
4767c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(sgrp));
4777c478bd9Sstevel@tonic-gate } while (cpc_setgrp_nextset(sgrp) != start);
4787c478bd9Sstevel@tonic-gate
4797c478bd9Sstevel@tonic-gate return (0);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate
4827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4837c478bd9Sstevel@tonic-gate static int
pexec(pctx_t * pctx,pid_t pid,id_t lwpid,char * name,void * arg)4847c478bd9Sstevel@tonic-gate pexec(pctx_t *pctx, pid_t pid, id_t lwpid, char *name, void *arg)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate struct pstate *state = arg;
4877c478bd9Sstevel@tonic-gate float now = 0.0;
4887c478bd9Sstevel@tonic-gate cpc_set_t *start;
4897c478bd9Sstevel@tonic-gate int nreq;
4907c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
4917c478bd9Sstevel@tonic-gate hrtime_t hrt;
4927c478bd9Sstevel@tonic-gate
493b885580bSAlexander Kolbasov if (interrupt)
494b885580bSAlexander Kolbasov return (0);
495b885580bSAlexander Kolbasov
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate * Print the accumulated results from the previous program image
4987c478bd9Sstevel@tonic-gate */
4997c478bd9Sstevel@tonic-gate cpc_setgrp_reset(state->accum);
5007c478bd9Sstevel@tonic-gate start = cpc_setgrp_getset(state->accum);
5017c478bd9Sstevel@tonic-gate do {
5027c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
5037c478bd9Sstevel@tonic-gate &scratch);
5047c478bd9Sstevel@tonic-gate hrt = cpc_buf_hrtime(cpc, *data1);
5057c478bd9Sstevel@tonic-gate if (hrt == 0)
5067c478bd9Sstevel@tonic-gate continue;
5077c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "exec",
5087c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(state->accum));
5097c478bd9Sstevel@tonic-gate if (now < mstimestamp(hrt))
5107c478bd9Sstevel@tonic-gate now = mstimestamp(hrt);
5117c478bd9Sstevel@tonic-gate } while (cpc_setgrp_nextset(state->accum) != start);
5127c478bd9Sstevel@tonic-gate
5137c478bd9Sstevel@tonic-gate print_exec(now, pid, name);
5147c478bd9Sstevel@tonic-gate
5157c478bd9Sstevel@tonic-gate if (state->accum != NULL) {
5167c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->accum);
5177c478bd9Sstevel@tonic-gate state->accum = NULL;
5187c478bd9Sstevel@tonic-gate }
5197c478bd9Sstevel@tonic-gate
5207c478bd9Sstevel@tonic-gate if (opts->followexec) {
5217c478bd9Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master);
5227c478bd9Sstevel@tonic-gate return (0);
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate return (-1);
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate
5277c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5287c478bd9Sstevel@tonic-gate static void
pexit(pctx_t * pctx,pid_t pid,id_t lwpid,int status,void * arg)5297c478bd9Sstevel@tonic-gate pexit(pctx_t *pctx, pid_t pid, id_t lwpid, int status, void *arg)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate struct pstate *state = arg;
5327c478bd9Sstevel@tonic-gate cpc_set_t *start;
5337c478bd9Sstevel@tonic-gate int nreq;
5347c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch;
5357c478bd9Sstevel@tonic-gate
536b885580bSAlexander Kolbasov if (interrupt)
537b885580bSAlexander Kolbasov return;
538b885580bSAlexander Kolbasov
5397c478bd9Sstevel@tonic-gate cpc_setgrp_reset(state->accum);
5407c478bd9Sstevel@tonic-gate start = cpc_setgrp_getset(state->accum);
5417c478bd9Sstevel@tonic-gate do {
5427c478bd9Sstevel@tonic-gate nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
5437c478bd9Sstevel@tonic-gate &scratch);
5447c478bd9Sstevel@tonic-gate if (cpc_buf_hrtime(cpc, *data1) == 0)
5457c478bd9Sstevel@tonic-gate continue;
5467c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "exit",
5477c478bd9Sstevel@tonic-gate *data1, nreq, cpc_setgrp_getname(state->accum));
5487c478bd9Sstevel@tonic-gate } while (cpc_setgrp_nextset(state->accum) != start);
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->accum);
5517c478bd9Sstevel@tonic-gate state->accum = NULL;
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate for (lwpid = 1; lwpid < state->maxlwpid; lwpid++)
5547c478bd9Sstevel@tonic-gate if (state->sgrps[lwpid-1] != NULL) {
5557c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->sgrps[lwpid-1]);
5567c478bd9Sstevel@tonic-gate state->sgrps[lwpid-1] = NULL;
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate free(state->sgrps);
5597c478bd9Sstevel@tonic-gate state->sgrps = NULL;
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate static int
ptick(pctx_t * pctx,pid_t pid,id_t lwpid,void * arg)5637c478bd9Sstevel@tonic-gate ptick(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate struct pstate *state = arg;
5667c478bd9Sstevel@tonic-gate cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
5677c478bd9Sstevel@tonic-gate cpc_set_t *this = cpc_setgrp_getset(sgrp);
5687c478bd9Sstevel@tonic-gate const char *name = cpc_setgrp_getname(sgrp);
5697c478bd9Sstevel@tonic-gate cpc_buf_t **data1, **data2, **scratch, *tmp;
5707c478bd9Sstevel@tonic-gate char *errstr;
5717c478bd9Sstevel@tonic-gate int nreqs;
5727c478bd9Sstevel@tonic-gate
573b885580bSAlexander Kolbasov if (interrupt)
574b885580bSAlexander Kolbasov return (0);
575b885580bSAlexander Kolbasov
5767c478bd9Sstevel@tonic-gate nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
5777c478bd9Sstevel@tonic-gate
5787c478bd9Sstevel@tonic-gate if (opts->nsets == 1) {
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate * If we're dealing with one set, buffer usage is:
5817c478bd9Sstevel@tonic-gate *
5827c478bd9Sstevel@tonic-gate * data1 = most recent data snapshot
5837c478bd9Sstevel@tonic-gate * data2 = previous data snapshot
5847c478bd9Sstevel@tonic-gate * scratch = used for diffing data1 and data2
5857c478bd9Sstevel@tonic-gate *
5867c478bd9Sstevel@tonic-gate * Save the snapshot from the previous sample in data2
5877c478bd9Sstevel@tonic-gate * before putting the current sample in data1.
5887c478bd9Sstevel@tonic-gate */
5897c478bd9Sstevel@tonic-gate tmp = *data1;
5907c478bd9Sstevel@tonic-gate *data1 = *data2;
5917c478bd9Sstevel@tonic-gate *data2 = tmp;
5927c478bd9Sstevel@tonic-gate if (cpc_set_sample(cpc, this, *data1) != 0)
5937c478bd9Sstevel@tonic-gate goto broken;
5947c478bd9Sstevel@tonic-gate cpc_buf_sub(cpc, *scratch, *data1, *data2);
5957c478bd9Sstevel@tonic-gate } else {
5967c478bd9Sstevel@tonic-gate cpc_set_t *next = cpc_setgrp_nextset(sgrp);
5977c478bd9Sstevel@tonic-gate /*
5987c478bd9Sstevel@tonic-gate * If there is more than set in use, we will need to
5997c478bd9Sstevel@tonic-gate * unbind and re-bind on each go-around because each
6007c478bd9Sstevel@tonic-gate * time a counter is bound, it is preset to 0 (as it was
6017c478bd9Sstevel@tonic-gate * specified when the requests were added to the set).
6027c478bd9Sstevel@tonic-gate *
6037c478bd9Sstevel@tonic-gate * Buffer usage in this case is:
6047c478bd9Sstevel@tonic-gate *
6057c478bd9Sstevel@tonic-gate * data1 = total counts for this set since program began
6067c478bd9Sstevel@tonic-gate * data2 = unused
6077c478bd9Sstevel@tonic-gate * scratch = most recent data snapshot
6087c478bd9Sstevel@tonic-gate */
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate if (cpc_set_sample(cpc, this, *scratch) != 0)
6117c478bd9Sstevel@tonic-gate goto broken;
6127c478bd9Sstevel@tonic-gate cpc_buf_add(cpc, *data1, *data1, *scratch);
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate /*
6157c478bd9Sstevel@tonic-gate * No need to unbind the previous set, as binding another set
6167c478bd9Sstevel@tonic-gate * automatically unbinds the most recently bound set.
6177c478bd9Sstevel@tonic-gate */
6187c478bd9Sstevel@tonic-gate if (cpc_bind_pctx(cpc, pctx, lwpid, next, 0) != 0)
6197c478bd9Sstevel@tonic-gate goto broken;
6207c478bd9Sstevel@tonic-gate }
6217c478bd9Sstevel@tonic-gate print_sample(pid, lwpid, "tick", *scratch, nreqs, name);
6227c478bd9Sstevel@tonic-gate
6237c478bd9Sstevel@tonic-gate return (0);
6247c478bd9Sstevel@tonic-gate
6257c478bd9Sstevel@tonic-gate broken:
6267c478bd9Sstevel@tonic-gate switch (errno) {
6277c478bd9Sstevel@tonic-gate case EAGAIN:
6287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
6297c478bd9Sstevel@tonic-gate "%6d: tick: lwp%d: perf counter contents invalidated\n"),
6307c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid);
6317c478bd9Sstevel@tonic-gate break;
6327c478bd9Sstevel@tonic-gate default:
6337c478bd9Sstevel@tonic-gate errstr = strerror(errno);
6347c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
6357c478bd9Sstevel@tonic-gate "%6d: tick: lwp%d: can't access perf counter - %s\n"),
6367c478bd9Sstevel@tonic-gate (int)pid, (int)lwpid, errstr);
6377c478bd9Sstevel@tonic-gate break;
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate (void) cpc_unbind(cpc, this);
6407c478bd9Sstevel@tonic-gate return (-1);
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate
6437c478bd9Sstevel@tonic-gate /*
6447c478bd9Sstevel@tonic-gate * The system has just created a new address space that has a new pid.
6457c478bd9Sstevel@tonic-gate * We're running in a child of the controlling process, with a new
6467c478bd9Sstevel@tonic-gate * pctx handle already opened on the child of the original controlled process.
6477c478bd9Sstevel@tonic-gate */
6487c478bd9Sstevel@tonic-gate static void
pfork(pctx_t * pctx,pid_t oldpid,pid_t pid,id_t lwpid,void * arg)6497c478bd9Sstevel@tonic-gate pfork(pctx_t *pctx, pid_t oldpid, pid_t pid, id_t lwpid, void *arg)
6507c478bd9Sstevel@tonic-gate {
6517c478bd9Sstevel@tonic-gate struct pstate *state = arg;
6527c478bd9Sstevel@tonic-gate
6537c478bd9Sstevel@tonic-gate print_fork(mstimestamp(0), pid, lwpid, oldpid);
6547c478bd9Sstevel@tonic-gate
6557c478bd9Sstevel@tonic-gate if (!opts->followfork)
6567c478bd9Sstevel@tonic-gate return;
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate if (pctx_set_events(pctx,
6597c478bd9Sstevel@tonic-gate PCTX_SYSC_EXEC_EVENT, pexec,
6607c478bd9Sstevel@tonic-gate PCTX_SYSC_FORK_EVENT, pfork,
6617c478bd9Sstevel@tonic-gate PCTX_SYSC_EXIT_EVENT, pexit,
6627c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
6637c478bd9Sstevel@tonic-gate PCTX_INIT_LWP_EVENT, pinit_lwp,
6647c478bd9Sstevel@tonic-gate PCTX_FINI_LWP_EVENT, pfini_lwp,
6657c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
6667c478bd9Sstevel@tonic-gate PCTX_NULL_EVENT) == 0) {
6677c478bd9Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master);
6687c478bd9Sstevel@tonic-gate (void) pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
6697c478bd9Sstevel@tonic-gate if (state->accum) {
6707c478bd9Sstevel@tonic-gate free(state->accum);
6717c478bd9Sstevel@tonic-gate state->accum = NULL;
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate * Translate the incoming options into actions, and get the
6787c478bd9Sstevel@tonic-gate * tool and the process to control running.
6797c478bd9Sstevel@tonic-gate */
6807c478bd9Sstevel@tonic-gate static int
cputrack(int argc,char * argv[],int optind)6817c478bd9Sstevel@tonic-gate cputrack(int argc, char *argv[], int optind)
6827c478bd9Sstevel@tonic-gate {
6837c478bd9Sstevel@tonic-gate struct pstate __state, *state = &__state;
6847c478bd9Sstevel@tonic-gate pctx_t *pctx;
6857c478bd9Sstevel@tonic-gate int err;
6867c478bd9Sstevel@tonic-gate
6877c478bd9Sstevel@tonic-gate bzero(state, sizeof (*state));
6887c478bd9Sstevel@tonic-gate
6897c478bd9Sstevel@tonic-gate if (opts->pid == 0) {
6907c478bd9Sstevel@tonic-gate if (argc <= optind) {
6917c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n",
6927c478bd9Sstevel@tonic-gate opts->pgmname,
6937c478bd9Sstevel@tonic-gate gettext("no program to start"));
6947c478bd9Sstevel@tonic-gate return (1);
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate pctx = pctx_create(argv[optind],
6977c478bd9Sstevel@tonic-gate &argv[optind], state, 1, cputrack_pctx_errfn);
6987c478bd9Sstevel@tonic-gate if (pctx == NULL) {
6997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s '%s'\n",
7007c478bd9Sstevel@tonic-gate opts->pgmname,
7017c478bd9Sstevel@tonic-gate gettext("failed to start program"),
7027c478bd9Sstevel@tonic-gate argv[optind]);
7037c478bd9Sstevel@tonic-gate return (1);
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate } else {
7067c478bd9Sstevel@tonic-gate pctx = pctx_capture(opts->pid, state, 1, cputrack_pctx_errfn);
7077c478bd9Sstevel@tonic-gate if (pctx == NULL) {
7087c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s %d\n",
7097c478bd9Sstevel@tonic-gate opts->pgmname,
7107c478bd9Sstevel@tonic-gate gettext("failed to capture pid"),
7117c478bd9Sstevel@tonic-gate (int)opts->pid);
7127c478bd9Sstevel@tonic-gate return (1);
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate
7167c478bd9Sstevel@tonic-gate err = pctx_set_events(pctx,
7177c478bd9Sstevel@tonic-gate PCTX_SYSC_EXEC_EVENT, pexec,
7187c478bd9Sstevel@tonic-gate PCTX_SYSC_FORK_EVENT, pfork,
7197c478bd9Sstevel@tonic-gate PCTX_SYSC_EXIT_EVENT, pexit,
7207c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
7217c478bd9Sstevel@tonic-gate PCTX_INIT_LWP_EVENT, pinit_lwp,
7227c478bd9Sstevel@tonic-gate PCTX_FINI_LWP_EVENT, pfini_lwp,
7237c478bd9Sstevel@tonic-gate PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
7247c478bd9Sstevel@tonic-gate PCTX_NULL_EVENT);
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate if (err != 0) {
7277c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: %s\n",
7287c478bd9Sstevel@tonic-gate opts->pgmname,
7297c478bd9Sstevel@tonic-gate gettext("can't bind process context ops to process"));
7307c478bd9Sstevel@tonic-gate } else {
7317c478bd9Sstevel@tonic-gate if (opts->dotitle)
7327c478bd9Sstevel@tonic-gate print_title(opts->master);
7337c478bd9Sstevel@tonic-gate state->accum = cpc_setgrp_clone(opts->master);
7347c478bd9Sstevel@tonic-gate zerotime();
7357c478bd9Sstevel@tonic-gate err = pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
7367c478bd9Sstevel@tonic-gate if (state->accum) {
7377c478bd9Sstevel@tonic-gate cpc_setgrp_free(state->accum);
7387c478bd9Sstevel@tonic-gate state->accum = NULL;
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate
7427c478bd9Sstevel@tonic-gate return (err != 0 ? 1 : 0);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate
745b885580bSAlexander Kolbasov /*ARGSUSED*/
746b885580bSAlexander Kolbasov static void
intr(int sig)747b885580bSAlexander Kolbasov intr(int sig)
748b885580bSAlexander Kolbasov {
749b885580bSAlexander Kolbasov interrupt++;
750b885580bSAlexander Kolbasov if (cpc != NULL)
751b885580bSAlexander Kolbasov cpc_terminate(cpc);
752b885580bSAlexander Kolbasov }
753