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