1 /*- 2 * Copyright (c) 2003-2007, Joseph Koshy 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/types.h> 31 #include <sys/event.h> 32 #include <sys/param.h> 33 #include <sys/queue.h> 34 #include <sys/socket.h> 35 #include <sys/stat.h> 36 #include <sys/sysctl.h> 37 #include <sys/time.h> 38 #include <sys/ttycom.h> 39 #include <sys/user.h> 40 #include <sys/wait.h> 41 42 #include <assert.h> 43 #include <err.h> 44 #include <errno.h> 45 #include <fcntl.h> 46 #include <kvm.h> 47 #include <libgen.h> 48 #include <limits.h> 49 #include <math.h> 50 #include <pmc.h> 51 #include <pmclog.h> 52 #include <regex.h> 53 #include <signal.h> 54 #include <stdarg.h> 55 #include <stdint.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <sysexits.h> 60 #include <unistd.h> 61 62 #include "pmcstat.h" 63 64 /* 65 * A given invocation of pmcstat(8) can manage multiple PMCs of both 66 * the system-wide and per-process variety. Each of these could be in 67 * 'counting mode' or in 'sampling mode'. 68 * 69 * For 'counting mode' PMCs, pmcstat(8) will periodically issue a 70 * pmc_read() at the configured time interval and print out the value 71 * of the requested PMCs. 72 * 73 * For 'sampling mode' PMCs it can log to a file for offline analysis, 74 * or can analyse sampling data "on the fly", either by converting 75 * samples to printed textual form or by creating gprof(1) compatible 76 * profiles, one per program executed. When creating gprof(1) 77 * profiles it can optionally merge entries from multiple processes 78 * for a given executable into a single profile file. 79 * 80 * pmcstat(8) can also execute a command line and attach PMCs to the 81 * resulting child process. The protocol used is as follows: 82 * 83 * - parent creates a socketpair for two way communication and 84 * fork()s. 85 * - subsequently: 86 * 87 * /Parent/ /Child/ 88 * 89 * - Wait for childs token. 90 * - Sends token. 91 * - Awaits signal to start. 92 * - Attaches PMCs to the child's pid 93 * and starts them. Sets up 94 * monitoring for the child. 95 * - Signals child to start. 96 * - Recieves signal, attempts exec(). 97 * 98 * After this point normal processing can happen. 99 */ 100 101 /* Globals */ 102 103 int pmcstat_interrupt = 0; 104 int pmcstat_displayheight = DEFAULT_DISPLAY_HEIGHT; 105 int pmcstat_sockpair[NSOCKPAIRFD]; 106 int pmcstat_kq; 107 kvm_t *pmcstat_kvm; 108 struct kinfo_proc *pmcstat_plist; 109 110 void 111 pmcstat_attach_pmcs(struct pmcstat_args *a) 112 { 113 struct pmcstat_ev *ev; 114 struct pmcstat_target *pt; 115 int count; 116 117 /* Attach all process PMCs to target processes. */ 118 count = 0; 119 STAILQ_FOREACH(ev, &a->pa_events, ev_next) { 120 if (PMC_IS_SYSTEM_MODE(ev->ev_mode)) 121 continue; 122 SLIST_FOREACH(pt, &a->pa_targets, pt_next) 123 if (pmc_attach(ev->ev_pmcid, pt->pt_pid) == 0) 124 count++; 125 else if (errno != ESRCH) 126 err(EX_OSERR, "ERROR: cannot attach pmc " 127 "\"%s\" to process %d", ev->ev_name, 128 (int) pt->pt_pid); 129 } 130 131 if (count == 0) 132 errx(EX_DATAERR, "ERROR: No processes were attached to."); 133 } 134 135 136 void 137 pmcstat_cleanup(struct pmcstat_args *a) 138 { 139 struct pmcstat_ev *ev, *tmp; 140 141 /* release allocated PMCs. */ 142 STAILQ_FOREACH_SAFE(ev, &a->pa_events, ev_next, tmp) 143 if (ev->ev_pmcid != PMC_ID_INVALID) { 144 if (pmc_release(ev->ev_pmcid) < 0) 145 err(EX_OSERR, "ERROR: cannot release pmc " 146 "0x%x \"%s\"", ev->ev_pmcid, ev->ev_name); 147 free(ev->ev_name); 148 free(ev->ev_spec); 149 STAILQ_REMOVE(&a->pa_events, ev, pmcstat_ev, ev_next); 150 free(ev); 151 } 152 153 /* de-configure the log file if present. */ 154 if (a->pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE)) 155 (void) pmc_configure_logfile(-1); 156 157 if (a->pa_logparser) { 158 pmclog_close(a->pa_logparser); 159 a->pa_logparser = NULL; 160 } 161 162 if (a->pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE)) 163 pmcstat_shutdown_logging(a); 164 } 165 166 void 167 pmcstat_clone_event_descriptor(struct pmcstat_args *a, struct pmcstat_ev *ev, 168 uint32_t cpumask) 169 { 170 int cpu; 171 struct pmcstat_ev *ev_clone; 172 173 while ((cpu = ffs(cpumask)) > 0) { 174 cpu--; 175 176 if ((ev_clone = malloc(sizeof(*ev_clone))) == NULL) 177 errx(EX_SOFTWARE, "ERROR: Out of memory"); 178 (void) memset(ev_clone, 0, sizeof(*ev_clone)); 179 180 ev_clone->ev_count = ev->ev_count; 181 ev_clone->ev_cpu = cpu; 182 ev_clone->ev_cumulative = ev->ev_cumulative; 183 ev_clone->ev_flags = ev->ev_flags; 184 ev_clone->ev_mode = ev->ev_mode; 185 ev_clone->ev_name = strdup(ev->ev_name); 186 ev_clone->ev_pmcid = ev->ev_pmcid; 187 ev_clone->ev_saved = ev->ev_saved; 188 ev_clone->ev_spec = strdup(ev->ev_spec); 189 190 STAILQ_INSERT_TAIL(&a->pa_events, ev_clone, ev_next); 191 192 cpumask &= ~(1 << cpu); 193 } 194 } 195 196 void 197 pmcstat_create_process(struct pmcstat_args *a) 198 { 199 char token; 200 pid_t pid; 201 struct kevent kev; 202 struct pmcstat_target *pt; 203 204 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pmcstat_sockpair) < 0) 205 err(EX_OSERR, "ERROR: cannot create socket pair"); 206 207 switch (pid = fork()) { 208 case -1: 209 err(EX_OSERR, "ERROR: cannot fork"); 210 /*NOTREACHED*/ 211 212 case 0: /* child */ 213 (void) close(pmcstat_sockpair[PARENTSOCKET]); 214 215 /* Write a token to tell our parent we've started executing. */ 216 if (write(pmcstat_sockpair[CHILDSOCKET], "+", 1) != 1) 217 err(EX_OSERR, "ERROR (child): cannot write token"); 218 219 /* Wait for our parent to signal us to start. */ 220 if (read(pmcstat_sockpair[CHILDSOCKET], &token, 1) < 0) 221 err(EX_OSERR, "ERROR (child): cannot read token"); 222 (void) close(pmcstat_sockpair[CHILDSOCKET]); 223 224 /* exec() the program requested */ 225 execvp(*a->pa_argv, a->pa_argv); 226 /* and if that fails, notify the parent */ 227 kill(getppid(), SIGCHLD); 228 err(EX_OSERR, "ERROR: execvp \"%s\" failed", *a->pa_argv); 229 /*NOTREACHED*/ 230 231 default: /* parent */ 232 (void) close(pmcstat_sockpair[CHILDSOCKET]); 233 break; 234 } 235 236 /* Ask to be notified via a kevent when the target process exits. */ 237 EV_SET(&kev, pid, EVFILT_PROC, EV_ADD|EV_ONESHOT, NOTE_EXIT, 0, 238 NULL); 239 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 240 err(EX_OSERR, "ERROR: cannot monitor child process %d", pid); 241 242 if ((pt = malloc(sizeof(*pt))) == NULL) 243 errx(EX_SOFTWARE, "ERROR: Out of memory."); 244 245 pt->pt_pid = pid; 246 SLIST_INSERT_HEAD(&a->pa_targets, pt, pt_next); 247 248 /* Wait for the child to signal that its ready to go. */ 249 if (read(pmcstat_sockpair[PARENTSOCKET], &token, 1) < 0) 250 err(EX_OSERR, "ERROR (parent): cannot read token"); 251 252 return; 253 } 254 255 void 256 pmcstat_find_targets(struct pmcstat_args *a, const char *spec) 257 { 258 int n, nproc, pid, rv; 259 struct pmcstat_target *pt; 260 char errbuf[_POSIX2_LINE_MAX], *end; 261 static struct kinfo_proc *kp; 262 regex_t reg; 263 regmatch_t regmatch; 264 265 /* First check if we've been given a process id. */ 266 pid = strtol(spec, &end, 0); 267 if (end != spec && pid >= 0) { 268 if ((pt = malloc(sizeof(*pt))) == NULL) 269 goto outofmemory; 270 pt->pt_pid = pid; 271 SLIST_INSERT_HEAD(&a->pa_targets, pt, pt_next); 272 return; 273 } 274 275 /* Otherwise treat arg as a regular expression naming processes. */ 276 if (pmcstat_kvm == NULL) { 277 if ((pmcstat_kvm = kvm_openfiles(NULL, "/dev/null", NULL, 0, 278 errbuf)) == NULL) 279 err(EX_OSERR, "ERROR: Cannot open kernel \"%s\"", 280 errbuf); 281 if ((pmcstat_plist = kvm_getprocs(pmcstat_kvm, KERN_PROC_PROC, 282 0, &nproc)) == NULL) 283 err(EX_OSERR, "ERROR: Cannot get process list: %s", 284 kvm_geterr(pmcstat_kvm)); 285 } 286 287 if ((rv = regcomp(®, spec, REG_EXTENDED|REG_NOSUB)) != 0) { 288 regerror(rv, ®, errbuf, sizeof(errbuf)); 289 err(EX_DATAERR, "ERROR: Failed to compile regex \"%s\": %s", 290 spec, errbuf); 291 } 292 293 for (n = 0, kp = pmcstat_plist; n < nproc; n++, kp++) { 294 if ((rv = regexec(®, kp->ki_comm, 1, ®match, 0)) == 0) { 295 if ((pt = malloc(sizeof(*pt))) == NULL) 296 goto outofmemory; 297 pt->pt_pid = kp->ki_pid; 298 SLIST_INSERT_HEAD(&a->pa_targets, pt, pt_next); 299 } else if (rv != REG_NOMATCH) { 300 regerror(rv, ®, errbuf, sizeof(errbuf)); 301 errx(EX_SOFTWARE, "ERROR: Regex evalation failed: %s", 302 errbuf); 303 } 304 } 305 306 regfree(®); 307 308 return; 309 310 outofmemory: 311 errx(EX_SOFTWARE, "Out of memory."); 312 /*NOTREACHED*/ 313 } 314 315 uint32_t 316 pmcstat_get_cpumask(const char *cpuspec) 317 { 318 uint32_t cpumask; 319 int cpu; 320 const char *s; 321 char *end; 322 323 s = cpuspec; 324 cpumask = 0ULL; 325 326 do { 327 cpu = strtol(s, &end, 0); 328 if (cpu < 0 || end == s) 329 errx(EX_USAGE, "ERROR: Illegal CPU specification " 330 "\"%s\".", cpuspec); 331 cpumask |= (1 << cpu); 332 s = end + strspn(end, ", \t"); 333 } while (*s); 334 335 return (cpumask); 336 } 337 338 void 339 pmcstat_kill_process(struct pmcstat_args *a) 340 { 341 struct pmcstat_target *pt; 342 343 assert(a->pa_flags & FLAG_HAS_COMMANDLINE); 344 345 /* 346 * If a command line was specified, it would be the very first 347 * in the list, before any other processes specified by -t. 348 */ 349 pt = SLIST_FIRST(&a->pa_targets); 350 assert(pt != NULL); 351 352 if (kill(pt->pt_pid, SIGINT) != 0) 353 err(EX_OSERR, "ERROR: cannot signal child process"); 354 } 355 356 void 357 pmcstat_start_pmcs(struct pmcstat_args *a) 358 { 359 struct pmcstat_ev *ev; 360 361 STAILQ_FOREACH(ev, &args.pa_events, ev_next) { 362 363 assert(ev->ev_pmcid != PMC_ID_INVALID); 364 365 if (pmc_start(ev->ev_pmcid) < 0) { 366 warn("ERROR: Cannot start pmc 0x%x \"%s\"", 367 ev->ev_pmcid, ev->ev_name); 368 pmcstat_cleanup(a); 369 exit(EX_OSERR); 370 } 371 } 372 373 } 374 375 void 376 pmcstat_print_headers(struct pmcstat_args *a) 377 { 378 struct pmcstat_ev *ev; 379 int c, w; 380 381 (void) fprintf(a->pa_printfile, PRINT_HEADER_PREFIX); 382 383 STAILQ_FOREACH(ev, &a->pa_events, ev_next) { 384 if (PMC_IS_SAMPLING_MODE(ev->ev_mode)) 385 continue; 386 387 c = PMC_IS_SYSTEM_MODE(ev->ev_mode) ? 's' : 'p'; 388 389 if (ev->ev_fieldskip != 0) 390 (void) fprintf(a->pa_printfile, "%*s", 391 ev->ev_fieldskip, ""); 392 w = ev->ev_fieldwidth - ev->ev_fieldskip - 2; 393 394 if (c == 's') 395 (void) fprintf(a->pa_printfile, "s/%02d/%-*s ", 396 ev->ev_cpu, w-3, ev->ev_name); 397 else 398 (void) fprintf(a->pa_printfile, "p/%*s ", w, 399 ev->ev_name); 400 } 401 402 (void) fflush(a->pa_printfile); 403 } 404 405 void 406 pmcstat_print_counters(struct pmcstat_args *a) 407 { 408 int extra_width; 409 struct pmcstat_ev *ev; 410 pmc_value_t value; 411 412 extra_width = sizeof(PRINT_HEADER_PREFIX) - 1; 413 414 STAILQ_FOREACH(ev, &a->pa_events, ev_next) { 415 416 /* skip sampling mode counters */ 417 if (PMC_IS_SAMPLING_MODE(ev->ev_mode)) 418 continue; 419 420 if (pmc_read(ev->ev_pmcid, &value) < 0) 421 err(EX_OSERR, "ERROR: Cannot read pmc " 422 "\"%s\"", ev->ev_name); 423 424 (void) fprintf(a->pa_printfile, "%*ju ", 425 ev->ev_fieldwidth + extra_width, 426 (uintmax_t) ev->ev_cumulative ? value : 427 (value - ev->ev_saved)); 428 429 if (ev->ev_cumulative == 0) 430 ev->ev_saved = value; 431 extra_width = 0; 432 } 433 434 (void) fflush(a->pa_printfile); 435 } 436 437 /* 438 * Print output 439 */ 440 441 void 442 pmcstat_print_pmcs(struct pmcstat_args *a) 443 { 444 static int linecount = 0; 445 446 /* check if we need to print a header line */ 447 if (++linecount > pmcstat_displayheight) { 448 (void) fprintf(a->pa_printfile, "\n"); 449 linecount = 1; 450 } 451 if (linecount == 1) 452 pmcstat_print_headers(a); 453 (void) fprintf(a->pa_printfile, "\n"); 454 455 pmcstat_print_counters(a); 456 457 return; 458 } 459 460 /* 461 * Do process profiling 462 * 463 * If a pid was specified, attach each allocated PMC to the target 464 * process. Otherwise, fork a child and attach the PMCs to the child, 465 * and have the child exec() the target program. 466 */ 467 468 void 469 pmcstat_start_process(void) 470 { 471 /* Signal the child to proceed. */ 472 if (write(pmcstat_sockpair[PARENTSOCKET], "!", 1) != 1) 473 err(EX_OSERR, "ERROR (parent): write of token failed"); 474 475 (void) close(pmcstat_sockpair[PARENTSOCKET]); 476 } 477 478 void 479 pmcstat_show_usage(void) 480 { 481 errx(EX_USAGE, 482 "[options] [commandline]\n" 483 "\t Measure process and/or system performance using hardware\n" 484 "\t performance monitoring counters.\n" 485 "\t Options include:\n" 486 "\t -C\t\t (toggle) show cumulative counts\n" 487 "\t -D path\t create profiles in directory \"path\"\n" 488 "\t -E\t\t (toggle) show counts at process exit\n" 489 "\t -M file\t print executable/gmon file map to \"file\"\n" 490 "\t -O file\t send log output to \"file\"\n" 491 "\t -P spec\t allocate a process-private sampling PMC\n" 492 "\t -R file\t read events from \"file\"\n" 493 "\t -S spec\t allocate a system-wide sampling PMC\n" 494 "\t -W\t\t (toggle) show counts per context switch\n" 495 "\t -c cpu-list\t set cpus for subsequent system-wide PMCs\n" 496 "\t -d\t\t (toggle) track descendants\n" 497 "\t -g\t\t produce gprof(1) compatible profiles\n" 498 "\t -k dir\t\t set the path to the kernel\n" 499 "\t -n rate\t set sampling rate\n" 500 "\t -o file\t send print output to \"file\"\n" 501 "\t -p spec\t allocate a process-private counting PMC\n" 502 "\t -q\t\t suppress verbosity\n" 503 "\t -r fsroot\t specify FS root directory\n" 504 "\t -s spec\t allocate a system-wide counting PMC\n" 505 "\t -t pid\t\t attach to running process with pid \"pid\"\n" 506 "\t -v\t\t increase verbosity\n" 507 "\t -w secs\t set printing time interval" 508 ); 509 } 510 511 /* 512 * Main 513 */ 514 515 int 516 main(int argc, char **argv) 517 { 518 double interval; 519 int option, npmc, ncpu; 520 int c, check_driver_stats, current_cpu, current_sampling_count; 521 int do_print, do_descendants; 522 int do_logproccsw, do_logprocexit; 523 size_t dummy; 524 int pipefd[2]; 525 int use_cumulative_counts; 526 uint32_t cpumask; 527 char *end, *tmp; 528 const char *errmsg; 529 enum pmcstat_state runstate; 530 struct pmc_driverstats ds_start, ds_end; 531 struct pmcstat_ev *ev; 532 struct sigaction sa; 533 struct kevent kev; 534 struct winsize ws; 535 struct stat sb; 536 char buffer[PATH_MAX]; 537 538 check_driver_stats = 0; 539 current_cpu = 0; 540 current_sampling_count = DEFAULT_SAMPLE_COUNT; 541 do_descendants = 0; 542 do_logproccsw = 0; 543 do_logprocexit = 0; 544 use_cumulative_counts = 0; 545 args.pa_required = 0; 546 args.pa_flags = 0; 547 args.pa_verbosity = 1; 548 args.pa_logfd = -1; 549 args.pa_fsroot = ""; 550 args.pa_kernel = strdup("/boot/kernel"); 551 args.pa_samplesdir = "."; 552 args.pa_printfile = stderr; 553 args.pa_interval = DEFAULT_WAIT_INTERVAL; 554 args.pa_mapfilename = NULL; 555 STAILQ_INIT(&args.pa_events); 556 SLIST_INIT(&args.pa_targets); 557 bzero(&ds_start, sizeof(ds_start)); 558 bzero(&ds_end, sizeof(ds_end)); 559 ev = NULL; 560 561 dummy = sizeof(ncpu); 562 if (sysctlbyname("hw.ncpu", &ncpu, &dummy, NULL, 0) < 0) 563 err(EX_OSERR, "ERROR: Cannot determine #cpus"); 564 cpumask = (1 << ncpu) - 1; 565 566 while ((option = getopt(argc, argv, 567 "CD:EM:O:P:R:S:Wc:dgk:n:o:p:qr:s:t:vw:")) != -1) 568 switch (option) { 569 case 'C': /* cumulative values */ 570 use_cumulative_counts = !use_cumulative_counts; 571 args.pa_required |= FLAG_HAS_COUNTING_PMCS; 572 break; 573 574 case 'c': /* CPU */ 575 576 if (optarg[0] == '*' && optarg[1] == '\0') 577 cpumask = (1 << ncpu) - 1; 578 else 579 cpumask = pmcstat_get_cpumask(optarg); 580 581 args.pa_required |= FLAG_HAS_SYSTEM_PMCS; 582 break; 583 584 case 'D': 585 if (stat(optarg, &sb) < 0) 586 err(EX_OSERR, "ERROR: Cannot stat \"%s\"", 587 optarg); 588 if (!S_ISDIR(sb.st_mode)) 589 errx(EX_USAGE, "ERROR: \"%s\" is not a " 590 "directory.", optarg); 591 args.pa_samplesdir = optarg; 592 args.pa_flags |= FLAG_HAS_SAMPLESDIR; 593 args.pa_required |= FLAG_DO_GPROF; 594 break; 595 596 case 'd': /* toggle descendents */ 597 do_descendants = !do_descendants; 598 args.pa_required |= FLAG_HAS_PROCESS_PMCS; 599 break; 600 601 case 'g': /* produce gprof compatible profiles */ 602 args.pa_flags |= FLAG_DO_GPROF; 603 break; 604 605 case 'k': /* pathname to the kernel */ 606 free(args.pa_kernel); 607 args.pa_kernel = strdup(optarg); 608 args.pa_required |= FLAG_DO_GPROF; 609 args.pa_flags |= FLAG_HAS_KERNELPATH; 610 break; 611 612 case 'E': /* log process exit */ 613 do_logprocexit = !do_logprocexit; 614 args.pa_required |= (FLAG_HAS_PROCESS_PMCS | 615 FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE); 616 break; 617 618 case 'M': /* mapfile */ 619 args.pa_mapfilename = optarg; 620 break; 621 622 case 'p': /* process virtual counting PMC */ 623 case 's': /* system-wide counting PMC */ 624 case 'P': /* process virtual sampling PMC */ 625 case 'S': /* system-wide sampling PMC */ 626 if ((ev = malloc(sizeof(*ev))) == NULL) 627 errx(EX_SOFTWARE, "ERROR: Out of memory."); 628 629 switch (option) { 630 case 'p': ev->ev_mode = PMC_MODE_TC; break; 631 case 's': ev->ev_mode = PMC_MODE_SC; break; 632 case 'P': ev->ev_mode = PMC_MODE_TS; break; 633 case 'S': ev->ev_mode = PMC_MODE_SS; break; 634 } 635 636 if (option == 'P' || option == 'p') { 637 args.pa_flags |= FLAG_HAS_PROCESS_PMCS; 638 args.pa_required |= (FLAG_HAS_COMMANDLINE | 639 FLAG_HAS_TARGET); 640 } 641 642 if (option == 'P' || option == 'S') { 643 args.pa_flags |= FLAG_HAS_SAMPLING_PMCS; 644 args.pa_required |= (FLAG_HAS_PIPE | 645 FLAG_HAS_OUTPUT_LOGFILE); 646 } 647 648 if (option == 'p' || option == 's') 649 args.pa_flags |= FLAG_HAS_COUNTING_PMCS; 650 651 if (option == 's' || option == 'S') 652 args.pa_flags |= FLAG_HAS_SYSTEM_PMCS; 653 654 ev->ev_spec = strdup(optarg); 655 656 if (option == 'S' || option == 'P') 657 ev->ev_count = current_sampling_count; 658 else 659 ev->ev_count = -1; 660 661 if (option == 'S' || option == 's') 662 ev->ev_cpu = ffs(cpumask) - 1; 663 else 664 ev->ev_cpu = PMC_CPU_ANY; 665 666 ev->ev_flags = 0; 667 if (do_descendants) 668 ev->ev_flags |= PMC_F_DESCENDANTS; 669 if (do_logprocexit) 670 ev->ev_flags |= PMC_F_LOG_PROCEXIT; 671 if (do_logproccsw) 672 ev->ev_flags |= PMC_F_LOG_PROCCSW; 673 674 ev->ev_cumulative = use_cumulative_counts; 675 676 ev->ev_saved = 0LL; 677 ev->ev_pmcid = PMC_ID_INVALID; 678 679 /* extract event name */ 680 c = strcspn(optarg, ", \t"); 681 ev->ev_name = malloc(c + 1); 682 (void) strncpy(ev->ev_name, optarg, c); 683 *(ev->ev_name + c) = '\0'; 684 685 STAILQ_INSERT_TAIL(&args.pa_events, ev, ev_next); 686 687 if (option == 's' || option == 'S') 688 pmcstat_clone_event_descriptor(&args, ev, 689 cpumask & ~(1 << ev->ev_cpu)); 690 691 break; 692 693 case 'n': /* sampling count */ 694 current_sampling_count = strtol(optarg, &end, 0); 695 if (*end != '\0' || current_sampling_count <= 0) 696 errx(EX_USAGE, 697 "ERROR: Illegal count value \"%s\".", 698 optarg); 699 args.pa_required |= FLAG_HAS_SAMPLING_PMCS; 700 break; 701 702 case 'o': /* outputfile */ 703 if (args.pa_printfile != NULL) 704 (void) fclose(args.pa_printfile); 705 if ((args.pa_printfile = fopen(optarg, "w")) == NULL) 706 errx(EX_OSERR, "ERROR: cannot open \"%s\" for " 707 "writing.", optarg); 708 args.pa_flags |= FLAG_DO_PRINT; 709 break; 710 711 case 'O': /* sampling output */ 712 if (args.pa_outputpath) 713 errx(EX_USAGE, "ERROR: option -O may only be " 714 "specified once."); 715 args.pa_outputpath = optarg; 716 args.pa_flags |= FLAG_HAS_OUTPUT_LOGFILE; 717 break; 718 719 case 'q': /* quiet mode */ 720 args.pa_verbosity = 0; 721 break; 722 723 case 'r': /* root FS path */ 724 args.pa_fsroot = optarg; 725 break; 726 727 case 'R': /* read an existing log file */ 728 if (args.pa_logparser != NULL) 729 errx(EX_USAGE, "ERROR: option -R may only be " 730 "specified once."); 731 args.pa_inputpath = optarg; 732 if (args.pa_printfile == stderr) 733 args.pa_printfile = stdout; 734 args.pa_flags |= FLAG_READ_LOGFILE; 735 break; 736 737 case 't': /* target pid or process name */ 738 pmcstat_find_targets(&args, optarg); 739 740 args.pa_flags |= FLAG_HAS_TARGET; 741 args.pa_required |= FLAG_HAS_PROCESS_PMCS; 742 break; 743 744 case 'v': /* verbose */ 745 args.pa_verbosity++; 746 break; 747 748 case 'w': /* wait interval */ 749 interval = strtod(optarg, &end); 750 if (*end != '\0' || interval <= 0) 751 errx(EX_USAGE, "ERROR: Illegal wait interval " 752 "value \"%s\".", optarg); 753 args.pa_flags |= FLAG_HAS_WAIT_INTERVAL; 754 args.pa_required |= FLAG_HAS_COUNTING_PMCS; 755 args.pa_interval = interval; 756 break; 757 758 case 'W': /* toggle LOG_CSW */ 759 do_logproccsw = !do_logproccsw; 760 args.pa_required |= (FLAG_HAS_PROCESS_PMCS | 761 FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE); 762 break; 763 764 case '?': 765 default: 766 pmcstat_show_usage(); 767 break; 768 769 } 770 771 args.pa_argc = (argc -= optind); 772 args.pa_argv = (argv += optind); 773 774 if (argc) /* command line present */ 775 args.pa_flags |= FLAG_HAS_COMMANDLINE; 776 777 /* 778 * Check invocation syntax. 779 */ 780 781 /* disallow -O and -R together */ 782 if (args.pa_outputpath && args.pa_inputpath) 783 errx(EX_USAGE, "ERROR: options -O and -R are mutually " 784 "exclusive."); 785 786 if (args.pa_flags & FLAG_READ_LOGFILE) { 787 errmsg = NULL; 788 if (args.pa_flags & FLAG_HAS_COMMANDLINE) 789 errmsg = "a command line specification"; 790 else if (args.pa_flags & FLAG_HAS_TARGET) 791 errmsg = "option -t"; 792 else if (!STAILQ_EMPTY(&args.pa_events)) 793 errmsg = "a PMC event specification"; 794 if (errmsg) 795 errx(EX_USAGE, "ERROR: option -R may not be used with " 796 "%s.", errmsg); 797 } else if (STAILQ_EMPTY(&args.pa_events)) 798 /* All other uses require a PMC spec. */ 799 pmcstat_show_usage(); 800 801 /* check for -t pid without a process PMC spec */ 802 if ((args.pa_required & FLAG_HAS_TARGET) && 803 (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0) 804 errx(EX_USAGE, "ERROR: option -t requires a process mode PMC " 805 "to be specified."); 806 807 /* check for process-mode options without a command or -t pid */ 808 if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) && 809 (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0) 810 errx(EX_USAGE, "ERROR: options -d, -E, -p, -P, and -W require " 811 "a command line or target process."); 812 813 /* check for -p | -P without a target process of some sort */ 814 if ((args.pa_required & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) && 815 (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0) 816 errx(EX_USAGE, "ERROR: options -P and -p require a " 817 "target process or a command line."); 818 819 /* check for process-mode options without a process-mode PMC */ 820 if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) && 821 (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0) 822 errx(EX_USAGE, "ERROR: options -d, -E, and -W require a " 823 "process mode PMC to be specified."); 824 825 /* check for -c cpu and not system mode PMCs */ 826 if ((args.pa_required & FLAG_HAS_SYSTEM_PMCS) && 827 (args.pa_flags & FLAG_HAS_SYSTEM_PMCS) == 0) 828 errx(EX_USAGE, "ERROR: option -c requires at least one " 829 "system mode PMC to be specified."); 830 831 /* check for counting mode options without a counting PMC */ 832 if ((args.pa_required & FLAG_HAS_COUNTING_PMCS) && 833 (args.pa_flags & FLAG_HAS_COUNTING_PMCS) == 0) 834 errx(EX_USAGE, "ERROR: options -C, -W, -o and -w require at " 835 "least one counting mode PMC to be specified."); 836 837 /* check for sampling mode options without a sampling PMC spec */ 838 if ((args.pa_required & FLAG_HAS_SAMPLING_PMCS) && 839 (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) == 0) 840 errx(EX_USAGE, "ERROR: options -n and -O require at least " 841 "one sampling mode PMC to be specified."); 842 843 /* check if -g is being used correctly */ 844 if ((args.pa_flags & FLAG_DO_GPROF) && 845 !(args.pa_flags & (FLAG_HAS_SAMPLING_PMCS|FLAG_READ_LOGFILE))) 846 errx(EX_USAGE, "ERROR: option -g requires sampling PMCs or -R " 847 "to be specified."); 848 849 /* check if -O was spuriously specified */ 850 if ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) && 851 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) 852 errx(EX_USAGE, 853 "ERROR: option -O is used only with options " 854 "-E, -P, -S and -W."); 855 856 /* -D dir and -k kernel path require -g or -R */ 857 if ((args.pa_flags & FLAG_HAS_KERNELPATH) && 858 (args.pa_flags & FLAG_DO_GPROF) == 0 && 859 (args.pa_flags & FLAG_READ_LOGFILE) == 0) 860 errx(EX_USAGE, "ERROR: option -k is only used with -g/-R."); 861 862 if ((args.pa_flags & FLAG_HAS_SAMPLESDIR) && 863 (args.pa_flags & FLAG_DO_GPROF) == 0 && 864 (args.pa_flags & FLAG_READ_LOGFILE) == 0) 865 errx(EX_USAGE, "ERROR: option -D is only used with -g/-R."); 866 867 /* -M mapfile requires -g or -R */ 868 if (args.pa_mapfilename != NULL && 869 (args.pa_flags & FLAG_DO_GPROF) == 0 && 870 (args.pa_flags & FLAG_READ_LOGFILE) == 0) 871 errx(EX_USAGE, "ERROR: option -M is only used with -g/-R."); 872 873 /* 874 * Disallow textual output of sampling PMCs if counting PMCs 875 * have also been asked for, mostly because the combined output 876 * is difficult to make sense of. 877 */ 878 if ((args.pa_flags & FLAG_HAS_COUNTING_PMCS) && 879 (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) && 880 ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) == 0)) 881 errx(EX_USAGE, "ERROR: option -O is required if counting and " 882 "sampling PMCs are specified together."); 883 884 /* 885 * Check if "-k kerneldir" was specified, and if whether 'kerneldir' 886 * actually refers to a a file. If so, use `dirname path` to determine 887 * the kernel directory. 888 */ 889 if (args.pa_flags & FLAG_HAS_KERNELPATH) { 890 (void) snprintf(buffer, sizeof(buffer), "%s%s", args.pa_fsroot, 891 args.pa_kernel); 892 if (stat(buffer, &sb) < 0) 893 err(EX_OSERR, "ERROR: Cannot locate kernel \"%s\"", 894 buffer); 895 if (!S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode)) 896 errx(EX_USAGE, "ERROR: \"%s\": Unsupported file type.", 897 buffer); 898 if (!S_ISDIR(sb.st_mode)) { 899 tmp = args.pa_kernel; 900 args.pa_kernel = strdup(dirname(args.pa_kernel)); 901 free(tmp); 902 (void) snprintf(buffer, sizeof(buffer), "%s%s", 903 args.pa_fsroot, args.pa_kernel); 904 if (stat(buffer, &sb) < 0) 905 err(EX_OSERR, "ERROR: Cannot stat \"%s\"", 906 buffer); 907 if (!S_ISDIR(sb.st_mode)) 908 errx(EX_USAGE, "ERROR: \"%s\" is not a " 909 "directory.", buffer); 910 } 911 } 912 913 /* if we've been asked to process a log file, do that and exit */ 914 if (args.pa_flags & FLAG_READ_LOGFILE) { 915 /* 916 * Print the log in textual form if we haven't been 917 * asked to generate gmon.out files. 918 */ 919 if ((args.pa_flags & FLAG_DO_GPROF) == 0) 920 args.pa_flags |= FLAG_DO_PRINT; 921 922 pmcstat_initialize_logging(&args); 923 args.pa_logfd = pmcstat_open_log(args.pa_inputpath, 924 PMCSTAT_OPEN_FOR_READ); 925 if ((args.pa_logparser = pmclog_open(args.pa_logfd)) == NULL) 926 err(EX_OSERR, "ERROR: Cannot create parser"); 927 pmcstat_process_log(&args); 928 pmcstat_shutdown_logging(&args); 929 exit(EX_OK); 930 } 931 932 /* otherwise, we've been asked to collect data */ 933 if (pmc_init() < 0) 934 err(EX_UNAVAILABLE, 935 "ERROR: Initialization of the pmc(3) library failed"); 936 937 if ((npmc = pmc_npmc(0)) < 0) /* assume all CPUs are identical */ 938 err(EX_OSERR, "ERROR: Cannot determine the number of PMCs " 939 "on CPU %d", 0); 940 941 /* Allocate a kqueue */ 942 if ((pmcstat_kq = kqueue()) < 0) 943 err(EX_OSERR, "ERROR: Cannot allocate kqueue"); 944 945 /* 946 * Configure the specified log file or setup a default log 947 * consumer via a pipe. 948 */ 949 if (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) { 950 if (args.pa_outputpath) 951 args.pa_logfd = pmcstat_open_log(args.pa_outputpath, 952 PMCSTAT_OPEN_FOR_WRITE); 953 else { 954 /* 955 * process the log on the fly by reading it in 956 * through a pipe. 957 */ 958 if (pipe(pipefd) < 0) 959 err(EX_OSERR, "ERROR: pipe(2) failed"); 960 961 if (fcntl(pipefd[READPIPEFD], F_SETFL, O_NONBLOCK) < 0) 962 err(EX_OSERR, "ERROR: fcntl(2) failed"); 963 964 EV_SET(&kev, pipefd[READPIPEFD], EVFILT_READ, EV_ADD, 965 0, 0, NULL); 966 967 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 968 err(EX_OSERR, "ERROR: Cannot register kevent"); 969 970 args.pa_logfd = pipefd[WRITEPIPEFD]; 971 972 args.pa_flags |= (FLAG_HAS_PIPE | FLAG_DO_PRINT); 973 args.pa_logparser = pmclog_open(pipefd[READPIPEFD]); 974 } 975 976 if (pmc_configure_logfile(args.pa_logfd) < 0) 977 err(EX_OSERR, "ERROR: Cannot configure log file"); 978 } 979 980 /* remember to check for driver errors if we are sampling or logging */ 981 check_driver_stats = (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) || 982 (args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE); 983 984 /* 985 * Allocate PMCs. 986 */ 987 988 STAILQ_FOREACH(ev, &args.pa_events, ev_next) { 989 if (pmc_allocate(ev->ev_spec, ev->ev_mode, 990 ev->ev_flags, ev->ev_cpu, &ev->ev_pmcid) < 0) 991 err(EX_OSERR, "ERROR: Cannot allocate %s-mode pmc with " 992 "specification \"%s\"", 993 PMC_IS_SYSTEM_MODE(ev->ev_mode) ? "system" : "process", 994 ev->ev_spec); 995 996 if (PMC_IS_SAMPLING_MODE(ev->ev_mode) && 997 pmc_set(ev->ev_pmcid, ev->ev_count) < 0) 998 err(EX_OSERR, "ERROR: Cannot set sampling count " 999 "for PMC \"%s\"", ev->ev_name); 1000 } 1001 1002 /* compute printout widths */ 1003 STAILQ_FOREACH(ev, &args.pa_events, ev_next) { 1004 int counter_width; 1005 int display_width; 1006 int header_width; 1007 1008 (void) pmc_width(ev->ev_pmcid, &counter_width); 1009 header_width = strlen(ev->ev_name) + 2; /* prefix '%c/' */ 1010 display_width = (int) floor(counter_width / 3.32193) + 1; 1011 1012 if (PMC_IS_SYSTEM_MODE(ev->ev_mode)) 1013 header_width += 3; /* 2 digit CPU number + '/' */ 1014 1015 if (header_width > display_width) { 1016 ev->ev_fieldskip = 0; 1017 ev->ev_fieldwidth = header_width; 1018 } else { 1019 ev->ev_fieldskip = display_width - 1020 header_width; 1021 ev->ev_fieldwidth = display_width; 1022 } 1023 } 1024 1025 /* 1026 * If our output is being set to a terminal, register a handler 1027 * for window size changes. 1028 */ 1029 1030 if (isatty(fileno(args.pa_printfile))) { 1031 1032 if (ioctl(fileno(args.pa_printfile), TIOCGWINSZ, &ws) < 0) 1033 err(EX_OSERR, "ERROR: Cannot determine window size"); 1034 1035 pmcstat_displayheight = ws.ws_row - 1; 1036 1037 EV_SET(&kev, SIGWINCH, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); 1038 1039 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1040 err(EX_OSERR, "ERROR: Cannot register kevent for " 1041 "SIGWINCH"); 1042 } 1043 1044 EV_SET(&kev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); 1045 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1046 err(EX_OSERR, "ERROR: Cannot register kevent for SIGINT"); 1047 1048 EV_SET(&kev, SIGIO, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); 1049 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1050 err(EX_OSERR, "ERROR: Cannot register kevent for SIGIO"); 1051 1052 /* 1053 * An exec() failure of a forked child is signalled by the 1054 * child sending the parent a SIGCHLD. We don't register an 1055 * actual signal handler for SIGCHLD, but instead use our 1056 * kqueue to pick up the signal. 1057 */ 1058 EV_SET(&kev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); 1059 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1060 err(EX_OSERR, "ERROR: Cannot register kevent for SIGCHLD"); 1061 1062 /* setup a timer if we have counting mode PMCs needing to be printed */ 1063 if ((args.pa_flags & FLAG_HAS_COUNTING_PMCS) && 1064 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) { 1065 EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0, 1066 args.pa_interval * 1000, NULL); 1067 1068 if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1069 err(EX_OSERR, "ERROR: Cannot register kevent for " 1070 "timer"); 1071 } 1072 1073 /* attach PMCs to the target process, starting it if specified */ 1074 if (args.pa_flags & FLAG_HAS_COMMANDLINE) 1075 pmcstat_create_process(&args); 1076 1077 if (check_driver_stats && pmc_get_driver_stats(&ds_start) < 0) 1078 err(EX_OSERR, "ERROR: Cannot retrieve driver statistics"); 1079 1080 /* Attach process pmcs to the target process. */ 1081 if (args.pa_flags & FLAG_HAS_TARGET) { 1082 if (SLIST_EMPTY(&args.pa_targets)) 1083 errx(EX_DATAERR, "ERROR: No matching target " 1084 "processes."); 1085 else 1086 pmcstat_attach_pmcs(&args); 1087 1088 if (pmcstat_kvm) { 1089 kvm_close(pmcstat_kvm); 1090 pmcstat_kvm = NULL; 1091 } 1092 } 1093 1094 /* start the pmcs */ 1095 pmcstat_start_pmcs(&args); 1096 1097 /* start the (commandline) process if needed */ 1098 if (args.pa_flags & FLAG_HAS_COMMANDLINE) 1099 pmcstat_start_process(); 1100 1101 /* initialize logging if printing the configured log */ 1102 if ((args.pa_flags & FLAG_DO_PRINT) && 1103 (args.pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))) 1104 pmcstat_initialize_logging(&args); 1105 1106 /* Handle SIGINT using the kqueue loop */ 1107 sa.sa_handler = SIG_IGN; 1108 sa.sa_flags = 0; 1109 (void) sigemptyset(&sa.sa_mask); 1110 1111 if (sigaction(SIGINT, &sa, NULL) < 0) 1112 err(EX_OSERR, "ERROR: Cannot install signal handler"); 1113 1114 /* 1115 * loop till either the target process (if any) exits, or we 1116 * are killed by a SIGINT. 1117 */ 1118 runstate = PMCSTAT_RUNNING; 1119 do_print = 0; 1120 do { 1121 if ((c = kevent(pmcstat_kq, NULL, 0, &kev, 1, NULL)) <= 0) { 1122 if (errno != EINTR) 1123 err(EX_OSERR, "ERROR: kevent failed"); 1124 else 1125 continue; 1126 } 1127 1128 if (kev.flags & EV_ERROR) 1129 errc(EX_OSERR, kev.data, "ERROR: kevent failed"); 1130 1131 switch (kev.filter) { 1132 case EVFILT_PROC: /* target has exited */ 1133 if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE | 1134 FLAG_HAS_PIPE)) 1135 runstate = pmcstat_close_log(&args); 1136 else 1137 runstate = PMCSTAT_FINISHED; 1138 do_print = 1; 1139 break; 1140 1141 case EVFILT_READ: /* log file data is present */ 1142 runstate = pmcstat_process_log(&args); 1143 break; 1144 1145 case EVFILT_SIGNAL: 1146 if (kev.ident == SIGCHLD) { 1147 /* 1148 * The child process sends us a 1149 * SIGCHLD if its exec() failed. We 1150 * wait for it to exit and then exit 1151 * ourselves. 1152 */ 1153 (void) wait(&c); 1154 runstate = PMCSTAT_FINISHED; 1155 } else if (kev.ident == SIGIO) { 1156 /* 1157 * We get a SIGIO if a PMC loses all 1158 * of its targets, or if logfile 1159 * writes encounter an error. 1160 */ 1161 if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE | 1162 FLAG_HAS_PIPE)) { 1163 runstate = pmcstat_close_log(&args); 1164 if (args.pa_flags & 1165 (FLAG_DO_PRINT|FLAG_DO_GPROF)) 1166 pmcstat_process_log(&args); 1167 } 1168 do_print = 1; /* print PMCs at exit */ 1169 runstate = PMCSTAT_FINISHED; 1170 } else if (kev.ident == SIGINT) { 1171 /* Kill the child process if we started it */ 1172 if (args.pa_flags & FLAG_HAS_COMMANDLINE) 1173 pmcstat_kill_process(&args); 1174 runstate = PMCSTAT_FINISHED; 1175 } else if (kev.ident == SIGWINCH) { 1176 if (ioctl(fileno(args.pa_printfile), 1177 TIOCGWINSZ, &ws) < 0) 1178 err(EX_OSERR, "ERROR: Cannot determine " 1179 "window size"); 1180 pmcstat_displayheight = ws.ws_row - 1; 1181 } else 1182 assert(0); 1183 1184 break; 1185 1186 case EVFILT_TIMER: /* print out counting PMCs */ 1187 do_print = 1; 1188 break; 1189 1190 } 1191 1192 if (do_print && 1193 (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) { 1194 pmcstat_print_pmcs(&args); 1195 if (runstate == PMCSTAT_FINISHED && /* final newline */ 1196 (args.pa_flags & FLAG_DO_PRINT) == 0) 1197 (void) fprintf(args.pa_printfile, "\n"); 1198 do_print = 0; 1199 } 1200 1201 } while (runstate != PMCSTAT_FINISHED); 1202 1203 /* flush any pending log entries */ 1204 if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE)) 1205 pmc_flush_logfile(); 1206 1207 pmcstat_cleanup(&args); 1208 1209 free(args.pa_kernel); 1210 1211 /* check if the driver lost any samples or events */ 1212 if (check_driver_stats) { 1213 if (pmc_get_driver_stats(&ds_end) < 0) 1214 err(EX_OSERR, "ERROR: Cannot retrieve driver " 1215 "statistics"); 1216 if (ds_start.pm_intr_bufferfull != ds_end.pm_intr_bufferfull && 1217 args.pa_verbosity > 0) 1218 warnx("WARNING: some samples were dropped. Please " 1219 "consider tuning the \"kern.hwpmc.nsamples\" " 1220 "tunable."); 1221 if (ds_start.pm_buffer_requests_failed != 1222 ds_end.pm_buffer_requests_failed && 1223 args.pa_verbosity > 0) 1224 warnx("WARNING: some events were discarded. Please " 1225 "consider tuning the \"kern.hwpmc.nbuffers\" " 1226 "tunable."); 1227 } 1228 1229 exit(EX_OK); 1230 } 1231