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>
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 * - Recieves signal, attempts exec().
103 * After this point normal processing can happen.
108 int pmcstat_interrupt = 0;
109 int pmcstat_displayheight = DEFAULT_DISPLAY_HEIGHT;
110 int pmcstat_displaywidth = DEFAULT_DISPLAY_WIDTH;
111 int pmcstat_sockpair[NSOCKPAIRFD];
114 struct kinfo_proc *pmcstat_plist;
115 struct pmcstat_args args;
118 pmcstat_attach_pmcs(void)
120 struct pmcstat_ev *ev;
121 struct pmcstat_target *pt;
124 /* Attach all process PMCs to target processes. */
126 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
127 if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
129 SLIST_FOREACH(pt, &args.pa_targets, pt_next)
130 if (pmc_attach(ev->ev_pmcid, pt->pt_pid) == 0)
132 else if (errno != ESRCH)
133 err(EX_OSERR, "ERROR: cannot attach pmc "
134 "\"%s\" to process %d", ev->ev_name,
139 errx(EX_DATAERR, "ERROR: No processes were attached to.");
144 pmcstat_cleanup(void)
146 struct pmcstat_ev *ev, *tmp;
148 /* release allocated PMCs. */
149 STAILQ_FOREACH_SAFE(ev, &args.pa_events, ev_next, tmp)
150 if (ev->ev_pmcid != PMC_ID_INVALID) {
151 if (pmc_stop(ev->ev_pmcid) < 0)
152 err(EX_OSERR, "ERROR: cannot stop pmc 0x%x "
153 "\"%s\"", ev->ev_pmcid, ev->ev_name);
154 if (pmc_release(ev->ev_pmcid) < 0)
155 err(EX_OSERR, "ERROR: cannot release pmc "
156 "0x%x \"%s\"", ev->ev_pmcid, ev->ev_name);
159 STAILQ_REMOVE(&args.pa_events, ev, pmcstat_ev, ev_next);
163 /* de-configure the log file if present. */
164 if (args.pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))
165 (void) pmc_configure_logfile(-1);
167 if (args.pa_logparser) {
168 pmclog_close(args.pa_logparser);
169 args.pa_logparser = NULL;
172 pmcstat_shutdown_logging();
176 pmcstat_clone_event_descriptor(struct pmcstat_ev *ev,
180 struct pmcstat_ev *ev_clone;
182 while ((cpu = ffs(cpumask)) > 0) {
185 if ((ev_clone = malloc(sizeof(*ev_clone))) == NULL)
186 errx(EX_SOFTWARE, "ERROR: Out of memory");
187 (void) memset(ev_clone, 0, sizeof(*ev_clone));
189 ev_clone->ev_count = ev->ev_count;
190 ev_clone->ev_cpu = cpu;
191 ev_clone->ev_cumulative = ev->ev_cumulative;
192 ev_clone->ev_flags = ev->ev_flags;
193 ev_clone->ev_mode = ev->ev_mode;
194 ev_clone->ev_name = strdup(ev->ev_name);
195 ev_clone->ev_pmcid = ev->ev_pmcid;
196 ev_clone->ev_saved = ev->ev_saved;
197 ev_clone->ev_spec = strdup(ev->ev_spec);
199 STAILQ_INSERT_TAIL(&args.pa_events, ev_clone, ev_next);
201 cpumask &= ~(1 << cpu);
206 pmcstat_create_process(void)
211 struct pmcstat_target *pt;
213 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pmcstat_sockpair) < 0)
214 err(EX_OSERR, "ERROR: cannot create socket pair");
216 switch (pid = fork()) {
218 err(EX_OSERR, "ERROR: cannot fork");
222 (void) close(pmcstat_sockpair[PARENTSOCKET]);
224 /* Write a token to tell our parent we've started executing. */
225 if (write(pmcstat_sockpair[CHILDSOCKET], "+", 1) != 1)
226 err(EX_OSERR, "ERROR (child): cannot write token");
228 /* Wait for our parent to signal us to start. */
229 if (read(pmcstat_sockpair[CHILDSOCKET], &token, 1) < 0)
230 err(EX_OSERR, "ERROR (child): cannot read token");
231 (void) close(pmcstat_sockpair[CHILDSOCKET]);
233 /* exec() the program requested */
234 execvp(*args.pa_argv, args.pa_argv);
235 /* and if that fails, notify the parent */
236 kill(getppid(), SIGCHLD);
237 err(EX_OSERR, "ERROR: execvp \"%s\" failed", *args.pa_argv);
240 default: /* parent */
241 (void) close(pmcstat_sockpair[CHILDSOCKET]);
245 /* Ask to be notified via a kevent when the target process exits. */
246 EV_SET(&kev, pid, EVFILT_PROC, EV_ADD|EV_ONESHOT, NOTE_EXIT, 0,
248 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
249 err(EX_OSERR, "ERROR: cannot monitor child process %d", pid);
251 if ((pt = malloc(sizeof(*pt))) == NULL)
252 errx(EX_SOFTWARE, "ERROR: Out of memory.");
255 SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
257 /* Wait for the child to signal that its ready to go. */
258 if (read(pmcstat_sockpair[PARENTSOCKET], &token, 1) < 0)
259 err(EX_OSERR, "ERROR (parent): cannot read token");
265 pmcstat_find_targets(const char *spec)
267 int n, nproc, pid, rv;
268 struct pmcstat_target *pt;
269 char errbuf[_POSIX2_LINE_MAX], *end;
270 static struct kinfo_proc *kp;
274 /* First check if we've been given a process id. */
275 pid = strtol(spec, &end, 0);
276 if (end != spec && pid >= 0) {
277 if ((pt = malloc(sizeof(*pt))) == NULL)
280 SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
284 /* Otherwise treat arg as a regular expression naming processes. */
285 if (pmcstat_kvm == NULL) {
286 if ((pmcstat_kvm = kvm_openfiles(NULL, "/dev/null", NULL, 0,
288 err(EX_OSERR, "ERROR: Cannot open kernel \"%s\"",
290 if ((pmcstat_plist = kvm_getprocs(pmcstat_kvm, KERN_PROC_PROC,
292 err(EX_OSERR, "ERROR: Cannot get process list: %s",
293 kvm_geterr(pmcstat_kvm));
297 if ((rv = regcomp(®, spec, REG_EXTENDED|REG_NOSUB)) != 0) {
298 regerror(rv, ®, errbuf, sizeof(errbuf));
299 err(EX_DATAERR, "ERROR: Failed to compile regex \"%s\": %s",
303 for (n = 0, kp = pmcstat_plist; n < nproc; n++, kp++) {
304 if ((rv = regexec(®, kp->ki_comm, 1, ®match, 0)) == 0) {
305 if ((pt = malloc(sizeof(*pt))) == NULL)
307 pt->pt_pid = kp->ki_pid;
308 SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
309 } else if (rv != REG_NOMATCH) {
310 regerror(rv, ®, errbuf, sizeof(errbuf));
311 errx(EX_SOFTWARE, "ERROR: Regex evalation failed: %s",
321 errx(EX_SOFTWARE, "Out of memory.");
326 pmcstat_get_cpumask(const char *cpuspec)
337 cpu = strtol(s, &end, 0);
338 if (cpu < 0 || end == s)
339 errx(EX_USAGE, "ERROR: Illegal CPU specification "
341 cpumask |= (1 << cpu);
342 s = end + strspn(end, ", \t");
349 pmcstat_kill_process(void)
351 struct pmcstat_target *pt;
353 assert(args.pa_flags & FLAG_HAS_COMMANDLINE);
356 * If a command line was specified, it would be the very first
357 * in the list, before any other processes specified by -t.
359 pt = SLIST_FIRST(&args.pa_targets);
362 if (kill(pt->pt_pid, SIGINT) != 0)
363 err(EX_OSERR, "ERROR: cannot signal child process");
367 pmcstat_start_pmcs(void)
369 struct pmcstat_ev *ev;
371 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
373 assert(ev->ev_pmcid != PMC_ID_INVALID);
375 if (pmc_start(ev->ev_pmcid) < 0) {
376 warn("ERROR: Cannot start pmc 0x%x \"%s\"",
377 ev->ev_pmcid, ev->ev_name);
386 pmcstat_print_headers(void)
388 struct pmcstat_ev *ev;
391 (void) fprintf(args.pa_printfile, PRINT_HEADER_PREFIX);
393 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
394 if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
397 c = PMC_IS_SYSTEM_MODE(ev->ev_mode) ? 's' : 'p';
399 if (ev->ev_fieldskip != 0)
400 (void) fprintf(args.pa_printfile, "%*s",
401 ev->ev_fieldskip, "");
402 w = ev->ev_fieldwidth - ev->ev_fieldskip - 2;
405 (void) fprintf(args.pa_printfile, "s/%02d/%-*s ",
406 ev->ev_cpu, w-3, ev->ev_name);
408 (void) fprintf(args.pa_printfile, "p/%*s ", w,
412 (void) fflush(args.pa_printfile);
416 pmcstat_print_counters(void)
419 struct pmcstat_ev *ev;
422 extra_width = sizeof(PRINT_HEADER_PREFIX) - 1;
424 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
426 /* skip sampling mode counters */
427 if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
430 if (pmc_read(ev->ev_pmcid, &value) < 0)
431 err(EX_OSERR, "ERROR: Cannot read pmc "
432 "\"%s\"", ev->ev_name);
434 (void) fprintf(args.pa_printfile, "%*ju ",
435 ev->ev_fieldwidth + extra_width,
436 (uintmax_t) ev->ev_cumulative ? value :
437 (value - ev->ev_saved));
439 if (ev->ev_cumulative == 0)
440 ev->ev_saved = value;
444 (void) fflush(args.pa_printfile);
452 pmcstat_print_pmcs(void)
454 static int linecount = 0;
456 /* check if we need to print a header line */
457 if (++linecount > pmcstat_displayheight) {
458 (void) fprintf(args.pa_printfile, "\n");
462 pmcstat_print_headers();
463 (void) fprintf(args.pa_printfile, "\n");
465 pmcstat_print_counters();
471 * Do process profiling
473 * If a pid was specified, attach each allocated PMC to the target
474 * process. Otherwise, fork a child and attach the PMCs to the child,
475 * and have the child exec() the target program.
479 pmcstat_start_process(void)
481 /* Signal the child to proceed. */
482 if (write(pmcstat_sockpair[PARENTSOCKET], "!", 1) != 1)
483 err(EX_OSERR, "ERROR (parent): write of token failed");
485 (void) close(pmcstat_sockpair[PARENTSOCKET]);
489 pmcstat_show_usage(void)
492 "[options] [commandline]\n"
493 "\t Measure process and/or system performance using hardware\n"
494 "\t performance monitoring counters.\n"
495 "\t Options include:\n"
496 "\t -C\t\t (toggle) show cumulative counts\n"
497 "\t -D path\t create profiles in directory \"path\"\n"
498 "\t -E\t\t (toggle) show counts at process exit\n"
499 "\t -F file\t write a system-wide callgraph (Kcachegrind format)"
501 "\t -G file\t write a system-wide callgraph to \"file\"\n"
502 "\t -M file\t print executable/gmon file map to \"file\"\n"
503 "\t -N\t\t (toggle) capture callchains\n"
504 "\t -O file\t send log output to \"file\"\n"
505 "\t -P spec\t allocate a process-private sampling PMC\n"
506 "\t -R file\t read events from \"file\"\n"
507 "\t -S spec\t allocate a system-wide sampling PMC\n"
508 "\t -T\t\t start in top mode\n"
509 "\t -W\t\t (toggle) show counts per context switch\n"
510 "\t -c cpu-list\t set cpus for subsequent system-wide PMCs\n"
511 "\t -d\t\t (toggle) track descendants\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 -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)
555 int option, npmc, ncpu, haltedcpus;
556 int c, check_driver_stats, current_cpu, current_sampling_count;
557 int do_callchain, do_descendants, do_logproccsw, do_logprocexit;
558 int do_print, do_read;
562 int use_cumulative_counts;
566 const char *errmsg, *graphfilename;
567 enum pmcstat_state runstate;
568 struct pmc_driverstats ds_start, ds_end;
569 struct pmcstat_ev *ev;
574 char buffer[PATH_MAX];
576 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_kernel = strdup("/boot/kernel");
591 args.pa_samplesdir = ".";
592 args.pa_printfile = stderr;
593 args.pa_graphdepth = DEFAULT_CALLGRAPH_DEPTH;
594 args.pa_graphfile = NULL;
595 args.pa_interval = DEFAULT_WAIT_INTERVAL;
596 args.pa_mapfilename = NULL;
597 args.pa_inputpath = NULL;
598 args.pa_outputpath = NULL;
599 args.pa_pplugin = PMCSTAT_PL_NONE;
600 args.pa_plugin = PMCSTAT_PL_NONE;
601 args.pa_ctdumpinstr = 1;
602 args.pa_topmode = PMCSTAT_TOP_DELTA;
604 args.pa_topcolor = 0;
605 args.pa_mergepmc = 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 * The initial CPU mask specifies all non-halted CPUS in the
617 if (sysctlbyname("hw.ncpu", &ncpu, &dummy, NULL, 0) < 0)
618 err(EX_OSERR, "ERROR: Cannot determine the number of CPUs");
619 cpumask = (1 << ncpu) - 1;
622 if (sysctlbyname("machdep.hlt_cpus", &haltedcpus, &dummy,
624 err(EX_OSERR, "ERROR: Cannot determine which CPUs are "
626 cpumask &= ~haltedcpus;
629 while ((option = getopt(argc, argv,
630 "CD:EF:G:M:NO:P:R:S:TWc:df:gk:m:n:o:p:qr:s:t:vw:z:")) != -1)
632 case 'C': /* cumulative values */
633 use_cumulative_counts = !use_cumulative_counts;
634 args.pa_required |= FLAG_HAS_COUNTING_PMCS;
639 if (optarg[0] == '*' && optarg[1] == '\0')
640 cpumask = ((1 << ncpu) - 1) & ~haltedcpus;
642 cpumask = pmcstat_get_cpumask(optarg);
644 args.pa_flags |= FLAGS_HAS_CPUMASK;
645 args.pa_required |= FLAG_HAS_SYSTEM_PMCS;
649 if (stat(optarg, &sb) < 0)
650 err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
652 if (!S_ISDIR(sb.st_mode))
653 errx(EX_USAGE, "ERROR: \"%s\" is not a "
654 "directory.", optarg);
655 args.pa_samplesdir = optarg;
656 args.pa_flags |= FLAG_HAS_SAMPLESDIR;
657 args.pa_required |= FLAG_DO_GPROF;
660 case 'd': /* toggle descendents */
661 do_descendants = !do_descendants;
662 args.pa_required |= FLAG_HAS_PROCESS_PMCS;
665 case 'F': /* produce a system-wide calltree */
666 args.pa_flags |= FLAG_DO_CALLGRAPHS;
667 args.pa_plugin = PMCSTAT_PL_CALLTREE;
668 graphfilename = optarg;
671 case 'f': /* plugins options */
672 if (args.pa_plugin == PMCSTAT_PL_NONE)
673 err(EX_USAGE, "ERROR: Need -g/-G/-m/-T.");
674 pmcstat_pluginconfigure_log(optarg);
677 case 'G': /* produce a system-wide callgraph */
678 args.pa_flags |= FLAG_DO_CALLGRAPHS;
679 args.pa_plugin = PMCSTAT_PL_CALLGRAPH;
680 graphfilename = optarg;
683 case 'g': /* produce gprof compatible profiles */
684 args.pa_flags |= FLAG_DO_GPROF;
685 args.pa_pplugin = PMCSTAT_PL_CALLGRAPH;
686 args.pa_plugin = PMCSTAT_PL_GPROF;
689 case 'k': /* pathname to the kernel */
690 free(args.pa_kernel);
691 args.pa_kernel = strdup(optarg);
692 args.pa_required |= FLAG_DO_ANALYSIS;
693 args.pa_flags |= FLAG_HAS_KERNELPATH;
697 args.pa_flags |= FLAG_DO_ANNOTATE;
698 args.pa_plugin = PMCSTAT_PL_ANNOTATE;
699 graphfilename = optarg;
702 case 'E': /* log process exit */
703 do_logprocexit = !do_logprocexit;
704 args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
705 FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
708 case 'M': /* mapfile */
709 args.pa_mapfilename = optarg;
713 do_callchain = !do_callchain;
714 args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
717 case 'p': /* process virtual counting PMC */
718 case 's': /* system-wide counting PMC */
719 case 'P': /* process virtual sampling PMC */
720 case 'S': /* system-wide sampling PMC */
721 if ((ev = malloc(sizeof(*ev))) == NULL)
722 errx(EX_SOFTWARE, "ERROR: Out of memory.");
725 case 'p': ev->ev_mode = PMC_MODE_TC; break;
726 case 's': ev->ev_mode = PMC_MODE_SC; break;
727 case 'P': ev->ev_mode = PMC_MODE_TS; break;
728 case 'S': ev->ev_mode = PMC_MODE_SS; break;
731 if (option == 'P' || option == 'p') {
732 args.pa_flags |= FLAG_HAS_PROCESS_PMCS;
733 args.pa_required |= (FLAG_HAS_COMMANDLINE |
737 if (option == 'P' || option == 'S') {
738 args.pa_flags |= FLAG_HAS_SAMPLING_PMCS;
739 args.pa_required |= (FLAG_HAS_PIPE |
740 FLAG_HAS_OUTPUT_LOGFILE);
743 if (option == 'p' || option == 's')
744 args.pa_flags |= FLAG_HAS_COUNTING_PMCS;
746 if (option == 's' || option == 'S')
747 args.pa_flags |= FLAG_HAS_SYSTEM_PMCS;
749 ev->ev_spec = strdup(optarg);
751 if (option == 'S' || option == 'P')
752 ev->ev_count = current_sampling_count;
756 if (option == 'S' || option == 's')
757 ev->ev_cpu = ffs(cpumask) - 1;
759 ev->ev_cpu = PMC_CPU_ANY;
763 ev->ev_flags |= PMC_F_CALLCHAIN;
765 ev->ev_flags |= PMC_F_DESCENDANTS;
767 ev->ev_flags |= PMC_F_LOG_PROCEXIT;
769 ev->ev_flags |= PMC_F_LOG_PROCCSW;
771 ev->ev_cumulative = use_cumulative_counts;
774 ev->ev_pmcid = PMC_ID_INVALID;
776 /* extract event name */
777 c = strcspn(optarg, ", \t");
778 ev->ev_name = malloc(c + 1);
779 (void) strncpy(ev->ev_name, optarg, c);
780 *(ev->ev_name + c) = '\0';
782 STAILQ_INSERT_TAIL(&args.pa_events, ev, ev_next);
784 if (option == 's' || option == 'S')
785 pmcstat_clone_event_descriptor(ev,
786 cpumask & ~(1 << ev->ev_cpu));
790 case 'n': /* sampling count */
791 current_sampling_count = strtol(optarg, &end, 0);
792 if (*end != '\0' || current_sampling_count <= 0)
794 "ERROR: Illegal count value \"%s\".",
796 args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
799 case 'o': /* outputfile */
800 if (args.pa_printfile != NULL &&
801 args.pa_printfile != stdout &&
802 args.pa_printfile != stderr)
803 (void) fclose(args.pa_printfile);
804 if ((args.pa_printfile = fopen(optarg, "w")) == NULL)
805 errx(EX_OSERR, "ERROR: cannot open \"%s\" for "
807 args.pa_flags |= FLAG_DO_PRINT;
810 case 'O': /* sampling output */
811 if (args.pa_outputpath)
812 errx(EX_USAGE, "ERROR: option -O may only be "
814 args.pa_outputpath = optarg;
815 args.pa_flags |= FLAG_HAS_OUTPUT_LOGFILE;
818 case 'q': /* quiet mode */
819 args.pa_verbosity = 0;
822 case 'r': /* root FS path */
823 args.pa_fsroot = optarg;
826 case 'R': /* read an existing log file */
827 if (args.pa_inputpath != NULL)
828 errx(EX_USAGE, "ERROR: option -R may only be "
830 args.pa_inputpath = optarg;
831 if (args.pa_printfile == stderr)
832 args.pa_printfile = stdout;
833 args.pa_flags |= FLAG_READ_LOGFILE;
836 case 't': /* target pid or process name */
837 pmcstat_find_targets(optarg);
839 args.pa_flags |= FLAG_HAS_TARGET;
840 args.pa_required |= FLAG_HAS_PROCESS_PMCS;
843 case 'T': /* top mode */
844 args.pa_flags |= FLAG_DO_TOP;
845 args.pa_plugin = PMCSTAT_PL_CALLGRAPH;
846 args.pa_ctdumpinstr = 0;
847 args.pa_mergepmc = 1;
848 if (args.pa_printfile == stderr)
849 args.pa_printfile = stdout;
852 case 'v': /* verbose */
856 case 'w': /* wait interval */
857 interval = strtod(optarg, &end);
858 if (*end != '\0' || interval <= 0)
859 errx(EX_USAGE, "ERROR: Illegal wait interval "
860 "value \"%s\".", optarg);
861 args.pa_flags |= FLAG_HAS_WAIT_INTERVAL;
862 args.pa_interval = interval;
865 case 'W': /* toggle LOG_CSW */
866 do_logproccsw = !do_logproccsw;
867 args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
868 FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
872 graphdepth = strtod(optarg, &end);
873 if (*end != '\0' || graphdepth <= 0)
874 errx(EX_USAGE, "ERROR: Illegal callchain "
875 "depth \"%s\".", optarg);
876 args.pa_graphdepth = graphdepth;
877 args.pa_required |= FLAG_DO_CALLGRAPHS;
882 pmcstat_show_usage();
887 args.pa_argc = (argc -= optind);
888 args.pa_argv = (argv += optind);
890 /* If we read from logfile and no specified CPU mask use
891 * the maximum CPU count.
893 if ((args.pa_flags & FLAG_READ_LOGFILE) &&
894 (args.pa_flags & FLAGS_HAS_CPUMASK) == 0)
895 cpumask = 0xffffffff;
897 args.pa_cpumask = cpumask; /* For selecting CPUs using -R. */
899 if (argc) /* command line present */
900 args.pa_flags |= FLAG_HAS_COMMANDLINE;
902 if (args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS |
903 FLAG_DO_ANNOTATE | FLAG_DO_TOP))
904 args.pa_flags |= FLAG_DO_ANALYSIS;
907 * Check invocation syntax.
910 /* disallow -O and -R together */
911 if (args.pa_outputpath && args.pa_inputpath)
912 errx(EX_USAGE, "ERROR: options -O and -R are mutually "
915 /* -m option is allowed with -R only. */
916 if (args.pa_flags & FLAG_DO_ANNOTATE && args.pa_inputpath == NULL)
917 errx(EX_USAGE, "ERROR: option -m requires an input file");
919 /* -m option is not allowed combined with -g or -G. */
920 if (args.pa_flags & FLAG_DO_ANNOTATE &&
921 args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS))
922 errx(EX_USAGE, "ERROR: option -m and -g | -G are mutually "
925 if (args.pa_flags & FLAG_READ_LOGFILE) {
927 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
928 errmsg = "a command line specification";
929 else if (args.pa_flags & FLAG_HAS_TARGET)
930 errmsg = "option -t";
931 else if (!STAILQ_EMPTY(&args.pa_events))
932 errmsg = "a PMC event specification";
934 errx(EX_USAGE, "ERROR: option -R may not be used with "
936 } else if (STAILQ_EMPTY(&args.pa_events))
937 /* All other uses require a PMC spec. */
938 pmcstat_show_usage();
940 /* check for -t pid without a process PMC spec */
941 if ((args.pa_required & FLAG_HAS_TARGET) &&
942 (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
943 errx(EX_USAGE, "ERROR: option -t requires a process mode PMC "
946 /* check for process-mode options without a command or -t pid */
947 if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
948 (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
949 errx(EX_USAGE, "ERROR: options -d, -E, -p, -P, and -W require "
950 "a command line or target process.");
952 /* check for -p | -P without a target process of some sort */
953 if ((args.pa_required & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) &&
954 (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
955 errx(EX_USAGE, "ERROR: options -P and -p require a "
956 "target process or a command line.");
958 /* check for process-mode options without a process-mode PMC */
959 if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
960 (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
961 errx(EX_USAGE, "ERROR: options -d, -E, and -W require a "
962 "process mode PMC to be specified.");
964 /* check for -c cpu with no system mode PMCs or logfile. */
965 if ((args.pa_required & FLAG_HAS_SYSTEM_PMCS) &&
966 (args.pa_flags & FLAG_HAS_SYSTEM_PMCS) == 0 &&
967 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
968 errx(EX_USAGE, "ERROR: option -c requires at least one "
969 "system mode PMC to be specified.");
971 /* check for counting mode options without a counting PMC */
972 if ((args.pa_required & FLAG_HAS_COUNTING_PMCS) &&
973 (args.pa_flags & FLAG_HAS_COUNTING_PMCS) == 0)
974 errx(EX_USAGE, "ERROR: options -C, -W and -o require at "
975 "least one counting mode PMC to be specified.");
977 /* check for sampling mode options without a sampling PMC spec */
978 if ((args.pa_required & FLAG_HAS_SAMPLING_PMCS) &&
979 (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) == 0)
980 errx(EX_USAGE, "ERROR: options -N, -n and -O require at "
981 "least one sampling mode PMC to be specified.");
983 /* check if -g/-G/-m/-T are being used correctly */
984 if ((args.pa_flags & FLAG_DO_ANALYSIS) &&
985 !(args.pa_flags & (FLAG_HAS_SAMPLING_PMCS|FLAG_READ_LOGFILE)))
986 errx(EX_USAGE, "ERROR: options -g/-G/-m/-T require sampling PMCs "
987 "or -R to be specified.");
989 /* check if -O was spuriously specified */
990 if ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) &&
991 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0)
993 "ERROR: option -O is used only with options "
994 "-E, -P, -S and -W.");
996 /* -k kernel path require -g/-G/-m/-T or -R */
997 if ((args.pa_flags & FLAG_HAS_KERNELPATH) &&
998 (args.pa_flags & FLAG_DO_ANALYSIS) == 0 &&
999 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
1000 errx(EX_USAGE, "ERROR: option -k is only used with -g/-R/-m/-T.");
1002 /* -D only applies to gprof output mode (-g) */
1003 if ((args.pa_flags & FLAG_HAS_SAMPLESDIR) &&
1004 (args.pa_flags & FLAG_DO_GPROF) == 0)
1005 errx(EX_USAGE, "ERROR: option -D is only used with -g.");
1007 /* -M mapfile requires -g or -R */
1008 if (args.pa_mapfilename != NULL &&
1009 (args.pa_flags & FLAG_DO_GPROF) == 0 &&
1010 (args.pa_flags & FLAG_READ_LOGFILE) == 0)
1011 errx(EX_USAGE, "ERROR: option -M is only used with -g/-R.");
1014 * Disallow textual output of sampling PMCs if counting PMCs
1015 * have also been asked for, mostly because the combined output
1016 * is difficult to make sense of.
1018 if ((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
1019 (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) &&
1020 ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) == 0))
1021 errx(EX_USAGE, "ERROR: option -O is required if counting and "
1022 "sampling PMCs are specified together.");
1025 * Check if "-k kerneldir" was specified, and if whether
1026 * 'kerneldir' actually refers to a a file. If so, use
1027 * `dirname path` to determine the kernel directory.
1029 if (args.pa_flags & FLAG_HAS_KERNELPATH) {
1030 (void) snprintf(buffer, sizeof(buffer), "%s%s", args.pa_fsroot,
1032 if (stat(buffer, &sb) < 0)
1033 err(EX_OSERR, "ERROR: Cannot locate kernel \"%s\"",
1035 if (!S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode))
1036 errx(EX_USAGE, "ERROR: \"%s\": Unsupported file type.",
1038 if (!S_ISDIR(sb.st_mode)) {
1039 tmp = args.pa_kernel;
1040 args.pa_kernel = strdup(dirname(args.pa_kernel));
1042 (void) snprintf(buffer, sizeof(buffer), "%s%s",
1043 args.pa_fsroot, args.pa_kernel);
1044 if (stat(buffer, &sb) < 0)
1045 err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
1047 if (!S_ISDIR(sb.st_mode))
1048 errx(EX_USAGE, "ERROR: \"%s\" is not a "
1049 "directory.", buffer);
1054 * If we have a callgraph be created, select the outputfile.
1056 if (args.pa_flags & FLAG_DO_CALLGRAPHS) {
1057 if (strcmp(graphfilename, "-") == 0)
1058 args.pa_graphfile = args.pa_printfile;
1060 args.pa_graphfile = fopen(graphfilename, "w");
1061 if (args.pa_graphfile == NULL)
1062 err(EX_OSERR, "ERROR: cannot open \"%s\" "
1063 "for writing", graphfilename);
1066 if (args.pa_flags & FLAG_DO_ANNOTATE) {
1067 args.pa_graphfile = fopen(graphfilename, "w");
1068 if (args.pa_graphfile == NULL)
1069 err(EX_OSERR, "ERROR: cannot open \"%s\" for writing",
1073 /* if we've been asked to process a log file, skip init */
1074 if ((args.pa_flags & FLAG_READ_LOGFILE) == 0) {
1077 "ERROR: Initialization of the pmc(3) library failed");
1079 if ((npmc = pmc_npmc(0)) < 0) /* assume all CPUs are identical */
1080 err(EX_OSERR, "ERROR: Cannot determine the number of PMCs "
1084 /* Allocate a kqueue */
1085 if ((pmcstat_kq = kqueue()) < 0)
1086 err(EX_OSERR, "ERROR: Cannot allocate kqueue");
1088 /* Setup the logfile as the source. */
1089 if (args.pa_flags & FLAG_READ_LOGFILE) {
1091 * Print the log in textual form if we haven't been
1092 * asked to generate profiling information.
1094 if ((args.pa_flags & FLAG_DO_ANALYSIS) == 0)
1095 args.pa_flags |= FLAG_DO_PRINT;
1097 pmcstat_initialize_logging();
1098 rfd = pmcstat_open_log(args.pa_inputpath,
1099 PMCSTAT_OPEN_FOR_READ);
1100 if ((args.pa_logparser = pmclog_open(rfd)) == NULL)
1101 err(EX_OSERR, "ERROR: Cannot create parser");
1102 if (fcntl(rfd, F_SETFL, O_NONBLOCK) < 0)
1103 err(EX_OSERR, "ERROR: fcntl(2) failed");
1104 EV_SET(&kev, rfd, EVFILT_READ, EV_ADD,
1106 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1107 err(EX_OSERR, "ERROR: Cannot register kevent");
1110 * Configure the specified log file or setup a default log
1111 * consumer via a pipe.
1113 if (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) {
1114 if (args.pa_outputpath)
1115 args.pa_logfd = pmcstat_open_log(args.pa_outputpath,
1116 PMCSTAT_OPEN_FOR_WRITE);
1119 * process the log on the fly by reading it in
1122 if (pipe(pipefd) < 0)
1123 err(EX_OSERR, "ERROR: pipe(2) failed");
1125 if (fcntl(pipefd[READPIPEFD], F_SETFL, O_NONBLOCK) < 0)
1126 err(EX_OSERR, "ERROR: fcntl(2) failed");
1128 EV_SET(&kev, pipefd[READPIPEFD], EVFILT_READ, EV_ADD,
1131 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1132 err(EX_OSERR, "ERROR: Cannot register kevent");
1134 args.pa_logfd = pipefd[WRITEPIPEFD];
1136 args.pa_flags |= FLAG_HAS_PIPE;
1137 if ((args.pa_flags & FLAG_DO_TOP) == 0)
1138 args.pa_flags |= FLAG_DO_PRINT;
1139 args.pa_logparser = pmclog_open(pipefd[READPIPEFD]);
1142 if (pmc_configure_logfile(args.pa_logfd) < 0)
1143 err(EX_OSERR, "ERROR: Cannot configure log file");
1146 /* remember to check for driver errors if we are sampling or logging */
1147 check_driver_stats = (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) ||
1148 (args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE);
1151 if (args.pa_flags & FLAG_READ_LOGFILE) {
1155 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
1156 if (pmc_allocate(ev->ev_spec, ev->ev_mode,
1157 ev->ev_flags, ev->ev_cpu, &ev->ev_pmcid) < 0)
1158 err(EX_OSERR, "ERROR: Cannot allocate %s-mode pmc with "
1159 "specification \"%s\"",
1160 PMC_IS_SYSTEM_MODE(ev->ev_mode) ? "system" : "process",
1163 if (PMC_IS_SAMPLING_MODE(ev->ev_mode) &&
1164 pmc_set(ev->ev_pmcid, ev->ev_count) < 0)
1165 err(EX_OSERR, "ERROR: Cannot set sampling count "
1166 "for PMC \"%s\"", ev->ev_name);
1169 /* compute printout widths */
1170 STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
1175 (void) pmc_width(ev->ev_pmcid, &counter_width);
1176 header_width = strlen(ev->ev_name) + 2; /* prefix '%c/' */
1177 display_width = (int) floor(counter_width / 3.32193) + 1;
1179 if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
1180 header_width += 3; /* 2 digit CPU number + '/' */
1182 if (header_width > display_width) {
1183 ev->ev_fieldskip = 0;
1184 ev->ev_fieldwidth = header_width;
1186 ev->ev_fieldskip = display_width -
1188 ev->ev_fieldwidth = display_width;
1193 * If our output is being set to a terminal, register a handler
1194 * for window size changes.
1197 if (isatty(fileno(args.pa_printfile))) {
1199 if (ioctl(fileno(args.pa_printfile), TIOCGWINSZ, &ws) < 0)
1200 err(EX_OSERR, "ERROR: Cannot determine window size");
1202 pmcstat_displayheight = ws.ws_row - 1;
1203 pmcstat_displaywidth = ws.ws_col - 1;
1205 EV_SET(&kev, SIGWINCH, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1207 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1208 err(EX_OSERR, "ERROR: Cannot register kevent for "
1215 * Listen to key input in top mode.
1217 if (args.pa_flags & FLAG_DO_TOP) {
1218 EV_SET(&kev, fileno(stdin), EVFILT_READ, EV_ADD, 0, 0, NULL);
1219 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1220 err(EX_OSERR, "ERROR: Cannot register kevent");
1223 EV_SET(&kev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1224 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1225 err(EX_OSERR, "ERROR: Cannot register kevent for SIGINT");
1227 EV_SET(&kev, SIGIO, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1228 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1229 err(EX_OSERR, "ERROR: Cannot register kevent for SIGIO");
1232 * An exec() failure of a forked child is signalled by the
1233 * child sending the parent a SIGCHLD. We don't register an
1234 * actual signal handler for SIGCHLD, but instead use our
1235 * kqueue to pick up the signal.
1237 EV_SET(&kev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
1238 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1239 err(EX_OSERR, "ERROR: Cannot register kevent for SIGCHLD");
1242 * Setup a timer if we have counting mode PMCs needing to be printed or
1243 * top mode plugin is active.
1245 if (((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
1246 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) ||
1247 (args.pa_flags & FLAG_DO_TOP)) {
1248 EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0,
1249 args.pa_interval * 1000, NULL);
1251 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
1252 err(EX_OSERR, "ERROR: Cannot register kevent for "
1256 /* attach PMCs to the target process, starting it if specified */
1257 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1258 pmcstat_create_process();
1260 if (check_driver_stats && pmc_get_driver_stats(&ds_start) < 0)
1261 err(EX_OSERR, "ERROR: Cannot retrieve driver statistics");
1263 /* Attach process pmcs to the target process. */
1264 if (args.pa_flags & (FLAG_HAS_TARGET | FLAG_HAS_COMMANDLINE)) {
1265 if (SLIST_EMPTY(&args.pa_targets))
1266 errx(EX_DATAERR, "ERROR: No matching target "
1268 if (args.pa_flags & FLAG_HAS_PROCESS_PMCS)
1269 pmcstat_attach_pmcs();
1272 kvm_close(pmcstat_kvm);
1277 /* start the pmcs */
1278 pmcstat_start_pmcs();
1280 /* start the (commandline) process if needed */
1281 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1282 pmcstat_start_process();
1284 /* initialize logging */
1285 pmcstat_initialize_logging();
1287 /* Handle SIGINT using the kqueue loop */
1288 sa.sa_handler = SIG_IGN;
1290 (void) sigemptyset(&sa.sa_mask);
1292 if (sigaction(SIGINT, &sa, NULL) < 0)
1293 err(EX_OSERR, "ERROR: Cannot install signal handler");
1296 * Setup the top mode display.
1298 if (args.pa_flags & FLAG_DO_TOP) {
1299 args.pa_flags &= ~FLAG_DO_PRINT;
1301 if (args.pa_toptty) {
1306 if(has_colors() == TRUE) {
1307 args.pa_topcolor = 1;
1309 use_default_colors();
1310 pair_content(0, &cf, &cb);
1311 init_pair(1, COLOR_RED, cb);
1312 init_pair(2, COLOR_YELLOW, cb);
1313 init_pair(3, COLOR_GREEN, cb);
1319 intrflush(stdscr, FALSE);
1320 keypad(stdscr, TRUE);
1322 /* Get terminal width / height with ncurses. */
1323 getmaxyx(stdscr, pmcstat_displayheight, pmcstat_displaywidth);
1324 pmcstat_displayheight--; pmcstat_displaywidth--;
1325 atexit(pmcstat_topexit);
1330 * loop till either the target process (if any) exits, or we
1331 * are killed by a SIGINT.
1333 runstate = PMCSTAT_RUNNING;
1334 do_print = do_read = 0;
1336 if ((c = kevent(pmcstat_kq, NULL, 0, &kev, 1, NULL)) <= 0) {
1338 err(EX_OSERR, "ERROR: kevent failed");
1343 if (kev.flags & EV_ERROR)
1344 errc(EX_OSERR, kev.data, "ERROR: kevent failed");
1346 switch (kev.filter) {
1347 case EVFILT_PROC: /* target has exited */
1348 runstate = pmcstat_close_log();
1352 case EVFILT_READ: /* log file data is present */
1353 if (kev.ident == (unsigned)fileno(stdin) &&
1354 (args.pa_flags & FLAG_DO_TOP)) {
1355 if (pmcstat_keypress_log())
1356 runstate = pmcstat_close_log();
1359 runstate = pmcstat_process_log();
1364 if (kev.ident == SIGCHLD) {
1366 * The child process sends us a
1367 * SIGCHLD if its exec() failed. We
1368 * wait for it to exit and then exit
1372 runstate = PMCSTAT_FINISHED;
1373 } else if (kev.ident == SIGIO) {
1375 * We get a SIGIO if a PMC loses all
1376 * of its targets, or if logfile
1377 * writes encounter an error.
1379 runstate = pmcstat_close_log();
1380 do_print = 1; /* print PMCs at exit */
1381 } else if (kev.ident == SIGINT) {
1382 /* Kill the child process if we started it */
1383 if (args.pa_flags & FLAG_HAS_COMMANDLINE)
1384 pmcstat_kill_process();
1385 runstate = pmcstat_close_log();
1386 } else if (kev.ident == SIGWINCH) {
1387 if (ioctl(fileno(args.pa_printfile),
1388 TIOCGWINSZ, &ws) < 0)
1389 err(EX_OSERR, "ERROR: Cannot determine "
1391 pmcstat_displayheight = ws.ws_row - 1;
1392 pmcstat_displaywidth = ws.ws_col - 1;
1398 case EVFILT_TIMER: /* print out counting PMCs */
1399 if ((args.pa_flags & FLAG_DO_TOP) &&
1400 pmc_flush_logfile() == 0)
1407 if (do_print && !do_read) {
1408 if ((args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) {
1409 pmcstat_print_pmcs();
1410 if (runstate == PMCSTAT_FINISHED && /* final newline */
1411 (args.pa_flags & FLAG_DO_PRINT) == 0)
1412 (void) fprintf(args.pa_printfile, "\n");
1414 if (args.pa_flags & FLAG_DO_TOP)
1415 pmcstat_display_log();
1419 } while (runstate != PMCSTAT_FINISHED);
1421 if ((args.pa_flags & FLAG_DO_TOP) && args.pa_toptty) {
1426 /* flush any pending log entries */
1427 if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE))
1428 pmc_close_logfile();
1432 free(args.pa_kernel);
1434 /* check if the driver lost any samples or events */
1435 if (check_driver_stats) {
1436 if (pmc_get_driver_stats(&ds_end) < 0)
1437 err(EX_OSERR, "ERROR: Cannot retrieve driver "
1439 if (ds_start.pm_intr_bufferfull != ds_end.pm_intr_bufferfull &&
1440 args.pa_verbosity > 0)
1441 warnx("WARNING: some samples were dropped. Please "
1442 "consider tuning the \"kern.hwpmc.nsamples\" "
1444 if (ds_start.pm_buffer_requests_failed !=
1445 ds_end.pm_buffer_requests_failed &&
1446 args.pa_verbosity > 0)
1447 warnx("WARNING: some events were discarded. Please "
1448 "consider tuning the \"kern.hwpmc.nbuffers\" "