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/types.h>
35 #include <sys/event.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/socket.h>
40 #include <sys/sysctl.h>
42 #include <sys/ttycom.h>
69 * A given invocation of pmcstat(8) can manage multiple PMCs of both
70 * the system-wide and per-process variety. Each of these could be in
71 * 'counting mode' or in 'sampling mode'.
73 * For 'counting mode' PMCs, pmcstat(8) will periodically issue a
74 * pmc_read() at the configured time interval and print out the value
75 * of the requested PMCs.
77 * For 'sampling mode' PMCs it can log to a file for offline analysis,
78 * or can analyse sampling data "on the fly", either by converting
79 * samples to printed textual form or by creating gprof(1) compatible
80 * profiles, one per program executed. When creating gprof(1)
81 * profiles it can optionally merge entries from multiple processes
82 * for a given executable into a single profile file.
84 * pmcstat(8) can also execute a command line and attach PMCs to the
85 * resulting child process. The protocol used is as follows:
87 * - parent creates a socketpair for two way communication and
93 * - Wait for childs token.
95 * - Awaits signal to start.
96 * - Attaches PMCs to the child's pid
97 * and starts them. Sets up
98 * monitoring for the child.
99 * - Signals child to start.
100 * - Recieves signal, attempts exec().
102 * After this point normal processing can happen.
107 int pmcstat_interrupt = 0;
108 int pmcstat_displayheight = DEFAULT_DISPLAY_HEIGHT;
109 int pmcstat_sockpair[NSOCKPAIRFD];
112 struct kinfo_proc *pmcstat_plist;
115 pmcstat_attach_pmcs(struct pmcstat_args *a)
117 struct pmcstat_ev *ev;
118 struct pmcstat_target *pt;
121 /* Attach all process PMCs to target processes. */
123 STAILQ_FOREACH(ev, &a->pa_events, ev_next) {
124 if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
126 SLIST_FOREACH(pt, &a->pa_targets, pt_next)
127 if (pmc_attach(ev->ev_pmcid, pt->pt_pid) == 0)
129 else if (errno != ESRCH)
130 err(EX_OSERR, "ERROR: cannot attach pmc "
131 "\"%s\" to process %d", ev->ev_name,
136 errx(EX_DATAERR, "ERROR: No processes were attached to.");
141 pmcstat_cleanup(struct pmcstat_args *a)
143 struct pmcstat_ev *ev, *tmp;
145 /* release allocated PMCs. */
146 STAILQ_FOREACH_SAFE(ev, &a->pa_events, ev_next, tmp)
147 if (ev->ev_pmcid != PMC_ID_INVALID) {
148 if (pmc_release(ev->ev_pmcid) < 0)
149 err(EX_OSERR, "ERROR: cannot release pmc "
150 "0x%x \"%s\"", ev->ev_pmcid, ev->ev_name);
153 STAILQ_REMOVE(&a->pa_events, ev, pmcstat_ev, ev_next);
157 /* de-configure the log file if present. */
158 if (a->pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))
159 (void) pmc_configure_logfile(-1);
161 if (a->pa_logparser) {
162 pmclog_close(a->pa_logparser);
163 a->pa_logparser = NULL;
166 if (a->pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))
167 pmcstat_shutdown_logging(a);
171 pmcstat_clone_event_descriptor(struct pmcstat_args *a, struct pmcstat_ev *ev,
175 struct pmcstat_ev *ev_clone;
177 while ((cpu = ffs(cpumask)) > 0) {
180 if ((ev_clone = malloc(sizeof(*ev_clone))) == NULL)
181 errx(EX_SOFTWARE, "ERROR: Out of memory");
182 (void) memset(ev_clone, 0, sizeof(*ev_clone));
184 ev_clone->ev_count = ev->ev_count;
185 ev_clone->ev_cpu = cpu;
186 ev_clone->ev_cumulative = ev->ev_cumulative;
187 ev_clone->ev_flags = ev->ev_flags;
188 ev_clone->ev_mode = ev->ev_mode;
189 ev_clone->ev_name = strdup(ev->ev_name);
190 ev_clone->ev_pmcid = ev->ev_pmcid;
191 ev_clone->ev_saved = ev->ev_saved;
192 ev_clone->ev_spec = strdup(ev->ev_spec);
194 STAILQ_INSERT_TAIL(&a->pa_events, ev_clone, ev_next);
196 cpumask &= ~(1 << cpu);
201 pmcstat_create_process(struct pmcstat_args *a)
206 struct pmcstat_target *pt;
208 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pmcstat_sockpair) < 0)
209 err(EX_OSERR, "ERROR: cannot create socket pair");
211 switch (pid = fork()) {
213 err(EX_OSERR, "ERROR: cannot fork");
217 (void) close(pmcstat_sockpair[PARENTSOCKET]);
219 /* Write a token to tell our parent we've started executing. */
220 if (write(pmcstat_sockpair[CHILDSOCKET], "+", 1) != 1)
221 err(EX_OSERR, "ERROR (child): cannot write token");
223 /* Wait for our parent to signal us to start. */
224 if (read(pmcstat_sockpair[CHILDSOCKET], &token, 1) < 0)
225 err(EX_OSERR, "ERROR (child): cannot read token");
226 (void) close(pmcstat_sockpair[CHILDSOCKET]);
228 /* exec() the program requested */
229 execvp(*a->pa_argv, a->pa_argv);
230 /* and if that fails, notify the parent */
231 kill(getppid(), SIGCHLD);
232 err(EX_OSERR, "ERROR: execvp \"%s\" failed", *a->pa_argv);
235 default: /* parent */
236 (void) close(pmcstat_sockpair[CHILDSOCKET]);
240 /* Ask to be notified via a kevent when the target process exits. */
241 EV_SET(&kev, pid, EVFILT_PROC, EV_ADD|EV_ONESHOT, NOTE_EXIT, 0,
243 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
244 err(EX_OSERR, "ERROR: cannot monitor child process %d", pid);
246 if ((pt = malloc(sizeof(*pt))) == NULL)
247 errx(EX_SOFTWARE, "ERROR: Out of memory.");
250 SLIST_INSERT_HEAD(&a->pa_targets, pt, pt_next);
252 /* Wait for the child to signal that its ready to go. */
253 if (read(pmcstat_sockpair[PARENTSOCKET], &token, 1) < 0)
254 err(EX_OSERR, "ERROR (parent): cannot read token");
260 pmcstat_find_targets(struct pmcstat_args *a, const char *spec)
262 int n, nproc, pid, rv;
263 struct pmcstat_target *pt;
264 char errbuf[_POSIX2_LINE_MAX], *end;
265 static struct kinfo_proc *kp;
269 /* First check if we've been given a process id. */
270 pid = strtol(spec, &end, 0);
271 if (end != spec && pid >= 0) {
272 if ((pt = malloc(sizeof(*pt))) == NULL)
275 SLIST_INSERT_HEAD(&a->pa_targets, pt, pt_next);
279 /* Otherwise treat arg as a regular expression naming processes. */
280 if (pmcstat_kvm == NULL) {
281 if ((pmcstat_kvm = kvm_openfiles(NULL, "/dev/null", NULL, 0,
283 err(EX_OSERR, "ERROR: Cannot open kernel \"%s\"",
285 if ((pmcstat_plist = kvm_getprocs(pmcstat_kvm, KERN_PROC_PROC,
287 err(EX_OSERR, "ERROR: Cannot get process list: %s",
288 kvm_geterr(pmcstat_kvm));
291 if ((rv = regcomp(®, spec, REG_EXTENDED|REG_NOSUB)) != 0) {
292 regerror(rv, ®, errbuf, sizeof(errbuf));
293 err(EX_DATAERR, "ERROR: Failed to compile regex \"%s\": %s",
297 for (n = 0, kp = pmcstat_plist; n < nproc; n++, kp++) {
298 if ((rv = regexec(®, kp->ki_comm, 1, ®match, 0)) == 0) {
299 if ((pt = malloc(sizeof(*pt))) == NULL)
301 pt->pt_pid = kp->ki_pid;
302 SLIST_INSERT_HEAD(&a->pa_targets, pt, pt_next);
303 } else if (rv != REG_NOMATCH) {
304 regerror(rv, ®, errbuf, sizeof(errbuf));
305 errx(EX_SOFTWARE, "ERROR: Regex evalation failed: %s",
315 errx(EX_SOFTWARE, "Out of memory.");
320 pmcstat_get_cpumask(const char *cpuspec)
331 cpu = strtol(s, &end, 0);
332 if (cpu < 0 || end == s)
333 errx(EX_USAGE, "ERROR: Illegal CPU specification "
335 cpumask |= (1 << cpu);
336 s = end + strspn(end, ", \t");
343 pmcstat_kill_process(struct pmcstat_args *a)
345 struct pmcstat_target *pt;
347 assert(a->pa_flags & FLAG_HAS_COMMANDLINE);
350 * If a command line was specified, it would be the very first
351 * in the list, before any other processes specified by -t.
353 pt = SLIST_FIRST(&a->pa_targets);
356 if (kill(pt->pt_pid, SIGINT) != 0)
357 err(EX_OSERR, "ERROR: cannot signal child process");
361 pmcstat_start_pmcs(struct pmcstat_args *a)
363 struct pmcstat_ev *ev;
365 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
367 assert(ev->ev_pmcid != PMC_ID_INVALID);
369 if (pmc_start(ev->ev_pmcid) < 0) {
370 warn("ERROR: Cannot start pmc 0x%x \"%s\"",
371 ev->ev_pmcid, ev->ev_name);
380 pmcstat_print_headers(struct pmcstat_args *a)
382 struct pmcstat_ev *ev;
385 (void) fprintf(a->pa_printfile, PRINT_HEADER_PREFIX);
387 STAILQ_FOREACH(ev, &a->pa_events, ev_next) {
388 if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
391 c = PMC_IS_SYSTEM_MODE(ev->ev_mode) ? 's' : 'p';
393 if (ev->ev_fieldskip != 0)
394 (void) fprintf(a->pa_printfile, "%*s",
395 ev->ev_fieldskip, "");
396 w = ev->ev_fieldwidth - ev->ev_fieldskip - 2;
399 (void) fprintf(a->pa_printfile, "s/%02d/%-*s ",
400 ev->ev_cpu, w-3, ev->ev_name);
402 (void) fprintf(a->pa_printfile, "p/%*s ", w,
406 (void) fflush(a->pa_printfile);
410 pmcstat_print_counters(struct pmcstat_args *a)
413 struct pmcstat_ev *ev;
416 extra_width = sizeof(PRINT_HEADER_PREFIX) - 1;
418 STAILQ_FOREACH(ev, &a->pa_events, ev_next) {
420 /* skip sampling mode counters */
421 if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
424 if (pmc_read(ev->ev_pmcid, &value) < 0)
425 err(EX_OSERR, "ERROR: Cannot read pmc "
426 "\"%s\"", ev->ev_name);
428 (void) fprintf(a->pa_printfile, "%*ju ",
429 ev->ev_fieldwidth + extra_width,
430 (uintmax_t) ev->ev_cumulative ? value :
431 (value - ev->ev_saved));
433 if (ev->ev_cumulative == 0)
434 ev->ev_saved = value;
438 (void) fflush(a->pa_printfile);
446 pmcstat_print_pmcs(struct pmcstat_args *a)
448 static int linecount = 0;
450 /* check if we need to print a header line */
451 if (++linecount > pmcstat_displayheight) {
452 (void) fprintf(a->pa_printfile, "\n");
456 pmcstat_print_headers(a);
457 (void) fprintf(a->pa_printfile, "\n");
459 pmcstat_print_counters(a);
465 * Do process profiling
467 * If a pid was specified, attach each allocated PMC to the target
468 * process. Otherwise, fork a child and attach the PMCs to the child,
469 * and have the child exec() the target program.
473 pmcstat_start_process(void)
475 /* Signal the child to proceed. */
476 if (write(pmcstat_sockpair[PARENTSOCKET], "!", 1) != 1)
477 err(EX_OSERR, "ERROR (parent): write of token failed");
479 (void) close(pmcstat_sockpair[PARENTSOCKET]);
483 pmcstat_show_usage(void)
486 "[options] [commandline]\n"
487 "\t Measure process and/or system performance using hardware\n"
488 "\t performance monitoring counters.\n"
489 "\t Options include:\n"
490 "\t -C\t\t (toggle) show cumulative counts\n"
491 "\t -D path\t create profiles in directory \"path\"\n"
492 "\t -E\t\t (toggle) show counts at process exit\n"
493 "\t -G file\t write a system-wide callgraph to \"file\"\n"
494 "\t -M file\t print executable/gmon file map to \"file\"\n"
495 "\t -N\t\t (toggle) capture callchains\n"
496 "\t -O file\t send log output to \"file\"\n"
497 "\t -P spec\t allocate a process-private sampling PMC\n"
498 "\t -R file\t read events from \"file\"\n"
499 "\t -S spec\t allocate a system-wide sampling PMC\n"
500 "\t -W\t\t (toggle) show counts per context switch\n"
501 "\t -c cpu-list\t set cpus for subsequent system-wide PMCs\n"
502 "\t -d\t\t (toggle) track descendants\n"
503 "\t -g\t\t produce gprof(1) compatible profiles\n"
504 "\t -k dir\t\t set the path to the kernel\n"
505 "\t -n rate\t set sampling rate\n"
506 "\t -o file\t send print output to \"file\"\n"
507 "\t -p spec\t allocate a process-private counting PMC\n"
508 "\t -q\t\t suppress verbosity\n"
509 "\t -r fsroot\t specify FS root directory\n"
510 "\t -s spec\t allocate a system-wide counting PMC\n"
511 "\t -t process-spec attach to running processes matching "
513 "\t -v\t\t increase verbosity\n"
514 "\t -w secs\t set printing time interval\n"
515 "\t -z depth\t limit callchain display depth"
524 main(int argc, char **argv)
527 int option, npmc, ncpu, haltedcpus;
528 int c, check_driver_stats, current_cpu, current_sampling_count;
529 int do_callchain, do_descendants, do_logproccsw, do_logprocexit;
534 int use_cumulative_counts;
537 const char *errmsg, *graphfilename;
538 enum pmcstat_state runstate;
539 struct pmc_driverstats ds_start, ds_end;
540 struct pmcstat_ev *ev;
545 char buffer[PATH_MAX];
547 check_driver_stats = 0;
549 current_sampling_count = DEFAULT_SAMPLE_COUNT;
554 use_cumulative_counts = 0;
556 args.pa_required = 0;
558 args.pa_verbosity = 1;
561 args.pa_kernel = strdup("/boot/kernel");
562 args.pa_samplesdir = ".";
563 args.pa_printfile = stderr;
564 args.pa_graphdepth = DEFAULT_CALLGRAPH_DEPTH;
565 args.pa_graphfile = NULL;
566 args.pa_interval = DEFAULT_WAIT_INTERVAL;
567 args.pa_mapfilename = NULL;
568 args.pa_inputpath = NULL;
569 args.pa_outputpath = NULL;
570 STAILQ_INIT(&args.pa_events);
571 SLIST_INIT(&args.pa_targets);
572 bzero(&ds_start, sizeof(ds_start));
573 bzero(&ds_end, sizeof(ds_end));
577 * The initial CPU mask specifies all non-halted CPUS in the
581 if (sysctlbyname("hw.ncpu", &ncpu, &dummy, NULL, 0) < 0)
582 err(EX_OSERR, "ERROR: Cannot determine the number of CPUs");
583 cpumask = (1 << ncpu) - 1;
586 if (sysctlbyname("machdep.hlt_cpus", &haltedcpus, &dummy,
588 err(EX_OSERR, "ERROR: Cannot determine which CPUs are "
590 cpumask &= ~haltedcpus;
593 while ((option = getopt(argc, argv,
594 "CD:EG:M:NO:P:R:S:Wc:dgk:n:o:p:qr:s:t:vw:z:")) != -1)
596 case 'C': /* cumulative values */
597 use_cumulative_counts = !use_cumulative_counts;
598 args.pa_required |= FLAG_HAS_COUNTING_PMCS;
603 if (optarg[0] == '*' && optarg[1] == '\0')
604 cpumask = ((1 << ncpu) - 1) & ~haltedcpus;
606 cpumask = pmcstat_get_cpumask(optarg);
608 args.pa_required |= FLAG_HAS_SYSTEM_PMCS;
612 if (stat(optarg, &sb) < 0)
613 err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
615 if (!S_ISDIR(sb.st_mode))
616 errx(EX_USAGE, "ERROR: \"%s\" is not a "
617 "directory.", optarg);
618 args.pa_samplesdir = optarg;
619 args.pa_flags |= FLAG_HAS_SAMPLESDIR;
620 args.pa_required |= FLAG_DO_GPROF;
623 case 'd': /* toggle descendents */
624 do_descendants = !do_descendants;
625 args.pa_required |= FLAG_HAS_PROCESS_PMCS;
628 case 'G': /* produce a system-wide callgraph */
629 args.pa_flags |= FLAG_DO_CALLGRAPHS;
630 graphfilename = optarg;
633 case 'g': /* produce gprof compatible profiles */
634 args.pa_flags |= FLAG_DO_GPROF;
637 case 'k': /* pathname to the kernel */
638 free(args.pa_kernel);
639 args.pa_kernel = strdup(optarg);
640 args.pa_required |= FLAG_DO_ANALYSIS;
641 args.pa_flags |= FLAG_HAS_KERNELPATH;
644 case 'E': /* log process exit */
645 do_logprocexit = !do_logprocexit;
646 args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
647 FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
650 case 'M': /* mapfile */
651 args.pa_mapfilename = optarg;
655 do_callchain = !do_callchain;
656 args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
659 case 'p': /* process virtual counting PMC */
660 case 's': /* system-wide counting PMC */
661 case 'P': /* process virtual sampling PMC */
662 case 'S': /* system-wide sampling PMC */
663 if ((ev = malloc(sizeof(*ev))) == NULL)
664 errx(EX_SOFTWARE, "ERROR: Out of memory.");
667 case 'p': ev->ev_mode = PMC_MODE_TC; break;
668 case 's': ev->ev_mode = PMC_MODE_SC; break;
669 case 'P': ev->ev_mode = PMC_MODE_TS; break;
670 case 'S': ev->ev_mode = PMC_MODE_SS; break;
673 if (option == 'P' || option == 'p') {
674 args.pa_flags |= FLAG_HAS_PROCESS_PMCS;
675 args.pa_required |= (FLAG_HAS_COMMANDLINE |
679 if (option == 'P' || option == 'S') {
680 args.pa_flags |= FLAG_HAS_SAMPLING_PMCS;
681 args.pa_required |= (FLAG_HAS_PIPE |
682 FLAG_HAS_OUTPUT_LOGFILE);
685 if (option == 'p' || option == 's')
686 args.pa_flags |= FLAG_HAS_COUNTING_PMCS;
688 if (option == 's' || option == 'S')
689 args.pa_flags |= FLAG_HAS_SYSTEM_PMCS;
691 ev->ev_spec = strdup(optarg);
693 if (option == 'S' || option == 'P')
694 ev->ev_count = current_sampling_count;
698 if (option == 'S' || option == 's')
699 ev->ev_cpu = ffs(cpumask) - 1;
701 ev->ev_cpu = PMC_CPU_ANY;
705 ev->ev_flags |= PMC_F_CALLCHAIN;
707 ev->ev_flags |= PMC_F_DESCENDANTS;
709 ev->ev_flags |= PMC_F_LOG_PROCEXIT;
711 ev->ev_flags |= PMC_F_LOG_PROCCSW;
713 ev->ev_cumulative = use_cumulative_counts;
716 ev->ev_pmcid = PMC_ID_INVALID;
718 /* extract event name */
719 c = strcspn(optarg, ", \t");
720 ev->ev_name = malloc(c + 1);
721 (void) strncpy(ev->ev_name, optarg, c);
722 *(ev->ev_name + c) = '\0';
724 STAILQ_INSERT_TAIL(&args.pa_events, ev, ev_next);
726 if (option == 's' || option == 'S')
727 pmcstat_clone_event_descriptor(&args, ev,
728 cpumask & ~(1 << ev->ev_cpu));
732 case 'n': /* sampling count */
733 current_sampling_count = strtol(optarg, &end, 0);
734 if (*end != '\0' || current_sampling_count <= 0)
736 "ERROR: Illegal count value \"%s\".",
738 args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
741 case 'o': /* outputfile */
742 if (args.pa_printfile != NULL)
743 (void) fclose(args.pa_printfile);
744 if ((args.pa_printfile = fopen(optarg, "w")) == NULL)
745 errx(EX_OSERR, "ERROR: cannot open \"%s\" for "
747 args.pa_flags |= FLAG_DO_PRINT;
750 case 'O': /* sampling output */
751 if (args.pa_outputpath)
752 errx(EX_USAGE, "ERROR: option -O may only be "
754 args.pa_outputpath = optarg;
755 args.pa_flags |= FLAG_HAS_OUTPUT_LOGFILE;
758 case 'q': /* quiet mode */
759 args.pa_verbosity = 0;
762 case 'r': /* root FS path */
763 args.pa_fsroot = optarg;
766 case 'R': /* read an existing log file */
767 if (args.pa_inputpath != NULL)
768 errx(EX_USAGE, "ERROR: option -R may only be "
770 args.pa_inputpath = optarg;
771 if (args.pa_printfile == stderr)
772 args.pa_printfile = stdout;
773 args.pa_flags |= FLAG_READ_LOGFILE;
776 case 't': /* target pid or process name */
777 pmcstat_find_targets(&args, optarg);
779 args.pa_flags |= FLAG_HAS_TARGET;
780 args.pa_required |= FLAG_HAS_PROCESS_PMCS;
783 case 'v': /* verbose */
787 case 'w': /* wait interval */
788 interval = strtod(optarg, &end);
789 if (*end != '\0' || interval <= 0)
790 errx(EX_USAGE, "ERROR: Illegal wait interval "
791 "value \"%s\".", optarg);
792 args.pa_flags |= FLAG_HAS_WAIT_INTERVAL;
793 args.pa_required |= FLAG_HAS_COUNTING_PMCS;
794 args.pa_interval = interval;
797 case 'W': /* toggle LOG_CSW */
798 do_logproccsw = !do_logproccsw;
799 args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
800 FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
804 graphdepth = strtod(optarg, &end);
805 if (*end != '\0' || graphdepth <= 0)
806 errx(EX_USAGE, "ERROR: Illegal callchain "
807 "depth \"%s\".", optarg);
808 args.pa_graphdepth = graphdepth;
809 args.pa_required |= FLAG_DO_CALLGRAPHS;
814 pmcstat_show_usage();
819 args.pa_argc = (argc -= optind);
820 args.pa_argv = (argv += optind);
822 args.pa_cpumask = cpumask; /* For selecting CPUs using -R. */
824 if (argc) /* command line present */
825 args.pa_flags |= FLAG_HAS_COMMANDLINE;
827 if (args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS))
828 args.pa_flags |= FLAG_DO_ANALYSIS;
831 * Check invocation syntax.
834 /* disallow -O and -R together */
835 if (args.pa_outputpath && args.pa_inputpath)
836 errx(EX_USAGE, "ERROR: options -O and -R are mutually "
839 if (args.pa_flags & FLAG_READ_LOGFILE) {
841 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
842 errmsg = "a command line specification";
843 else if (args.pa_flags & FLAG_HAS_TARGET)
844 errmsg = "option -t";
845 else if (!STAILQ_EMPTY(&args.pa_events))
846 errmsg = "a PMC event specification";
848 errx(EX_USAGE, "ERROR: option -R may not be used with "
850 } else if (STAILQ_EMPTY(&args.pa_events))
851 /* All other uses require a PMC spec. */
852 pmcstat_show_usage();
854 /* check for -t pid without a process PMC spec */
855 if ((args.pa_required & FLAG_HAS_TARGET) &&
856 (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
857 errx(EX_USAGE, "ERROR: option -t requires a process mode PMC "
860 /* check for process-mode options without a command or -t pid */
861 if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
862 (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
863 errx(EX_USAGE, "ERROR: options -d, -E, -p, -P, and -W require "
864 "a command line or target process.");
866 /* check for -p | -P without a target process of some sort */
867 if ((args.pa_required & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) &&
868 (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
869 errx(EX_USAGE, "ERROR: options -P and -p require a "
870 "target process or a command line.");
872 /* check for process-mode options without a process-mode PMC */
873 if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
874 (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
875 errx(EX_USAGE, "ERROR: options -d, -E, and -W require a "
876 "process mode PMC to be specified.");
878 /* check for -c cpu with no system mode PMCs or logfile. */
879 if ((args.pa_required & FLAG_HAS_SYSTEM_PMCS) &&
880 (args.pa_flags & FLAG_HAS_SYSTEM_PMCS) == 0 &&
881 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
882 errx(EX_USAGE, "ERROR: option -c requires at least one "
883 "system mode PMC to be specified.");
885 /* check for counting mode options without a counting PMC */
886 if ((args.pa_required & FLAG_HAS_COUNTING_PMCS) &&
887 (args.pa_flags & FLAG_HAS_COUNTING_PMCS) == 0)
888 errx(EX_USAGE, "ERROR: options -C, -W, -o and -w require at "
889 "least one counting mode PMC to be specified.");
891 /* check for sampling mode options without a sampling PMC spec */
892 if ((args.pa_required & FLAG_HAS_SAMPLING_PMCS) &&
893 (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) == 0)
894 errx(EX_USAGE, "ERROR: options -N, -n and -O require at "
895 "least one sampling mode PMC to be specified.");
897 /* check if -g/-G are being used correctly */
898 if ((args.pa_flags & FLAG_DO_ANALYSIS) &&
899 !(args.pa_flags & (FLAG_HAS_SAMPLING_PMCS|FLAG_READ_LOGFILE)))
900 errx(EX_USAGE, "ERROR: options -g/-G require sampling PMCs "
901 "or -R to be specified.");
903 /* check if -O was spuriously specified */
904 if ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) &&
905 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0)
907 "ERROR: option -O is used only with options "
908 "-E, -P, -S and -W.");
910 /* -k kernel path require -g/-G or -R */
911 if ((args.pa_flags & FLAG_HAS_KERNELPATH) &&
912 (args.pa_flags & FLAG_DO_ANALYSIS) == 0 &&
913 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
914 errx(EX_USAGE, "ERROR: option -k is only used with -g/-R.");
916 /* -D only applies to gprof output mode (-g) */
917 if ((args.pa_flags & FLAG_HAS_SAMPLESDIR) &&
918 (args.pa_flags & FLAG_DO_GPROF) == 0)
919 errx(EX_USAGE, "ERROR: option -D is only used with -g.");
921 /* -M mapfile requires -g or -R */
922 if (args.pa_mapfilename != NULL &&
923 (args.pa_flags & FLAG_DO_GPROF) == 0 &&
924 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
925 errx(EX_USAGE, "ERROR: option -M is only used with -g/-R.");
928 * Disallow textual output of sampling PMCs if counting PMCs
929 * have also been asked for, mostly because the combined output
930 * is difficult to make sense of.
932 if ((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
933 (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) &&
934 ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) == 0))
935 errx(EX_USAGE, "ERROR: option -O is required if counting and "
936 "sampling PMCs are specified together.");
939 * Check if "-k kerneldir" was specified, and if whether
940 * 'kerneldir' actually refers to a a file. If so, use
941 * `dirname path` to determine the kernel directory.
943 if (args.pa_flags & FLAG_HAS_KERNELPATH) {
944 (void) snprintf(buffer, sizeof(buffer), "%s%s", args.pa_fsroot,
946 if (stat(buffer, &sb) < 0)
947 err(EX_OSERR, "ERROR: Cannot locate kernel \"%s\"",
949 if (!S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode))
950 errx(EX_USAGE, "ERROR: \"%s\": Unsupported file type.",
952 if (!S_ISDIR(sb.st_mode)) {
953 tmp = args.pa_kernel;
954 args.pa_kernel = strdup(dirname(args.pa_kernel));
956 (void) snprintf(buffer, sizeof(buffer), "%s%s",
957 args.pa_fsroot, args.pa_kernel);
958 if (stat(buffer, &sb) < 0)
959 err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
961 if (!S_ISDIR(sb.st_mode))
962 errx(EX_USAGE, "ERROR: \"%s\" is not a "
963 "directory.", buffer);
968 * If we have a callgraph be created, select the outputfile.
970 if (args.pa_flags & FLAG_DO_CALLGRAPHS) {
971 if (strcmp(graphfilename, "-") == 0)
972 args.pa_graphfile = args.pa_printfile;
974 args.pa_graphfile = fopen(graphfilename, "w");
975 if (args.pa_graphfile == NULL)
976 err(EX_OSERR, "ERROR: cannot open \"%s\" "
977 "for writing", graphfilename);
981 /* if we've been asked to process a log file, do that and exit */
982 if (args.pa_flags & FLAG_READ_LOGFILE) {
984 * Print the log in textual form if we haven't been
985 * asked to generate profiling information.
987 if ((args.pa_flags & FLAG_DO_ANALYSIS) == 0)
988 args.pa_flags |= FLAG_DO_PRINT;
990 pmcstat_initialize_logging(&args);
991 args.pa_logfd = pmcstat_open_log(args.pa_inputpath,
992 PMCSTAT_OPEN_FOR_READ);
993 if ((args.pa_logparser = pmclog_open(args.pa_logfd)) == NULL)
994 err(EX_OSERR, "ERROR: Cannot create parser");
995 pmcstat_process_log(&args);
996 pmcstat_shutdown_logging(&args);
1000 /* otherwise, we've been asked to collect data */
1003 "ERROR: Initialization of the pmc(3) library failed");
1005 if ((npmc = pmc_npmc(0)) < 0) /* assume all CPUs are identical */
1006 err(EX_OSERR, "ERROR: Cannot determine the number of PMCs "
1009 /* Allocate a kqueue */
1010 if ((pmcstat_kq = kqueue()) < 0)
1011 err(EX_OSERR, "ERROR: Cannot allocate kqueue");
1014 * Configure the specified log file or setup a default log
1015 * consumer via a pipe.
1017 if (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) {
1018 if (args.pa_outputpath)
1019 args.pa_logfd = pmcstat_open_log(args.pa_outputpath,
1020 PMCSTAT_OPEN_FOR_WRITE);
1023 * process the log on the fly by reading it in
1026 if (pipe(pipefd) < 0)
1027 err(EX_OSERR, "ERROR: pipe(2) failed");
1029 if (fcntl(pipefd[READPIPEFD], F_SETFL, O_NONBLOCK) < 0)
1030 err(EX_OSERR, "ERROR: fcntl(2) failed");
1032 EV_SET(&kev, pipefd[READPIPEFD], EVFILT_READ, EV_ADD,
1035 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1036 err(EX_OSERR, "ERROR: Cannot register kevent");
1038 args.pa_logfd = pipefd[WRITEPIPEFD];
1040 args.pa_flags |= (FLAG_HAS_PIPE | FLAG_DO_PRINT);
1041 args.pa_logparser = pmclog_open(pipefd[READPIPEFD]);
1044 if (pmc_configure_logfile(args.pa_logfd) < 0)
1045 err(EX_OSERR, "ERROR: Cannot configure log file");
1048 /* remember to check for driver errors if we are sampling or logging */
1049 check_driver_stats = (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) ||
1050 (args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE);
1056 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
1057 if (pmc_allocate(ev->ev_spec, ev->ev_mode,
1058 ev->ev_flags, ev->ev_cpu, &ev->ev_pmcid) < 0)
1059 err(EX_OSERR, "ERROR: Cannot allocate %s-mode pmc with "
1060 "specification \"%s\"",
1061 PMC_IS_SYSTEM_MODE(ev->ev_mode) ? "system" : "process",
1064 if (PMC_IS_SAMPLING_MODE(ev->ev_mode) &&
1065 pmc_set(ev->ev_pmcid, ev->ev_count) < 0)
1066 err(EX_OSERR, "ERROR: Cannot set sampling count "
1067 "for PMC \"%s\"", ev->ev_name);
1070 /* compute printout widths */
1071 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
1076 (void) pmc_width(ev->ev_pmcid, &counter_width);
1077 header_width = strlen(ev->ev_name) + 2; /* prefix '%c/' */
1078 display_width = (int) floor(counter_width / 3.32193) + 1;
1080 if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
1081 header_width += 3; /* 2 digit CPU number + '/' */
1083 if (header_width > display_width) {
1084 ev->ev_fieldskip = 0;
1085 ev->ev_fieldwidth = header_width;
1087 ev->ev_fieldskip = display_width -
1089 ev->ev_fieldwidth = display_width;
1094 * If our output is being set to a terminal, register a handler
1095 * for window size changes.
1098 if (isatty(fileno(args.pa_printfile))) {
1100 if (ioctl(fileno(args.pa_printfile), TIOCGWINSZ, &ws) < 0)
1101 err(EX_OSERR, "ERROR: Cannot determine window size");
1103 pmcstat_displayheight = ws.ws_row - 1;
1105 EV_SET(&kev, SIGWINCH, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1107 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1108 err(EX_OSERR, "ERROR: Cannot register kevent for "
1112 EV_SET(&kev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1113 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1114 err(EX_OSERR, "ERROR: Cannot register kevent for SIGINT");
1116 EV_SET(&kev, SIGIO, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1117 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1118 err(EX_OSERR, "ERROR: Cannot register kevent for SIGIO");
1121 * An exec() failure of a forked child is signalled by the
1122 * child sending the parent a SIGCHLD. We don't register an
1123 * actual signal handler for SIGCHLD, but instead use our
1124 * kqueue to pick up the signal.
1126 EV_SET(&kev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1127 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1128 err(EX_OSERR, "ERROR: Cannot register kevent for SIGCHLD");
1130 /* setup a timer if we have counting mode PMCs needing to be printed */
1131 if ((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
1132 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) {
1133 EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0,
1134 args.pa_interval * 1000, NULL);
1136 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1137 err(EX_OSERR, "ERROR: Cannot register kevent for "
1141 /* attach PMCs to the target process, starting it if specified */
1142 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1143 pmcstat_create_process(&args);
1145 if (check_driver_stats && pmc_get_driver_stats(&ds_start) < 0)
1146 err(EX_OSERR, "ERROR: Cannot retrieve driver statistics");
1148 /* Attach process pmcs to the target process. */
1149 if (args.pa_flags & (FLAG_HAS_TARGET | FLAG_HAS_COMMANDLINE)) {
1150 if (SLIST_EMPTY(&args.pa_targets))
1151 errx(EX_DATAERR, "ERROR: No matching target "
1153 if (args.pa_flags & FLAG_HAS_PROCESS_PMCS)
1154 pmcstat_attach_pmcs(&args);
1157 kvm_close(pmcstat_kvm);
1162 /* start the pmcs */
1163 pmcstat_start_pmcs(&args);
1165 /* start the (commandline) process if needed */
1166 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1167 pmcstat_start_process();
1169 /* initialize logging if printing the configured log */
1170 if ((args.pa_flags & FLAG_DO_PRINT) &&
1171 (args.pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE)))
1172 pmcstat_initialize_logging(&args);
1174 /* Handle SIGINT using the kqueue loop */
1175 sa.sa_handler = SIG_IGN;
1177 (void) sigemptyset(&sa.sa_mask);
1179 if (sigaction(SIGINT, &sa, NULL) < 0)
1180 err(EX_OSERR, "ERROR: Cannot install signal handler");
1183 * loop till either the target process (if any) exits, or we
1184 * are killed by a SIGINT.
1186 runstate = PMCSTAT_RUNNING;
1189 if ((c = kevent(pmcstat_kq, NULL, 0, &kev, 1, NULL)) <= 0) {
1191 err(EX_OSERR, "ERROR: kevent failed");
1196 if (kev.flags & EV_ERROR)
1197 errc(EX_OSERR, kev.data, "ERROR: kevent failed");
1199 switch (kev.filter) {
1200 case EVFILT_PROC: /* target has exited */
1201 if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE |
1203 runstate = pmcstat_close_log(&args);
1205 runstate = PMCSTAT_FINISHED;
1209 case EVFILT_READ: /* log file data is present */
1210 runstate = pmcstat_process_log(&args);
1214 if (kev.ident == SIGCHLD) {
1216 * The child process sends us a
1217 * SIGCHLD if its exec() failed. We
1218 * wait for it to exit and then exit
1222 runstate = PMCSTAT_FINISHED;
1223 } else if (kev.ident == SIGIO) {
1225 * We get a SIGIO if a PMC loses all
1226 * of its targets, or if logfile
1227 * writes encounter an error.
1229 if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE |
1231 runstate = pmcstat_close_log(&args);
1233 (FLAG_DO_PRINT|FLAG_DO_ANALYSIS))
1234 pmcstat_process_log(&args);
1236 do_print = 1; /* print PMCs at exit */
1237 runstate = PMCSTAT_FINISHED;
1238 } else if (kev.ident == SIGINT) {
1239 /* Kill the child process if we started it */
1240 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1241 pmcstat_kill_process(&args);
1242 runstate = PMCSTAT_FINISHED;
1243 } else if (kev.ident == SIGWINCH) {
1244 if (ioctl(fileno(args.pa_printfile),
1245 TIOCGWINSZ, &ws) < 0)
1246 err(EX_OSERR, "ERROR: Cannot determine "
1248 pmcstat_displayheight = ws.ws_row - 1;
1254 case EVFILT_TIMER: /* print out counting PMCs */
1261 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) {
1262 pmcstat_print_pmcs(&args);
1263 if (runstate == PMCSTAT_FINISHED && /* final newline */
1264 (args.pa_flags & FLAG_DO_PRINT) == 0)
1265 (void) fprintf(args.pa_printfile, "\n");
1269 } while (runstate != PMCSTAT_FINISHED);
1271 /* flush any pending log entries */
1272 if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE))
1273 pmc_flush_logfile();
1275 pmcstat_cleanup(&args);
1277 free(args.pa_kernel);
1279 /* check if the driver lost any samples or events */
1280 if (check_driver_stats) {
1281 if (pmc_get_driver_stats(&ds_end) < 0)
1282 err(EX_OSERR, "ERROR: Cannot retrieve driver "
1284 if (ds_start.pm_intr_bufferfull != ds_end.pm_intr_bufferfull &&
1285 args.pa_verbosity > 0)
1286 warnx("WARNING: some samples were dropped. Please "
1287 "consider tuning the \"kern.hwpmc.nsamples\" "
1289 if (ds_start.pm_buffer_requests_failed !=
1290 ds_end.pm_buffer_requests_failed &&
1291 args.pa_verbosity > 0)
1292 warnx("WARNING: some events were discarded. Please "
1293 "consider tuning the \"kern.hwpmc.nbuffers\" "