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 if (ev_clone->ev_name == NULL)
137 errx(EX_SOFTWARE, "ERROR: Out of memory");
138 ev_clone->ev_pmcid = ev->ev_pmcid;
139 ev_clone->ev_saved = ev->ev_saved;
140 ev_clone->ev_spec = strdup(ev->ev_spec);
141 if (ev_clone->ev_spec == NULL)
142 errx(EX_SOFTWARE, "ERROR: Out of memory");
144 STAILQ_INSERT_TAIL(&args.pa_events, ev_clone, ev_next);
149 pmcstat_get_cpumask(const char *cpuspec, cpuset_t *cpumask)
159 cpu = strtol(s, &end, 0);
160 if (cpu < 0 || end == s)
162 "ERROR: Illegal CPU specification \"%s\".",
164 CPU_SET(cpu, cpumask);
165 s = end + strspn(end, ", \t");
167 assert(!CPU_EMPTY(cpumask));
171 pmcstat_attach_pmcs(void)
173 struct pmcstat_ev *ev;
174 struct pmcstat_target *pt;
177 /* Attach all process PMCs to target processes. */
179 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
180 if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
182 SLIST_FOREACH(pt, &args.pa_targets, pt_next)
183 if (pmc_attach(ev->ev_pmcid, pt->pt_pid) == 0)
185 else if (errno != ESRCH)
187 "ERROR: cannot attach pmc \"%s\" to process %d",
188 ev->ev_name, (int)pt->pt_pid);
192 errx(EX_DATAERR, "ERROR: No processes were attached to.");
197 pmcstat_cleanup(void)
199 struct pmcstat_ev *ev;
201 /* release allocated PMCs. */
202 STAILQ_FOREACH(ev, &args.pa_events, ev_next)
203 if (ev->ev_pmcid != PMC_ID_INVALID) {
204 if (pmc_stop(ev->ev_pmcid) < 0)
206 "ERROR: cannot stop pmc 0x%x \"%s\"",
207 ev->ev_pmcid, ev->ev_name);
208 if (pmc_release(ev->ev_pmcid) < 0)
210 "ERROR: cannot release pmc 0x%x \"%s\"",
211 ev->ev_pmcid, ev->ev_name);
214 /* de-configure the log file if present. */
215 if (args.pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))
216 (void) pmc_configure_logfile(-1);
218 if (args.pa_logparser) {
219 pmclog_close(args.pa_logparser);
220 args.pa_logparser = NULL;
223 pmcstat_shutdown_logging();
227 pmcstat_create_process(void)
232 struct pmcstat_target *pt;
234 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pmcstat_sockpair) < 0)
235 err(EX_OSERR, "ERROR: cannot create socket pair");
237 switch (pid = fork()) {
239 err(EX_OSERR, "ERROR: cannot fork");
243 (void) close(pmcstat_sockpair[PARENTSOCKET]);
245 /* Write a token to tell our parent we've started executing. */
246 if (write(pmcstat_sockpair[CHILDSOCKET], "+", 1) != 1)
247 err(EX_OSERR, "ERROR (child): cannot write token");
249 /* Wait for our parent to signal us to start. */
250 if (read(pmcstat_sockpair[CHILDSOCKET], &token, 1) < 0)
251 err(EX_OSERR, "ERROR (child): cannot read token");
252 (void) close(pmcstat_sockpair[CHILDSOCKET]);
254 /* exec() the program requested */
255 execvp(*args.pa_argv, args.pa_argv);
256 /* and if that fails, notify the parent */
257 kill(getppid(), SIGCHLD);
258 err(EX_OSERR, "ERROR: execvp \"%s\" failed", *args.pa_argv);
261 default: /* parent */
262 (void) close(pmcstat_sockpair[CHILDSOCKET]);
266 /* Ask to be notified via a kevent when the target process exits. */
267 EV_SET(&kev, pid, EVFILT_PROC, EV_ADD|EV_ONESHOT, NOTE_EXIT, 0,
269 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
270 err(EX_OSERR, "ERROR: cannot monitor child process %d", pid);
272 if ((pt = malloc(sizeof(*pt))) == NULL)
273 errx(EX_SOFTWARE, "ERROR: Out of memory.");
276 SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
278 /* Wait for the child to signal that its ready to go. */
279 if (read(pmcstat_sockpair[PARENTSOCKET], &token, 1) < 0)
280 err(EX_OSERR, "ERROR (parent): cannot read token");
286 pmcstat_find_targets(const char *spec)
288 int n, nproc, pid, rv;
289 struct pmcstat_target *pt;
290 char errbuf[_POSIX2_LINE_MAX], *end;
291 static struct kinfo_proc *kp;
295 /* First check if we've been given a process id. */
296 pid = strtol(spec, &end, 0);
297 if (end != spec && pid >= 0) {
298 if ((pt = malloc(sizeof(*pt))) == NULL)
301 SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
305 /* Otherwise treat arg as a regular expression naming processes. */
306 if (pmcstat_kvm == NULL) {
307 if ((pmcstat_kvm = kvm_openfiles(NULL, "/dev/null", NULL, 0,
309 err(EX_OSERR, "ERROR: Cannot open kernel \"%s\"",
311 if ((pmcstat_plist = kvm_getprocs(pmcstat_kvm, KERN_PROC_PROC,
313 err(EX_OSERR, "ERROR: Cannot get process list: %s",
314 kvm_geterr(pmcstat_kvm));
318 if ((rv = regcomp(®, spec, REG_EXTENDED|REG_NOSUB)) != 0) {
319 regerror(rv, ®, errbuf, sizeof(errbuf));
320 err(EX_DATAERR, "ERROR: Failed to compile regex \"%s\": %s",
324 for (n = 0, kp = pmcstat_plist; n < nproc; n++, kp++) {
325 if ((rv = regexec(®, kp->ki_comm, 1, ®match, 0)) == 0) {
326 if ((pt = malloc(sizeof(*pt))) == NULL)
328 pt->pt_pid = kp->ki_pid;
329 SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
330 } else if (rv != REG_NOMATCH) {
331 regerror(rv, ®, errbuf, sizeof(errbuf));
332 errx(EX_SOFTWARE, "ERROR: Regex evalation failed: %s",
342 errx(EX_SOFTWARE, "Out of memory.");
347 pmcstat_kill_process(void)
349 struct pmcstat_target *pt;
351 assert(args.pa_flags & FLAG_HAS_COMMANDLINE);
354 * If a command line was specified, it would be the very first
355 * in the list, before any other processes specified by -t.
357 pt = SLIST_FIRST(&args.pa_targets);
360 if (kill(pt->pt_pid, SIGINT) != 0)
361 err(EX_OSERR, "ERROR: cannot signal child process");
365 pmcstat_start_pmcs(void)
367 struct pmcstat_ev *ev;
369 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
371 assert(ev->ev_pmcid != PMC_ID_INVALID);
373 if (pmc_start(ev->ev_pmcid) < 0) {
374 warn("ERROR: Cannot start pmc 0x%x \"%s\"",
375 ev->ev_pmcid, ev->ev_name);
384 pmcstat_print_headers(void)
386 struct pmcstat_ev *ev;
389 (void) fprintf(args.pa_printfile, PRINT_HEADER_PREFIX);
391 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
392 if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
395 c = PMC_IS_SYSTEM_MODE(ev->ev_mode) ? 's' : 'p';
397 if (ev->ev_fieldskip != 0)
398 (void) fprintf(args.pa_printfile, "%*s",
399 ev->ev_fieldskip, "");
400 w = ev->ev_fieldwidth - ev->ev_fieldskip - 2;
403 (void) fprintf(args.pa_printfile, "s/%02d/%-*s ",
404 ev->ev_cpu, w-3, ev->ev_name);
406 (void) fprintf(args.pa_printfile, "p/%*s ", w,
410 (void) fflush(args.pa_printfile);
414 pmcstat_print_counters(void)
417 struct pmcstat_ev *ev;
420 extra_width = sizeof(PRINT_HEADER_PREFIX) - 1;
422 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
424 /* skip sampling mode counters */
425 if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
428 if (pmc_read(ev->ev_pmcid, &value) < 0)
429 err(EX_OSERR, "ERROR: Cannot read pmc \"%s\"",
432 (void) fprintf(args.pa_printfile, "%*ju ",
433 ev->ev_fieldwidth + extra_width,
434 (uintmax_t) ev->ev_cumulative ? value :
435 (value - ev->ev_saved));
437 if (ev->ev_cumulative == 0)
438 ev->ev_saved = value;
442 (void) fflush(args.pa_printfile);
450 pmcstat_print_pmcs(void)
452 static int linecount = 0;
454 /* check if we need to print a header line */
455 if (++linecount > pmcstat_displayheight) {
456 (void) fprintf(args.pa_printfile, "\n");
460 pmcstat_print_headers();
461 (void) fprintf(args.pa_printfile, "\n");
463 pmcstat_print_counters();
469 * Do process profiling
471 * If a pid was specified, attach each allocated PMC to the target
472 * process. Otherwise, fork a child and attach the PMCs to the child,
473 * and have the child exec() the target program.
477 pmcstat_start_process(void)
479 /* Signal the child to proceed. */
480 if (write(pmcstat_sockpair[PARENTSOCKET], "!", 1) != 1)
481 err(EX_OSERR, "ERROR (parent): write of token failed");
483 (void) close(pmcstat_sockpair[PARENTSOCKET]);
487 pmcstat_show_usage(void)
490 "[options] [commandline]\n"
491 "\t Measure process and/or system performance using hardware\n"
492 "\t performance monitoring counters.\n"
493 "\t Options include:\n"
494 "\t -C\t\t (toggle) show cumulative counts\n"
495 "\t -D path\t create profiles in directory \"path\"\n"
496 "\t -E\t\t (toggle) show counts at process exit\n"
497 "\t -F file\t write a system-wide callgraph (Kcachegrind format)"
499 "\t -G file\t write a system-wide callgraph to \"file\"\n"
500 "\t -M file\t print executable/gmon file map to \"file\"\n"
501 "\t -N\t\t (toggle) capture callchains\n"
502 "\t -O file\t send log output to \"file\"\n"
503 "\t -P spec\t allocate a process-private sampling PMC\n"
504 "\t -R file\t read events from \"file\"\n"
505 "\t -S spec\t allocate a system-wide sampling PMC\n"
506 "\t -T\t\t start in top mode\n"
507 "\t -W\t\t (toggle) show counts per context switch\n"
508 "\t -a file\t print sampled PCs and callgraph to \"file\"\n"
509 "\t -c cpu-list\t set cpus for subsequent system-wide PMCs\n"
510 "\t -d\t\t (toggle) track descendants\n"
511 "\t -e\t\t use wide history counter for gprof(1) output\n"
512 "\t -f spec\t pass \"spec\" to as plugin option\n"
513 "\t -g\t\t produce gprof(1) compatible profiles\n"
514 "\t -k dir\t\t set the path to the kernel\n"
515 "\t -l secs\t set duration time\n"
516 "\t -m file\t print sampled PCs to \"file\"\n"
517 "\t -n rate\t set sampling rate\n"
518 "\t -o file\t send print output to \"file\"\n"
519 "\t -p spec\t allocate a process-private counting PMC\n"
520 "\t -q\t\t suppress verbosity\n"
521 "\t -r fsroot\t specify FS root directory\n"
522 "\t -s spec\t allocate a system-wide counting PMC\n"
523 "\t -t process-spec attach to running processes matching "
525 "\t -v\t\t increase verbosity\n"
526 "\t -w secs\t set printing time interval\n"
527 "\t -z depth\t limit callchain display depth"
532 * At exit handler for top mode
536 pmcstat_topexit(void)
554 main(int argc, char **argv)
556 cpuset_t cpumask, rootmask;
560 int c, check_driver_stats, current_sampling_count;
561 int do_callchain, do_descendants, do_logproccsw, do_logprocexit;
562 int do_print, do_read;
566 int use_cumulative_counts;
569 const char *errmsg, *graphfilename;
570 enum pmcstat_state runstate;
571 struct pmc_driverstats ds_start, ds_end;
572 struct pmcstat_ev *ev;
577 char buffer[PATH_MAX];
579 check_driver_stats = 0;
580 current_sampling_count = DEFAULT_SAMPLE_COUNT;
585 use_cumulative_counts = 0;
587 args.pa_required = 0;
589 args.pa_verbosity = 1;
592 args.pa_samplesdir = ".";
593 args.pa_printfile = stderr;
594 args.pa_graphdepth = DEFAULT_CALLGRAPH_DEPTH;
595 args.pa_graphfile = NULL;
596 args.pa_interval = DEFAULT_WAIT_INTERVAL;
597 args.pa_mapfilename = NULL;
598 args.pa_inputpath = NULL;
599 args.pa_outputpath = NULL;
600 args.pa_pplugin = PMCSTAT_PL_NONE;
601 args.pa_plugin = PMCSTAT_PL_NONE;
602 args.pa_ctdumpinstr = 1;
603 args.pa_topmode = PMCSTAT_TOP_DELTA;
605 args.pa_topcolor = 0;
606 args.pa_mergepmc = 0;
607 args.pa_duration = 0.0;
608 STAILQ_INIT(&args.pa_events);
609 SLIST_INIT(&args.pa_targets);
610 bzero(&ds_start, sizeof(ds_start));
611 bzero(&ds_end, sizeof(ds_end));
615 /* Default to using the running system kernel. */
617 if (sysctlbyname("kern.bootfile", NULL, &len, NULL, 0) == -1)
618 err(EX_OSERR, "ERROR: Cannot determine path of running kernel");
619 args.pa_kernel = malloc(len);
620 if (args.pa_kernel == NULL)
621 errx(EX_SOFTWARE, "ERROR: Out of memory.");
622 if (sysctlbyname("kern.bootfile", args.pa_kernel, &len, NULL, 0) == -1)
623 err(EX_OSERR, "ERROR: Cannot determine path of running kernel");
626 * The initial CPU mask specifies the root mask of this process
627 * which is usually all CPUs in the system.
629 if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
630 sizeof(rootmask), &rootmask) == -1)
631 err(EX_OSERR, "ERROR: Cannot determine the root set of CPUs");
632 CPU_COPY(&rootmask, &cpumask);
634 while ((option = getopt(argc, argv,
635 "CD:EF:G:M:NO:P:R:S:TWa:c:def:gk:l:m:n:o:p:qr:s:t:vw:z:")) != -1)
637 case 'a': /* Annotate + callgraph */
638 args.pa_flags |= FLAG_DO_ANNOTATE;
639 args.pa_plugin = PMCSTAT_PL_ANNOTATE_CG;
640 graphfilename = optarg;
643 case 'C': /* cumulative values */
644 use_cumulative_counts = !use_cumulative_counts;
645 args.pa_required |= FLAG_HAS_COUNTING_PMCS;
649 if (optarg[0] == '*' && optarg[1] == '\0')
650 CPU_COPY(&rootmask, &cpumask);
652 pmcstat_get_cpumask(optarg, &cpumask);
654 args.pa_flags |= FLAGS_HAS_CPUMASK;
655 args.pa_required |= FLAG_HAS_SYSTEM_PMCS;
659 if (stat(optarg, &sb) < 0)
660 err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
662 if (!S_ISDIR(sb.st_mode))
664 "ERROR: \"%s\" is not a directory.",
666 args.pa_samplesdir = optarg;
667 args.pa_flags |= FLAG_HAS_SAMPLESDIR;
668 args.pa_required |= FLAG_DO_GPROF;
671 case 'd': /* toggle descendents */
672 do_descendants = !do_descendants;
673 args.pa_required |= FLAG_HAS_PROCESS_PMCS;
676 case 'e': /* wide gprof metrics */
677 args.pa_flags |= FLAG_DO_WIDE_GPROF_HC;
680 case 'F': /* produce a system-wide calltree */
681 args.pa_flags |= FLAG_DO_CALLGRAPHS;
682 args.pa_plugin = PMCSTAT_PL_CALLTREE;
683 graphfilename = optarg;
686 case 'f': /* plugins options */
687 if (args.pa_plugin == PMCSTAT_PL_NONE)
688 err(EX_USAGE, "ERROR: Need -g/-G/-m/-T.");
689 pmcstat_pluginconfigure_log(optarg);
692 case 'G': /* produce a system-wide callgraph */
693 args.pa_flags |= FLAG_DO_CALLGRAPHS;
694 args.pa_plugin = PMCSTAT_PL_CALLGRAPH;
695 graphfilename = optarg;
698 case 'g': /* produce gprof compatible profiles */
699 args.pa_flags |= FLAG_DO_GPROF;
700 args.pa_pplugin = PMCSTAT_PL_CALLGRAPH;
701 args.pa_plugin = PMCSTAT_PL_GPROF;
704 case 'k': /* pathname to the kernel */
705 free(args.pa_kernel);
706 args.pa_kernel = strdup(optarg);
707 if (args.pa_kernel == NULL)
708 errx(EX_SOFTWARE, "ERROR: Out of memory");
709 args.pa_required |= FLAG_DO_ANALYSIS;
710 args.pa_flags |= FLAG_HAS_KERNELPATH;
713 case 'l': /* time duration in seconds */
714 duration = strtod(optarg, &end);
715 if (*end != '\0' || duration <= 0)
716 errx(EX_USAGE, "ERROR: Illegal duration time "
717 "value \"%s\".", optarg);
718 args.pa_flags |= FLAG_HAS_DURATION;
719 args.pa_duration = duration;
723 args.pa_flags |= FLAG_DO_ANNOTATE;
724 args.pa_plugin = PMCSTAT_PL_ANNOTATE;
725 graphfilename = optarg;
728 case 'E': /* log process exit */
729 do_logprocexit = !do_logprocexit;
730 args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
731 FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
734 case 'M': /* mapfile */
735 args.pa_mapfilename = optarg;
739 do_callchain = !do_callchain;
740 args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
743 case 'p': /* process virtual counting PMC */
744 case 's': /* system-wide counting PMC */
745 case 'P': /* process virtual sampling PMC */
746 case 'S': /* system-wide sampling PMC */
747 if ((ev = malloc(sizeof(*ev))) == NULL)
748 errx(EX_SOFTWARE, "ERROR: Out of memory.");
751 case 'p': ev->ev_mode = PMC_MODE_TC; break;
752 case 's': ev->ev_mode = PMC_MODE_SC; break;
753 case 'P': ev->ev_mode = PMC_MODE_TS; break;
754 case 'S': ev->ev_mode = PMC_MODE_SS; break;
757 if (option == 'P' || option == 'p') {
758 args.pa_flags |= FLAG_HAS_PROCESS_PMCS;
759 args.pa_required |= (FLAG_HAS_COMMANDLINE |
763 if (option == 'P' || option == 'S') {
764 args.pa_flags |= FLAG_HAS_SAMPLING_PMCS;
765 args.pa_required |= (FLAG_HAS_PIPE |
766 FLAG_HAS_OUTPUT_LOGFILE);
769 if (option == 'p' || option == 's')
770 args.pa_flags |= FLAG_HAS_COUNTING_PMCS;
772 if (option == 's' || option == 'S')
773 args.pa_flags |= FLAG_HAS_SYSTEM_PMCS;
775 ev->ev_spec = strdup(optarg);
776 if (ev->ev_spec == NULL)
777 errx(EX_SOFTWARE, "ERROR: Out of memory.");
779 if (option == 'S' || option == 'P')
780 ev->ev_count = current_sampling_count;
784 if (option == 'S' || option == 's')
785 ev->ev_cpu = CPU_FFS(&cpumask) - 1;
787 ev->ev_cpu = PMC_CPU_ANY;
791 ev->ev_flags |= PMC_F_CALLCHAIN;
793 ev->ev_flags |= PMC_F_DESCENDANTS;
795 ev->ev_flags |= PMC_F_LOG_PROCEXIT;
797 ev->ev_flags |= PMC_F_LOG_PROCCSW;
799 ev->ev_cumulative = use_cumulative_counts;
802 ev->ev_pmcid = PMC_ID_INVALID;
804 /* extract event name */
805 c = strcspn(optarg, ", \t");
806 ev->ev_name = malloc(c + 1);
807 if (ev->ev_name == NULL)
808 errx(EX_SOFTWARE, "ERROR: Out of memory.");
809 (void) strncpy(ev->ev_name, optarg, c);
810 *(ev->ev_name + c) = '\0';
812 STAILQ_INSERT_TAIL(&args.pa_events, ev, ev_next);
814 if (option == 's' || option == 'S') {
815 CPU_CLR(ev->ev_cpu, &cpumask);
816 pmcstat_clone_event_descriptor(ev, &cpumask);
817 CPU_SET(ev->ev_cpu, &cpumask);
822 case 'n': /* sampling count */
823 current_sampling_count = strtol(optarg, &end, 0);
824 if (*end != '\0' || current_sampling_count <= 0)
826 "ERROR: Illegal count value \"%s\".",
828 args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
831 case 'o': /* outputfile */
832 if (args.pa_printfile != NULL &&
833 args.pa_printfile != stdout &&
834 args.pa_printfile != stderr)
835 (void) fclose(args.pa_printfile);
836 if ((args.pa_printfile = fopen(optarg, "w")) == NULL)
838 "ERROR: cannot open \"%s\" for writing.",
840 args.pa_flags |= FLAG_DO_PRINT;
843 case 'O': /* sampling output */
844 if (args.pa_outputpath)
846 "ERROR: option -O may only be specified once.");
847 args.pa_outputpath = optarg;
848 args.pa_flags |= FLAG_HAS_OUTPUT_LOGFILE;
851 case 'q': /* quiet mode */
852 args.pa_verbosity = 0;
855 case 'r': /* root FS path */
856 args.pa_fsroot = optarg;
859 case 'R': /* read an existing log file */
860 if (args.pa_inputpath != NULL)
862 "ERROR: option -R may only be specified once.");
863 args.pa_inputpath = optarg;
864 if (args.pa_printfile == stderr)
865 args.pa_printfile = stdout;
866 args.pa_flags |= FLAG_READ_LOGFILE;
869 case 't': /* target pid or process name */
870 pmcstat_find_targets(optarg);
872 args.pa_flags |= FLAG_HAS_TARGET;
873 args.pa_required |= FLAG_HAS_PROCESS_PMCS;
876 case 'T': /* top mode */
877 args.pa_flags |= FLAG_DO_TOP;
878 args.pa_plugin = PMCSTAT_PL_CALLGRAPH;
879 args.pa_ctdumpinstr = 0;
880 args.pa_mergepmc = 1;
881 if (args.pa_printfile == stderr)
882 args.pa_printfile = stdout;
885 case 'v': /* verbose */
889 case 'w': /* wait interval */
890 interval = strtod(optarg, &end);
891 if (*end != '\0' || interval <= 0)
893 "ERROR: Illegal wait interval value \"%s\".",
895 args.pa_flags |= FLAG_HAS_WAIT_INTERVAL;
896 args.pa_interval = interval;
899 case 'W': /* toggle LOG_CSW */
900 do_logproccsw = !do_logproccsw;
901 args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
902 FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
906 graphdepth = strtod(optarg, &end);
907 if (*end != '\0' || graphdepth <= 0)
909 "ERROR: Illegal callchain depth \"%s\".",
911 args.pa_graphdepth = graphdepth;
912 args.pa_required |= FLAG_DO_CALLGRAPHS;
917 pmcstat_show_usage();
922 args.pa_argc = (argc -= optind);
923 args.pa_argv = (argv += optind);
925 /* If we read from logfile and no specified CPU mask use
926 * the maximum CPU count.
928 if ((args.pa_flags & FLAG_READ_LOGFILE) &&
929 (args.pa_flags & FLAGS_HAS_CPUMASK) == 0)
932 args.pa_cpumask = cpumask; /* For selecting CPUs using -R. */
934 if (argc) /* command line present */
935 args.pa_flags |= FLAG_HAS_COMMANDLINE;
937 if (args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS |
938 FLAG_DO_ANNOTATE | FLAG_DO_TOP))
939 args.pa_flags |= FLAG_DO_ANALYSIS;
942 * Check invocation syntax.
945 /* disallow -O and -R together */
946 if (args.pa_outputpath && args.pa_inputpath)
948 "ERROR: options -O and -R are mutually exclusive.");
950 /* disallow -T and -l together */
951 if ((args.pa_flags & FLAG_HAS_DURATION) &&
952 (args.pa_flags & FLAG_DO_TOP))
953 errx(EX_USAGE, "ERROR: options -T and -l are mutually "
956 /* -a and -m require -R */
957 if (args.pa_flags & FLAG_DO_ANNOTATE && args.pa_inputpath == NULL)
958 errx(EX_USAGE, "ERROR: option %s requires an input file",
959 args.pa_plugin == PMCSTAT_PL_ANNOTATE ? "-m" : "-a");
961 /* -m option is not allowed combined with -g or -G. */
962 if (args.pa_flags & FLAG_DO_ANNOTATE &&
963 args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS))
965 "ERROR: option -m and -g | -G are mutually exclusive");
967 if (args.pa_flags & FLAG_READ_LOGFILE) {
969 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
970 errmsg = "a command line specification";
971 else if (args.pa_flags & FLAG_HAS_TARGET)
972 errmsg = "option -t";
973 else if (!STAILQ_EMPTY(&args.pa_events))
974 errmsg = "a PMC event specification";
977 "ERROR: option -R may not be used with %s.",
979 } else if (STAILQ_EMPTY(&args.pa_events))
980 /* All other uses require a PMC spec. */
981 pmcstat_show_usage();
983 /* check for -t pid without a process PMC spec */
984 if ((args.pa_required & FLAG_HAS_TARGET) &&
985 (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
987 "ERROR: option -t requires a process mode PMC to be specified."
990 /* check for process-mode options without a command or -t pid */
991 if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
992 (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
994 "ERROR: options -d, -E, -p, -P, and -W require a command line or target process."
997 /* check for -p | -P without a target process of some sort */
998 if ((args.pa_required & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) &&
999 (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
1001 "ERROR: options -P and -p require a target process or a command line."
1004 /* check for process-mode options without a process-mode PMC */
1005 if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
1006 (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
1008 "ERROR: options -d, -E, and -W require a process mode PMC to be specified."
1011 /* check for -c cpu with no system mode PMCs or logfile. */
1012 if ((args.pa_required & FLAG_HAS_SYSTEM_PMCS) &&
1013 (args.pa_flags & FLAG_HAS_SYSTEM_PMCS) == 0 &&
1014 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
1016 "ERROR: option -c requires at least one system mode PMC to be specified."
1019 /* check for counting mode options without a counting PMC */
1020 if ((args.pa_required & FLAG_HAS_COUNTING_PMCS) &&
1021 (args.pa_flags & FLAG_HAS_COUNTING_PMCS) == 0)
1023 "ERROR: options -C, -W and -o require at least one counting mode PMC to be specified."
1026 /* check for sampling mode options without a sampling PMC spec */
1027 if ((args.pa_required & FLAG_HAS_SAMPLING_PMCS) &&
1028 (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) == 0)
1030 "ERROR: options -N, -n and -O require at least one sampling mode PMC to be specified."
1033 /* check if -g/-G/-m/-T are being used correctly */
1034 if ((args.pa_flags & FLAG_DO_ANALYSIS) &&
1035 !(args.pa_flags & (FLAG_HAS_SAMPLING_PMCS|FLAG_READ_LOGFILE)))
1037 "ERROR: options -g/-G/-m/-T require sampling PMCs or -R to be specified."
1040 /* check if -e was specified without -g */
1041 if ((args.pa_flags & FLAG_DO_WIDE_GPROF_HC) &&
1042 !(args.pa_flags & FLAG_DO_GPROF))
1044 "ERROR: option -e requires gprof mode to be specified."
1047 /* check if -O was spuriously specified */
1048 if ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) &&
1049 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0)
1051 "ERROR: option -O is used only with options -E, -P, -S and -W."
1054 /* -k kernel path require -g/-G/-m/-T or -R */
1055 if ((args.pa_flags & FLAG_HAS_KERNELPATH) &&
1056 (args.pa_flags & FLAG_DO_ANALYSIS) == 0 &&
1057 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
1058 errx(EX_USAGE, "ERROR: option -k is only used with -g/-R/-m/-T.");
1060 /* -D only applies to gprof output mode (-g) */
1061 if ((args.pa_flags & FLAG_HAS_SAMPLESDIR) &&
1062 (args.pa_flags & FLAG_DO_GPROF) == 0)
1063 errx(EX_USAGE, "ERROR: option -D is only used with -g.");
1065 /* -M mapfile requires -g or -R */
1066 if (args.pa_mapfilename != NULL &&
1067 (args.pa_flags & FLAG_DO_GPROF) == 0 &&
1068 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
1069 errx(EX_USAGE, "ERROR: option -M is only used with -g/-R.");
1072 * Disallow textual output of sampling PMCs if counting PMCs
1073 * have also been asked for, mostly because the combined output
1074 * is difficult to make sense of.
1076 if ((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
1077 (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) &&
1078 ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) == 0))
1080 "ERROR: option -O is required if counting and sampling PMCs are specified together."
1084 * Check if 'kerneldir' refers to a file rather than a
1085 * directory. If so, use `dirname path` to determine the
1088 (void) snprintf(buffer, sizeof(buffer), "%s%s", args.pa_fsroot,
1090 if (stat(buffer, &sb) < 0)
1091 err(EX_OSERR, "ERROR: Cannot locate kernel \"%s\"",
1093 if (!S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode))
1094 errx(EX_USAGE, "ERROR: \"%s\": Unsupported file type.",
1096 if (!S_ISDIR(sb.st_mode)) {
1097 tmp = args.pa_kernel;
1098 args.pa_kernel = strdup(dirname(args.pa_kernel));
1099 if (args.pa_kernel == NULL)
1100 errx(EX_SOFTWARE, "ERROR: Out of memory");
1102 (void) snprintf(buffer, sizeof(buffer), "%s%s",
1103 args.pa_fsroot, args.pa_kernel);
1104 if (stat(buffer, &sb) < 0)
1105 err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
1107 if (!S_ISDIR(sb.st_mode))
1109 "ERROR: \"%s\" is not a directory.",
1114 * If we have a callgraph be created, select the outputfile.
1116 if (args.pa_flags & FLAG_DO_CALLGRAPHS) {
1117 if (strcmp(graphfilename, "-") == 0)
1118 args.pa_graphfile = args.pa_printfile;
1120 args.pa_graphfile = fopen(graphfilename, "w");
1121 if (args.pa_graphfile == NULL)
1123 "ERROR: cannot open \"%s\" for writing",
1127 if (args.pa_flags & FLAG_DO_ANNOTATE) {
1128 args.pa_graphfile = fopen(graphfilename, "w");
1129 if (args.pa_graphfile == NULL)
1130 err(EX_OSERR, "ERROR: cannot open \"%s\" for writing",
1134 /* if we've been asked to process a log file, skip init */
1135 if ((args.pa_flags & FLAG_READ_LOGFILE) == 0) {
1138 "ERROR: Initialization of the pmc(3) library failed"
1141 if ((npmc = pmc_npmc(0)) < 0) /* assume all CPUs are identical */
1143 "ERROR: Cannot determine the number of PMCs on CPU %d",
1147 /* Allocate a kqueue */
1148 if ((pmcstat_kq = kqueue()) < 0)
1149 err(EX_OSERR, "ERROR: Cannot allocate kqueue");
1151 /* Setup the logfile as the source. */
1152 if (args.pa_flags & FLAG_READ_LOGFILE) {
1154 * Print the log in textual form if we haven't been
1155 * asked to generate profiling information.
1157 if ((args.pa_flags & FLAG_DO_ANALYSIS) == 0)
1158 args.pa_flags |= FLAG_DO_PRINT;
1160 pmcstat_initialize_logging();
1161 rfd = pmcstat_open_log(args.pa_inputpath,
1162 PMCSTAT_OPEN_FOR_READ);
1163 if ((args.pa_logparser = pmclog_open(rfd)) == NULL)
1164 err(EX_OSERR, "ERROR: Cannot create parser");
1165 if (fcntl(rfd, F_SETFL, O_NONBLOCK) < 0)
1166 err(EX_OSERR, "ERROR: fcntl(2) failed");
1167 EV_SET(&kev, rfd, EVFILT_READ, EV_ADD,
1169 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1170 err(EX_OSERR, "ERROR: Cannot register kevent");
1173 * Configure the specified log file or setup a default log
1174 * consumer via a pipe.
1176 if (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) {
1177 if (args.pa_outputpath)
1178 args.pa_logfd = pmcstat_open_log(args.pa_outputpath,
1179 PMCSTAT_OPEN_FOR_WRITE);
1182 * process the log on the fly by reading it in
1185 if (pipe(pipefd) < 0)
1186 err(EX_OSERR, "ERROR: pipe(2) failed");
1188 if (fcntl(pipefd[READPIPEFD], F_SETFL, O_NONBLOCK) < 0)
1189 err(EX_OSERR, "ERROR: fcntl(2) failed");
1191 EV_SET(&kev, pipefd[READPIPEFD], EVFILT_READ, EV_ADD,
1194 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1195 err(EX_OSERR, "ERROR: Cannot register kevent");
1197 args.pa_logfd = pipefd[WRITEPIPEFD];
1199 args.pa_flags |= FLAG_HAS_PIPE;
1200 if ((args.pa_flags & FLAG_DO_TOP) == 0)
1201 args.pa_flags |= FLAG_DO_PRINT;
1202 args.pa_logparser = pmclog_open(pipefd[READPIPEFD]);
1205 if (pmc_configure_logfile(args.pa_logfd) < 0)
1206 err(EX_OSERR, "ERROR: Cannot configure log file");
1209 /* remember to check for driver errors if we are sampling or logging */
1210 check_driver_stats = (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) ||
1211 (args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE);
1214 if (args.pa_flags & FLAG_READ_LOGFILE) {
1218 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
1219 if (pmc_allocate(ev->ev_spec, ev->ev_mode,
1220 ev->ev_flags, ev->ev_cpu, &ev->ev_pmcid) < 0)
1222 "ERROR: Cannot allocate %s-mode pmc with specification \"%s\"",
1223 PMC_IS_SYSTEM_MODE(ev->ev_mode) ?
1224 "system" : "process", ev->ev_spec);
1226 if (PMC_IS_SAMPLING_MODE(ev->ev_mode) &&
1227 pmc_set(ev->ev_pmcid, ev->ev_count) < 0)
1229 "ERROR: Cannot set sampling count for PMC \"%s\"",
1233 /* compute printout widths */
1234 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
1239 (void) pmc_width(ev->ev_pmcid, &counter_width);
1240 header_width = strlen(ev->ev_name) + 2; /* prefix '%c/' */
1241 display_width = (int) floor(counter_width / 3.32193) + 1;
1243 if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
1244 header_width += 3; /* 2 digit CPU number + '/' */
1246 if (header_width > display_width) {
1247 ev->ev_fieldskip = 0;
1248 ev->ev_fieldwidth = header_width;
1250 ev->ev_fieldskip = display_width -
1252 ev->ev_fieldwidth = display_width;
1257 * If our output is being set to a terminal, register a handler
1258 * for window size changes.
1261 if (isatty(fileno(args.pa_printfile))) {
1263 if (ioctl(fileno(args.pa_printfile), TIOCGWINSZ, &ws) < 0)
1264 err(EX_OSERR, "ERROR: Cannot determine window size");
1266 pmcstat_displayheight = ws.ws_row - 1;
1267 pmcstat_displaywidth = ws.ws_col - 1;
1269 EV_SET(&kev, SIGWINCH, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1271 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1273 "ERROR: Cannot register kevent for SIGWINCH");
1279 * Listen to key input in top mode.
1281 if (args.pa_flags & FLAG_DO_TOP) {
1282 EV_SET(&kev, fileno(stdin), EVFILT_READ, EV_ADD, 0, 0, NULL);
1283 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1284 err(EX_OSERR, "ERROR: Cannot register kevent");
1287 EV_SET(&kev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1288 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1289 err(EX_OSERR, "ERROR: Cannot register kevent for SIGINT");
1291 EV_SET(&kev, SIGIO, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1292 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1293 err(EX_OSERR, "ERROR: Cannot register kevent for SIGIO");
1296 * An exec() failure of a forked child is signalled by the
1297 * child sending the parent a SIGCHLD. We don't register an
1298 * actual signal handler for SIGCHLD, but instead use our
1299 * kqueue to pick up the signal.
1301 EV_SET(&kev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1302 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1303 err(EX_OSERR, "ERROR: Cannot register kevent for SIGCHLD");
1306 * Setup a timer if we have counting mode PMCs needing to be printed or
1307 * top mode plugin is active.
1309 if (((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
1310 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) ||
1311 (args.pa_flags & FLAG_DO_TOP)) {
1312 EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0,
1313 args.pa_interval * 1000, NULL);
1315 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1317 "ERROR: Cannot register kevent for timer");
1321 * Setup a duration timer if we have sampling mode PMCs and
1322 * a duration time is set
1324 if ((args.pa_flags & FLAG_HAS_SAMPLING_PMCS) &&
1325 (args.pa_flags & FLAG_HAS_DURATION)) {
1326 EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0,
1327 args.pa_duration * 1000, NULL);
1329 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1330 err(EX_OSERR, "ERROR: Cannot register kevent for "
1334 /* attach PMCs to the target process, starting it if specified */
1335 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1336 pmcstat_create_process();
1338 if (check_driver_stats && pmc_get_driver_stats(&ds_start) < 0)
1339 err(EX_OSERR, "ERROR: Cannot retrieve driver statistics");
1341 /* Attach process pmcs to the target process. */
1342 if (args.pa_flags & (FLAG_HAS_TARGET | FLAG_HAS_COMMANDLINE)) {
1343 if (SLIST_EMPTY(&args.pa_targets))
1345 "ERROR: No matching target processes.");
1346 if (args.pa_flags & FLAG_HAS_PROCESS_PMCS)
1347 pmcstat_attach_pmcs();
1350 kvm_close(pmcstat_kvm);
1355 /* start the pmcs */
1356 pmcstat_start_pmcs();
1358 /* start the (commandline) process if needed */
1359 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1360 pmcstat_start_process();
1362 /* initialize logging */
1363 pmcstat_initialize_logging();
1365 /* Handle SIGINT using the kqueue loop */
1366 sa.sa_handler = SIG_IGN;
1368 (void) sigemptyset(&sa.sa_mask);
1370 if (sigaction(SIGINT, &sa, NULL) < 0)
1371 err(EX_OSERR, "ERROR: Cannot install signal handler");
1374 * Setup the top mode display.
1376 if (args.pa_flags & FLAG_DO_TOP) {
1377 args.pa_flags &= ~FLAG_DO_PRINT;
1379 if (args.pa_toptty) {
1384 if(has_colors() == TRUE) {
1385 args.pa_topcolor = 1;
1387 use_default_colors();
1388 pair_content(0, &cf, &cb);
1389 init_pair(1, COLOR_RED, cb);
1390 init_pair(2, COLOR_YELLOW, cb);
1391 init_pair(3, COLOR_GREEN, cb);
1397 intrflush(stdscr, FALSE);
1398 keypad(stdscr, TRUE);
1400 /* Get terminal width / height with ncurses. */
1402 pmcstat_displayheight, pmcstat_displaywidth);
1403 pmcstat_displayheight--; pmcstat_displaywidth--;
1404 atexit(pmcstat_topexit);
1409 * loop till either the target process (if any) exits, or we
1410 * are killed by a SIGINT or we reached the time duration.
1412 runstate = PMCSTAT_RUNNING;
1413 do_print = do_read = 0;
1415 if ((c = kevent(pmcstat_kq, NULL, 0, &kev, 1, NULL)) <= 0) {
1417 err(EX_OSERR, "ERROR: kevent failed");
1422 if (kev.flags & EV_ERROR)
1423 errc(EX_OSERR, kev.data, "ERROR: kevent failed");
1425 switch (kev.filter) {
1426 case EVFILT_PROC: /* target has exited */
1427 runstate = pmcstat_close_log();
1431 case EVFILT_READ: /* log file data is present */
1432 if (kev.ident == (unsigned)fileno(stdin) &&
1433 (args.pa_flags & FLAG_DO_TOP)) {
1434 if (pmcstat_keypress_log())
1435 runstate = pmcstat_close_log();
1438 runstate = pmcstat_process_log();
1443 if (kev.ident == SIGCHLD) {
1445 * The child process sends us a
1446 * SIGCHLD if its exec() failed. We
1447 * wait for it to exit and then exit
1451 runstate = PMCSTAT_FINISHED;
1452 } else if (kev.ident == SIGIO) {
1454 * We get a SIGIO if a PMC loses all
1455 * of its targets, or if logfile
1456 * writes encounter an error.
1458 runstate = pmcstat_close_log();
1459 do_print = 1; /* print PMCs at exit */
1460 } else if (kev.ident == SIGINT) {
1461 /* Kill the child process if we started it */
1462 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1463 pmcstat_kill_process();
1464 runstate = pmcstat_close_log();
1465 } else if (kev.ident == SIGWINCH) {
1466 if (ioctl(fileno(args.pa_printfile),
1467 TIOCGWINSZ, &ws) < 0)
1469 "ERROR: Cannot determine window size");
1470 pmcstat_displayheight = ws.ws_row - 1;
1471 pmcstat_displaywidth = ws.ws_col - 1;
1478 /* time duration reached, exit */
1479 if (args.pa_flags & FLAG_HAS_DURATION) {
1480 runstate = PMCSTAT_FINISHED;
1483 /* print out counting PMCs */
1484 if ((args.pa_flags & FLAG_DO_TOP) &&
1485 pmc_flush_logfile() == 0)
1492 if (do_print && !do_read) {
1493 if ((args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) {
1494 pmcstat_print_pmcs();
1495 if (runstate == PMCSTAT_FINISHED &&
1497 (args.pa_flags & FLAG_DO_PRINT) == 0)
1498 (void) fprintf(args.pa_printfile, "\n");
1500 if (args.pa_flags & FLAG_DO_TOP)
1501 pmcstat_display_log();
1505 } while (runstate != PMCSTAT_FINISHED);
1507 if ((args.pa_flags & FLAG_DO_TOP) && args.pa_toptty) {
1512 /* flush any pending log entries */
1513 if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE))
1514 pmc_close_logfile();
1518 /* check if the driver lost any samples or events */
1519 if (check_driver_stats) {
1520 if (pmc_get_driver_stats(&ds_end) < 0)
1522 "ERROR: Cannot retrieve driver statistics");
1523 if (ds_start.pm_intr_bufferfull != ds_end.pm_intr_bufferfull &&
1524 args.pa_verbosity > 0)
1526 "WARNING: sampling was paused at least %u time%s.\n"
1527 "Please consider tuning the \"kern.hwpmc.nsamples\" tunable.",
1528 ds_end.pm_intr_bufferfull -
1529 ds_start.pm_intr_bufferfull,
1530 ((ds_end.pm_intr_bufferfull -
1531 ds_start.pm_intr_bufferfull) != 1) ? "s" : ""
1533 if (ds_start.pm_buffer_requests_failed !=
1534 ds_end.pm_buffer_requests_failed &&
1535 args.pa_verbosity > 0)
1537 "WARNING: at least %u event%s were discarded while running.\n"
1538 "Please consider tuning the \"kern.hwpmc.nbuffers\" tunable.",
1539 ds_end.pm_buffer_requests_failed -
1540 ds_start.pm_buffer_requests_failed,
1541 ((ds_end.pm_buffer_requests_failed -
1542 ds_start.pm_buffer_requests_failed) != 1) ? "s" : ""