xref: /freebsd/usr.sbin/pmcstat/pmcstat.c (revision aa64588d28258aef88cc33b8043112e8856948d0)
1 /*-
2  * Copyright (c) 2003-2008, Joseph Koshy
3  * Copyright (c) 2007 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by A. Joseph Koshy under
7  * sponsorship from the FreeBSD Foundation and Google, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/types.h>
35 #include <sys/event.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
40 #include <sys/sysctl.h>
41 #include <sys/time.h>
42 #include <sys/ttycom.h>
43 #include <sys/user.h>
44 #include <sys/wait.h>
45 
46 #include <assert.h>
47 #include <curses.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <kvm.h>
52 #include <libgen.h>
53 #include <limits.h>
54 #include <math.h>
55 #include <pmc.h>
56 #include <pmclog.h>
57 #include <regex.h>
58 #include <signal.h>
59 #include <stdarg.h>
60 #include <stdint.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <sysexits.h>
65 #include <unistd.h>
66 
67 #include "pmcstat.h"
68 
69 /*
70  * A given invocation of pmcstat(8) can manage multiple PMCs of both
71  * the system-wide and per-process variety.  Each of these could be in
72  * 'counting mode' or in 'sampling mode'.
73  *
74  * For 'counting mode' PMCs, pmcstat(8) will periodically issue a
75  * pmc_read() at the configured time interval and print out the value
76  * of the requested PMCs.
77  *
78  * For 'sampling mode' PMCs it can log to a file for offline analysis,
79  * or can analyse sampling data "on the fly", either by converting
80  * samples to printed textual form or by creating gprof(1) compatible
81  * profiles, one per program executed.  When creating gprof(1)
82  * profiles it can optionally merge entries from multiple processes
83  * for a given executable into a single profile file.
84  *
85  * pmcstat(8) can also execute a command line and attach PMCs to the
86  * resulting child process.  The protocol used is as follows:
87  *
88  * - parent creates a socketpair for two way communication and
89  *   fork()s.
90  * - subsequently:
91  *
92  *   /Parent/				/Child/
93  *
94  *   - Wait for childs token.
95  *					- Sends token.
96  *					- Awaits signal to start.
97  *  - Attaches PMCs to the child's pid
98  *    and starts them. Sets up
99  *    monitoring for the child.
100  *  - Signals child to start.
101  *					- Recieves signal, attempts exec().
102  *
103  * After this point normal processing can happen.
104  */
105 
106 /* Globals */
107 
108 int	pmcstat_interrupt = 0;
109 int	pmcstat_displayheight = DEFAULT_DISPLAY_HEIGHT;
110 int	pmcstat_displaywidth  = DEFAULT_DISPLAY_WIDTH;
111 int	pmcstat_sockpair[NSOCKPAIRFD];
112 int	pmcstat_kq;
113 kvm_t	*pmcstat_kvm;
114 struct kinfo_proc *pmcstat_plist;
115 struct pmcstat_args args;
116 
117 void
118 pmcstat_attach_pmcs(void)
119 {
120 	struct pmcstat_ev *ev;
121 	struct pmcstat_target *pt;
122 	int count;
123 
124 	/* Attach all process PMCs to target processes. */
125 	count = 0;
126 	STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
127 		if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
128 			continue;
129 		SLIST_FOREACH(pt, &args.pa_targets, pt_next)
130 			if (pmc_attach(ev->ev_pmcid, pt->pt_pid) == 0)
131 				count++;
132 			else if (errno != ESRCH)
133 				err(EX_OSERR, "ERROR: cannot attach pmc "
134 				    "\"%s\" to process %d", ev->ev_name,
135 				    (int) pt->pt_pid);
136 	}
137 
138 	if (count == 0)
139 		errx(EX_DATAERR, "ERROR: No processes were attached to.");
140 }
141 
142 
143 void
144 pmcstat_cleanup(void)
145 {
146 	struct pmcstat_ev *ev, *tmp;
147 
148 	/* release allocated PMCs. */
149 	STAILQ_FOREACH_SAFE(ev, &args.pa_events, ev_next, tmp)
150 	    if (ev->ev_pmcid != PMC_ID_INVALID) {
151 		if (pmc_stop(ev->ev_pmcid) < 0)
152 			err(EX_OSERR, "ERROR: cannot stop pmc 0x%x "
153 			    "\"%s\"", ev->ev_pmcid, ev->ev_name);
154 		if (pmc_release(ev->ev_pmcid) < 0)
155 			err(EX_OSERR, "ERROR: cannot release pmc "
156 			    "0x%x \"%s\"", ev->ev_pmcid, ev->ev_name);
157 		free(ev->ev_name);
158 		free(ev->ev_spec);
159 		STAILQ_REMOVE(&args.pa_events, ev, pmcstat_ev, ev_next);
160 		free(ev);
161 	    }
162 
163 	/* de-configure the log file if present. */
164 	if (args.pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))
165 		(void) pmc_configure_logfile(-1);
166 
167 	if (args.pa_logparser) {
168 		pmclog_close(args.pa_logparser);
169 		args.pa_logparser = NULL;
170 	}
171 
172 	if (args.pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))
173 		pmcstat_shutdown_logging();
174 }
175 
176 void
177 pmcstat_clone_event_descriptor(struct pmcstat_ev *ev,
178     uint32_t cpumask)
179 {
180 	int cpu;
181 	struct pmcstat_ev *ev_clone;
182 
183 	while ((cpu = ffs(cpumask)) > 0) {
184 		cpu--;
185 
186 		if ((ev_clone = malloc(sizeof(*ev_clone))) == NULL)
187 			errx(EX_SOFTWARE, "ERROR: Out of memory");
188 		(void) memset(ev_clone, 0, sizeof(*ev_clone));
189 
190 		ev_clone->ev_count = ev->ev_count;
191 		ev_clone->ev_cpu   = cpu;
192 		ev_clone->ev_cumulative = ev->ev_cumulative;
193 		ev_clone->ev_flags = ev->ev_flags;
194 		ev_clone->ev_mode  = ev->ev_mode;
195 		ev_clone->ev_name  = strdup(ev->ev_name);
196 		ev_clone->ev_pmcid = ev->ev_pmcid;
197 		ev_clone->ev_saved = ev->ev_saved;
198 		ev_clone->ev_spec  = strdup(ev->ev_spec);
199 
200 		STAILQ_INSERT_TAIL(&args.pa_events, ev_clone, ev_next);
201 
202 		cpumask &= ~(1 << cpu);
203 	}
204 }
205 
206 void
207 pmcstat_create_process(void)
208 {
209 	char token;
210 	pid_t pid;
211 	struct kevent kev;
212 	struct pmcstat_target *pt;
213 
214 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pmcstat_sockpair) < 0)
215 		err(EX_OSERR, "ERROR: cannot create socket pair");
216 
217 	switch (pid = fork()) {
218 	case -1:
219 		err(EX_OSERR, "ERROR: cannot fork");
220 		/*NOTREACHED*/
221 
222 	case 0:		/* child */
223 		(void) close(pmcstat_sockpair[PARENTSOCKET]);
224 
225 		/* Write a token to tell our parent we've started executing. */
226 		if (write(pmcstat_sockpair[CHILDSOCKET], "+", 1) != 1)
227 			err(EX_OSERR, "ERROR (child): cannot write token");
228 
229 		/* Wait for our parent to signal us to start. */
230 		if (read(pmcstat_sockpair[CHILDSOCKET], &token, 1) < 0)
231 			err(EX_OSERR, "ERROR (child): cannot read token");
232 		(void) close(pmcstat_sockpair[CHILDSOCKET]);
233 
234 		/* exec() the program requested */
235 		execvp(*args.pa_argv, args.pa_argv);
236 		/* and if that fails, notify the parent */
237 		kill(getppid(), SIGCHLD);
238 		err(EX_OSERR, "ERROR: execvp \"%s\" failed", *args.pa_argv);
239 		/*NOTREACHED*/
240 
241 	default:	/* parent */
242 		(void) close(pmcstat_sockpair[CHILDSOCKET]);
243 		break;
244 	}
245 
246 	/* Ask to be notified via a kevent when the target process exits. */
247 	EV_SET(&kev, pid, EVFILT_PROC, EV_ADD|EV_ONESHOT, NOTE_EXIT, 0,
248 	    NULL);
249 	if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
250 		err(EX_OSERR, "ERROR: cannot monitor child process %d", pid);
251 
252 	if ((pt = malloc(sizeof(*pt))) == NULL)
253 		errx(EX_SOFTWARE, "ERROR: Out of memory.");
254 
255 	pt->pt_pid = pid;
256 	SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
257 
258 	/* Wait for the child to signal that its ready to go. */
259 	if (read(pmcstat_sockpair[PARENTSOCKET], &token, 1) < 0)
260 		err(EX_OSERR, "ERROR (parent): cannot read token");
261 
262 	return;
263 }
264 
265 void
266 pmcstat_find_targets(const char *spec)
267 {
268 	int n, nproc, pid, rv;
269 	struct pmcstat_target *pt;
270 	char errbuf[_POSIX2_LINE_MAX], *end;
271 	static struct kinfo_proc *kp;
272 	regex_t reg;
273 	regmatch_t regmatch;
274 
275 	/* First check if we've been given a process id. */
276       	pid = strtol(spec, &end, 0);
277 	if (end != spec && pid >= 0) {
278 		if ((pt = malloc(sizeof(*pt))) == NULL)
279 			goto outofmemory;
280 		pt->pt_pid = pid;
281 		SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
282 		return;
283 	}
284 
285 	/* Otherwise treat arg as a regular expression naming processes. */
286 	if (pmcstat_kvm == NULL) {
287 		if ((pmcstat_kvm = kvm_openfiles(NULL, "/dev/null", NULL, 0,
288 		    errbuf)) == NULL)
289 			err(EX_OSERR, "ERROR: Cannot open kernel \"%s\"",
290 			    errbuf);
291 		if ((pmcstat_plist = kvm_getprocs(pmcstat_kvm, KERN_PROC_PROC,
292 		    0, &nproc)) == NULL)
293 			err(EX_OSERR, "ERROR: Cannot get process list: %s",
294 			    kvm_geterr(pmcstat_kvm));
295 	} else
296 		nproc = 0;
297 
298 	if ((rv = regcomp(&reg, spec, REG_EXTENDED|REG_NOSUB)) != 0) {
299 		regerror(rv, &reg, errbuf, sizeof(errbuf));
300 		err(EX_DATAERR, "ERROR: Failed to compile regex \"%s\": %s",
301 		    spec, errbuf);
302 	}
303 
304 	for (n = 0, kp = pmcstat_plist; n < nproc; n++, kp++) {
305 		if ((rv = regexec(&reg, kp->ki_comm, 1, &regmatch, 0)) == 0) {
306 			if ((pt = malloc(sizeof(*pt))) == NULL)
307 				goto outofmemory;
308 			pt->pt_pid = kp->ki_pid;
309 			SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
310 		} else if (rv != REG_NOMATCH) {
311 			regerror(rv, &reg, errbuf, sizeof(errbuf));
312 			errx(EX_SOFTWARE, "ERROR: Regex evalation failed: %s",
313 			    errbuf);
314 		}
315 	}
316 
317 	regfree(&reg);
318 
319 	return;
320 
321  outofmemory:
322 	errx(EX_SOFTWARE, "Out of memory.");
323 	/*NOTREACHED*/
324 }
325 
326 uint32_t
327 pmcstat_get_cpumask(const char *cpuspec)
328 {
329 	uint32_t cpumask;
330 	int cpu;
331 	const char *s;
332 	char *end;
333 
334 	s = cpuspec;
335 	cpumask = 0ULL;
336 
337 	do {
338 		cpu = strtol(s, &end, 0);
339 		if (cpu < 0 || end == s)
340 			errx(EX_USAGE, "ERROR: Illegal CPU specification "
341 			    "\"%s\".", cpuspec);
342 		cpumask |= (1 << cpu);
343 		s = end + strspn(end, ", \t");
344 	} while (*s);
345 
346 	return (cpumask);
347 }
348 
349 void
350 pmcstat_kill_process(void)
351 {
352 	struct pmcstat_target *pt;
353 
354 	assert(args.pa_flags & FLAG_HAS_COMMANDLINE);
355 
356 	/*
357 	 * If a command line was specified, it would be the very first
358 	 * in the list, before any other processes specified by -t.
359 	 */
360 	pt = SLIST_FIRST(&args.pa_targets);
361 	assert(pt != NULL);
362 
363 	if (kill(pt->pt_pid, SIGINT) != 0)
364 		err(EX_OSERR, "ERROR: cannot signal child process");
365 }
366 
367 void
368 pmcstat_start_pmcs(void)
369 {
370 	struct pmcstat_ev *ev;
371 
372 	STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
373 
374 	    assert(ev->ev_pmcid != PMC_ID_INVALID);
375 
376 	    if (pmc_start(ev->ev_pmcid) < 0) {
377 	        warn("ERROR: Cannot start pmc 0x%x \"%s\"",
378 		    ev->ev_pmcid, ev->ev_name);
379 		pmcstat_cleanup();
380 		exit(EX_OSERR);
381 	    }
382 	}
383 
384 }
385 
386 void
387 pmcstat_print_headers(void)
388 {
389 	struct pmcstat_ev *ev;
390 	int c, w;
391 
392 	(void) fprintf(args.pa_printfile, PRINT_HEADER_PREFIX);
393 
394 	STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
395 		if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
396 			continue;
397 
398 		c = PMC_IS_SYSTEM_MODE(ev->ev_mode) ? 's' : 'p';
399 
400 		if (ev->ev_fieldskip != 0)
401 			(void) fprintf(args.pa_printfile, "%*s",
402 			    ev->ev_fieldskip, "");
403 		w = ev->ev_fieldwidth - ev->ev_fieldskip - 2;
404 
405 		if (c == 's')
406 			(void) fprintf(args.pa_printfile, "s/%02d/%-*s ",
407 			    ev->ev_cpu, w-3, ev->ev_name);
408 		else
409 			(void) fprintf(args.pa_printfile, "p/%*s ", w,
410 			    ev->ev_name);
411 	}
412 
413 	(void) fflush(args.pa_printfile);
414 }
415 
416 void
417 pmcstat_print_counters(void)
418 {
419 	int extra_width;
420 	struct pmcstat_ev *ev;
421 	pmc_value_t value;
422 
423 	extra_width = sizeof(PRINT_HEADER_PREFIX) - 1;
424 
425 	STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
426 
427 		/* skip sampling mode counters */
428 		if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
429 			continue;
430 
431 		if (pmc_read(ev->ev_pmcid, &value) < 0)
432 			err(EX_OSERR, "ERROR: Cannot read pmc "
433 			    "\"%s\"", ev->ev_name);
434 
435 		(void) fprintf(args.pa_printfile, "%*ju ",
436 		    ev->ev_fieldwidth + extra_width,
437 		    (uintmax_t) ev->ev_cumulative ? value :
438 		    (value - ev->ev_saved));
439 
440 		if (ev->ev_cumulative == 0)
441 			ev->ev_saved = value;
442 		extra_width = 0;
443 	}
444 
445 	(void) fflush(args.pa_printfile);
446 }
447 
448 /*
449  * Print output
450  */
451 
452 void
453 pmcstat_print_pmcs(void)
454 {
455 	static int linecount = 0;
456 
457 	/* check if we need to print a header line */
458 	if (++linecount > pmcstat_displayheight) {
459 		(void) fprintf(args.pa_printfile, "\n");
460 		linecount = 1;
461 	}
462 	if (linecount == 1)
463 		pmcstat_print_headers();
464 	(void) fprintf(args.pa_printfile, "\n");
465 
466 	pmcstat_print_counters();
467 
468 	return;
469 }
470 
471 /*
472  * Do process profiling
473  *
474  * If a pid was specified, attach each allocated PMC to the target
475  * process.  Otherwise, fork a child and attach the PMCs to the child,
476  * and have the child exec() the target program.
477  */
478 
479 void
480 pmcstat_start_process(void)
481 {
482 	/* Signal the child to proceed. */
483 	if (write(pmcstat_sockpair[PARENTSOCKET], "!", 1) != 1)
484 		err(EX_OSERR, "ERROR (parent): write of token failed");
485 
486 	(void) close(pmcstat_sockpair[PARENTSOCKET]);
487 }
488 
489 void
490 pmcstat_show_usage(void)
491 {
492 	errx(EX_USAGE,
493 	    "[options] [commandline]\n"
494 	    "\t Measure process and/or system performance using hardware\n"
495 	    "\t performance monitoring counters.\n"
496 	    "\t Options include:\n"
497 	    "\t -C\t\t (toggle) show cumulative counts\n"
498 	    "\t -D path\t create profiles in directory \"path\"\n"
499 	    "\t -E\t\t (toggle) show counts at process exit\n"
500 	    "\t -F file\t write a system-wide callgraph (Kcachegrind format)"
501 		" to \"file\"\n"
502 	    "\t -G file\t write a system-wide callgraph to \"file\"\n"
503 	    "\t -M file\t print executable/gmon file map to \"file\"\n"
504 	    "\t -N\t\t (toggle) capture callchains\n"
505 	    "\t -O file\t send log output to \"file\"\n"
506 	    "\t -P spec\t allocate a process-private sampling PMC\n"
507 	    "\t -R file\t read events from \"file\"\n"
508 	    "\t -S spec\t allocate a system-wide sampling PMC\n"
509 	    "\t -T\t\t start in top mode\n"
510 	    "\t -W\t\t (toggle) show counts per context switch\n"
511 	    "\t -c cpu-list\t set cpus for subsequent system-wide PMCs\n"
512 	    "\t -d\t\t (toggle) track descendants\n"
513 	    "\t -f spec\t pass \"spec\" to as plugin option\n"
514 	    "\t -g\t\t produce gprof(1) compatible profiles\n"
515 	    "\t -k dir\t\t set the path to the kernel\n"
516 	    "\t -n rate\t set sampling rate\n"
517 	    "\t -o file\t send print output to \"file\"\n"
518 	    "\t -p spec\t allocate a process-private counting PMC\n"
519 	    "\t -q\t\t suppress verbosity\n"
520 	    "\t -r fsroot\t specify FS root directory\n"
521 	    "\t -s spec\t allocate a system-wide counting PMC\n"
522 	    "\t -t process-spec attach to running processes matching "
523 		"\"process-spec\"\n"
524 	    "\t -v\t\t increase verbosity\n"
525 	    "\t -w secs\t set printing time interval\n"
526 	    "\t -z depth\t limit callchain display depth"
527 	);
528 }
529 
530 /*
531  * At exit handler for top mode
532  */
533 
534 void
535 pmcstat_topexit(void)
536 {
537 	if (!args.pa_toptty)
538 		return;
539 
540 	/*
541 	 * Shutdown ncurses.
542 	 */
543 	clrtoeol();
544 	refresh();
545 	endwin();
546 }
547 
548 /*
549  * Main
550  */
551 
552 int
553 main(int argc, char **argv)
554 {
555 	double interval;
556 	int option, npmc, ncpu, haltedcpus;
557 	int c, check_driver_stats, current_cpu, current_sampling_count;
558 	int do_callchain, do_descendants, do_logproccsw, do_logprocexit;
559 	int do_print;
560 	size_t dummy;
561 	int graphdepth;
562 	int pipefd[2];
563 	int use_cumulative_counts;
564 	short cf, cb;
565 	uint32_t cpumask;
566 	char *end, *tmp;
567 	const char *errmsg, *graphfilename;
568 	enum pmcstat_state runstate;
569 	struct pmc_driverstats ds_start, ds_end;
570 	struct pmcstat_ev *ev;
571 	struct sigaction sa;
572 	struct kevent kev;
573 	struct winsize ws;
574 	struct stat sb;
575 	char buffer[PATH_MAX];
576 
577 	check_driver_stats      = 0;
578 	current_cpu 		= 0;
579 	current_sampling_count  = DEFAULT_SAMPLE_COUNT;
580 	do_callchain		= 1;
581 	do_descendants          = 0;
582 	do_logproccsw           = 0;
583 	do_logprocexit          = 0;
584 	use_cumulative_counts   = 0;
585 	graphfilename		= "-";
586 	args.pa_required	= 0;
587 	args.pa_flags		= 0;
588 	args.pa_verbosity	= 1;
589 	args.pa_logfd		= -1;
590 	args.pa_fsroot		= "";
591 	args.pa_kernel		= strdup("/boot/kernel");
592 	args.pa_samplesdir	= ".";
593 	args.pa_printfile	= stderr;
594 	args.pa_graphdepth	= DEFAULT_CALLGRAPH_DEPTH;
595 	args.pa_graphfile	= NULL;
596 	args.pa_interval	= DEFAULT_WAIT_INTERVAL;
597 	args.pa_mapfilename	= NULL;
598 	args.pa_inputpath	= NULL;
599 	args.pa_outputpath	= NULL;
600 	args.pa_pplugin		= PMCSTAT_PL_NONE;
601 	args.pa_plugin		= PMCSTAT_PL_NONE;
602 	args.pa_ctdumpinstr	= 1;
603 	args.pa_topmode		= PMCSTAT_TOP_DELTA;
604 	args.pa_toptty		= 0;
605 	args.pa_topcolor	= 0;
606 	args.pa_mergepmc	= 0;
607 	STAILQ_INIT(&args.pa_events);
608 	SLIST_INIT(&args.pa_targets);
609 	bzero(&ds_start, sizeof(ds_start));
610 	bzero(&ds_end, sizeof(ds_end));
611 	ev = NULL;
612 
613 	/*
614 	 * The initial CPU mask specifies all non-halted CPUS in the
615 	 * system.
616 	 */
617 	dummy = sizeof(int);
618 	if (sysctlbyname("hw.ncpu", &ncpu, &dummy, NULL, 0) < 0)
619 		err(EX_OSERR, "ERROR: Cannot determine the number of CPUs");
620 	cpumask = (1 << ncpu) - 1;
621 	haltedcpus = 0;
622 	if (ncpu > 1) {
623 		if (sysctlbyname("machdep.hlt_cpus", &haltedcpus, &dummy,
624 		    NULL, 0) < 0)
625 			err(EX_OSERR, "ERROR: Cannot determine which CPUs are "
626 			    "halted");
627 		cpumask &= ~haltedcpus;
628 	}
629 
630 	while ((option = getopt(argc, argv,
631 	    "CD:EF:G:M:NO:P:R:S:TWc:df:gk:m:n:o:p:qr:s:t:vw:z:")) != -1)
632 		switch (option) {
633 		case 'C':	/* cumulative values */
634 			use_cumulative_counts = !use_cumulative_counts;
635 			args.pa_required |= FLAG_HAS_COUNTING_PMCS;
636 			break;
637 
638 		case 'c':	/* CPU */
639 
640 			if (optarg[0] == '*' && optarg[1] == '\0')
641 				cpumask = ((1 << ncpu) - 1) & ~haltedcpus;
642 			else
643 				cpumask = pmcstat_get_cpumask(optarg);
644 
645 			args.pa_required |= FLAG_HAS_SYSTEM_PMCS;
646 			break;
647 
648 		case 'D':
649 			if (stat(optarg, &sb) < 0)
650 				err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
651 				    optarg);
652 			if (!S_ISDIR(sb.st_mode))
653 				errx(EX_USAGE, "ERROR: \"%s\" is not a "
654 				    "directory.", optarg);
655 			args.pa_samplesdir = optarg;
656 			args.pa_flags     |= FLAG_HAS_SAMPLESDIR;
657 			args.pa_required  |= FLAG_DO_GPROF;
658 			break;
659 
660 		case 'd':	/* toggle descendents */
661 			do_descendants = !do_descendants;
662 			args.pa_required |= FLAG_HAS_PROCESS_PMCS;
663 			break;
664 
665 		case 'F':	/* produce a system-wide calltree */
666 			args.pa_flags |= FLAG_DO_CALLGRAPHS;
667 			args.pa_plugin = PMCSTAT_PL_CALLTREE;
668 			graphfilename = optarg;
669 			break;
670 
671 		case 'f':	/* plugins options */
672 			if (args.pa_plugin == PMCSTAT_PL_NONE)
673 				err(EX_USAGE, "ERROR: Need -g/-G/-m/-T.");
674 			pmcstat_pluginconfigure_log(optarg);
675 			break;
676 
677 		case 'G':	/* produce a system-wide callgraph */
678 			args.pa_flags |= FLAG_DO_CALLGRAPHS;
679 			args.pa_plugin = PMCSTAT_PL_CALLGRAPH;
680 			graphfilename = optarg;
681 			break;
682 
683 		case 'g':	/* produce gprof compatible profiles */
684 			args.pa_flags |= FLAG_DO_GPROF;
685 			args.pa_pplugin = PMCSTAT_PL_CALLGRAPH;
686 			args.pa_plugin	= PMCSTAT_PL_GPROF;
687 			break;
688 
689 		case 'k':	/* pathname to the kernel */
690 			free(args.pa_kernel);
691 			args.pa_kernel = strdup(optarg);
692 			args.pa_required |= FLAG_DO_ANALYSIS;
693 			args.pa_flags    |= FLAG_HAS_KERNELPATH;
694 			break;
695 
696 		case 'm':
697 			args.pa_flags |= FLAG_DO_ANNOTATE;
698 			args.pa_plugin = PMCSTAT_PL_ANNOTATE;
699 			graphfilename  = optarg;
700 			break;
701 
702 		case 'E':	/* log process exit */
703 			do_logprocexit = !do_logprocexit;
704 			args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
705 			    FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
706 			break;
707 
708 		case 'M':	/* mapfile */
709 			args.pa_mapfilename = optarg;
710 			break;
711 
712 		case 'N':
713 			do_callchain = !do_callchain;
714 			args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
715 			break;
716 
717 		case 'p':	/* process virtual counting PMC */
718 		case 's':	/* system-wide counting PMC */
719 		case 'P':	/* process virtual sampling PMC */
720 		case 'S':	/* system-wide sampling PMC */
721 			if ((ev = malloc(sizeof(*ev))) == NULL)
722 				errx(EX_SOFTWARE, "ERROR: Out of memory.");
723 
724 			switch (option) {
725 			case 'p': ev->ev_mode = PMC_MODE_TC; break;
726 			case 's': ev->ev_mode = PMC_MODE_SC; break;
727 			case 'P': ev->ev_mode = PMC_MODE_TS; break;
728 			case 'S': ev->ev_mode = PMC_MODE_SS; break;
729 			}
730 
731 			if (option == 'P' || option == 'p') {
732 				args.pa_flags |= FLAG_HAS_PROCESS_PMCS;
733 				args.pa_required |= (FLAG_HAS_COMMANDLINE |
734 				    FLAG_HAS_TARGET);
735 			}
736 
737 			if (option == 'P' || option == 'S') {
738 				args.pa_flags |= FLAG_HAS_SAMPLING_PMCS;
739 				args.pa_required |= (FLAG_HAS_PIPE |
740 				    FLAG_HAS_OUTPUT_LOGFILE);
741 			}
742 
743 			if (option == 'p' || option == 's')
744 				args.pa_flags |= FLAG_HAS_COUNTING_PMCS;
745 
746 			if (option == 's' || option == 'S')
747 				args.pa_flags |= FLAG_HAS_SYSTEM_PMCS;
748 
749 			ev->ev_spec  = strdup(optarg);
750 
751 			if (option == 'S' || option == 'P')
752 				ev->ev_count = current_sampling_count;
753 			else
754 				ev->ev_count = -1;
755 
756 			if (option == 'S' || option == 's')
757 				ev->ev_cpu = ffs(cpumask) - 1;
758 			else
759 				ev->ev_cpu = PMC_CPU_ANY;
760 
761 			ev->ev_flags = 0;
762 			if (do_callchain)
763 				ev->ev_flags |= PMC_F_CALLCHAIN;
764 			if (do_descendants)
765 				ev->ev_flags |= PMC_F_DESCENDANTS;
766 			if (do_logprocexit)
767 				ev->ev_flags |= PMC_F_LOG_PROCEXIT;
768 			if (do_logproccsw)
769 				ev->ev_flags |= PMC_F_LOG_PROCCSW;
770 
771 			ev->ev_cumulative  = use_cumulative_counts;
772 
773 			ev->ev_saved = 0LL;
774 			ev->ev_pmcid = PMC_ID_INVALID;
775 
776 			/* extract event name */
777 			c = strcspn(optarg, ", \t");
778 			ev->ev_name = malloc(c + 1);
779 			(void) strncpy(ev->ev_name, optarg, c);
780 			*(ev->ev_name + c) = '\0';
781 
782 			STAILQ_INSERT_TAIL(&args.pa_events, ev, ev_next);
783 
784 			if (option == 's' || option == 'S')
785 				pmcstat_clone_event_descriptor(ev,
786 				    cpumask & ~(1 << ev->ev_cpu));
787 
788 			break;
789 
790 		case 'n':	/* sampling count */
791 			current_sampling_count = strtol(optarg, &end, 0);
792 			if (*end != '\0' || current_sampling_count <= 0)
793 				errx(EX_USAGE,
794 				    "ERROR: Illegal count value \"%s\".",
795 				    optarg);
796 			args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
797 			break;
798 
799 		case 'o':	/* outputfile */
800 			if (args.pa_printfile != NULL)
801 				(void) fclose(args.pa_printfile);
802 			if ((args.pa_printfile = fopen(optarg, "w")) == NULL)
803 				errx(EX_OSERR, "ERROR: cannot open \"%s\" for "
804 				    "writing.", optarg);
805 			args.pa_flags |= FLAG_DO_PRINT;
806 			break;
807 
808 		case 'O':	/* sampling output */
809 			if (args.pa_outputpath)
810 				errx(EX_USAGE, "ERROR: option -O may only be "
811 				    "specified once.");
812 			args.pa_outputpath = optarg;
813 			args.pa_flags |= FLAG_HAS_OUTPUT_LOGFILE;
814 			break;
815 
816 		case 'q':	/* quiet mode */
817 			args.pa_verbosity = 0;
818 			break;
819 
820 		case 'r':	/* root FS path */
821 			args.pa_fsroot = optarg;
822 			break;
823 
824 		case 'R':	/* read an existing log file */
825 			if (args.pa_inputpath != NULL)
826 				errx(EX_USAGE, "ERROR: option -R may only be "
827 				    "specified once.");
828 			args.pa_inputpath = optarg;
829 			if (args.pa_printfile == stderr)
830 				args.pa_printfile = stdout;
831 			args.pa_flags |= FLAG_READ_LOGFILE;
832 			break;
833 
834 		case 't':	/* target pid or process name */
835 			pmcstat_find_targets(optarg);
836 
837 			args.pa_flags |= FLAG_HAS_TARGET;
838 			args.pa_required |= FLAG_HAS_PROCESS_PMCS;
839 			break;
840 
841 		case 'T':	/* top mode */
842 			args.pa_flags |= FLAG_DO_TOP;
843 			args.pa_plugin = PMCSTAT_PL_CALLGRAPH;
844 			args.pa_ctdumpinstr = 0;
845 			args.pa_mergepmc = 1;
846 			if (args.pa_printfile == stderr)
847 				args.pa_printfile = stdout;
848 			break;
849 
850 		case 'v':	/* verbose */
851 			args.pa_verbosity++;
852 			break;
853 
854 		case 'w':	/* wait interval */
855 			interval = strtod(optarg, &end);
856 			if (*end != '\0' || interval <= 0)
857 				errx(EX_USAGE, "ERROR: Illegal wait interval "
858 				    "value \"%s\".", optarg);
859 			args.pa_flags |= FLAG_HAS_WAIT_INTERVAL;
860 			args.pa_interval = interval;
861 			break;
862 
863 		case 'W':	/* toggle LOG_CSW */
864 			do_logproccsw = !do_logproccsw;
865 			args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
866 			    FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
867 			break;
868 
869 		case 'z':
870 			graphdepth = strtod(optarg, &end);
871 			if (*end != '\0' || graphdepth <= 0)
872 				errx(EX_USAGE, "ERROR: Illegal callchain "
873 				    "depth \"%s\".", optarg);
874 			args.pa_graphdepth = graphdepth;
875 			args.pa_required |= FLAG_DO_CALLGRAPHS;
876 			break;
877 
878 		case '?':
879 		default:
880 			pmcstat_show_usage();
881 			break;
882 
883 		}
884 
885 	args.pa_argc = (argc -= optind);
886 	args.pa_argv = (argv += optind);
887 
888 	args.pa_cpumask = cpumask; /* For selecting CPUs using -R. */
889 
890 	if (argc)	/* command line present */
891 		args.pa_flags |= FLAG_HAS_COMMANDLINE;
892 
893 	if (args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS |
894 	    FLAG_DO_ANNOTATE | FLAG_DO_TOP))
895 		args.pa_flags |= FLAG_DO_ANALYSIS;
896 
897 	/*
898 	 * Check invocation syntax.
899 	 */
900 
901 	/* disallow -O and -R together */
902 	if (args.pa_outputpath && args.pa_inputpath)
903 		errx(EX_USAGE, "ERROR: options -O and -R are mutually "
904 		    "exclusive.");
905 
906 	/* -m option is allowed with -R only. */
907 	if (args.pa_flags & FLAG_DO_ANNOTATE && args.pa_inputpath == NULL)
908 		errx(EX_USAGE, "ERROR: option -m requires an input file");
909 
910 	/* -m option is not allowed combined with -g or -G. */
911 	if (args.pa_flags & FLAG_DO_ANNOTATE &&
912 	    args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS))
913 		errx(EX_USAGE, "ERROR: option -m and -g | -G are mutually "
914 		    "exclusive");
915 
916 	if (args.pa_flags & FLAG_READ_LOGFILE) {
917 		errmsg = NULL;
918 		if (args.pa_flags & FLAG_HAS_COMMANDLINE)
919 			errmsg = "a command line specification";
920 		else if (args.pa_flags & FLAG_HAS_TARGET)
921 			errmsg = "option -t";
922 		else if (!STAILQ_EMPTY(&args.pa_events))
923 			errmsg = "a PMC event specification";
924 		if (errmsg)
925 			errx(EX_USAGE, "ERROR: option -R may not be used with "
926 			    "%s.", errmsg);
927 	} else if (STAILQ_EMPTY(&args.pa_events))
928 		/* All other uses require a PMC spec. */
929 		pmcstat_show_usage();
930 
931 	/* check for -t pid without a process PMC spec */
932 	if ((args.pa_required & FLAG_HAS_TARGET) &&
933 	    (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
934 		errx(EX_USAGE, "ERROR: option -t requires a process mode PMC "
935 		    "to be specified.");
936 
937 	/* check for process-mode options without a command or -t pid */
938 	if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
939 	    (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
940 		errx(EX_USAGE, "ERROR: options -d, -E, -p, -P, and -W require "
941 		    "a command line or target process.");
942 
943 	/* check for -p | -P without a target process of some sort */
944 	if ((args.pa_required & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) &&
945 	    (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
946 		errx(EX_USAGE, "ERROR: options -P and -p require a "
947 		    "target process or a command line.");
948 
949 	/* check for process-mode options without a process-mode PMC */
950 	if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
951 	    (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
952 		errx(EX_USAGE, "ERROR: options -d, -E, and -W require a "
953 		    "process mode PMC to be specified.");
954 
955 	/* check for -c cpu with no system mode PMCs or logfile. */
956 	if ((args.pa_required & FLAG_HAS_SYSTEM_PMCS) &&
957 	    (args.pa_flags & FLAG_HAS_SYSTEM_PMCS) == 0 &&
958 	    (args.pa_flags & FLAG_READ_LOGFILE) == 0)
959 		errx(EX_USAGE, "ERROR: option -c requires at least one "
960 		    "system mode PMC to be specified.");
961 
962 	/* check for counting mode options without a counting PMC */
963 	if ((args.pa_required & FLAG_HAS_COUNTING_PMCS) &&
964 	    (args.pa_flags & FLAG_HAS_COUNTING_PMCS) == 0)
965 		errx(EX_USAGE, "ERROR: options -C, -W and -o require at "
966 		    "least one counting mode PMC to be specified.");
967 
968 	/* check for sampling mode options without a sampling PMC spec */
969 	if ((args.pa_required & FLAG_HAS_SAMPLING_PMCS) &&
970 	    (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) == 0)
971 		errx(EX_USAGE, "ERROR: options -N, -n and -O require at "
972 		    "least one sampling mode PMC to be specified.");
973 
974 	/* check if -g/-G/-m/-T are being used correctly */
975 	if ((args.pa_flags & FLAG_DO_ANALYSIS) &&
976 	    !(args.pa_flags & (FLAG_HAS_SAMPLING_PMCS|FLAG_READ_LOGFILE)))
977 		errx(EX_USAGE, "ERROR: options -g/-G/-m/-T require sampling PMCs "
978 		    "or -R to be specified.");
979 
980 	/* check if -O was spuriously specified */
981 	if ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) &&
982 	    (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0)
983 		errx(EX_USAGE,
984 		    "ERROR: option -O is used only with options "
985 		    "-E, -P, -S and -W.");
986 
987 	/* -k kernel path require -g/-G/-m/-T or -R */
988 	if ((args.pa_flags & FLAG_HAS_KERNELPATH) &&
989 	    (args.pa_flags & FLAG_DO_ANALYSIS) == 0 &&
990 	    (args.pa_flags & FLAG_READ_LOGFILE) == 0)
991 	    errx(EX_USAGE, "ERROR: option -k is only used with -g/-R/-m/-T.");
992 
993 	/* -D only applies to gprof output mode (-g) */
994 	if ((args.pa_flags & FLAG_HAS_SAMPLESDIR) &&
995 	    (args.pa_flags & FLAG_DO_GPROF) == 0)
996 	    errx(EX_USAGE, "ERROR: option -D is only used with -g.");
997 
998 	/* -M mapfile requires -g or -R */
999 	if (args.pa_mapfilename != NULL &&
1000 	    (args.pa_flags & FLAG_DO_GPROF) == 0 &&
1001 	    (args.pa_flags & FLAG_READ_LOGFILE) == 0)
1002 	    errx(EX_USAGE, "ERROR: option -M is only used with -g/-R.");
1003 
1004 	/* -T is incompatible with -R (replay logfile is a TODO) */
1005 	if ((args.pa_flags & FLAG_DO_TOP) &&
1006 	    (args.pa_flags & FLAG_READ_LOGFILE))
1007 		errx(EX_USAGE, "ERROR: option -T is incompatible with -R.");
1008 
1009 	/*
1010 	 * Disallow textual output of sampling PMCs if counting PMCs
1011 	 * have also been asked for, mostly because the combined output
1012 	 * is difficult to make sense of.
1013 	 */
1014 	if ((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
1015 	    (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) &&
1016 	    ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) == 0))
1017 		errx(EX_USAGE, "ERROR: option -O is required if counting and "
1018 		    "sampling PMCs are specified together.");
1019 
1020 	/*
1021 	 * Check if "-k kerneldir" was specified, and if whether
1022 	 * 'kerneldir' actually refers to a a file.  If so, use
1023 	 * `dirname path` to determine the kernel directory.
1024 	 */
1025 	if (args.pa_flags & FLAG_HAS_KERNELPATH) {
1026 		(void) snprintf(buffer, sizeof(buffer), "%s%s", args.pa_fsroot,
1027 		    args.pa_kernel);
1028 		if (stat(buffer, &sb) < 0)
1029 			err(EX_OSERR, "ERROR: Cannot locate kernel \"%s\"",
1030 			    buffer);
1031 		if (!S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode))
1032 			errx(EX_USAGE, "ERROR: \"%s\": Unsupported file type.",
1033 			    buffer);
1034 		if (!S_ISDIR(sb.st_mode)) {
1035 			tmp = args.pa_kernel;
1036 			args.pa_kernel = strdup(dirname(args.pa_kernel));
1037 			free(tmp);
1038 			(void) snprintf(buffer, sizeof(buffer), "%s%s",
1039 			    args.pa_fsroot, args.pa_kernel);
1040 			if (stat(buffer, &sb) < 0)
1041 				err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
1042 				    buffer);
1043 			if (!S_ISDIR(sb.st_mode))
1044 				errx(EX_USAGE, "ERROR: \"%s\" is not a "
1045 				    "directory.", buffer);
1046 		}
1047 	}
1048 
1049 	/*
1050 	 * If we have a callgraph be created, select the outputfile.
1051 	 */
1052 	if (args.pa_flags & FLAG_DO_CALLGRAPHS) {
1053 		if (strcmp(graphfilename, "-") == 0)
1054 		    args.pa_graphfile = args.pa_printfile;
1055 		else {
1056 			args.pa_graphfile = fopen(graphfilename, "w");
1057 			if (args.pa_graphfile == NULL)
1058 				err(EX_OSERR, "ERROR: cannot open \"%s\" "
1059 				    "for writing", graphfilename);
1060 		}
1061 	}
1062 	if (args.pa_flags & FLAG_DO_ANNOTATE) {
1063 		args.pa_graphfile = fopen(graphfilename, "w");
1064 		if (args.pa_graphfile == NULL)
1065 			err(EX_OSERR, "ERROR: cannot open \"%s\" for writing",
1066 			    graphfilename);
1067 	}
1068 
1069 	/* if we've been asked to process a log file, do that and exit */
1070 	if (args.pa_flags & FLAG_READ_LOGFILE) {
1071 		/*
1072 		 * Print the log in textual form if we haven't been
1073 		 * asked to generate profiling information.
1074 		 */
1075 		if ((args.pa_flags & FLAG_DO_ANALYSIS) == 0)
1076 			args.pa_flags |= FLAG_DO_PRINT;
1077 
1078 		pmcstat_initialize_logging();
1079 		args.pa_logfd = pmcstat_open_log(args.pa_inputpath,
1080 		    PMCSTAT_OPEN_FOR_READ);
1081 		if ((args.pa_logparser = pmclog_open(args.pa_logfd)) == NULL)
1082 			err(EX_OSERR, "ERROR: Cannot create parser");
1083 		pmcstat_process_log();
1084 		pmcstat_shutdown_logging();
1085 		exit(EX_OK);
1086 	}
1087 
1088 	/* otherwise, we've been asked to collect data */
1089 	if (pmc_init() < 0)
1090 		err(EX_UNAVAILABLE,
1091 		    "ERROR: Initialization of the pmc(3) library failed");
1092 
1093 	if ((npmc = pmc_npmc(0)) < 0) /* assume all CPUs are identical */
1094 		err(EX_OSERR, "ERROR: Cannot determine the number of PMCs "
1095 		    "on CPU %d", 0);
1096 
1097 	/* Allocate a kqueue */
1098 	if ((pmcstat_kq = kqueue()) < 0)
1099 		err(EX_OSERR, "ERROR: Cannot allocate kqueue");
1100 
1101 	/*
1102 	 * Configure the specified log file or setup a default log
1103 	 * consumer via a pipe.
1104 	 */
1105 	if (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) {
1106 		if (args.pa_outputpath)
1107 			args.pa_logfd = pmcstat_open_log(args.pa_outputpath,
1108 			    PMCSTAT_OPEN_FOR_WRITE);
1109 		else {
1110 			/*
1111 			 * process the log on the fly by reading it in
1112 			 * through a pipe.
1113 			 */
1114 			if (pipe(pipefd) < 0)
1115 				err(EX_OSERR, "ERROR: pipe(2) failed");
1116 
1117 			if (fcntl(pipefd[READPIPEFD], F_SETFL, O_NONBLOCK) < 0)
1118 				err(EX_OSERR, "ERROR: fcntl(2) failed");
1119 
1120 			EV_SET(&kev, pipefd[READPIPEFD], EVFILT_READ, EV_ADD,
1121 			    0, 0, NULL);
1122 
1123 			if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1124 				err(EX_OSERR, "ERROR: Cannot register kevent");
1125 
1126 			args.pa_logfd = pipefd[WRITEPIPEFD];
1127 
1128 			args.pa_flags |= FLAG_HAS_PIPE;
1129 			if ((args.pa_flags & FLAG_DO_TOP) == 0)
1130 				args.pa_flags |= FLAG_DO_PRINT;
1131 			args.pa_logparser = pmclog_open(pipefd[READPIPEFD]);
1132 		}
1133 
1134 		if (pmc_configure_logfile(args.pa_logfd) < 0)
1135 			err(EX_OSERR, "ERROR: Cannot configure log file");
1136 	}
1137 
1138 	/* remember to check for driver errors if we are sampling or logging */
1139 	check_driver_stats = (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) ||
1140 	    (args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE);
1141 
1142 	/*
1143 	 * Allocate PMCs.
1144 	 */
1145 
1146 	STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
1147 	    if (pmc_allocate(ev->ev_spec, ev->ev_mode,
1148 		    ev->ev_flags, ev->ev_cpu, &ev->ev_pmcid) < 0)
1149 		    err(EX_OSERR, "ERROR: Cannot allocate %s-mode pmc with "
1150 			"specification \"%s\"",
1151 			PMC_IS_SYSTEM_MODE(ev->ev_mode) ? "system" : "process",
1152 			ev->ev_spec);
1153 
1154 	    if (PMC_IS_SAMPLING_MODE(ev->ev_mode) &&
1155 		pmc_set(ev->ev_pmcid, ev->ev_count) < 0)
1156 		    err(EX_OSERR, "ERROR: Cannot set sampling count "
1157 			"for PMC \"%s\"", ev->ev_name);
1158 	}
1159 
1160 	/* compute printout widths */
1161 	STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
1162 		int counter_width;
1163 		int display_width;
1164 		int header_width;
1165 
1166 		(void) pmc_width(ev->ev_pmcid, &counter_width);
1167 		header_width = strlen(ev->ev_name) + 2; /* prefix '%c/' */
1168 		display_width = (int) floor(counter_width / 3.32193) + 1;
1169 
1170 		if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
1171 			header_width += 3; /* 2 digit CPU number + '/' */
1172 
1173 		if (header_width > display_width) {
1174 			ev->ev_fieldskip = 0;
1175 			ev->ev_fieldwidth = header_width;
1176 		} else {
1177 			ev->ev_fieldskip = display_width -
1178 			    header_width;
1179 			ev->ev_fieldwidth = display_width;
1180 		}
1181 	}
1182 
1183 	/*
1184 	 * If our output is being set to a terminal, register a handler
1185 	 * for window size changes.
1186 	 */
1187 
1188 	if (isatty(fileno(args.pa_printfile))) {
1189 
1190 		if (ioctl(fileno(args.pa_printfile), TIOCGWINSZ, &ws) < 0)
1191 			err(EX_OSERR, "ERROR: Cannot determine window size");
1192 
1193 		pmcstat_displayheight = ws.ws_row - 1;
1194 		pmcstat_displaywidth  = ws.ws_col - 1;
1195 
1196 		EV_SET(&kev, SIGWINCH, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1197 
1198 		if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1199 			err(EX_OSERR, "ERROR: Cannot register kevent for "
1200 			    "SIGWINCH");
1201 
1202 		args.pa_toptty = 1;
1203 	}
1204 
1205 	/*
1206 	 * Listen to key input in top mode.
1207 	 */
1208 	if (args.pa_flags & FLAG_DO_TOP) {
1209 		EV_SET(&kev, fileno(stdin), EVFILT_READ, EV_ADD, 0, 0, NULL);
1210 		if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1211 			err(EX_OSERR, "ERROR: Cannot register kevent");
1212 	}
1213 
1214 	EV_SET(&kev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1215 	if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1216 		err(EX_OSERR, "ERROR: Cannot register kevent for SIGINT");
1217 
1218 	EV_SET(&kev, SIGIO, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1219 	if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1220 		err(EX_OSERR, "ERROR: Cannot register kevent for SIGIO");
1221 
1222 	/*
1223 	 * An exec() failure of a forked child is signalled by the
1224 	 * child sending the parent a SIGCHLD.  We don't register an
1225 	 * actual signal handler for SIGCHLD, but instead use our
1226 	 * kqueue to pick up the signal.
1227 	 */
1228 	EV_SET(&kev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1229 	if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1230 		err(EX_OSERR, "ERROR: Cannot register kevent for SIGCHLD");
1231 
1232 	/*
1233 	 * Setup a timer if we have counting mode PMCs needing to be printed or
1234 	 * top mode plugin is active.
1235 	 */
1236 	if (((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
1237 	     (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) ||
1238 	    (args.pa_flags & FLAG_DO_TOP)) {
1239 		EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0,
1240 		    args.pa_interval * 1000, NULL);
1241 
1242 		if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1243 			err(EX_OSERR, "ERROR: Cannot register kevent for "
1244 			    "timer");
1245 	}
1246 
1247 	/* attach PMCs to the target process, starting it if specified */
1248 	if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1249 		pmcstat_create_process();
1250 
1251 	if (check_driver_stats && pmc_get_driver_stats(&ds_start) < 0)
1252 		err(EX_OSERR, "ERROR: Cannot retrieve driver statistics");
1253 
1254 	/* Attach process pmcs to the target process. */
1255 	if (args.pa_flags & (FLAG_HAS_TARGET | FLAG_HAS_COMMANDLINE)) {
1256 		if (SLIST_EMPTY(&args.pa_targets))
1257 			errx(EX_DATAERR, "ERROR: No matching target "
1258 			    "processes.");
1259 		if (args.pa_flags & FLAG_HAS_PROCESS_PMCS)
1260 			pmcstat_attach_pmcs();
1261 
1262 		if (pmcstat_kvm) {
1263 			kvm_close(pmcstat_kvm);
1264 			pmcstat_kvm = NULL;
1265 		}
1266 	}
1267 
1268 	/* start the pmcs */
1269 	pmcstat_start_pmcs();
1270 
1271 	/* start the (commandline) process if needed */
1272 	if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1273 		pmcstat_start_process();
1274 
1275 	/* initialize logging if printing the configured log */
1276 	if ((args.pa_flags & (FLAG_DO_PRINT | FLAG_DO_TOP)) &&
1277 	    (args.pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE)))
1278 		pmcstat_initialize_logging();
1279 
1280 	/* Handle SIGINT using the kqueue loop */
1281 	sa.sa_handler = SIG_IGN;
1282 	sa.sa_flags   = 0;
1283 	(void) sigemptyset(&sa.sa_mask);
1284 
1285 	if (sigaction(SIGINT, &sa, NULL) < 0)
1286 		err(EX_OSERR, "ERROR: Cannot install signal handler");
1287 
1288 	/*
1289 	 * Setup the top mode display.
1290 	 */
1291 	if (args.pa_flags & FLAG_DO_TOP) {
1292 		args.pa_flags &= ~FLAG_DO_PRINT;
1293 
1294 		if (args.pa_toptty) {
1295 			/*
1296 			 * Init ncurses.
1297 			 */
1298 			initscr();
1299 			if(has_colors() == TRUE) {
1300 				args.pa_topcolor = 1;
1301 				start_color();
1302 				use_default_colors();
1303 				pair_content(0, &cf, &cb);
1304 				init_pair(1, COLOR_RED, cb);
1305 				init_pair(2, COLOR_YELLOW, cb);
1306 				init_pair(3, COLOR_GREEN, cb);
1307 			}
1308 			cbreak();
1309 			noecho();
1310 			nonl();
1311 			nodelay(stdscr, 1);
1312 			intrflush(stdscr, FALSE);
1313 			keypad(stdscr, TRUE);
1314 			clear();
1315 			/* Get terminal width / height with ncurses. */
1316 			getmaxyx(stdscr, pmcstat_displayheight, pmcstat_displaywidth);
1317 			pmcstat_displayheight--; pmcstat_displaywidth--;
1318 			atexit(pmcstat_topexit);
1319 		}
1320 	}
1321 
1322 	/*
1323 	 * loop till either the target process (if any) exits, or we
1324 	 * are killed by a SIGINT.
1325 	 */
1326 	runstate = PMCSTAT_RUNNING;
1327 	do_print = 0;
1328 	do {
1329 		if ((c = kevent(pmcstat_kq, NULL, 0, &kev, 1, NULL)) <= 0) {
1330 			if (errno != EINTR)
1331 				err(EX_OSERR, "ERROR: kevent failed");
1332 			else
1333 				continue;
1334 		}
1335 
1336 		if (kev.flags & EV_ERROR)
1337 			errc(EX_OSERR, kev.data, "ERROR: kevent failed");
1338 
1339 		switch (kev.filter) {
1340 		case EVFILT_PROC:  /* target has exited */
1341 			if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE |
1342 				FLAG_HAS_PIPE))
1343 				runstate = pmcstat_close_log();
1344 			else
1345 				runstate = PMCSTAT_FINISHED;
1346 			do_print = 1;
1347 			break;
1348 
1349 		case EVFILT_READ:  /* log file data is present */
1350 			if (kev.ident == (unsigned)fileno(stdin)) {
1351 				if (pmcstat_keypress_log())
1352 					runstate = pmcstat_close_log();
1353 			} else
1354 				runstate = pmcstat_process_log();
1355 			break;
1356 
1357 		case EVFILT_SIGNAL:
1358 			if (kev.ident == SIGCHLD) {
1359 				/*
1360 				 * The child process sends us a
1361 				 * SIGCHLD if its exec() failed.  We
1362 				 * wait for it to exit and then exit
1363 				 * ourselves.
1364 				 */
1365 				(void) wait(&c);
1366 				runstate = PMCSTAT_FINISHED;
1367 			} else if (kev.ident == SIGIO) {
1368 				/*
1369 				 * We get a SIGIO if a PMC loses all
1370 				 * of its targets, or if logfile
1371 				 * writes encounter an error.
1372 				 */
1373 				if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE |
1374 				    FLAG_HAS_PIPE)) {
1375 					runstate = pmcstat_close_log();
1376 					if (args.pa_flags &
1377 					    (FLAG_DO_PRINT|FLAG_DO_ANALYSIS))
1378 						pmcstat_process_log();
1379 				}
1380 				do_print = 1; /* print PMCs at exit */
1381 				runstate = PMCSTAT_FINISHED;
1382 			} else if (kev.ident == SIGINT) {
1383 				/* Kill the child process if we started it */
1384 				if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1385 					pmcstat_kill_process();
1386 				/* Close the pipe to self, if present. */
1387 				if (args.pa_flags & FLAG_HAS_PIPE)
1388 					(void) close(pipefd[READPIPEFD]);
1389 				runstate = PMCSTAT_FINISHED;
1390 			} else if (kev.ident == SIGWINCH) {
1391 				if (ioctl(fileno(args.pa_printfile),
1392 					TIOCGWINSZ, &ws) < 0)
1393 				    err(EX_OSERR, "ERROR: Cannot determine "
1394 					"window size");
1395 				pmcstat_displayheight = ws.ws_row - 1;
1396 				pmcstat_displaywidth  = ws.ws_col - 1;
1397 			} else
1398 				assert(0);
1399 
1400 			break;
1401 
1402 		case EVFILT_TIMER: /* print out counting PMCs */
1403 			do_print = 1;
1404 			break;
1405 
1406 		}
1407 
1408 		if (do_print) {
1409 			if ((args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) {
1410 				pmcstat_print_pmcs();
1411 				if (runstate == PMCSTAT_FINISHED && /* final newline */
1412 				    (args.pa_flags & FLAG_DO_PRINT) == 0)
1413 					(void) fprintf(args.pa_printfile, "\n");
1414 			}
1415 			if (args.pa_flags & FLAG_DO_TOP)
1416 				pmcstat_display_log();
1417 			do_print = 0;
1418 		}
1419 
1420 	} while (runstate != PMCSTAT_FINISHED);
1421 
1422 	if ((args.pa_flags & FLAG_DO_TOP) && args.pa_toptty) {
1423 		pmcstat_topexit();
1424 		args.pa_toptty = 0;
1425 	}
1426 
1427 	/* flush any pending log entries */
1428 	if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE))
1429 		pmc_flush_logfile();
1430 
1431 	pmcstat_cleanup();
1432 
1433 	free(args.pa_kernel);
1434 
1435 	/* check if the driver lost any samples or events */
1436 	if (check_driver_stats) {
1437 		if (pmc_get_driver_stats(&ds_end) < 0)
1438 			err(EX_OSERR, "ERROR: Cannot retrieve driver "
1439 			    "statistics");
1440 		if (ds_start.pm_intr_bufferfull != ds_end.pm_intr_bufferfull &&
1441 		    args.pa_verbosity > 0)
1442 			warnx("WARNING: some samples were dropped.  Please "
1443 			    "consider tuning the \"kern.hwpmc.nsamples\" "
1444 			    "tunable.");
1445 		if (ds_start.pm_buffer_requests_failed !=
1446 		    ds_end.pm_buffer_requests_failed &&
1447 		    args.pa_verbosity > 0)
1448 			warnx("WARNING: some events were discarded.  Please "
1449 			    "consider tuning the \"kern.hwpmc.nbuffers\" "
1450 			    "tunable.");
1451 	}
1452 
1453 	exit(EX_OK);
1454 }
1455