1145256Sjkoshy/*- 2180148Sjkoshy * Copyright (c) 2003-2008, Joseph Koshy 3174396Sjkoshy * Copyright (c) 2007 The FreeBSD Foundation 4145256Sjkoshy * All rights reserved. 5145256Sjkoshy * 6174396Sjkoshy * Portions of this software were developed by A. Joseph Koshy under 7174396Sjkoshy * sponsorship from the FreeBSD Foundation and Google, Inc. 8174396Sjkoshy * 9145256Sjkoshy * Redistribution and use in source and binary forms, with or without 10145256Sjkoshy * modification, are permitted provided that the following conditions 11145256Sjkoshy * are met: 12145256Sjkoshy * 1. Redistributions of source code must retain the above copyright 13145256Sjkoshy * notice, this list of conditions and the following disclaimer. 14145256Sjkoshy * 2. Redistributions in binary form must reproduce the above copyright 15145256Sjkoshy * notice, this list of conditions and the following disclaimer in the 16145256Sjkoshy * documentation and/or other materials provided with the distribution. 17145256Sjkoshy * 18145256Sjkoshy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19145256Sjkoshy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20145256Sjkoshy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21145256Sjkoshy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22145256Sjkoshy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23145256Sjkoshy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24145256Sjkoshy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25145256Sjkoshy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26145256Sjkoshy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27145256Sjkoshy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28145256Sjkoshy * SUCH DAMAGE. 29145256Sjkoshy */ 30145256Sjkoshy 31145256Sjkoshy#include <sys/cdefs.h> 32145256Sjkoshy__FBSDID("$FreeBSD$"); 33145256Sjkoshy 34224698Sattilio#include <sys/param.h> 35224698Sattilio#include <sys/cpuset.h> 36145256Sjkoshy#include <sys/event.h> 37145256Sjkoshy#include <sys/queue.h> 38147708Sjkoshy#include <sys/socket.h> 39147708Sjkoshy#include <sys/stat.h> 40168949Sjkoshy#include <sys/sysctl.h> 41145256Sjkoshy#include <sys/time.h> 42145256Sjkoshy#include <sys/ttycom.h> 43169069Sjkoshy#include <sys/user.h> 44145256Sjkoshy#include <sys/wait.h> 45145256Sjkoshy 46145256Sjkoshy#include <assert.h> 47203790Sfabient#include <curses.h> 48145256Sjkoshy#include <err.h> 49145256Sjkoshy#include <errno.h> 50145256Sjkoshy#include <fcntl.h> 51169069Sjkoshy#include <kvm.h> 52157144Sjkoshy#include <libgen.h> 53145256Sjkoshy#include <limits.h> 54145256Sjkoshy#include <math.h> 55145256Sjkoshy#include <pmc.h> 56147191Sjkoshy#include <pmclog.h> 57169069Sjkoshy#include <regex.h> 58145256Sjkoshy#include <signal.h> 59145256Sjkoshy#include <stdarg.h> 60147708Sjkoshy#include <stdint.h> 61145256Sjkoshy#include <stdio.h> 62145256Sjkoshy#include <stdlib.h> 63145256Sjkoshy#include <string.h> 64145256Sjkoshy#include <sysexits.h> 65145256Sjkoshy#include <unistd.h> 66145256Sjkoshy 67147708Sjkoshy#include "pmcstat.h" 68147708Sjkoshy 69147191Sjkoshy/* 70147191Sjkoshy * A given invocation of pmcstat(8) can manage multiple PMCs of both 71147191Sjkoshy * the system-wide and per-process variety. Each of these could be in 72147191Sjkoshy * 'counting mode' or in 'sampling mode'. 73147191Sjkoshy * 74147191Sjkoshy * For 'counting mode' PMCs, pmcstat(8) will periodically issue a 75147191Sjkoshy * pmc_read() at the configured time interval and print out the value 76147191Sjkoshy * of the requested PMCs. 77147191Sjkoshy * 78147191Sjkoshy * For 'sampling mode' PMCs it can log to a file for offline analysis, 79147191Sjkoshy * or can analyse sampling data "on the fly", either by converting 80147191Sjkoshy * samples to printed textual form or by creating gprof(1) compatible 81147191Sjkoshy * profiles, one per program executed. When creating gprof(1) 82147191Sjkoshy * profiles it can optionally merge entries from multiple processes 83147191Sjkoshy * for a given executable into a single profile file. 84168949Sjkoshy * 85168949Sjkoshy * pmcstat(8) can also execute a command line and attach PMCs to the 86168949Sjkoshy * resulting child process. The protocol used is as follows: 87168949Sjkoshy * 88168949Sjkoshy * - parent creates a socketpair for two way communication and 89168949Sjkoshy * fork()s. 90168949Sjkoshy * - subsequently: 91168949Sjkoshy * 92168949Sjkoshy * /Parent/ /Child/ 93168949Sjkoshy * 94168949Sjkoshy * - Wait for childs token. 95168949Sjkoshy * - Sends token. 96168949Sjkoshy * - Awaits signal to start. 97168949Sjkoshy * - Attaches PMCs to the child's pid 98168949Sjkoshy * and starts them. Sets up 99168949Sjkoshy * monitoring for the child. 100168949Sjkoshy * - Signals child to start. 101228990Suqs * - Receives signal, attempts exec(). 102168949Sjkoshy * 103168949Sjkoshy * After this point normal processing can happen. 104147191Sjkoshy */ 105147191Sjkoshy 106147708Sjkoshy/* Globals */ 107145256Sjkoshy 108241737Sedint pmcstat_displayheight = DEFAULT_DISPLAY_HEIGHT; 109241737Sedint pmcstat_displaywidth = DEFAULT_DISPLAY_WIDTH; 110241737Sedstatic int pmcstat_sockpair[NSOCKPAIRFD]; 111241737Sedstatic int pmcstat_kq; 112241737Sedstatic kvm_t *pmcstat_kvm; 113241737Sedstatic struct kinfo_proc *pmcstat_plist; 114203790Sfabientstruct pmcstat_args args; 115145256Sjkoshy 116224698Sattiliostatic void 117224698Sattiliopmcstat_clone_event_descriptor(struct pmcstat_ev *ev, const cpuset_t *cpumask) 118224698Sattilio{ 119288387Sjhb int cpu; 120224698Sattilio struct pmcstat_ev *ev_clone; 121224698Sattilio 122288387Sjhb for (cpu = 0; cpu < CPU_SETSIZE; cpu++) { 123224698Sattilio if (!CPU_ISSET(cpu, cpumask)) 124224698Sattilio continue; 125224698Sattilio 126224698Sattilio if ((ev_clone = malloc(sizeof(*ev_clone))) == NULL) 127224698Sattilio errx(EX_SOFTWARE, "ERROR: Out of memory"); 128224698Sattilio (void) memset(ev_clone, 0, sizeof(*ev_clone)); 129224698Sattilio 130224698Sattilio ev_clone->ev_count = ev->ev_count; 131224698Sattilio ev_clone->ev_cpu = cpu; 132224698Sattilio ev_clone->ev_cumulative = ev->ev_cumulative; 133224698Sattilio ev_clone->ev_flags = ev->ev_flags; 134224698Sattilio ev_clone->ev_mode = ev->ev_mode; 135224698Sattilio ev_clone->ev_name = strdup(ev->ev_name); 136224698Sattilio ev_clone->ev_pmcid = ev->ev_pmcid; 137224698Sattilio ev_clone->ev_saved = ev->ev_saved; 138224698Sattilio ev_clone->ev_spec = strdup(ev->ev_spec); 139224698Sattilio 140224698Sattilio STAILQ_INSERT_TAIL(&args.pa_events, ev_clone, ev_next); 141224698Sattilio } 142224698Sattilio} 143224698Sattilio 144224698Sattiliostatic void 145224698Sattiliopmcstat_get_cpumask(const char *cpuspec, cpuset_t *cpumask) 146224698Sattilio{ 147224698Sattilio int cpu; 148224698Sattilio const char *s; 149224698Sattilio char *end; 150224698Sattilio 151224698Sattilio CPU_ZERO(cpumask); 152224698Sattilio s = cpuspec; 153224698Sattilio 154224698Sattilio do { 155224698Sattilio cpu = strtol(s, &end, 0); 156224698Sattilio if (cpu < 0 || end == s) 157227524Sobrien errx(EX_USAGE, 158227524Sobrien "ERROR: Illegal CPU specification \"%s\".", 159227524Sobrien cpuspec); 160224698Sattilio CPU_SET(cpu, cpumask); 161224698Sattilio s = end + strspn(end, ", \t"); 162224698Sattilio } while (*s); 163288387Sjhb assert(!CPU_EMPTY(cpumask)); 164224698Sattilio} 165224698Sattilio 166168949Sjkoshyvoid 167203790Sfabientpmcstat_attach_pmcs(void) 168168949Sjkoshy{ 169168949Sjkoshy struct pmcstat_ev *ev; 170169069Sjkoshy struct pmcstat_target *pt; 171169069Sjkoshy int count; 172145256Sjkoshy 173169069Sjkoshy /* Attach all process PMCs to target processes. */ 174169069Sjkoshy count = 0; 175203790Sfabient STAILQ_FOREACH(ev, &args.pa_events, ev_next) { 176169069Sjkoshy if (PMC_IS_SYSTEM_MODE(ev->ev_mode)) 177169069Sjkoshy continue; 178203790Sfabient SLIST_FOREACH(pt, &args.pa_targets, pt_next) 179169069Sjkoshy if (pmc_attach(ev->ev_pmcid, pt->pt_pid) == 0) 180169069Sjkoshy count++; 181169069Sjkoshy else if (errno != ESRCH) 182227524Sobrien err(EX_OSERR, 183227524Sobrien"ERROR: cannot attach pmc \"%s\" to process %d", 184227524Sobrien ev->ev_name, (int)pt->pt_pid); 185169069Sjkoshy } 186168949Sjkoshy 187169069Sjkoshy if (count == 0) 188169069Sjkoshy errx(EX_DATAERR, "ERROR: No processes were attached to."); 189168949Sjkoshy} 190168949Sjkoshy 191168949Sjkoshy 192145256Sjkoshyvoid 193203790Sfabientpmcstat_cleanup(void) 194145256Sjkoshy{ 195145256Sjkoshy struct pmcstat_ev *ev, *tmp; 196145256Sjkoshy 197145256Sjkoshy /* release allocated PMCs. */ 198203790Sfabient STAILQ_FOREACH_SAFE(ev, &args.pa_events, ev_next, tmp) 199145256Sjkoshy if (ev->ev_pmcid != PMC_ID_INVALID) { 200185079Sjkoshy if (pmc_stop(ev->ev_pmcid) < 0) 201227524Sobrien err(EX_OSERR, "ERROR: cannot stop pmc 0x%x \"%s\"", 202227524Sobrien ev->ev_pmcid, ev->ev_name); 203145256Sjkoshy if (pmc_release(ev->ev_pmcid) < 0) 204227524Sobrien err(EX_OSERR, "ERROR: cannot release pmc 0x%x \"%s\"", 205227524Sobrien ev->ev_pmcid, ev->ev_name); 206145256Sjkoshy free(ev->ev_name); 207145256Sjkoshy free(ev->ev_spec); 208203790Sfabient STAILQ_REMOVE(&args.pa_events, ev, pmcstat_ev, ev_next); 209145256Sjkoshy free(ev); 210145256Sjkoshy } 211147191Sjkoshy 212147863Sjkoshy /* de-configure the log file if present. */ 213203790Sfabient if (args.pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE)) 214147863Sjkoshy (void) pmc_configure_logfile(-1); 215147863Sjkoshy 216203790Sfabient if (args.pa_logparser) { 217203790Sfabient pmclog_close(args.pa_logparser); 218203790Sfabient args.pa_logparser = NULL; 219147191Sjkoshy } 220147708Sjkoshy 221210794Sfabient pmcstat_shutdown_logging(); 222145256Sjkoshy} 223145256Sjkoshy 224145256Sjkoshyvoid 225203790Sfabientpmcstat_create_process(void) 226168949Sjkoshy{ 227168949Sjkoshy char token; 228169069Sjkoshy pid_t pid; 229168949Sjkoshy struct kevent kev; 230169069Sjkoshy struct pmcstat_target *pt; 231168949Sjkoshy 232168949Sjkoshy if (socketpair(AF_UNIX, SOCK_STREAM, 0, pmcstat_sockpair) < 0) 233168949Sjkoshy err(EX_OSERR, "ERROR: cannot create socket pair"); 234168949Sjkoshy 235169069Sjkoshy switch (pid = fork()) { 236168949Sjkoshy case -1: 237168949Sjkoshy err(EX_OSERR, "ERROR: cannot fork"); 238168949Sjkoshy /*NOTREACHED*/ 239168949Sjkoshy 240168949Sjkoshy case 0: /* child */ 241168949Sjkoshy (void) close(pmcstat_sockpair[PARENTSOCKET]); 242168949Sjkoshy 243168949Sjkoshy /* Write a token to tell our parent we've started executing. */ 244168949Sjkoshy if (write(pmcstat_sockpair[CHILDSOCKET], "+", 1) != 1) 245168949Sjkoshy err(EX_OSERR, "ERROR (child): cannot write token"); 246168949Sjkoshy 247168949Sjkoshy /* Wait for our parent to signal us to start. */ 248168949Sjkoshy if (read(pmcstat_sockpair[CHILDSOCKET], &token, 1) < 0) 249168949Sjkoshy err(EX_OSERR, "ERROR (child): cannot read token"); 250168949Sjkoshy (void) close(pmcstat_sockpair[CHILDSOCKET]); 251168949Sjkoshy 252168949Sjkoshy /* exec() the program requested */ 253203790Sfabient execvp(*args.pa_argv, args.pa_argv); 254168949Sjkoshy /* and if that fails, notify the parent */ 255168949Sjkoshy kill(getppid(), SIGCHLD); 256203790Sfabient err(EX_OSERR, "ERROR: execvp \"%s\" failed", *args.pa_argv); 257168949Sjkoshy /*NOTREACHED*/ 258168949Sjkoshy 259168949Sjkoshy default: /* parent */ 260168949Sjkoshy (void) close(pmcstat_sockpair[CHILDSOCKET]); 261168949Sjkoshy break; 262168949Sjkoshy } 263168949Sjkoshy 264168949Sjkoshy /* Ask to be notified via a kevent when the target process exits. */ 265169069Sjkoshy EV_SET(&kev, pid, EVFILT_PROC, EV_ADD|EV_ONESHOT, NOTE_EXIT, 0, 266168949Sjkoshy NULL); 267168949Sjkoshy if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 268169069Sjkoshy err(EX_OSERR, "ERROR: cannot monitor child process %d", pid); 269168949Sjkoshy 270169069Sjkoshy if ((pt = malloc(sizeof(*pt))) == NULL) 271169069Sjkoshy errx(EX_SOFTWARE, "ERROR: Out of memory."); 272169069Sjkoshy 273169069Sjkoshy pt->pt_pid = pid; 274203790Sfabient SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next); 275169069Sjkoshy 276168949Sjkoshy /* Wait for the child to signal that its ready to go. */ 277168949Sjkoshy if (read(pmcstat_sockpair[PARENTSOCKET], &token, 1) < 0) 278168949Sjkoshy err(EX_OSERR, "ERROR (parent): cannot read token"); 279168949Sjkoshy 280168949Sjkoshy return; 281168949Sjkoshy} 282168949Sjkoshy 283169069Sjkoshyvoid 284203790Sfabientpmcstat_find_targets(const char *spec) 285169069Sjkoshy{ 286169069Sjkoshy int n, nproc, pid, rv; 287169069Sjkoshy struct pmcstat_target *pt; 288169069Sjkoshy char errbuf[_POSIX2_LINE_MAX], *end; 289169069Sjkoshy static struct kinfo_proc *kp; 290169069Sjkoshy regex_t reg; 291169069Sjkoshy regmatch_t regmatch; 292169069Sjkoshy 293169069Sjkoshy /* First check if we've been given a process id. */ 294169069Sjkoshy pid = strtol(spec, &end, 0); 295169069Sjkoshy if (end != spec && pid >= 0) { 296169069Sjkoshy if ((pt = malloc(sizeof(*pt))) == NULL) 297169069Sjkoshy goto outofmemory; 298169069Sjkoshy pt->pt_pid = pid; 299203790Sfabient SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next); 300169069Sjkoshy return; 301169069Sjkoshy } 302169069Sjkoshy 303169069Sjkoshy /* Otherwise treat arg as a regular expression naming processes. */ 304169069Sjkoshy if (pmcstat_kvm == NULL) { 305169069Sjkoshy if ((pmcstat_kvm = kvm_openfiles(NULL, "/dev/null", NULL, 0, 306169069Sjkoshy errbuf)) == NULL) 307169069Sjkoshy err(EX_OSERR, "ERROR: Cannot open kernel \"%s\"", 308169069Sjkoshy errbuf); 309169069Sjkoshy if ((pmcstat_plist = kvm_getprocs(pmcstat_kvm, KERN_PROC_PROC, 310169069Sjkoshy 0, &nproc)) == NULL) 311169069Sjkoshy err(EX_OSERR, "ERROR: Cannot get process list: %s", 312169069Sjkoshy kvm_geterr(pmcstat_kvm)); 313208858Sfabient } else 314208858Sfabient nproc = 0; 315169069Sjkoshy 316169069Sjkoshy if ((rv = regcomp(®, spec, REG_EXTENDED|REG_NOSUB)) != 0) { 317169069Sjkoshy regerror(rv, ®, errbuf, sizeof(errbuf)); 318169069Sjkoshy err(EX_DATAERR, "ERROR: Failed to compile regex \"%s\": %s", 319169069Sjkoshy spec, errbuf); 320169069Sjkoshy } 321169069Sjkoshy 322169069Sjkoshy for (n = 0, kp = pmcstat_plist; n < nproc; n++, kp++) { 323169069Sjkoshy if ((rv = regexec(®, kp->ki_comm, 1, ®match, 0)) == 0) { 324169069Sjkoshy if ((pt = malloc(sizeof(*pt))) == NULL) 325169069Sjkoshy goto outofmemory; 326169069Sjkoshy pt->pt_pid = kp->ki_pid; 327203790Sfabient SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next); 328169069Sjkoshy } else if (rv != REG_NOMATCH) { 329169069Sjkoshy regerror(rv, ®, errbuf, sizeof(errbuf)); 330169069Sjkoshy errx(EX_SOFTWARE, "ERROR: Regex evalation failed: %s", 331169069Sjkoshy errbuf); 332169069Sjkoshy } 333169069Sjkoshy } 334169069Sjkoshy 335169069Sjkoshy regfree(®); 336169069Sjkoshy 337169069Sjkoshy return; 338169069Sjkoshy 339169069Sjkoshy outofmemory: 340169069Sjkoshy errx(EX_SOFTWARE, "Out of memory."); 341169069Sjkoshy /*NOTREACHED*/ 342169069Sjkoshy} 343169069Sjkoshy 344168949Sjkoshyvoid 345203790Sfabientpmcstat_kill_process(void) 346169069Sjkoshy{ 347169069Sjkoshy struct pmcstat_target *pt; 348169069Sjkoshy 349203790Sfabient assert(args.pa_flags & FLAG_HAS_COMMANDLINE); 350169069Sjkoshy 351169069Sjkoshy /* 352169069Sjkoshy * If a command line was specified, it would be the very first 353169069Sjkoshy * in the list, before any other processes specified by -t. 354169069Sjkoshy */ 355203790Sfabient pt = SLIST_FIRST(&args.pa_targets); 356169069Sjkoshy assert(pt != NULL); 357169069Sjkoshy 358169069Sjkoshy if (kill(pt->pt_pid, SIGINT) != 0) 359169069Sjkoshy err(EX_OSERR, "ERROR: cannot signal child process"); 360169069Sjkoshy} 361169069Sjkoshy 362169069Sjkoshyvoid 363203790Sfabientpmcstat_start_pmcs(void) 364145256Sjkoshy{ 365145256Sjkoshy struct pmcstat_ev *ev; 366145256Sjkoshy 367169069Sjkoshy STAILQ_FOREACH(ev, &args.pa_events, ev_next) { 368145256Sjkoshy 369145256Sjkoshy assert(ev->ev_pmcid != PMC_ID_INVALID); 370145256Sjkoshy 371145256Sjkoshy if (pmc_start(ev->ev_pmcid) < 0) { 372147191Sjkoshy warn("ERROR: Cannot start pmc 0x%x \"%s\"", 373145256Sjkoshy ev->ev_pmcid, ev->ev_name); 374203790Sfabient pmcstat_cleanup(); 375147191Sjkoshy exit(EX_OSERR); 376145256Sjkoshy } 377145256Sjkoshy } 378145256Sjkoshy 379145256Sjkoshy} 380145256Sjkoshy 381145256Sjkoshyvoid 382203790Sfabientpmcstat_print_headers(void) 383145256Sjkoshy{ 384145256Sjkoshy struct pmcstat_ev *ev; 385168949Sjkoshy int c, w; 386145256Sjkoshy 387203790Sfabient (void) fprintf(args.pa_printfile, PRINT_HEADER_PREFIX); 388145256Sjkoshy 389203790Sfabient STAILQ_FOREACH(ev, &args.pa_events, ev_next) { 390145256Sjkoshy if (PMC_IS_SAMPLING_MODE(ev->ev_mode)) 391145256Sjkoshy continue; 392145256Sjkoshy 393145256Sjkoshy c = PMC_IS_SYSTEM_MODE(ev->ev_mode) ? 's' : 'p'; 394145256Sjkoshy 395168949Sjkoshy if (ev->ev_fieldskip != 0) 396203790Sfabient (void) fprintf(args.pa_printfile, "%*s", 397168949Sjkoshy ev->ev_fieldskip, ""); 398168949Sjkoshy w = ev->ev_fieldwidth - ev->ev_fieldskip - 2; 399168949Sjkoshy 400168949Sjkoshy if (c == 's') 401203790Sfabient (void) fprintf(args.pa_printfile, "s/%02d/%-*s ", 402168949Sjkoshy ev->ev_cpu, w-3, ev->ev_name); 403168949Sjkoshy else 404203790Sfabient (void) fprintf(args.pa_printfile, "p/%*s ", w, 405145256Sjkoshy ev->ev_name); 406145256Sjkoshy } 407145256Sjkoshy 408203790Sfabient (void) fflush(args.pa_printfile); 409145256Sjkoshy} 410145256Sjkoshy 411145256Sjkoshyvoid 412203790Sfabientpmcstat_print_counters(void) 413145256Sjkoshy{ 414145256Sjkoshy int extra_width; 415145256Sjkoshy struct pmcstat_ev *ev; 416145256Sjkoshy pmc_value_t value; 417145256Sjkoshy 418145256Sjkoshy extra_width = sizeof(PRINT_HEADER_PREFIX) - 1; 419145256Sjkoshy 420203790Sfabient STAILQ_FOREACH(ev, &args.pa_events, ev_next) { 421145256Sjkoshy 422145256Sjkoshy /* skip sampling mode counters */ 423145256Sjkoshy if (PMC_IS_SAMPLING_MODE(ev->ev_mode)) 424145256Sjkoshy continue; 425145256Sjkoshy 426145256Sjkoshy if (pmc_read(ev->ev_pmcid, &value) < 0) 427227524Sobrien err(EX_OSERR, "ERROR: Cannot read pmc \"%s\"", 428227524Sobrien ev->ev_name); 429145256Sjkoshy 430203790Sfabient (void) fprintf(args.pa_printfile, "%*ju ", 431147708Sjkoshy ev->ev_fieldwidth + extra_width, 432147708Sjkoshy (uintmax_t) ev->ev_cumulative ? value : 433147708Sjkoshy (value - ev->ev_saved)); 434147708Sjkoshy 435145256Sjkoshy if (ev->ev_cumulative == 0) 436145256Sjkoshy ev->ev_saved = value; 437145256Sjkoshy extra_width = 0; 438145256Sjkoshy } 439145256Sjkoshy 440203790Sfabient (void) fflush(args.pa_printfile); 441145256Sjkoshy} 442145256Sjkoshy 443145256Sjkoshy/* 444145256Sjkoshy * Print output 445145256Sjkoshy */ 446145256Sjkoshy 447145256Sjkoshyvoid 448203790Sfabientpmcstat_print_pmcs(void) 449145256Sjkoshy{ 450145256Sjkoshy static int linecount = 0; 451145256Sjkoshy 452147708Sjkoshy /* check if we need to print a header line */ 453145256Sjkoshy if (++linecount > pmcstat_displayheight) { 454203790Sfabient (void) fprintf(args.pa_printfile, "\n"); 455145256Sjkoshy linecount = 1; 456145256Sjkoshy } 457145256Sjkoshy if (linecount == 1) 458203790Sfabient pmcstat_print_headers(); 459203790Sfabient (void) fprintf(args.pa_printfile, "\n"); 460145256Sjkoshy 461203790Sfabient pmcstat_print_counters(); 462145256Sjkoshy 463145256Sjkoshy return; 464145256Sjkoshy} 465145256Sjkoshy 466145256Sjkoshy/* 467145256Sjkoshy * Do process profiling 468145256Sjkoshy * 469145256Sjkoshy * If a pid was specified, attach each allocated PMC to the target 470145256Sjkoshy * process. Otherwise, fork a child and attach the PMCs to the child, 471145256Sjkoshy * and have the child exec() the target program. 472145256Sjkoshy */ 473145256Sjkoshy 474145256Sjkoshyvoid 475168949Sjkoshypmcstat_start_process(void) 476145256Sjkoshy{ 477168949Sjkoshy /* Signal the child to proceed. */ 478168949Sjkoshy if (write(pmcstat_sockpair[PARENTSOCKET], "!", 1) != 1) 479168949Sjkoshy err(EX_OSERR, "ERROR (parent): write of token failed"); 480145256Sjkoshy 481168949Sjkoshy (void) close(pmcstat_sockpair[PARENTSOCKET]); 482145256Sjkoshy} 483145256Sjkoshy 484145256Sjkoshyvoid 485145256Sjkoshypmcstat_show_usage(void) 486145256Sjkoshy{ 487145256Sjkoshy errx(EX_USAGE, 488145256Sjkoshy "[options] [commandline]\n" 489145256Sjkoshy "\t Measure process and/or system performance using hardware\n" 490145256Sjkoshy "\t performance monitoring counters.\n" 491145256Sjkoshy "\t Options include:\n" 492147191Sjkoshy "\t -C\t\t (toggle) show cumulative counts\n" 493147191Sjkoshy "\t -D path\t create profiles in directory \"path\"\n" 494147191Sjkoshy "\t -E\t\t (toggle) show counts at process exit\n" 495203790Sfabient "\t -F file\t write a system-wide callgraph (Kcachegrind format)" 496203790Sfabient " to \"file\"\n" 497174396Sjkoshy "\t -G file\t write a system-wide callgraph to \"file\"\n" 498157144Sjkoshy "\t -M file\t print executable/gmon file map to \"file\"\n" 499174396Sjkoshy "\t -N\t\t (toggle) capture callchains\n" 500147191Sjkoshy "\t -O file\t send log output to \"file\"\n" 501147191Sjkoshy "\t -P spec\t allocate a process-private sampling PMC\n" 502147191Sjkoshy "\t -R file\t read events from \"file\"\n" 503147191Sjkoshy "\t -S spec\t allocate a system-wide sampling PMC\n" 504203790Sfabient "\t -T\t\t start in top mode\n" 505147191Sjkoshy "\t -W\t\t (toggle) show counts per context switch\n" 506265604Sscottl "\t -a <file>\t print sampled PCs and callgraph to \"file\"\n" 507168949Sjkoshy "\t -c cpu-list\t set cpus for subsequent system-wide PMCs\n" 508147191Sjkoshy "\t -d\t\t (toggle) track descendants\n" 509294046Sjtl "\t -e\t\t use wide history counter for gprof(1) output\n" 510203790Sfabient "\t -f spec\t pass \"spec\" to as plugin option\n" 511147191Sjkoshy "\t -g\t\t produce gprof(1) compatible profiles\n" 512162804Sru "\t -k dir\t\t set the path to the kernel\n" 513266890Sgnn "\t -l secs\t set duration time\n" 514239571Sjimharris "\t -m file\t print sampled PCs to \"file\"\n" 515145256Sjkoshy "\t -n rate\t set sampling rate\n" 516145256Sjkoshy "\t -o file\t send print output to \"file\"\n" 517147191Sjkoshy "\t -p spec\t allocate a process-private counting PMC\n" 518157144Sjkoshy "\t -q\t\t suppress verbosity\n" 519157144Sjkoshy "\t -r fsroot\t specify FS root directory\n" 520147191Sjkoshy "\t -s spec\t allocate a system-wide counting PMC\n" 521183186Sjkoshy "\t -t process-spec attach to running processes matching " 522183186Sjkoshy "\"process-spec\"\n" 523157144Sjkoshy "\t -v\t\t increase verbosity\n" 524174396Sjkoshy "\t -w secs\t set printing time interval\n" 525174396Sjkoshy "\t -z depth\t limit callchain display depth" 526145256Sjkoshy ); 527145256Sjkoshy} 528145256Sjkoshy 529145256Sjkoshy/* 530203790Sfabient * At exit handler for top mode 531203790Sfabient */ 532203790Sfabient 533203790Sfabientvoid 534203790Sfabientpmcstat_topexit(void) 535203790Sfabient{ 536203790Sfabient if (!args.pa_toptty) 537203790Sfabient return; 538203790Sfabient 539203790Sfabient /* 540203790Sfabient * Shutdown ncurses. 541203790Sfabient */ 542203790Sfabient clrtoeol(); 543203790Sfabient refresh(); 544203790Sfabient endwin(); 545203790Sfabient} 546203790Sfabient 547203790Sfabient/* 548145256Sjkoshy * Main 549145256Sjkoshy */ 550145256Sjkoshy 551145256Sjkoshyint 552145256Sjkoshymain(int argc, char **argv) 553145256Sjkoshy{ 554288387Sjhb cpuset_t cpumask, rootmask; 555145256Sjkoshy double interval; 556266890Sgnn double duration; 557288387Sjhb int option, npmc; 558230350Seadler int c, check_driver_stats, current_sampling_count; 559174396Sjkoshy int do_callchain, do_descendants, do_logproccsw, do_logprocexit; 560226514Sfabient int do_print, do_read; 561283885Sjhb size_t len; 562174396Sjkoshy int graphdepth; 563210794Sfabient int pipefd[2], rfd; 564147191Sjkoshy int use_cumulative_counts; 565203790Sfabient short cf, cb; 566157144Sjkoshy char *end, *tmp; 567174396Sjkoshy const char *errmsg, *graphfilename; 568147191Sjkoshy enum pmcstat_state runstate; 569151542Sjkoshy struct pmc_driverstats ds_start, ds_end; 570145256Sjkoshy struct pmcstat_ev *ev; 571145256Sjkoshy struct sigaction sa; 572145256Sjkoshy struct kevent kev; 573145256Sjkoshy struct winsize ws; 574147708Sjkoshy struct stat sb; 575157144Sjkoshy char buffer[PATH_MAX]; 576145256Sjkoshy 577151542Sjkoshy check_driver_stats = 0; 578145256Sjkoshy current_sampling_count = DEFAULT_SAMPLE_COUNT; 579174396Sjkoshy do_callchain = 1; 580145256Sjkoshy do_descendants = 0; 581147191Sjkoshy do_logproccsw = 0; 582147191Sjkoshy do_logprocexit = 0; 583145256Sjkoshy use_cumulative_counts = 0; 584174396Sjkoshy graphfilename = "-"; 585147191Sjkoshy args.pa_required = 0; 586145256Sjkoshy args.pa_flags = 0; 587157144Sjkoshy args.pa_verbosity = 1; 588147708Sjkoshy args.pa_logfd = -1; 589157144Sjkoshy args.pa_fsroot = ""; 590147708Sjkoshy args.pa_samplesdir = "."; 591147708Sjkoshy args.pa_printfile = stderr; 592174396Sjkoshy args.pa_graphdepth = DEFAULT_CALLGRAPH_DEPTH; 593174396Sjkoshy args.pa_graphfile = NULL; 594145256Sjkoshy args.pa_interval = DEFAULT_WAIT_INTERVAL; 595157144Sjkoshy args.pa_mapfilename = NULL; 596174396Sjkoshy args.pa_inputpath = NULL; 597174396Sjkoshy args.pa_outputpath = NULL; 598203790Sfabient args.pa_pplugin = PMCSTAT_PL_NONE; 599203790Sfabient args.pa_plugin = PMCSTAT_PL_NONE; 600203790Sfabient args.pa_ctdumpinstr = 1; 601203790Sfabient args.pa_topmode = PMCSTAT_TOP_DELTA; 602203790Sfabient args.pa_toptty = 0; 603203790Sfabient args.pa_topcolor = 0; 604203790Sfabient args.pa_mergepmc = 0; 605266890Sgnn args.pa_duration = 0.0; 606169069Sjkoshy STAILQ_INIT(&args.pa_events); 607169069Sjkoshy SLIST_INIT(&args.pa_targets); 608153704Sjkoshy bzero(&ds_start, sizeof(ds_start)); 609153704Sjkoshy bzero(&ds_end, sizeof(ds_end)); 610145256Sjkoshy ev = NULL; 611224698Sattilio CPU_ZERO(&cpumask); 612145256Sjkoshy 613283885Sjhb /* Default to using the running system kernel. */ 614283885Sjhb len = 0; 615283885Sjhb if (sysctlbyname("kern.bootfile", NULL, &len, NULL, 0) == -1) 616283885Sjhb err(EX_OSERR, "ERROR: Cannot determine path of running kernel"); 617283885Sjhb args.pa_kernel = malloc(len + 1); 618283885Sjhb if (sysctlbyname("kern.bootfile", args.pa_kernel, &len, NULL, 0) == -1) 619283885Sjhb err(EX_OSERR, "ERROR: Cannot determine path of running kernel"); 620283885Sjhb 621174396Sjkoshy /* 622288387Sjhb * The initial CPU mask specifies the root mask of this process 623288387Sjhb * which is usually all CPUs in the system. 624174396Sjkoshy */ 625288387Sjhb if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1, 626288387Sjhb sizeof(rootmask), &rootmask) == -1) 627288387Sjhb err(EX_OSERR, "ERROR: Cannot determine the root set of CPUs"); 628288387Sjhb CPU_COPY(&rootmask, &cpumask); 629168949Sjkoshy 630157144Sjkoshy while ((option = getopt(argc, argv, 631294046Sjtl "CD:EF:G:M:NO:P:R:S:TWa:c:def:gk:l:m:n:o:p:qr:s:t:vw:z:")) != -1) 632145256Sjkoshy switch (option) { 633265604Sscottl case 'a': /* Annotate + callgraph */ 634265604Sscottl args.pa_flags |= FLAG_DO_ANNOTATE; 635265604Sscottl args.pa_plugin = PMCSTAT_PL_ANNOTATE_CG; 636265604Sscottl graphfilename = optarg; 637265604Sscottl break; 638265604Sscottl 639145256Sjkoshy case 'C': /* cumulative values */ 640145256Sjkoshy use_cumulative_counts = !use_cumulative_counts; 641147191Sjkoshy args.pa_required |= FLAG_HAS_COUNTING_PMCS; 642145256Sjkoshy break; 643145256Sjkoshy 644145256Sjkoshy case 'c': /* CPU */ 645288387Sjhb if (optarg[0] == '*' && optarg[1] == '\0') 646288387Sjhb CPU_COPY(&rootmask, &cpumask); 647288387Sjhb else 648224698Sattilio pmcstat_get_cpumask(optarg, &cpumask); 649168949Sjkoshy 650210797Sfabient args.pa_flags |= FLAGS_HAS_CPUMASK; 651147191Sjkoshy args.pa_required |= FLAG_HAS_SYSTEM_PMCS; 652145256Sjkoshy break; 653145256Sjkoshy 654147708Sjkoshy case 'D': 655147708Sjkoshy if (stat(optarg, &sb) < 0) 656147708Sjkoshy err(EX_OSERR, "ERROR: Cannot stat \"%s\"", 657147708Sjkoshy optarg); 658147708Sjkoshy if (!S_ISDIR(sb.st_mode)) 659227524Sobrien errx(EX_USAGE, 660227524Sobrien "ERROR: \"%s\" is not a directory.", 661227524Sobrien optarg); 662147708Sjkoshy args.pa_samplesdir = optarg; 663147708Sjkoshy args.pa_flags |= FLAG_HAS_SAMPLESDIR; 664147708Sjkoshy args.pa_required |= FLAG_DO_GPROF; 665147708Sjkoshy break; 666147708Sjkoshy 667145256Sjkoshy case 'd': /* toggle descendents */ 668145256Sjkoshy do_descendants = !do_descendants; 669147191Sjkoshy args.pa_required |= FLAG_HAS_PROCESS_PMCS; 670145256Sjkoshy break; 671145256Sjkoshy 672294046Sjtl case 'e': /* wide gprof metrics */ 673294046Sjtl args.pa_flags |= FLAG_DO_WIDE_GPROF_HC; 674294046Sjtl break; 675294046Sjtl 676203790Sfabient case 'F': /* produce a system-wide calltree */ 677203790Sfabient args.pa_flags |= FLAG_DO_CALLGRAPHS; 678203790Sfabient args.pa_plugin = PMCSTAT_PL_CALLTREE; 679203790Sfabient graphfilename = optarg; 680203790Sfabient break; 681203790Sfabient 682203790Sfabient case 'f': /* plugins options */ 683203790Sfabient if (args.pa_plugin == PMCSTAT_PL_NONE) 684203790Sfabient err(EX_USAGE, "ERROR: Need -g/-G/-m/-T."); 685203790Sfabient pmcstat_pluginconfigure_log(optarg); 686203790Sfabient break; 687203790Sfabient 688174396Sjkoshy case 'G': /* produce a system-wide callgraph */ 689174396Sjkoshy args.pa_flags |= FLAG_DO_CALLGRAPHS; 690203790Sfabient args.pa_plugin = PMCSTAT_PL_CALLGRAPH; 691174396Sjkoshy graphfilename = optarg; 692174396Sjkoshy break; 693174396Sjkoshy 694147191Sjkoshy case 'g': /* produce gprof compatible profiles */ 695147191Sjkoshy args.pa_flags |= FLAG_DO_GPROF; 696203790Sfabient args.pa_pplugin = PMCSTAT_PL_CALLGRAPH; 697203790Sfabient args.pa_plugin = PMCSTAT_PL_GPROF; 698147191Sjkoshy break; 699147191Sjkoshy 700147708Sjkoshy case 'k': /* pathname to the kernel */ 701157144Sjkoshy free(args.pa_kernel); 702157144Sjkoshy args.pa_kernel = strdup(optarg); 703174396Sjkoshy args.pa_required |= FLAG_DO_ANALYSIS; 704147708Sjkoshy args.pa_flags |= FLAG_HAS_KERNELPATH; 705147191Sjkoshy break; 706147191Sjkoshy 707266890Sgnn case 'l': /* time duration in seconds */ 708266890Sgnn duration = strtod(optarg, &end); 709266890Sgnn if (*end != '\0' || duration <= 0) 710266890Sgnn errx(EX_USAGE, "ERROR: Illegal duration time " 711266890Sgnn "value \"%s\".", optarg); 712266890Sgnn args.pa_flags |= FLAG_HAS_DURATION; 713266890Sgnn args.pa_duration = duration; 714266890Sgnn break; 715266890Sgnn 716185322Sattilio case 'm': 717203790Sfabient args.pa_flags |= FLAG_DO_ANNOTATE; 718203790Sfabient args.pa_plugin = PMCSTAT_PL_ANNOTATE; 719203790Sfabient graphfilename = optarg; 720185322Sattilio break; 721185322Sattilio 722147191Sjkoshy case 'E': /* log process exit */ 723147191Sjkoshy do_logprocexit = !do_logprocexit; 724147191Sjkoshy args.pa_required |= (FLAG_HAS_PROCESS_PMCS | 725147708Sjkoshy FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE); 726147191Sjkoshy break; 727147191Sjkoshy 728157144Sjkoshy case 'M': /* mapfile */ 729157144Sjkoshy args.pa_mapfilename = optarg; 730157144Sjkoshy break; 731157144Sjkoshy 732174396Sjkoshy case 'N': 733174396Sjkoshy do_callchain = !do_callchain; 734174396Sjkoshy args.pa_required |= FLAG_HAS_SAMPLING_PMCS; 735174396Sjkoshy break; 736174396Sjkoshy 737145256Sjkoshy case 'p': /* process virtual counting PMC */ 738145256Sjkoshy case 's': /* system-wide counting PMC */ 739145256Sjkoshy case 'P': /* process virtual sampling PMC */ 740145256Sjkoshy case 'S': /* system-wide sampling PMC */ 741145256Sjkoshy if ((ev = malloc(sizeof(*ev))) == NULL) 742147191Sjkoshy errx(EX_SOFTWARE, "ERROR: Out of memory."); 743145256Sjkoshy 744145256Sjkoshy switch (option) { 745145256Sjkoshy case 'p': ev->ev_mode = PMC_MODE_TC; break; 746145256Sjkoshy case 's': ev->ev_mode = PMC_MODE_SC; break; 747145256Sjkoshy case 'P': ev->ev_mode = PMC_MODE_TS; break; 748145256Sjkoshy case 'S': ev->ev_mode = PMC_MODE_SS; break; 749145256Sjkoshy } 750145256Sjkoshy 751147191Sjkoshy if (option == 'P' || option == 'p') { 752147191Sjkoshy args.pa_flags |= FLAG_HAS_PROCESS_PMCS; 753147708Sjkoshy args.pa_required |= (FLAG_HAS_COMMANDLINE | 754169069Sjkoshy FLAG_HAS_TARGET); 755147191Sjkoshy } 756145256Sjkoshy 757147191Sjkoshy if (option == 'P' || option == 'S') { 758147191Sjkoshy args.pa_flags |= FLAG_HAS_SAMPLING_PMCS; 759147708Sjkoshy args.pa_required |= (FLAG_HAS_PIPE | 760147708Sjkoshy FLAG_HAS_OUTPUT_LOGFILE); 761147191Sjkoshy } 762145256Sjkoshy 763145256Sjkoshy if (option == 'p' || option == 's') 764147191Sjkoshy args.pa_flags |= FLAG_HAS_COUNTING_PMCS; 765145256Sjkoshy 766147191Sjkoshy if (option == 's' || option == 'S') 767147191Sjkoshy args.pa_flags |= FLAG_HAS_SYSTEM_PMCS; 768147191Sjkoshy 769145256Sjkoshy ev->ev_spec = strdup(optarg); 770145256Sjkoshy 771145256Sjkoshy if (option == 'S' || option == 'P') 772145256Sjkoshy ev->ev_count = current_sampling_count; 773145256Sjkoshy else 774145256Sjkoshy ev->ev_count = -1; 775145256Sjkoshy 776288387Sjhb if (option == 'S' || option == 's') 777288387Sjhb ev->ev_cpu = CPU_FFS(&cpumask) - 1; 778288387Sjhb else 779145256Sjkoshy ev->ev_cpu = PMC_CPU_ANY; 780145256Sjkoshy 781147191Sjkoshy ev->ev_flags = 0; 782174396Sjkoshy if (do_callchain) 783174396Sjkoshy ev->ev_flags |= PMC_F_CALLCHAIN; 784147191Sjkoshy if (do_descendants) 785147191Sjkoshy ev->ev_flags |= PMC_F_DESCENDANTS; 786147191Sjkoshy if (do_logprocexit) 787147191Sjkoshy ev->ev_flags |= PMC_F_LOG_PROCEXIT; 788147191Sjkoshy if (do_logproccsw) 789147191Sjkoshy ev->ev_flags |= PMC_F_LOG_PROCCSW; 790147191Sjkoshy 791145256Sjkoshy ev->ev_cumulative = use_cumulative_counts; 792145256Sjkoshy 793145256Sjkoshy ev->ev_saved = 0LL; 794145256Sjkoshy ev->ev_pmcid = PMC_ID_INVALID; 795145256Sjkoshy 796145256Sjkoshy /* extract event name */ 797145256Sjkoshy c = strcspn(optarg, ", \t"); 798145256Sjkoshy ev->ev_name = malloc(c + 1); 799145256Sjkoshy (void) strncpy(ev->ev_name, optarg, c); 800145256Sjkoshy *(ev->ev_name + c) = '\0'; 801145256Sjkoshy 802169069Sjkoshy STAILQ_INSERT_TAIL(&args.pa_events, ev, ev_next); 803145256Sjkoshy 804224698Sattilio if (option == 's' || option == 'S') { 805224698Sattilio CPU_CLR(ev->ev_cpu, &cpumask); 806224698Sattilio pmcstat_clone_event_descriptor(ev, &cpumask); 807288387Sjhb CPU_SET(ev->ev_cpu, &cpumask); 808224698Sattilio } 809168949Sjkoshy 810145256Sjkoshy break; 811145256Sjkoshy 812145256Sjkoshy case 'n': /* sampling count */ 813145256Sjkoshy current_sampling_count = strtol(optarg, &end, 0); 814145256Sjkoshy if (*end != '\0' || current_sampling_count <= 0) 815145256Sjkoshy errx(EX_USAGE, 816147191Sjkoshy "ERROR: Illegal count value \"%s\".", 817145256Sjkoshy optarg); 818147191Sjkoshy args.pa_required |= FLAG_HAS_SAMPLING_PMCS; 819145256Sjkoshy break; 820145256Sjkoshy 821145256Sjkoshy case 'o': /* outputfile */ 822226986Sfabient if (args.pa_printfile != NULL && 823226986Sfabient args.pa_printfile != stdout && 824226986Sfabient args.pa_printfile != stderr) 825147708Sjkoshy (void) fclose(args.pa_printfile); 826147708Sjkoshy if ((args.pa_printfile = fopen(optarg, "w")) == NULL) 827227524Sobrien errx(EX_OSERR, 828227524Sobrien "ERROR: cannot open \"%s\" for writing.", 829227524Sobrien optarg); 830147708Sjkoshy args.pa_flags |= FLAG_DO_PRINT; 831147191Sjkoshy break; 832145256Sjkoshy 833145256Sjkoshy case 'O': /* sampling output */ 834147708Sjkoshy if (args.pa_outputpath) 835227524Sobrien errx(EX_USAGE, 836227524Sobrien"ERROR: option -O may only be specified once."); 837147708Sjkoshy args.pa_outputpath = optarg; 838147708Sjkoshy args.pa_flags |= FLAG_HAS_OUTPUT_LOGFILE; 839145256Sjkoshy break; 840145256Sjkoshy 841157144Sjkoshy case 'q': /* quiet mode */ 842157144Sjkoshy args.pa_verbosity = 0; 843157144Sjkoshy break; 844157144Sjkoshy 845157144Sjkoshy case 'r': /* root FS path */ 846157144Sjkoshy args.pa_fsroot = optarg; 847157144Sjkoshy break; 848157144Sjkoshy 849147708Sjkoshy case 'R': /* read an existing log file */ 850174396Sjkoshy if (args.pa_inputpath != NULL) 851227524Sobrien errx(EX_USAGE, 852227524Sobrien"ERROR: option -R may only be specified once."); 853147708Sjkoshy args.pa_inputpath = optarg; 854147708Sjkoshy if (args.pa_printfile == stderr) 855147708Sjkoshy args.pa_printfile = stdout; 856147708Sjkoshy args.pa_flags |= FLAG_READ_LOGFILE; 857147708Sjkoshy break; 858147708Sjkoshy 859169069Sjkoshy case 't': /* target pid or process name */ 860203790Sfabient pmcstat_find_targets(optarg); 861145256Sjkoshy 862169069Sjkoshy args.pa_flags |= FLAG_HAS_TARGET; 863147191Sjkoshy args.pa_required |= FLAG_HAS_PROCESS_PMCS; 864145256Sjkoshy break; 865145256Sjkoshy 866203790Sfabient case 'T': /* top mode */ 867203790Sfabient args.pa_flags |= FLAG_DO_TOP; 868203790Sfabient args.pa_plugin = PMCSTAT_PL_CALLGRAPH; 869203790Sfabient args.pa_ctdumpinstr = 0; 870203790Sfabient args.pa_mergepmc = 1; 871203790Sfabient if (args.pa_printfile == stderr) 872203790Sfabient args.pa_printfile = stdout; 873203790Sfabient break; 874203790Sfabient 875157144Sjkoshy case 'v': /* verbose */ 876157144Sjkoshy args.pa_verbosity++; 877157144Sjkoshy break; 878157144Sjkoshy 879145256Sjkoshy case 'w': /* wait interval */ 880145256Sjkoshy interval = strtod(optarg, &end); 881145256Sjkoshy if (*end != '\0' || interval <= 0) 882227524Sobrien errx(EX_USAGE, 883227524Sobrien"ERROR: Illegal wait interval value \"%s\".", 884227524Sobrien optarg); 885145256Sjkoshy args.pa_flags |= FLAG_HAS_WAIT_INTERVAL; 886145256Sjkoshy args.pa_interval = interval; 887147191Sjkoshy break; 888145256Sjkoshy 889147191Sjkoshy case 'W': /* toggle LOG_CSW */ 890147191Sjkoshy do_logproccsw = !do_logproccsw; 891147191Sjkoshy args.pa_required |= (FLAG_HAS_PROCESS_PMCS | 892147708Sjkoshy FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE); 893145256Sjkoshy break; 894145256Sjkoshy 895174396Sjkoshy case 'z': 896174396Sjkoshy graphdepth = strtod(optarg, &end); 897174396Sjkoshy if (*end != '\0' || graphdepth <= 0) 898227524Sobrien errx(EX_USAGE, 899227524Sobrien "ERROR: Illegal callchain depth \"%s\".", 900227524Sobrien optarg); 901174396Sjkoshy args.pa_graphdepth = graphdepth; 902174396Sjkoshy args.pa_required |= FLAG_DO_CALLGRAPHS; 903174396Sjkoshy break; 904174396Sjkoshy 905145256Sjkoshy case '?': 906145256Sjkoshy default: 907145256Sjkoshy pmcstat_show_usage(); 908145256Sjkoshy break; 909145256Sjkoshy 910145256Sjkoshy } 911145256Sjkoshy 912145256Sjkoshy args.pa_argc = (argc -= optind); 913145256Sjkoshy args.pa_argv = (argv += optind); 914145256Sjkoshy 915210797Sfabient /* If we read from logfile and no specified CPU mask use 916210797Sfabient * the maximum CPU count. 917210797Sfabient */ 918210797Sfabient if ((args.pa_flags & FLAG_READ_LOGFILE) && 919210797Sfabient (args.pa_flags & FLAGS_HAS_CPUMASK) == 0) 920224698Sattilio CPU_FILL(&cpumask); 921210797Sfabient 922174396Sjkoshy args.pa_cpumask = cpumask; /* For selecting CPUs using -R. */ 923174396Sjkoshy 924147708Sjkoshy if (argc) /* command line present */ 925147708Sjkoshy args.pa_flags |= FLAG_HAS_COMMANDLINE; 926145256Sjkoshy 927185322Sattilio if (args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS | 928203790Sfabient FLAG_DO_ANNOTATE | FLAG_DO_TOP)) 929174396Sjkoshy args.pa_flags |= FLAG_DO_ANALYSIS; 930174396Sjkoshy 931145256Sjkoshy /* 932145256Sjkoshy * Check invocation syntax. 933145256Sjkoshy */ 934145256Sjkoshy 935147708Sjkoshy /* disallow -O and -R together */ 936147708Sjkoshy if (args.pa_outputpath && args.pa_inputpath) 937227524Sobrien errx(EX_USAGE, 938227524Sobrien "ERROR: options -O and -R are mutually exclusive."); 939147708Sjkoshy 940266890Sgnn /* disallow -T and -l together */ 941266890Sgnn if ((args.pa_flags & FLAG_HAS_DURATION) && 942266890Sgnn (args.pa_flags & FLAG_DO_TOP)) 943266890Sgnn errx(EX_USAGE, "ERROR: options -T and -l are mutually " 944266890Sgnn "exclusive."); 945266890Sgnn 946288495Svangyzen /* -a and -m require -R */ 947203790Sfabient if (args.pa_flags & FLAG_DO_ANNOTATE && args.pa_inputpath == NULL) 948265604Sscottl errx(EX_USAGE, "ERROR: option %s requires an input file", 949265604Sscottl args.pa_plugin == PMCSTAT_PL_ANNOTATE ? "-m" : "-a"); 950185322Sattilio 951185322Sattilio /* -m option is not allowed combined with -g or -G. */ 952203790Sfabient if (args.pa_flags & FLAG_DO_ANNOTATE && 953185322Sattilio args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS)) 954227524Sobrien errx(EX_USAGE, 955227524Sobrien "ERROR: option -m and -g | -G are mutually exclusive"); 956185322Sattilio 957147708Sjkoshy if (args.pa_flags & FLAG_READ_LOGFILE) { 958147191Sjkoshy errmsg = NULL; 959147708Sjkoshy if (args.pa_flags & FLAG_HAS_COMMANDLINE) 960147191Sjkoshy errmsg = "a command line specification"; 961169069Sjkoshy else if (args.pa_flags & FLAG_HAS_TARGET) 962147191Sjkoshy errmsg = "option -t"; 963169069Sjkoshy else if (!STAILQ_EMPTY(&args.pa_events)) 964147191Sjkoshy errmsg = "a PMC event specification"; 965147191Sjkoshy if (errmsg) 966227524Sobrien errx(EX_USAGE, 967227524Sobrien "ERROR: option -R may not be used with %s.", 968227524Sobrien errmsg); 969169069Sjkoshy } else if (STAILQ_EMPTY(&args.pa_events)) 970147708Sjkoshy /* All other uses require a PMC spec. */ 971145256Sjkoshy pmcstat_show_usage(); 972145256Sjkoshy 973147191Sjkoshy /* check for -t pid without a process PMC spec */ 974169069Sjkoshy if ((args.pa_required & FLAG_HAS_TARGET) && 975147191Sjkoshy (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0) 976227524Sobrien errx(EX_USAGE, 977227524Sobrien"ERROR: option -t requires a process mode PMC to be specified." 978227524Sobrien ); 979147191Sjkoshy 980147191Sjkoshy /* check for process-mode options without a command or -t pid */ 981147191Sjkoshy if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) && 982169069Sjkoshy (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0) 983227524Sobrien errx(EX_USAGE, 984227524Sobrien"ERROR: options -d, -E, -p, -P, and -W require a command line or target process." 985227524Sobrien ); 986147191Sjkoshy 987147191Sjkoshy /* check for -p | -P without a target process of some sort */ 988169069Sjkoshy if ((args.pa_required & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) && 989169069Sjkoshy (args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0) 990227524Sobrien errx(EX_USAGE, 991227524Sobrien"ERROR: options -P and -p require a target process or a command line." 992227524Sobrien ); 993147191Sjkoshy 994147191Sjkoshy /* check for process-mode options without a process-mode PMC */ 995147191Sjkoshy if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) && 996147191Sjkoshy (args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0) 997227524Sobrien errx(EX_USAGE, 998227524Sobrien"ERROR: options -d, -E, and -W require a process mode PMC to be specified." 999227524Sobrien ); 1000147191Sjkoshy 1001174396Sjkoshy /* check for -c cpu with no system mode PMCs or logfile. */ 1002147191Sjkoshy if ((args.pa_required & FLAG_HAS_SYSTEM_PMCS) && 1003174396Sjkoshy (args.pa_flags & FLAG_HAS_SYSTEM_PMCS) == 0 && 1004174396Sjkoshy (args.pa_flags & FLAG_READ_LOGFILE) == 0) 1005227524Sobrien errx(EX_USAGE, 1006227524Sobrien"ERROR: option -c requires at least one system mode PMC to be specified." 1007227524Sobrien ); 1008147191Sjkoshy 1009147191Sjkoshy /* check for counting mode options without a counting PMC */ 1010147191Sjkoshy if ((args.pa_required & FLAG_HAS_COUNTING_PMCS) && 1011147191Sjkoshy (args.pa_flags & FLAG_HAS_COUNTING_PMCS) == 0) 1012227524Sobrien errx(EX_USAGE, 1013227524Sobrien"ERROR: options -C, -W and -o require at least one counting mode PMC to be specified." 1014227524Sobrien ); 1015147191Sjkoshy 1016147191Sjkoshy /* check for sampling mode options without a sampling PMC spec */ 1017147191Sjkoshy if ((args.pa_required & FLAG_HAS_SAMPLING_PMCS) && 1018147191Sjkoshy (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) == 0) 1019227524Sobrien errx(EX_USAGE, 1020227524Sobrien"ERROR: options -N, -n and -O require at least one sampling mode PMC to be specified." 1021227524Sobrien ); 1022147191Sjkoshy 1023203790Sfabient /* check if -g/-G/-m/-T are being used correctly */ 1024174396Sjkoshy if ((args.pa_flags & FLAG_DO_ANALYSIS) && 1025147708Sjkoshy !(args.pa_flags & (FLAG_HAS_SAMPLING_PMCS|FLAG_READ_LOGFILE))) 1026227524Sobrien errx(EX_USAGE, 1027227524Sobrien"ERROR: options -g/-G/-m/-T require sampling PMCs or -R to be specified." 1028227524Sobrien ); 1029147708Sjkoshy 1030294046Sjtl /* check if -e was specified without -g */ 1031294046Sjtl if ((args.pa_flags & FLAG_DO_WIDE_GPROF_HC) && 1032294046Sjtl !(args.pa_flags & FLAG_DO_GPROF)) 1033294046Sjtl errx(EX_USAGE, 1034294046Sjtl"ERROR: option -e requires gprof mode to be specified." 1035294046Sjtl ); 1036294046Sjtl 1037147191Sjkoshy /* check if -O was spuriously specified */ 1038147708Sjkoshy if ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) && 1039147708Sjkoshy (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) 1040147191Sjkoshy errx(EX_USAGE, 1041227524Sobrien"ERROR: option -O is used only with options -E, -P, -S and -W." 1042227524Sobrien ); 1043147191Sjkoshy 1044203790Sfabient /* -k kernel path require -g/-G/-m/-T or -R */ 1045147708Sjkoshy if ((args.pa_flags & FLAG_HAS_KERNELPATH) && 1046174396Sjkoshy (args.pa_flags & FLAG_DO_ANALYSIS) == 0 && 1047157144Sjkoshy (args.pa_flags & FLAG_READ_LOGFILE) == 0) 1048203790Sfabient errx(EX_USAGE, "ERROR: option -k is only used with -g/-R/-m/-T."); 1049147708Sjkoshy 1050174396Sjkoshy /* -D only applies to gprof output mode (-g) */ 1051147708Sjkoshy if ((args.pa_flags & FLAG_HAS_SAMPLESDIR) && 1052174396Sjkoshy (args.pa_flags & FLAG_DO_GPROF) == 0) 1053174396Sjkoshy errx(EX_USAGE, "ERROR: option -D is only used with -g."); 1054147708Sjkoshy 1055157144Sjkoshy /* -M mapfile requires -g or -R */ 1056157144Sjkoshy if (args.pa_mapfilename != NULL && 1057157144Sjkoshy (args.pa_flags & FLAG_DO_GPROF) == 0 && 1058157144Sjkoshy (args.pa_flags & FLAG_READ_LOGFILE) == 0) 1059157144Sjkoshy errx(EX_USAGE, "ERROR: option -M is only used with -g/-R."); 1060157144Sjkoshy 1061147708Sjkoshy /* 1062147708Sjkoshy * Disallow textual output of sampling PMCs if counting PMCs 1063147708Sjkoshy * have also been asked for, mostly because the combined output 1064147708Sjkoshy * is difficult to make sense of. 1065147708Sjkoshy */ 1066147708Sjkoshy if ((args.pa_flags & FLAG_HAS_COUNTING_PMCS) && 1067147708Sjkoshy (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) && 1068168927Sjkoshy ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) == 0)) 1069227524Sobrien errx(EX_USAGE, 1070227524Sobrien"ERROR: option -O is required if counting and sampling PMCs are specified together." 1071227524Sobrien ); 1072147708Sjkoshy 1073157144Sjkoshy /* 1074283885Sjhb * Check if 'kerneldir' refers to a file rather than a 1075283885Sjhb * directory. If so, use `dirname path` to determine the 1076283885Sjhb * kernel directory. 1077157144Sjkoshy */ 1078283885Sjhb (void) snprintf(buffer, sizeof(buffer), "%s%s", args.pa_fsroot, 1079283885Sjhb args.pa_kernel); 1080283885Sjhb if (stat(buffer, &sb) < 0) 1081283885Sjhb err(EX_OSERR, "ERROR: Cannot locate kernel \"%s\"", 1082283885Sjhb buffer); 1083283885Sjhb if (!S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode)) 1084283885Sjhb errx(EX_USAGE, "ERROR: \"%s\": Unsupported file type.", 1085283885Sjhb buffer); 1086283885Sjhb if (!S_ISDIR(sb.st_mode)) { 1087283885Sjhb tmp = args.pa_kernel; 1088283885Sjhb args.pa_kernel = strdup(dirname(args.pa_kernel)); 1089283885Sjhb free(tmp); 1090283885Sjhb (void) snprintf(buffer, sizeof(buffer), "%s%s", 1091283885Sjhb args.pa_fsroot, args.pa_kernel); 1092157144Sjkoshy if (stat(buffer, &sb) < 0) 1093283885Sjhb err(EX_OSERR, "ERROR: Cannot stat \"%s\"", 1094157144Sjkoshy buffer); 1095283885Sjhb if (!S_ISDIR(sb.st_mode)) 1096283885Sjhb errx(EX_USAGE, 1097283885Sjhb "ERROR: \"%s\" is not a directory.", 1098157144Sjkoshy buffer); 1099157144Sjkoshy } 1100168949Sjkoshy 1101174396Sjkoshy /* 1102174396Sjkoshy * If we have a callgraph be created, select the outputfile. 1103174396Sjkoshy */ 1104174396Sjkoshy if (args.pa_flags & FLAG_DO_CALLGRAPHS) { 1105174396Sjkoshy if (strcmp(graphfilename, "-") == 0) 1106174396Sjkoshy args.pa_graphfile = args.pa_printfile; 1107174396Sjkoshy else { 1108174396Sjkoshy args.pa_graphfile = fopen(graphfilename, "w"); 1109174396Sjkoshy if (args.pa_graphfile == NULL) 1110227524Sobrien err(EX_OSERR, 1111227524Sobrien "ERROR: cannot open \"%s\" for writing", 1112227524Sobrien graphfilename); 1113174396Sjkoshy } 1114174396Sjkoshy } 1115203790Sfabient if (args.pa_flags & FLAG_DO_ANNOTATE) { 1116185322Sattilio args.pa_graphfile = fopen(graphfilename, "w"); 1117185322Sattilio if (args.pa_graphfile == NULL) 1118185322Sattilio err(EX_OSERR, "ERROR: cannot open \"%s\" for writing", 1119185322Sattilio graphfilename); 1120185322Sattilio } 1121174396Sjkoshy 1122210794Sfabient /* if we've been asked to process a log file, skip init */ 1123210794Sfabient if ((args.pa_flags & FLAG_READ_LOGFILE) == 0) { 1124210794Sfabient if (pmc_init() < 0) 1125210794Sfabient err(EX_UNAVAILABLE, 1126227524Sobrien "ERROR: Initialization of the pmc(3) library failed" 1127227524Sobrien ); 1128210794Sfabient 1129210794Sfabient if ((npmc = pmc_npmc(0)) < 0) /* assume all CPUs are identical */ 1130227524Sobrien err(EX_OSERR, 1131227524Sobrien"ERROR: Cannot determine the number of PMCs on CPU %d", 1132227524Sobrien 0); 1133210794Sfabient } 1134210794Sfabient 1135210794Sfabient /* Allocate a kqueue */ 1136210794Sfabient if ((pmcstat_kq = kqueue()) < 0) 1137210794Sfabient err(EX_OSERR, "ERROR: Cannot allocate kqueue"); 1138210794Sfabient 1139210794Sfabient /* Setup the logfile as the source. */ 1140147708Sjkoshy if (args.pa_flags & FLAG_READ_LOGFILE) { 1141147708Sjkoshy /* 1142147708Sjkoshy * Print the log in textual form if we haven't been 1143174396Sjkoshy * asked to generate profiling information. 1144147708Sjkoshy */ 1145174396Sjkoshy if ((args.pa_flags & FLAG_DO_ANALYSIS) == 0) 1146147708Sjkoshy args.pa_flags |= FLAG_DO_PRINT; 1147147708Sjkoshy 1148203790Sfabient pmcstat_initialize_logging(); 1149210794Sfabient rfd = pmcstat_open_log(args.pa_inputpath, 1150157406Sjkoshy PMCSTAT_OPEN_FOR_READ); 1151210794Sfabient if ((args.pa_logparser = pmclog_open(rfd)) == NULL) 1152147708Sjkoshy err(EX_OSERR, "ERROR: Cannot create parser"); 1153210794Sfabient if (fcntl(rfd, F_SETFL, O_NONBLOCK) < 0) 1154210794Sfabient err(EX_OSERR, "ERROR: fcntl(2) failed"); 1155210794Sfabient EV_SET(&kev, rfd, EVFILT_READ, EV_ADD, 1156210794Sfabient 0, 0, NULL); 1157210794Sfabient if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1158210794Sfabient err(EX_OSERR, "ERROR: Cannot register kevent"); 1159147191Sjkoshy } 1160145256Sjkoshy /* 1161147708Sjkoshy * Configure the specified log file or setup a default log 1162147708Sjkoshy * consumer via a pipe. 1163147708Sjkoshy */ 1164147708Sjkoshy if (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) { 1165157406Sjkoshy if (args.pa_outputpath) 1166157406Sjkoshy args.pa_logfd = pmcstat_open_log(args.pa_outputpath, 1167157406Sjkoshy PMCSTAT_OPEN_FOR_WRITE); 1168157406Sjkoshy else { 1169147708Sjkoshy /* 1170147708Sjkoshy * process the log on the fly by reading it in 1171147708Sjkoshy * through a pipe. 1172147708Sjkoshy */ 1173147708Sjkoshy if (pipe(pipefd) < 0) 1174147708Sjkoshy err(EX_OSERR, "ERROR: pipe(2) failed"); 1175147708Sjkoshy 1176147708Sjkoshy if (fcntl(pipefd[READPIPEFD], F_SETFL, O_NONBLOCK) < 0) 1177147708Sjkoshy err(EX_OSERR, "ERROR: fcntl(2) failed"); 1178147708Sjkoshy 1179147708Sjkoshy EV_SET(&kev, pipefd[READPIPEFD], EVFILT_READ, EV_ADD, 1180147708Sjkoshy 0, 0, NULL); 1181147708Sjkoshy 1182147708Sjkoshy if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1183147708Sjkoshy err(EX_OSERR, "ERROR: Cannot register kevent"); 1184147708Sjkoshy 1185147708Sjkoshy args.pa_logfd = pipefd[WRITEPIPEFD]; 1186147708Sjkoshy 1187203790Sfabient args.pa_flags |= FLAG_HAS_PIPE; 1188203790Sfabient if ((args.pa_flags & FLAG_DO_TOP) == 0) 1189203790Sfabient args.pa_flags |= FLAG_DO_PRINT; 1190147708Sjkoshy args.pa_logparser = pmclog_open(pipefd[READPIPEFD]); 1191147708Sjkoshy } 1192147708Sjkoshy 1193147708Sjkoshy if (pmc_configure_logfile(args.pa_logfd) < 0) 1194147708Sjkoshy err(EX_OSERR, "ERROR: Cannot configure log file"); 1195147708Sjkoshy } 1196147708Sjkoshy 1197151542Sjkoshy /* remember to check for driver errors if we are sampling or logging */ 1198151542Sjkoshy check_driver_stats = (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) || 1199151542Sjkoshy (args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE); 1200151542Sjkoshy 1201147708Sjkoshy /* 1202210794Sfabient if (args.pa_flags & FLAG_READ_LOGFILE) { 1203145256Sjkoshy * Allocate PMCs. 1204145256Sjkoshy */ 1205145256Sjkoshy 1206169069Sjkoshy STAILQ_FOREACH(ev, &args.pa_events, ev_next) { 1207227524Sobrien if (pmc_allocate(ev->ev_spec, ev->ev_mode, 1208147191Sjkoshy ev->ev_flags, ev->ev_cpu, &ev->ev_pmcid) < 0) 1209227524Sobrien err(EX_OSERR, 1210227524Sobrien"ERROR: Cannot allocate %s-mode pmc with specification \"%s\"", 1211227524Sobrien PMC_IS_SYSTEM_MODE(ev->ev_mode) ? 1212227524Sobrien "system" : "process", ev->ev_spec); 1213145256Sjkoshy 1214227524Sobrien if (PMC_IS_SAMPLING_MODE(ev->ev_mode) && 1215227524Sobrien pmc_set(ev->ev_pmcid, ev->ev_count) < 0) 1216227524Sobrien err(EX_OSERR, 1217227524Sobrien "ERROR: Cannot set sampling count for PMC \"%s\"", 1218227524Sobrien ev->ev_name); 1219147708Sjkoshy } 1220147708Sjkoshy 1221145256Sjkoshy /* compute printout widths */ 1222169069Sjkoshy STAILQ_FOREACH(ev, &args.pa_events, ev_next) { 1223145774Sjkoshy int counter_width; 1224145774Sjkoshy int display_width; 1225145774Sjkoshy int header_width; 1226145256Sjkoshy 1227145774Sjkoshy (void) pmc_width(ev->ev_pmcid, &counter_width); 1228168949Sjkoshy header_width = strlen(ev->ev_name) + 2; /* prefix '%c/' */ 1229145774Sjkoshy display_width = (int) floor(counter_width / 3.32193) + 1; 1230145256Sjkoshy 1231168949Sjkoshy if (PMC_IS_SYSTEM_MODE(ev->ev_mode)) 1232168949Sjkoshy header_width += 3; /* 2 digit CPU number + '/' */ 1233168949Sjkoshy 1234145774Sjkoshy if (header_width > display_width) { 1235145256Sjkoshy ev->ev_fieldskip = 0; 1236145774Sjkoshy ev->ev_fieldwidth = header_width; 1237145256Sjkoshy } else { 1238145774Sjkoshy ev->ev_fieldskip = display_width - 1239145774Sjkoshy header_width; 1240145774Sjkoshy ev->ev_fieldwidth = display_width; 1241145256Sjkoshy } 1242145256Sjkoshy } 1243145256Sjkoshy 1244145256Sjkoshy /* 1245145256Sjkoshy * If our output is being set to a terminal, register a handler 1246145256Sjkoshy * for window size changes. 1247145256Sjkoshy */ 1248145256Sjkoshy 1249147708Sjkoshy if (isatty(fileno(args.pa_printfile))) { 1250145256Sjkoshy 1251147708Sjkoshy if (ioctl(fileno(args.pa_printfile), TIOCGWINSZ, &ws) < 0) 1252145256Sjkoshy err(EX_OSERR, "ERROR: Cannot determine window size"); 1253145256Sjkoshy 1254145256Sjkoshy pmcstat_displayheight = ws.ws_row - 1; 1255203790Sfabient pmcstat_displaywidth = ws.ws_col - 1; 1256145256Sjkoshy 1257145256Sjkoshy EV_SET(&kev, SIGWINCH, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); 1258145256Sjkoshy 1259145256Sjkoshy if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1260227524Sobrien err(EX_OSERR, 1261227524Sobrien "ERROR: Cannot register kevent for SIGWINCH"); 1262203790Sfabient 1263203790Sfabient args.pa_toptty = 1; 1264145256Sjkoshy } 1265145256Sjkoshy 1266203790Sfabient /* 1267203790Sfabient * Listen to key input in top mode. 1268203790Sfabient */ 1269203790Sfabient if (args.pa_flags & FLAG_DO_TOP) { 1270203790Sfabient EV_SET(&kev, fileno(stdin), EVFILT_READ, EV_ADD, 0, 0, NULL); 1271203790Sfabient if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1272203790Sfabient err(EX_OSERR, "ERROR: Cannot register kevent"); 1273203790Sfabient } 1274203790Sfabient 1275145256Sjkoshy EV_SET(&kev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); 1276145256Sjkoshy if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1277145256Sjkoshy err(EX_OSERR, "ERROR: Cannot register kevent for SIGINT"); 1278145256Sjkoshy 1279147191Sjkoshy EV_SET(&kev, SIGIO, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); 1280147191Sjkoshy if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1281147191Sjkoshy err(EX_OSERR, "ERROR: Cannot register kevent for SIGIO"); 1282145256Sjkoshy 1283147191Sjkoshy /* 1284147191Sjkoshy * An exec() failure of a forked child is signalled by the 1285147191Sjkoshy * child sending the parent a SIGCHLD. We don't register an 1286147191Sjkoshy * actual signal handler for SIGCHLD, but instead use our 1287147191Sjkoshy * kqueue to pick up the signal. 1288147191Sjkoshy */ 1289147191Sjkoshy EV_SET(&kev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); 1290147191Sjkoshy if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1291147191Sjkoshy err(EX_OSERR, "ERROR: Cannot register kevent for SIGCHLD"); 1292145256Sjkoshy 1293203790Sfabient /* 1294203790Sfabient * Setup a timer if we have counting mode PMCs needing to be printed or 1295203790Sfabient * top mode plugin is active. 1296203790Sfabient */ 1297203790Sfabient if (((args.pa_flags & FLAG_HAS_COUNTING_PMCS) && 1298203790Sfabient (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) || 1299203790Sfabient (args.pa_flags & FLAG_DO_TOP)) { 1300145256Sjkoshy EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0, 1301145256Sjkoshy args.pa_interval * 1000, NULL); 1302145256Sjkoshy 1303145256Sjkoshy if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1304227524Sobrien err(EX_OSERR, 1305227524Sobrien "ERROR: Cannot register kevent for timer"); 1306145256Sjkoshy } 1307145256Sjkoshy 1308266890Sgnn /* 1309266890Sgnn * Setup a duration timer if we have sampling mode PMCs and 1310266890Sgnn * a duration time is set 1311266890Sgnn */ 1312266890Sgnn if ((args.pa_flags & FLAG_HAS_SAMPLING_PMCS) && 1313266890Sgnn (args.pa_flags & FLAG_HAS_DURATION)) { 1314266890Sgnn EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0, 1315266890Sgnn args.pa_duration * 1000, NULL); 1316266890Sgnn 1317266890Sgnn if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0) 1318266890Sgnn err(EX_OSERR, "ERROR: Cannot register kevent for " 1319266890Sgnn "time duration"); 1320266890Sgnn } 1321266890Sgnn 1322145256Sjkoshy /* attach PMCs to the target process, starting it if specified */ 1323168949Sjkoshy if (args.pa_flags & FLAG_HAS_COMMANDLINE) 1324203790Sfabient pmcstat_create_process(); 1325145256Sjkoshy 1326151542Sjkoshy if (check_driver_stats && pmc_get_driver_stats(&ds_start) < 0) 1327151542Sjkoshy err(EX_OSERR, "ERROR: Cannot retrieve driver statistics"); 1328151542Sjkoshy 1329168949Sjkoshy /* Attach process pmcs to the target process. */ 1330180148Sjkoshy if (args.pa_flags & (FLAG_HAS_TARGET | FLAG_HAS_COMMANDLINE)) { 1331169069Sjkoshy if (SLIST_EMPTY(&args.pa_targets)) 1332227524Sobrien errx(EX_DATAERR, 1333227524Sobrien "ERROR: No matching target processes."); 1334183672Sjkoshy if (args.pa_flags & FLAG_HAS_PROCESS_PMCS) 1335203790Sfabient pmcstat_attach_pmcs(); 1336168949Sjkoshy 1337169069Sjkoshy if (pmcstat_kvm) { 1338169069Sjkoshy kvm_close(pmcstat_kvm); 1339169069Sjkoshy pmcstat_kvm = NULL; 1340169069Sjkoshy } 1341169069Sjkoshy } 1342169069Sjkoshy 1343145256Sjkoshy /* start the pmcs */ 1344203790Sfabient pmcstat_start_pmcs(); 1345145256Sjkoshy 1346145256Sjkoshy /* start the (commandline) process if needed */ 1347147708Sjkoshy if (args.pa_flags & FLAG_HAS_COMMANDLINE) 1348168949Sjkoshy pmcstat_start_process(); 1349145256Sjkoshy 1350210794Sfabient /* initialize logging */ 1351210794Sfabient pmcstat_initialize_logging(); 1352147708Sjkoshy 1353145256Sjkoshy /* Handle SIGINT using the kqueue loop */ 1354145256Sjkoshy sa.sa_handler = SIG_IGN; 1355145256Sjkoshy sa.sa_flags = 0; 1356145256Sjkoshy (void) sigemptyset(&sa.sa_mask); 1357145256Sjkoshy 1358145256Sjkoshy if (sigaction(SIGINT, &sa, NULL) < 0) 1359145256Sjkoshy err(EX_OSERR, "ERROR: Cannot install signal handler"); 1360145256Sjkoshy 1361145256Sjkoshy /* 1362203790Sfabient * Setup the top mode display. 1363203790Sfabient */ 1364203790Sfabient if (args.pa_flags & FLAG_DO_TOP) { 1365203790Sfabient args.pa_flags &= ~FLAG_DO_PRINT; 1366203790Sfabient 1367203790Sfabient if (args.pa_toptty) { 1368203790Sfabient /* 1369203790Sfabient * Init ncurses. 1370203790Sfabient */ 1371203790Sfabient initscr(); 1372203790Sfabient if(has_colors() == TRUE) { 1373203790Sfabient args.pa_topcolor = 1; 1374203790Sfabient start_color(); 1375203790Sfabient use_default_colors(); 1376203790Sfabient pair_content(0, &cf, &cb); 1377203790Sfabient init_pair(1, COLOR_RED, cb); 1378203790Sfabient init_pair(2, COLOR_YELLOW, cb); 1379203790Sfabient init_pair(3, COLOR_GREEN, cb); 1380203790Sfabient } 1381203790Sfabient cbreak(); 1382203790Sfabient noecho(); 1383203790Sfabient nonl(); 1384203790Sfabient nodelay(stdscr, 1); 1385203790Sfabient intrflush(stdscr, FALSE); 1386203790Sfabient keypad(stdscr, TRUE); 1387203790Sfabient clear(); 1388204783Sfabient /* Get terminal width / height with ncurses. */ 1389227524Sobrien getmaxyx(stdscr, 1390227524Sobrien pmcstat_displayheight, pmcstat_displaywidth); 1391204783Sfabient pmcstat_displayheight--; pmcstat_displaywidth--; 1392203790Sfabient atexit(pmcstat_topexit); 1393203790Sfabient } 1394203790Sfabient } 1395203790Sfabient 1396203790Sfabient /* 1397145256Sjkoshy * loop till either the target process (if any) exits, or we 1398266890Sgnn * are killed by a SIGINT or we reached the time duration. 1399145256Sjkoshy */ 1400147191Sjkoshy runstate = PMCSTAT_RUNNING; 1401226514Sfabient do_print = do_read = 0; 1402145256Sjkoshy do { 1403145256Sjkoshy if ((c = kevent(pmcstat_kq, NULL, 0, &kev, 1, NULL)) <= 0) { 1404145256Sjkoshy if (errno != EINTR) 1405145256Sjkoshy err(EX_OSERR, "ERROR: kevent failed"); 1406145256Sjkoshy else 1407145256Sjkoshy continue; 1408145256Sjkoshy } 1409145256Sjkoshy 1410145256Sjkoshy if (kev.flags & EV_ERROR) 1411145256Sjkoshy errc(EX_OSERR, kev.data, "ERROR: kevent failed"); 1412145256Sjkoshy 1413145256Sjkoshy switch (kev.filter) { 1414147191Sjkoshy case EVFILT_PROC: /* target has exited */ 1415210794Sfabient runstate = pmcstat_close_log(); 1416148688Sjkoshy do_print = 1; 1417147191Sjkoshy break; 1418145256Sjkoshy 1419147191Sjkoshy case EVFILT_READ: /* log file data is present */ 1420210794Sfabient if (kev.ident == (unsigned)fileno(stdin) && 1421210794Sfabient (args.pa_flags & FLAG_DO_TOP)) { 1422203790Sfabient if (pmcstat_keypress_log()) 1423203790Sfabient runstate = pmcstat_close_log(); 1424226514Sfabient } else { 1425226514Sfabient do_read = 0; 1426203790Sfabient runstate = pmcstat_process_log(); 1427226514Sfabient } 1428145256Sjkoshy break; 1429145256Sjkoshy 1430145256Sjkoshy case EVFILT_SIGNAL: 1431147191Sjkoshy if (kev.ident == SIGCHLD) { 1432147191Sjkoshy /* 1433147191Sjkoshy * The child process sends us a 1434147191Sjkoshy * SIGCHLD if its exec() failed. We 1435147191Sjkoshy * wait for it to exit and then exit 1436147191Sjkoshy * ourselves. 1437147191Sjkoshy */ 1438147191Sjkoshy (void) wait(&c); 1439147191Sjkoshy runstate = PMCSTAT_FINISHED; 1440147191Sjkoshy } else if (kev.ident == SIGIO) { 1441147191Sjkoshy /* 1442147191Sjkoshy * We get a SIGIO if a PMC loses all 1443147191Sjkoshy * of its targets, or if logfile 1444147191Sjkoshy * writes encounter an error. 1445147191Sjkoshy */ 1446210794Sfabient runstate = pmcstat_close_log(); 1447147191Sjkoshy do_print = 1; /* print PMCs at exit */ 1448147191Sjkoshy } else if (kev.ident == SIGINT) { 1449147708Sjkoshy /* Kill the child process if we started it */ 1450147708Sjkoshy if (args.pa_flags & FLAG_HAS_COMMANDLINE) 1451203790Sfabient pmcstat_kill_process(); 1452210794Sfabient runstate = pmcstat_close_log(); 1453145256Sjkoshy } else if (kev.ident == SIGWINCH) { 1454147708Sjkoshy if (ioctl(fileno(args.pa_printfile), 1455145256Sjkoshy TIOCGWINSZ, &ws) < 0) 1456227524Sobrien err(EX_OSERR, 1457227524Sobrien "ERROR: Cannot determine window size"); 1458145256Sjkoshy pmcstat_displayheight = ws.ws_row - 1; 1459203790Sfabient pmcstat_displaywidth = ws.ws_col - 1; 1460145256Sjkoshy } else 1461145256Sjkoshy assert(0); 1462145256Sjkoshy 1463145256Sjkoshy break; 1464147191Sjkoshy 1465266890Sgnn case EVFILT_TIMER: 1466266890Sgnn /* time duration reached, exit */ 1467266890Sgnn if (args.pa_flags & FLAG_HAS_DURATION) { 1468266890Sgnn runstate = PMCSTAT_FINISHED; 1469266890Sgnn break; 1470266890Sgnn } 1471266890Sgnn /* print out counting PMCs */ 1472226514Sfabient if ((args.pa_flags & FLAG_DO_TOP) && 1473226986Sfabient pmc_flush_logfile() == 0) 1474226514Sfabient do_read = 1; 1475147191Sjkoshy do_print = 1; 1476147191Sjkoshy break; 1477147191Sjkoshy 1478145256Sjkoshy } 1479145256Sjkoshy 1480226514Sfabient if (do_print && !do_read) { 1481203790Sfabient if ((args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) { 1482203790Sfabient pmcstat_print_pmcs(); 1483227524Sobrien if (runstate == PMCSTAT_FINISHED && 1484227524Sobrien /* final newline */ 1485203790Sfabient (args.pa_flags & FLAG_DO_PRINT) == 0) 1486203790Sfabient (void) fprintf(args.pa_printfile, "\n"); 1487203790Sfabient } 1488203790Sfabient if (args.pa_flags & FLAG_DO_TOP) 1489203790Sfabient pmcstat_display_log(); 1490147191Sjkoshy do_print = 0; 1491147191Sjkoshy } 1492145256Sjkoshy 1493147191Sjkoshy } while (runstate != PMCSTAT_FINISHED); 1494147191Sjkoshy 1495203790Sfabient if ((args.pa_flags & FLAG_DO_TOP) && args.pa_toptty) { 1496203790Sfabient pmcstat_topexit(); 1497203790Sfabient args.pa_toptty = 0; 1498203790Sfabient } 1499203790Sfabient 1500147191Sjkoshy /* flush any pending log entries */ 1501147708Sjkoshy if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE)) 1502226514Sfabient pmc_close_logfile(); 1503147191Sjkoshy 1504203790Sfabient pmcstat_cleanup(); 1505145256Sjkoshy 1506157144Sjkoshy free(args.pa_kernel); 1507157144Sjkoshy 1508151542Sjkoshy /* check if the driver lost any samples or events */ 1509151542Sjkoshy if (check_driver_stats) { 1510151542Sjkoshy if (pmc_get_driver_stats(&ds_end) < 0) 1511227524Sobrien err(EX_OSERR, 1512227524Sobrien "ERROR: Cannot retrieve driver statistics"); 1513157144Sjkoshy if (ds_start.pm_intr_bufferfull != ds_end.pm_intr_bufferfull && 1514157144Sjkoshy args.pa_verbosity > 0) 1515294046Sjtl warnx( 1516294046Sjtl"WARNING: sampling was paused at least %u time%s.\n" 1517294046Sjtl"Please consider tuning the \"kern.hwpmc.nsamples\" tunable.", 1518294046Sjtl ds_end.pm_intr_bufferfull - 1519294046Sjtl ds_start.pm_intr_bufferfull, 1520294046Sjtl ((ds_end.pm_intr_bufferfull - 1521294046Sjtl ds_start.pm_intr_bufferfull) != 1) ? "s" : "" 1522227524Sobrien ); 1523151542Sjkoshy if (ds_start.pm_buffer_requests_failed != 1524157144Sjkoshy ds_end.pm_buffer_requests_failed && 1525157144Sjkoshy args.pa_verbosity > 0) 1526294046Sjtl warnx( 1527294046Sjtl"WARNING: at least %u event%s were discarded while running.\n" 1528294046Sjtl"Please consider tuning the \"kern.hwpmc.nbuffers\" tunable.", 1529294046Sjtl ds_end.pm_buffer_requests_failed - 1530294046Sjtl ds_start.pm_buffer_requests_failed, 1531294046Sjtl ((ds_end.pm_buffer_requests_failed - 1532294046Sjtl ds_start.pm_buffer_requests_failed) != 1) ? "s" : "" 1533227524Sobrien ); 1534151542Sjkoshy } 1535151542Sjkoshy 1536151542Sjkoshy exit(EX_OK); 1537145256Sjkoshy} 1538