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