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