2 * Copyright (c) 2003-2008, Joseph Koshy
3 * Copyright (c) 2007 The FreeBSD Foundation
6 * Portions of this software were developed by A. Joseph Koshy under
7 * sponsorship from the FreeBSD Foundation and Google, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/cpuset.h>
36 #include <sys/event.h>
37 #include <sys/queue.h>
38 #include <sys/socket.h>
40 #include <sys/sysctl.h>
42 #include <sys/ttycom.h>
70 * A given invocation of pmcstat(8) can manage multiple PMCs of both
71 * the system-wide and per-process variety. Each of these could be in
72 * 'counting mode' or in 'sampling mode'.
74 * For 'counting mode' PMCs, pmcstat(8) will periodically issue a
75 * pmc_read() at the configured time interval and print out the value
76 * of the requested PMCs.
78 * For 'sampling mode' PMCs it can log to a file for offline analysis,
79 * or can analyse sampling data "on the fly", either by converting
80 * samples to printed textual form or by creating gprof(1) compatible
81 * profiles, one per program executed. When creating gprof(1)
82 * profiles it can optionally merge entries from multiple processes
83 * for a given executable into a single profile file.
85 * pmcstat(8) can also execute a command line and attach PMCs to the
86 * resulting child process. The protocol used is as follows:
88 * - parent creates a socketpair for two way communication and
94 * - Wait for childs token.
96 * - Awaits signal to start.
97 * - Attaches PMCs to the child's pid
98 * and starts them. Sets up
99 * monitoring for the child.
100 * - Signals child to start.
101 * - Receives signal, attempts exec().
103 * After this point normal processing can happen.
108 int pmcstat_displayheight = DEFAULT_DISPLAY_HEIGHT;
109 int pmcstat_displaywidth = DEFAULT_DISPLAY_WIDTH;
110 static int pmcstat_sockpair[NSOCKPAIRFD];
111 static int pmcstat_kq;
112 static kvm_t *pmcstat_kvm;
113 static struct kinfo_proc *pmcstat_plist;
114 struct pmcstat_args args;
117 pmcstat_clone_event_descriptor(struct pmcstat_ev *ev, const cpuset_t *cpumask)
120 struct pmcstat_ev *ev_clone;
122 for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
123 if (!CPU_ISSET(cpu, cpumask))
126 if ((ev_clone = malloc(sizeof(*ev_clone))) == NULL)
127 errx(EX_SOFTWARE, "ERROR: Out of memory");
128 (void) memset(ev_clone, 0, sizeof(*ev_clone));
130 ev_clone->ev_count = ev->ev_count;
131 ev_clone->ev_cpu = cpu;
132 ev_clone->ev_cumulative = ev->ev_cumulative;
133 ev_clone->ev_flags = ev->ev_flags;
134 ev_clone->ev_mode = ev->ev_mode;
135 ev_clone->ev_name = strdup(ev->ev_name);
136 ev_clone->ev_pmcid = ev->ev_pmcid;
137 ev_clone->ev_saved = ev->ev_saved;
138 ev_clone->ev_spec = strdup(ev->ev_spec);
140 STAILQ_INSERT_TAIL(&args.pa_events, ev_clone, ev_next);
145 pmcstat_get_cpumask(const char *cpuspec, cpuset_t *cpumask)
155 cpu = strtol(s, &end, 0);
156 if (cpu < 0 || end == s)
158 "ERROR: Illegal CPU specification \"%s\".",
160 CPU_SET(cpu, cpumask);
161 s = end + strspn(end, ", \t");
163 assert(!CPU_EMPTY(cpumask));
167 pmcstat_attach_pmcs(void)
169 struct pmcstat_ev *ev;
170 struct pmcstat_target *pt;
173 /* Attach all process PMCs to target processes. */
175 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
176 if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
178 SLIST_FOREACH(pt, &args.pa_targets, pt_next)
179 if (pmc_attach(ev->ev_pmcid, pt->pt_pid) == 0)
181 else if (errno != ESRCH)
183 "ERROR: cannot attach pmc \"%s\" to process %d",
184 ev->ev_name, (int)pt->pt_pid);
188 errx(EX_DATAERR, "ERROR: No processes were attached to.");
193 pmcstat_cleanup(void)
195 struct pmcstat_ev *ev, *tmp;
197 /* release allocated PMCs. */
198 STAILQ_FOREACH_SAFE(ev, &args.pa_events, ev_next, tmp)
199 if (ev->ev_pmcid != PMC_ID_INVALID) {
200 if (pmc_stop(ev->ev_pmcid) < 0)
201 err(EX_OSERR, "ERROR: cannot stop pmc 0x%x \"%s\"",
202 ev->ev_pmcid, ev->ev_name);
203 if (pmc_release(ev->ev_pmcid) < 0)
204 err(EX_OSERR, "ERROR: cannot release pmc 0x%x \"%s\"",
205 ev->ev_pmcid, ev->ev_name);
208 STAILQ_REMOVE(&args.pa_events, ev, pmcstat_ev, ev_next);
212 /* de-configure the log file if present. */
213 if (args.pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))
214 (void) pmc_configure_logfile(-1);
216 if (args.pa_logparser) {
217 pmclog_close(args.pa_logparser);
218 args.pa_logparser = NULL;
221 pmcstat_shutdown_logging();
225 pmcstat_create_process(void)
230 struct pmcstat_target *pt;
232 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pmcstat_sockpair) < 0)
233 err(EX_OSERR, "ERROR: cannot create socket pair");
235 switch (pid = fork()) {
237 err(EX_OSERR, "ERROR: cannot fork");
241 (void) close(pmcstat_sockpair[PARENTSOCKET]);
243 /* Write a token to tell our parent we've started executing. */
244 if (write(pmcstat_sockpair[CHILDSOCKET], "+", 1) != 1)
245 err(EX_OSERR, "ERROR (child): cannot write token");
247 /* Wait for our parent to signal us to start. */
248 if (read(pmcstat_sockpair[CHILDSOCKET], &token, 1) < 0)
249 err(EX_OSERR, "ERROR (child): cannot read token");
250 (void) close(pmcstat_sockpair[CHILDSOCKET]);
252 /* exec() the program requested */
253 execvp(*args.pa_argv, args.pa_argv);
254 /* and if that fails, notify the parent */
255 kill(getppid(), SIGCHLD);
256 err(EX_OSERR, "ERROR: execvp \"%s\" failed", *args.pa_argv);
259 default: /* parent */
260 (void) close(pmcstat_sockpair[CHILDSOCKET]);
264 /* Ask to be notified via a kevent when the target process exits. */
265 EV_SET(&kev, pid, EVFILT_PROC, EV_ADD|EV_ONESHOT, NOTE_EXIT, 0,
267 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
268 err(EX_OSERR, "ERROR: cannot monitor child process %d", pid);
270 if ((pt = malloc(sizeof(*pt))) == NULL)
271 errx(EX_SOFTWARE, "ERROR: Out of memory.");
274 SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
276 /* Wait for the child to signal that its ready to go. */
277 if (read(pmcstat_sockpair[PARENTSOCKET], &token, 1) < 0)
278 err(EX_OSERR, "ERROR (parent): cannot read token");
284 pmcstat_find_targets(const char *spec)
286 int n, nproc, pid, rv;
287 struct pmcstat_target *pt;
288 char errbuf[_POSIX2_LINE_MAX], *end;
289 static struct kinfo_proc *kp;
293 /* First check if we've been given a process id. */
294 pid = strtol(spec, &end, 0);
295 if (end != spec && pid >= 0) {
296 if ((pt = malloc(sizeof(*pt))) == NULL)
299 SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
303 /* Otherwise treat arg as a regular expression naming processes. */
304 if (pmcstat_kvm == NULL) {
305 if ((pmcstat_kvm = kvm_openfiles(NULL, "/dev/null", NULL, 0,
307 err(EX_OSERR, "ERROR: Cannot open kernel \"%s\"",
309 if ((pmcstat_plist = kvm_getprocs(pmcstat_kvm, KERN_PROC_PROC,
311 err(EX_OSERR, "ERROR: Cannot get process list: %s",
312 kvm_geterr(pmcstat_kvm));
316 if ((rv = regcomp(®, spec, REG_EXTENDED|REG_NOSUB)) != 0) {
317 regerror(rv, ®, errbuf, sizeof(errbuf));
318 err(EX_DATAERR, "ERROR: Failed to compile regex \"%s\": %s",
322 for (n = 0, kp = pmcstat_plist; n < nproc; n++, kp++) {
323 if ((rv = regexec(®, kp->ki_comm, 1, ®match, 0)) == 0) {
324 if ((pt = malloc(sizeof(*pt))) == NULL)
326 pt->pt_pid = kp->ki_pid;
327 SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
328 } else if (rv != REG_NOMATCH) {
329 regerror(rv, ®, errbuf, sizeof(errbuf));
330 errx(EX_SOFTWARE, "ERROR: Regex evalation failed: %s",
340 errx(EX_SOFTWARE, "Out of memory.");
345 pmcstat_kill_process(void)
347 struct pmcstat_target *pt;
349 assert(args.pa_flags & FLAG_HAS_COMMANDLINE);
352 * If a command line was specified, it would be the very first
353 * in the list, before any other processes specified by -t.
355 pt = SLIST_FIRST(&args.pa_targets);
358 if (kill(pt->pt_pid, SIGINT) != 0)
359 err(EX_OSERR, "ERROR: cannot signal child process");
363 pmcstat_start_pmcs(void)
365 struct pmcstat_ev *ev;
367 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
369 assert(ev->ev_pmcid != PMC_ID_INVALID);
371 if (pmc_start(ev->ev_pmcid) < 0) {
372 warn("ERROR: Cannot start pmc 0x%x \"%s\"",
373 ev->ev_pmcid, ev->ev_name);
382 pmcstat_print_headers(void)
384 struct pmcstat_ev *ev;
387 (void) fprintf(args.pa_printfile, PRINT_HEADER_PREFIX);
389 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
390 if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
393 c = PMC_IS_SYSTEM_MODE(ev->ev_mode) ? 's' : 'p';
395 if (ev->ev_fieldskip != 0)
396 (void) fprintf(args.pa_printfile, "%*s",
397 ev->ev_fieldskip, "");
398 w = ev->ev_fieldwidth - ev->ev_fieldskip - 2;
401 (void) fprintf(args.pa_printfile, "s/%02d/%-*s ",
402 ev->ev_cpu, w-3, ev->ev_name);
404 (void) fprintf(args.pa_printfile, "p/%*s ", w,
408 (void) fflush(args.pa_printfile);
412 pmcstat_print_counters(void)
415 struct pmcstat_ev *ev;
418 extra_width = sizeof(PRINT_HEADER_PREFIX) - 1;
420 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
422 /* skip sampling mode counters */
423 if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
426 if (pmc_read(ev->ev_pmcid, &value) < 0)
427 err(EX_OSERR, "ERROR: Cannot read pmc \"%s\"",
430 (void) fprintf(args.pa_printfile, "%*ju ",
431 ev->ev_fieldwidth + extra_width,
432 (uintmax_t) ev->ev_cumulative ? value :
433 (value - ev->ev_saved));
435 if (ev->ev_cumulative == 0)
436 ev->ev_saved = value;
440 (void) fflush(args.pa_printfile);
448 pmcstat_print_pmcs(void)
450 static int linecount = 0;
452 /* check if we need to print a header line */
453 if (++linecount > pmcstat_displayheight) {
454 (void) fprintf(args.pa_printfile, "\n");
458 pmcstat_print_headers();
459 (void) fprintf(args.pa_printfile, "\n");
461 pmcstat_print_counters();
467 * Do process profiling
469 * If a pid was specified, attach each allocated PMC to the target
470 * process. Otherwise, fork a child and attach the PMCs to the child,
471 * and have the child exec() the target program.
475 pmcstat_start_process(void)
477 /* Signal the child to proceed. */
478 if (write(pmcstat_sockpair[PARENTSOCKET], "!", 1) != 1)
479 err(EX_OSERR, "ERROR (parent): write of token failed");
481 (void) close(pmcstat_sockpair[PARENTSOCKET]);
485 pmcstat_show_usage(void)
488 "[options] [commandline]\n"
489 "\t Measure process and/or system performance using hardware\n"
490 "\t performance monitoring counters.\n"
491 "\t Options include:\n"
492 "\t -C\t\t (toggle) show cumulative counts\n"
493 "\t -D path\t create profiles in directory \"path\"\n"
494 "\t -E\t\t (toggle) show counts at process exit\n"
495 "\t -F file\t write a system-wide callgraph (Kcachegrind format)"
497 "\t -G file\t write a system-wide callgraph to \"file\"\n"
498 "\t -M file\t print executable/gmon file map to \"file\"\n"
499 "\t -N\t\t (toggle) capture callchains\n"
500 "\t -O file\t send log output to \"file\"\n"
501 "\t -P spec\t allocate a process-private sampling PMC\n"
502 "\t -R file\t read events from \"file\"\n"
503 "\t -S spec\t allocate a system-wide sampling PMC\n"
504 "\t -T\t\t start in top mode\n"
505 "\t -W\t\t (toggle) show counts per context switch\n"
506 "\t -a <file>\t print sampled PCs and callgraph to \"file\"\n"
507 "\t -c cpu-list\t set cpus for subsequent system-wide PMCs\n"
508 "\t -d\t\t (toggle) track descendants\n"
509 "\t -e\t\t use wide history counter for gprof(1) output\n"
510 "\t -f spec\t pass \"spec\" to as plugin option\n"
511 "\t -g\t\t produce gprof(1) compatible profiles\n"
512 "\t -k dir\t\t set the path to the kernel\n"
513 "\t -l secs\t set duration time\n"
514 "\t -m file\t print sampled PCs to \"file\"\n"
515 "\t -n rate\t set sampling rate\n"
516 "\t -o file\t send print output to \"file\"\n"
517 "\t -p spec\t allocate a process-private counting PMC\n"
518 "\t -q\t\t suppress verbosity\n"
519 "\t -r fsroot\t specify FS root directory\n"
520 "\t -s spec\t allocate a system-wide counting PMC\n"
521 "\t -t process-spec attach to running processes matching "
523 "\t -v\t\t increase verbosity\n"
524 "\t -w secs\t set printing time interval\n"
525 "\t -z depth\t limit callchain display depth"
530 * At exit handler for top mode
534 pmcstat_topexit(void)
552 main(int argc, char **argv)
554 cpuset_t cpumask, rootmask;
558 int c, check_driver_stats, current_sampling_count;
559 int do_callchain, do_descendants, do_logproccsw, do_logprocexit;
560 int do_print, do_read;
564 int use_cumulative_counts;
567 const char *errmsg, *graphfilename;
568 enum pmcstat_state runstate;
569 struct pmc_driverstats ds_start, ds_end;
570 struct pmcstat_ev *ev;
575 char buffer[PATH_MAX];
577 check_driver_stats = 0;
578 current_sampling_count = DEFAULT_SAMPLE_COUNT;
583 use_cumulative_counts = 0;
585 args.pa_required = 0;
587 args.pa_verbosity = 1;
590 args.pa_samplesdir = ".";
591 args.pa_printfile = stderr;
592 args.pa_graphdepth = DEFAULT_CALLGRAPH_DEPTH;
593 args.pa_graphfile = NULL;
594 args.pa_interval = DEFAULT_WAIT_INTERVAL;
595 args.pa_mapfilename = NULL;
596 args.pa_inputpath = NULL;
597 args.pa_outputpath = NULL;
598 args.pa_pplugin = PMCSTAT_PL_NONE;
599 args.pa_plugin = PMCSTAT_PL_NONE;
600 args.pa_ctdumpinstr = 1;
601 args.pa_topmode = PMCSTAT_TOP_DELTA;
603 args.pa_topcolor = 0;
604 args.pa_mergepmc = 0;
605 args.pa_duration = 0.0;
606 STAILQ_INIT(&args.pa_events);
607 SLIST_INIT(&args.pa_targets);
608 bzero(&ds_start, sizeof(ds_start));
609 bzero(&ds_end, sizeof(ds_end));
613 /* Default to using the running system kernel. */
615 if (sysctlbyname("kern.bootfile", NULL, &len, NULL, 0) == -1)
616 err(EX_OSERR, "ERROR: Cannot determine path of running kernel");
617 args.pa_kernel = malloc(len + 1);
618 if (sysctlbyname("kern.bootfile", args.pa_kernel, &len, NULL, 0) == -1)
619 err(EX_OSERR, "ERROR: Cannot determine path of running kernel");
622 * The initial CPU mask specifies the root mask of this process
623 * which is usually all CPUs in the system.
625 if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
626 sizeof(rootmask), &rootmask) == -1)
627 err(EX_OSERR, "ERROR: Cannot determine the root set of CPUs");
628 CPU_COPY(&rootmask, &cpumask);
630 while ((option = getopt(argc, argv,
631 "CD:EF:G:M:NO:P:R:S:TWa:c:def:gk:l:m:n:o:p:qr:s:t:vw:z:")) != -1)
633 case 'a': /* Annotate + callgraph */
634 args.pa_flags |= FLAG_DO_ANNOTATE;
635 args.pa_plugin = PMCSTAT_PL_ANNOTATE_CG;
636 graphfilename = optarg;
639 case 'C': /* cumulative values */
640 use_cumulative_counts = !use_cumulative_counts;
641 args.pa_required |= FLAG_HAS_COUNTING_PMCS;
645 if (optarg[0] == '*' && optarg[1] == '\0')
646 CPU_COPY(&rootmask, &cpumask);
648 pmcstat_get_cpumask(optarg, &cpumask);
650 args.pa_flags |= FLAGS_HAS_CPUMASK;
651 args.pa_required |= FLAG_HAS_SYSTEM_PMCS;
655 if (stat(optarg, &sb) < 0)
656 err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
658 if (!S_ISDIR(sb.st_mode))
660 "ERROR: \"%s\" is not a directory.",
662 args.pa_samplesdir = optarg;
663 args.pa_flags |= FLAG_HAS_SAMPLESDIR;
664 args.pa_required |= FLAG_DO_GPROF;
667 case 'd': /* toggle descendents */
668 do_descendants = !do_descendants;
669 args.pa_required |= FLAG_HAS_PROCESS_PMCS;
672 case 'e': /* wide gprof metrics */
673 args.pa_flags |= FLAG_DO_WIDE_GPROF_HC;
676 case 'F': /* produce a system-wide calltree */
677 args.pa_flags |= FLAG_DO_CALLGRAPHS;
678 args.pa_plugin = PMCSTAT_PL_CALLTREE;
679 graphfilename = optarg;
682 case 'f': /* plugins options */
683 if (args.pa_plugin == PMCSTAT_PL_NONE)
684 err(EX_USAGE, "ERROR: Need -g/-G/-m/-T.");
685 pmcstat_pluginconfigure_log(optarg);
688 case 'G': /* produce a system-wide callgraph */
689 args.pa_flags |= FLAG_DO_CALLGRAPHS;
690 args.pa_plugin = PMCSTAT_PL_CALLGRAPH;
691 graphfilename = optarg;
694 case 'g': /* produce gprof compatible profiles */
695 args.pa_flags |= FLAG_DO_GPROF;
696 args.pa_pplugin = PMCSTAT_PL_CALLGRAPH;
697 args.pa_plugin = PMCSTAT_PL_GPROF;
700 case 'k': /* pathname to the kernel */
701 free(args.pa_kernel);
702 args.pa_kernel = strdup(optarg);
703 args.pa_required |= FLAG_DO_ANALYSIS;
704 args.pa_flags |= FLAG_HAS_KERNELPATH;
707 case 'l': /* time duration in seconds */
708 duration = strtod(optarg, &end);
709 if (*end != '\0' || duration <= 0)
710 errx(EX_USAGE, "ERROR: Illegal duration time "
711 "value \"%s\".", optarg);
712 args.pa_flags |= FLAG_HAS_DURATION;
713 args.pa_duration = duration;
717 args.pa_flags |= FLAG_DO_ANNOTATE;
718 args.pa_plugin = PMCSTAT_PL_ANNOTATE;
719 graphfilename = optarg;
722 case 'E': /* log process exit */
723 do_logprocexit = !do_logprocexit;
724 args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
725 FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
728 case 'M': /* mapfile */
729 args.pa_mapfilename = optarg;
733 do_callchain = !do_callchain;
734 args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
737 case 'p': /* process virtual counting PMC */
738 case 's': /* system-wide counting PMC */
739 case 'P': /* process virtual sampling PMC */
740 case 'S': /* system-wide sampling PMC */
741 if ((ev = malloc(sizeof(*ev))) == NULL)
742 errx(EX_SOFTWARE, "ERROR: Out of memory.");
745 case 'p': ev->ev_mode = PMC_MODE_TC; break;
746 case 's': ev->ev_mode = PMC_MODE_SC; break;
747 case 'P': ev->ev_mode = PMC_MODE_TS; break;
748 case 'S': ev->ev_mode = PMC_MODE_SS; break;
751 if (option == 'P' || option == 'p') {
752 args.pa_flags |= FLAG_HAS_PROCESS_PMCS;
753 args.pa_required |= (FLAG_HAS_COMMANDLINE |
757 if (option == 'P' || option == 'S') {
758 args.pa_flags |= FLAG_HAS_SAMPLING_PMCS;
759 args.pa_required |= (FLAG_HAS_PIPE |
760 FLAG_HAS_OUTPUT_LOGFILE);
763 if (option == 'p' || option == 's')
764 args.pa_flags |= FLAG_HAS_COUNTING_PMCS;
766 if (option == 's' || option == 'S')
767 args.pa_flags |= FLAG_HAS_SYSTEM_PMCS;
769 ev->ev_spec = strdup(optarg);
771 if (option == 'S' || option == 'P')
772 ev->ev_count = current_sampling_count;
776 if (option == 'S' || option == 's')
777 ev->ev_cpu = CPU_FFS(&cpumask) - 1;
779 ev->ev_cpu = PMC_CPU_ANY;
783 ev->ev_flags |= PMC_F_CALLCHAIN;
785 ev->ev_flags |= PMC_F_DESCENDANTS;
787 ev->ev_flags |= PMC_F_LOG_PROCEXIT;
789 ev->ev_flags |= PMC_F_LOG_PROCCSW;
791 ev->ev_cumulative = use_cumulative_counts;
794 ev->ev_pmcid = PMC_ID_INVALID;
796 /* extract event name */
797 c = strcspn(optarg, ", \t");
798 ev->ev_name = malloc(c + 1);
799 (void) strncpy(ev->ev_name, optarg, c);
800 *(ev->ev_name + c) = '\0';
802 STAILQ_INSERT_TAIL(&args.pa_events, ev, ev_next);
804 if (option == 's' || option == 'S') {
805 CPU_CLR(ev->ev_cpu, &cpumask);
806 pmcstat_clone_event_descriptor(ev, &cpumask);
807 CPU_SET(ev->ev_cpu, &cpumask);
812 case 'n': /* sampling count */
813 current_sampling_count = strtol(optarg, &end, 0);
814 if (*end != '\0' || current_sampling_count <= 0)
816 "ERROR: Illegal count value \"%s\".",
818 args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
821 case 'o': /* outputfile */
822 if (args.pa_printfile != NULL &&
823 args.pa_printfile != stdout &&
824 args.pa_printfile != stderr)
825 (void) fclose(args.pa_printfile);
826 if ((args.pa_printfile = fopen(optarg, "w")) == NULL)
828 "ERROR: cannot open \"%s\" for writing.",
830 args.pa_flags |= FLAG_DO_PRINT;
833 case 'O': /* sampling output */
834 if (args.pa_outputpath)
836 "ERROR: option -O may only be specified once.");
837 args.pa_outputpath = optarg;
838 args.pa_flags |= FLAG_HAS_OUTPUT_LOGFILE;
841 case 'q': /* quiet mode */
842 args.pa_verbosity = 0;
845 case 'r': /* root FS path */
846 args.pa_fsroot = optarg;
849 case 'R': /* read an existing log file */
850 if (args.pa_inputpath != NULL)
852 "ERROR: option -R may only be specified once.");
853 args.pa_inputpath = optarg;
854 if (args.pa_printfile == stderr)
855 args.pa_printfile = stdout;
856 args.pa_flags |= FLAG_READ_LOGFILE;
859 case 't': /* target pid or process name */
860 pmcstat_find_targets(optarg);
862 args.pa_flags |= FLAG_HAS_TARGET;
863 args.pa_required |= FLAG_HAS_PROCESS_PMCS;
866 case 'T': /* top mode */
867 args.pa_flags |= FLAG_DO_TOP;
868 args.pa_plugin = PMCSTAT_PL_CALLGRAPH;
869 args.pa_ctdumpinstr = 0;
870 args.pa_mergepmc = 1;
871 if (args.pa_printfile == stderr)
872 args.pa_printfile = stdout;
875 case 'v': /* verbose */
879 case 'w': /* wait interval */
880 interval = strtod(optarg, &end);
881 if (*end != '\0' || interval <= 0)
883 "ERROR: Illegal wait interval value \"%s\".",
885 args.pa_flags |= FLAG_HAS_WAIT_INTERVAL;
886 args.pa_interval = interval;
889 case 'W': /* toggle LOG_CSW */
890 do_logproccsw = !do_logproccsw;
891 args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
892 FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
896 graphdepth = strtod(optarg, &end);
897 if (*end != '\0' || graphdepth <= 0)
899 "ERROR: Illegal callchain depth \"%s\".",
901 args.pa_graphdepth = graphdepth;
902 args.pa_required |= FLAG_DO_CALLGRAPHS;
907 pmcstat_show_usage();
912 args.pa_argc = (argc -= optind);
913 args.pa_argv = (argv += optind);
915 /* If we read from logfile and no specified CPU mask use
916 * the maximum CPU count.
918 if ((args.pa_flags & FLAG_READ_LOGFILE) &&
919 (args.pa_flags & FLAGS_HAS_CPUMASK) == 0)
922 args.pa_cpumask = cpumask; /* For selecting CPUs using -R. */
924 if (argc) /* command line present */
925 args.pa_flags |= FLAG_HAS_COMMANDLINE;
927 if (args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS |
928 FLAG_DO_ANNOTATE | FLAG_DO_TOP))
929 args.pa_flags |= FLAG_DO_ANALYSIS;
932 * Check invocation syntax.
935 /* disallow -O and -R together */
936 if (args.pa_outputpath && args.pa_inputpath)
938 "ERROR: options -O and -R are mutually exclusive.");
940 /* disallow -T and -l together */
941 if ((args.pa_flags & FLAG_HAS_DURATION) &&
942 (args.pa_flags & FLAG_DO_TOP))
943 errx(EX_USAGE, "ERROR: options -T and -l are mutually "
946 /* -a and -m require -R */
947 if (args.pa_flags & FLAG_DO_ANNOTATE && args.pa_inputpath == NULL)
948 errx(EX_USAGE, "ERROR: option %s requires an input file",
949 args.pa_plugin == PMCSTAT_PL_ANNOTATE ? "-m" : "-a");
951 /* -m option is not allowed combined with -g or -G. */
952 if (args.pa_flags & FLAG_DO_ANNOTATE &&
953 args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS))
955 "ERROR: option -m and -g | -G are mutually exclusive");
957 if (args.pa_flags & FLAG_READ_LOGFILE) {
959 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
960 errmsg = "a command line specification";
961 else if (args.pa_flags & FLAG_HAS_TARGET)
962 errmsg = "option -t";
963 else if (!STAILQ_EMPTY(&args.pa_events))
964 errmsg = "a PMC event specification";
967 "ERROR: option -R may not be used with %s.",
969 } else if (STAILQ_EMPTY(&args.pa_events))
970 /* All other uses require a PMC spec. */
971 pmcstat_show_usage();
973 /* check for -t pid without a process PMC spec */
974 if ((args.pa_required & FLAG_HAS_TARGET) &&
975 (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
977 "ERROR: option -t requires a process mode PMC to be specified."
980 /* check for process-mode options without a command or -t pid */
981 if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
982 (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
984 "ERROR: options -d, -E, -p, -P, and -W require a command line or target process."
987 /* check for -p | -P without a target process of some sort */
988 if ((args.pa_required & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) &&
989 (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
991 "ERROR: options -P and -p require a target process or a command line."
994 /* check for process-mode options without a process-mode PMC */
995 if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
996 (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
998 "ERROR: options -d, -E, and -W require a process mode PMC to be specified."
1001 /* check for -c cpu with no system mode PMCs or logfile. */
1002 if ((args.pa_required & FLAG_HAS_SYSTEM_PMCS) &&
1003 (args.pa_flags & FLAG_HAS_SYSTEM_PMCS) == 0 &&
1004 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
1006 "ERROR: option -c requires at least one system mode PMC to be specified."
1009 /* check for counting mode options without a counting PMC */
1010 if ((args.pa_required & FLAG_HAS_COUNTING_PMCS) &&
1011 (args.pa_flags & FLAG_HAS_COUNTING_PMCS) == 0)
1013 "ERROR: options -C, -W and -o require at least one counting mode PMC to be specified."
1016 /* check for sampling mode options without a sampling PMC spec */
1017 if ((args.pa_required & FLAG_HAS_SAMPLING_PMCS) &&
1018 (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) == 0)
1020 "ERROR: options -N, -n and -O require at least one sampling mode PMC to be specified."
1023 /* check if -g/-G/-m/-T are being used correctly */
1024 if ((args.pa_flags & FLAG_DO_ANALYSIS) &&
1025 !(args.pa_flags & (FLAG_HAS_SAMPLING_PMCS|FLAG_READ_LOGFILE)))
1027 "ERROR: options -g/-G/-m/-T require sampling PMCs or -R to be specified."
1030 /* check if -e was specified without -g */
1031 if ((args.pa_flags & FLAG_DO_WIDE_GPROF_HC) &&
1032 !(args.pa_flags & FLAG_DO_GPROF))
1034 "ERROR: option -e requires gprof mode to be specified."
1037 /* check if -O was spuriously specified */
1038 if ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) &&
1039 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0)
1041 "ERROR: option -O is used only with options -E, -P, -S and -W."
1044 /* -k kernel path require -g/-G/-m/-T or -R */
1045 if ((args.pa_flags & FLAG_HAS_KERNELPATH) &&
1046 (args.pa_flags & FLAG_DO_ANALYSIS) == 0 &&
1047 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
1048 errx(EX_USAGE, "ERROR: option -k is only used with -g/-R/-m/-T.");
1050 /* -D only applies to gprof output mode (-g) */
1051 if ((args.pa_flags & FLAG_HAS_SAMPLESDIR) &&
1052 (args.pa_flags & FLAG_DO_GPROF) == 0)
1053 errx(EX_USAGE, "ERROR: option -D is only used with -g.");
1055 /* -M mapfile requires -g or -R */
1056 if (args.pa_mapfilename != NULL &&
1057 (args.pa_flags & FLAG_DO_GPROF) == 0 &&
1058 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
1059 errx(EX_USAGE, "ERROR: option -M is only used with -g/-R.");
1062 * Disallow textual output of sampling PMCs if counting PMCs
1063 * have also been asked for, mostly because the combined output
1064 * is difficult to make sense of.
1066 if ((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
1067 (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) &&
1068 ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) == 0))
1070 "ERROR: option -O is required if counting and sampling PMCs are specified together."
1074 * Check if 'kerneldir' refers to a file rather than a
1075 * directory. If so, use `dirname path` to determine the
1078 (void) snprintf(buffer, sizeof(buffer), "%s%s", args.pa_fsroot,
1080 if (stat(buffer, &sb) < 0)
1081 err(EX_OSERR, "ERROR: Cannot locate kernel \"%s\"",
1083 if (!S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode))
1084 errx(EX_USAGE, "ERROR: \"%s\": Unsupported file type.",
1086 if (!S_ISDIR(sb.st_mode)) {
1087 tmp = args.pa_kernel;
1088 args.pa_kernel = strdup(dirname(args.pa_kernel));
1090 (void) snprintf(buffer, sizeof(buffer), "%s%s",
1091 args.pa_fsroot, args.pa_kernel);
1092 if (stat(buffer, &sb) < 0)
1093 err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
1095 if (!S_ISDIR(sb.st_mode))
1097 "ERROR: \"%s\" is not a directory.",
1102 * If we have a callgraph be created, select the outputfile.
1104 if (args.pa_flags & FLAG_DO_CALLGRAPHS) {
1105 if (strcmp(graphfilename, "-") == 0)
1106 args.pa_graphfile = args.pa_printfile;
1108 args.pa_graphfile = fopen(graphfilename, "w");
1109 if (args.pa_graphfile == NULL)
1111 "ERROR: cannot open \"%s\" for writing",
1115 if (args.pa_flags & FLAG_DO_ANNOTATE) {
1116 args.pa_graphfile = fopen(graphfilename, "w");
1117 if (args.pa_graphfile == NULL)
1118 err(EX_OSERR, "ERROR: cannot open \"%s\" for writing",
1122 /* if we've been asked to process a log file, skip init */
1123 if ((args.pa_flags & FLAG_READ_LOGFILE) == 0) {
1126 "ERROR: Initialization of the pmc(3) library failed"
1129 if ((npmc = pmc_npmc(0)) < 0) /* assume all CPUs are identical */
1131 "ERROR: Cannot determine the number of PMCs on CPU %d",
1135 /* Allocate a kqueue */
1136 if ((pmcstat_kq = kqueue()) < 0)
1137 err(EX_OSERR, "ERROR: Cannot allocate kqueue");
1139 /* Setup the logfile as the source. */
1140 if (args.pa_flags & FLAG_READ_LOGFILE) {
1142 * Print the log in textual form if we haven't been
1143 * asked to generate profiling information.
1145 if ((args.pa_flags & FLAG_DO_ANALYSIS) == 0)
1146 args.pa_flags |= FLAG_DO_PRINT;
1148 pmcstat_initialize_logging();
1149 rfd = pmcstat_open_log(args.pa_inputpath,
1150 PMCSTAT_OPEN_FOR_READ);
1151 if ((args.pa_logparser = pmclog_open(rfd)) == NULL)
1152 err(EX_OSERR, "ERROR: Cannot create parser");
1153 if (fcntl(rfd, F_SETFL, O_NONBLOCK) < 0)
1154 err(EX_OSERR, "ERROR: fcntl(2) failed");
1155 EV_SET(&kev, rfd, EVFILT_READ, EV_ADD,
1157 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1158 err(EX_OSERR, "ERROR: Cannot register kevent");
1161 * Configure the specified log file or setup a default log
1162 * consumer via a pipe.
1164 if (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) {
1165 if (args.pa_outputpath)
1166 args.pa_logfd = pmcstat_open_log(args.pa_outputpath,
1167 PMCSTAT_OPEN_FOR_WRITE);
1170 * process the log on the fly by reading it in
1173 if (pipe(pipefd) < 0)
1174 err(EX_OSERR, "ERROR: pipe(2) failed");
1176 if (fcntl(pipefd[READPIPEFD], F_SETFL, O_NONBLOCK) < 0)
1177 err(EX_OSERR, "ERROR: fcntl(2) failed");
1179 EV_SET(&kev, pipefd[READPIPEFD], EVFILT_READ, EV_ADD,
1182 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1183 err(EX_OSERR, "ERROR: Cannot register kevent");
1185 args.pa_logfd = pipefd[WRITEPIPEFD];
1187 args.pa_flags |= FLAG_HAS_PIPE;
1188 if ((args.pa_flags & FLAG_DO_TOP) == 0)
1189 args.pa_flags |= FLAG_DO_PRINT;
1190 args.pa_logparser = pmclog_open(pipefd[READPIPEFD]);
1193 if (pmc_configure_logfile(args.pa_logfd) < 0)
1194 err(EX_OSERR, "ERROR: Cannot configure log file");
1197 /* remember to check for driver errors if we are sampling or logging */
1198 check_driver_stats = (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) ||
1199 (args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE);
1202 if (args.pa_flags & FLAG_READ_LOGFILE) {
1206 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
1207 if (pmc_allocate(ev->ev_spec, ev->ev_mode,
1208 ev->ev_flags, ev->ev_cpu, &ev->ev_pmcid) < 0)
1210 "ERROR: Cannot allocate %s-mode pmc with specification \"%s\"",
1211 PMC_IS_SYSTEM_MODE(ev->ev_mode) ?
1212 "system" : "process", ev->ev_spec);
1214 if (PMC_IS_SAMPLING_MODE(ev->ev_mode) &&
1215 pmc_set(ev->ev_pmcid, ev->ev_count) < 0)
1217 "ERROR: Cannot set sampling count for PMC \"%s\"",
1221 /* compute printout widths */
1222 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
1227 (void) pmc_width(ev->ev_pmcid, &counter_width);
1228 header_width = strlen(ev->ev_name) + 2; /* prefix '%c/' */
1229 display_width = (int) floor(counter_width / 3.32193) + 1;
1231 if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
1232 header_width += 3; /* 2 digit CPU number + '/' */
1234 if (header_width > display_width) {
1235 ev->ev_fieldskip = 0;
1236 ev->ev_fieldwidth = header_width;
1238 ev->ev_fieldskip = display_width -
1240 ev->ev_fieldwidth = display_width;
1245 * If our output is being set to a terminal, register a handler
1246 * for window size changes.
1249 if (isatty(fileno(args.pa_printfile))) {
1251 if (ioctl(fileno(args.pa_printfile), TIOCGWINSZ, &ws) < 0)
1252 err(EX_OSERR, "ERROR: Cannot determine window size");
1254 pmcstat_displayheight = ws.ws_row - 1;
1255 pmcstat_displaywidth = ws.ws_col - 1;
1257 EV_SET(&kev, SIGWINCH, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1259 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1261 "ERROR: Cannot register kevent for SIGWINCH");
1267 * Listen to key input in top mode.
1269 if (args.pa_flags & FLAG_DO_TOP) {
1270 EV_SET(&kev, fileno(stdin), EVFILT_READ, EV_ADD, 0, 0, NULL);
1271 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1272 err(EX_OSERR, "ERROR: Cannot register kevent");
1275 EV_SET(&kev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1276 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1277 err(EX_OSERR, "ERROR: Cannot register kevent for SIGINT");
1279 EV_SET(&kev, SIGIO, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1280 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1281 err(EX_OSERR, "ERROR: Cannot register kevent for SIGIO");
1284 * An exec() failure of a forked child is signalled by the
1285 * child sending the parent a SIGCHLD. We don't register an
1286 * actual signal handler for SIGCHLD, but instead use our
1287 * kqueue to pick up the signal.
1289 EV_SET(&kev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1290 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1291 err(EX_OSERR, "ERROR: Cannot register kevent for SIGCHLD");
1294 * Setup a timer if we have counting mode PMCs needing to be printed or
1295 * top mode plugin is active.
1297 if (((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
1298 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) ||
1299 (args.pa_flags & FLAG_DO_TOP)) {
1300 EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0,
1301 args.pa_interval * 1000, NULL);
1303 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1305 "ERROR: Cannot register kevent for timer");
1309 * Setup a duration timer if we have sampling mode PMCs and
1310 * a duration time is set
1312 if ((args.pa_flags & FLAG_HAS_SAMPLING_PMCS) &&
1313 (args.pa_flags & FLAG_HAS_DURATION)) {
1314 EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0,
1315 args.pa_duration * 1000, NULL);
1317 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1318 err(EX_OSERR, "ERROR: Cannot register kevent for "
1322 /* attach PMCs to the target process, starting it if specified */
1323 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1324 pmcstat_create_process();
1326 if (check_driver_stats && pmc_get_driver_stats(&ds_start) < 0)
1327 err(EX_OSERR, "ERROR: Cannot retrieve driver statistics");
1329 /* Attach process pmcs to the target process. */
1330 if (args.pa_flags & (FLAG_HAS_TARGET | FLAG_HAS_COMMANDLINE)) {
1331 if (SLIST_EMPTY(&args.pa_targets))
1333 "ERROR: No matching target processes.");
1334 if (args.pa_flags & FLAG_HAS_PROCESS_PMCS)
1335 pmcstat_attach_pmcs();
1338 kvm_close(pmcstat_kvm);
1343 /* start the pmcs */
1344 pmcstat_start_pmcs();
1346 /* start the (commandline) process if needed */
1347 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1348 pmcstat_start_process();
1350 /* initialize logging */
1351 pmcstat_initialize_logging();
1353 /* Handle SIGINT using the kqueue loop */
1354 sa.sa_handler = SIG_IGN;
1356 (void) sigemptyset(&sa.sa_mask);
1358 if (sigaction(SIGINT, &sa, NULL) < 0)
1359 err(EX_OSERR, "ERROR: Cannot install signal handler");
1362 * Setup the top mode display.
1364 if (args.pa_flags & FLAG_DO_TOP) {
1365 args.pa_flags &= ~FLAG_DO_PRINT;
1367 if (args.pa_toptty) {
1372 if(has_colors() == TRUE) {
1373 args.pa_topcolor = 1;
1375 use_default_colors();
1376 pair_content(0, &cf, &cb);
1377 init_pair(1, COLOR_RED, cb);
1378 init_pair(2, COLOR_YELLOW, cb);
1379 init_pair(3, COLOR_GREEN, cb);
1385 intrflush(stdscr, FALSE);
1386 keypad(stdscr, TRUE);
1388 /* Get terminal width / height with ncurses. */
1390 pmcstat_displayheight, pmcstat_displaywidth);
1391 pmcstat_displayheight--; pmcstat_displaywidth--;
1392 atexit(pmcstat_topexit);
1397 * loop till either the target process (if any) exits, or we
1398 * are killed by a SIGINT or we reached the time duration.
1400 runstate = PMCSTAT_RUNNING;
1401 do_print = do_read = 0;
1403 if ((c = kevent(pmcstat_kq, NULL, 0, &kev, 1, NULL)) <= 0) {
1405 err(EX_OSERR, "ERROR: kevent failed");
1410 if (kev.flags & EV_ERROR)
1411 errc(EX_OSERR, kev.data, "ERROR: kevent failed");
1413 switch (kev.filter) {
1414 case EVFILT_PROC: /* target has exited */
1415 runstate = pmcstat_close_log();
1419 case EVFILT_READ: /* log file data is present */
1420 if (kev.ident == (unsigned)fileno(stdin) &&
1421 (args.pa_flags & FLAG_DO_TOP)) {
1422 if (pmcstat_keypress_log())
1423 runstate = pmcstat_close_log();
1426 runstate = pmcstat_process_log();
1431 if (kev.ident == SIGCHLD) {
1433 * The child process sends us a
1434 * SIGCHLD if its exec() failed. We
1435 * wait for it to exit and then exit
1439 runstate = PMCSTAT_FINISHED;
1440 } else if (kev.ident == SIGIO) {
1442 * We get a SIGIO if a PMC loses all
1443 * of its targets, or if logfile
1444 * writes encounter an error.
1446 runstate = pmcstat_close_log();
1447 do_print = 1; /* print PMCs at exit */
1448 } else if (kev.ident == SIGINT) {
1449 /* Kill the child process if we started it */
1450 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1451 pmcstat_kill_process();
1452 runstate = pmcstat_close_log();
1453 } else if (kev.ident == SIGWINCH) {
1454 if (ioctl(fileno(args.pa_printfile),
1455 TIOCGWINSZ, &ws) < 0)
1457 "ERROR: Cannot determine window size");
1458 pmcstat_displayheight = ws.ws_row - 1;
1459 pmcstat_displaywidth = ws.ws_col - 1;
1466 /* time duration reached, exit */
1467 if (args.pa_flags & FLAG_HAS_DURATION) {
1468 runstate = PMCSTAT_FINISHED;
1471 /* print out counting PMCs */
1472 if ((args.pa_flags & FLAG_DO_TOP) &&
1473 pmc_flush_logfile() == 0)
1480 if (do_print && !do_read) {
1481 if ((args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) {
1482 pmcstat_print_pmcs();
1483 if (runstate == PMCSTAT_FINISHED &&
1485 (args.pa_flags & FLAG_DO_PRINT) == 0)
1486 (void) fprintf(args.pa_printfile, "\n");
1488 if (args.pa_flags & FLAG_DO_TOP)
1489 pmcstat_display_log();
1493 } while (runstate != PMCSTAT_FINISHED);
1495 if ((args.pa_flags & FLAG_DO_TOP) && args.pa_toptty) {
1500 /* flush any pending log entries */
1501 if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE))
1502 pmc_close_logfile();
1506 free(args.pa_kernel);
1508 /* check if the driver lost any samples or events */
1509 if (check_driver_stats) {
1510 if (pmc_get_driver_stats(&ds_end) < 0)
1512 "ERROR: Cannot retrieve driver statistics");
1513 if (ds_start.pm_intr_bufferfull != ds_end.pm_intr_bufferfull &&
1514 args.pa_verbosity > 0)
1516 "WARNING: sampling was paused at least %u time%s.\n"
1517 "Please consider tuning the \"kern.hwpmc.nsamples\" tunable.",
1518 ds_end.pm_intr_bufferfull -
1519 ds_start.pm_intr_bufferfull,
1520 ((ds_end.pm_intr_bufferfull -
1521 ds_start.pm_intr_bufferfull) != 1) ? "s" : ""
1523 if (ds_start.pm_buffer_requests_failed !=
1524 ds_end.pm_buffer_requests_failed &&
1525 args.pa_verbosity > 0)
1527 "WARNING: at least %u event%s were discarded while running.\n"
1528 "Please consider tuning the \"kern.hwpmc.nbuffers\" tunable.",
1529 ds_end.pm_buffer_requests_failed -
1530 ds_start.pm_buffer_requests_failed,
1531 ((ds_end.pm_buffer_requests_failed -
1532 ds_start.pm_buffer_requests_failed) != 1) ? "s" : ""