2005-04-19 04:01:25 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2008-07-01 15:20:15 +00:00
|
|
|
* Copyright (c) 2003-2008, Joseph Koshy
|
2007-12-07 08:26:21 +00:00
|
|
|
* Copyright (c) 2007 The FreeBSD Foundation
|
2005-04-19 04:01:25 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2007-12-07 08:26:21 +00:00
|
|
|
* Portions of this software were developed by A. Joseph Koshy under
|
|
|
|
* sponsorship from the FreeBSD Foundation and Google, Inc.
|
|
|
|
*
|
2005-04-19 04:01:25 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2007-04-27 12:09:31 +00:00
|
|
|
#include <sys/param.h>
|
2011-08-07 18:37:05 +00:00
|
|
|
#include <sys/cpuset.h>
|
|
|
|
#include <sys/event.h>
|
2005-04-19 04:01:25 +00:00
|
|
|
#include <sys/queue.h>
|
2005-06-30 19:01:26 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
2007-04-22 15:00:39 +00:00
|
|
|
#include <sys/sysctl.h>
|
2005-04-19 04:01:25 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/ttycom.h>
|
2007-04-27 12:09:31 +00:00
|
|
|
#include <sys/user.h>
|
2005-04-19 04:01:25 +00:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
2010-02-11 22:51:44 +00:00
|
|
|
#include <curses.h>
|
2005-04-19 04:01:25 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2007-04-27 12:09:31 +00:00
|
|
|
#include <kvm.h>
|
2006-03-26 12:20:54 +00:00
|
|
|
#include <libgen.h>
|
2005-04-19 04:01:25 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <pmc.h>
|
2005-06-09 19:45:09 +00:00
|
|
|
#include <pmclog.h>
|
2007-04-27 12:09:31 +00:00
|
|
|
#include <regex.h>
|
2005-04-19 04:01:25 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdint.h>
|
2005-06-30 19:01:26 +00:00
|
|
|
#include <stdio.h>
|
2005-04-19 04:01:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-10-24 16:28:00 +00:00
|
|
|
#include <libpmcstat.h>
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
#include "pmcstat.h"
|
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
/*
|
|
|
|
* A given invocation of pmcstat(8) can manage multiple PMCs of both
|
|
|
|
* the system-wide and per-process variety. Each of these could be in
|
|
|
|
* 'counting mode' or in 'sampling mode'.
|
|
|
|
*
|
|
|
|
* For 'counting mode' PMCs, pmcstat(8) will periodically issue a
|
|
|
|
* pmc_read() at the configured time interval and print out the value
|
|
|
|
* of the requested PMCs.
|
|
|
|
*
|
|
|
|
* For 'sampling mode' PMCs it can log to a file for offline analysis,
|
|
|
|
* or can analyse sampling data "on the fly", either by converting
|
|
|
|
* samples to printed textual form or by creating gprof(1) compatible
|
|
|
|
* profiles, one per program executed. When creating gprof(1)
|
|
|
|
* profiles it can optionally merge entries from multiple processes
|
|
|
|
* for a given executable into a single profile file.
|
2007-04-22 15:00:39 +00:00
|
|
|
*
|
|
|
|
* pmcstat(8) can also execute a command line and attach PMCs to the
|
|
|
|
* resulting child process. The protocol used is as follows:
|
|
|
|
*
|
|
|
|
* - parent creates a socketpair for two way communication and
|
|
|
|
* fork()s.
|
|
|
|
* - subsequently:
|
|
|
|
*
|
|
|
|
* /Parent/ /Child/
|
|
|
|
*
|
|
|
|
* - Wait for childs token.
|
|
|
|
* - Sends token.
|
|
|
|
* - Awaits signal to start.
|
|
|
|
* - Attaches PMCs to the child's pid
|
|
|
|
* and starts them. Sets up
|
|
|
|
* monitoring for the child.
|
|
|
|
* - Signals child to start.
|
2011-12-30 10:58:14 +00:00
|
|
|
* - Receives signal, attempts exec().
|
2007-04-22 15:00:39 +00:00
|
|
|
*
|
|
|
|
* After this point normal processing can happen.
|
2005-06-09 19:45:09 +00:00
|
|
|
*/
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
/* Globals */
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2012-10-19 14:49:42 +00:00
|
|
|
int pmcstat_displayheight = DEFAULT_DISPLAY_HEIGHT;
|
|
|
|
int pmcstat_displaywidth = DEFAULT_DISPLAY_WIDTH;
|
|
|
|
static int pmcstat_sockpair[NSOCKPAIRFD];
|
|
|
|
static int pmcstat_kq;
|
|
|
|
static kvm_t *pmcstat_kvm;
|
|
|
|
static struct kinfo_proc *pmcstat_plist;
|
2010-02-11 22:51:44 +00:00
|
|
|
struct pmcstat_args args;
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2011-08-07 18:37:05 +00:00
|
|
|
static void
|
|
|
|
pmcstat_get_cpumask(const char *cpuspec, cpuset_t *cpumask)
|
|
|
|
{
|
|
|
|
int cpu;
|
|
|
|
const char *s;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
CPU_ZERO(cpumask);
|
|
|
|
s = cpuspec;
|
|
|
|
|
|
|
|
do {
|
|
|
|
cpu = strtol(s, &end, 0);
|
|
|
|
if (cpu < 0 || end == s)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: Illegal CPU specification \"%s\".",
|
|
|
|
cpuspec);
|
2011-08-07 18:37:05 +00:00
|
|
|
CPU_SET(cpu, cpumask);
|
|
|
|
s = end + strspn(end, ", \t");
|
|
|
|
} while (*s);
|
2015-05-27 13:54:37 +00:00
|
|
|
assert(!CPU_EMPTY(cpumask));
|
2011-08-07 18:37:05 +00:00
|
|
|
}
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_cleanup(void)
|
2005-04-19 04:01:25 +00:00
|
|
|
{
|
2016-10-07 21:03:12 +00:00
|
|
|
struct pmcstat_ev *ev;
|
2005-04-19 04:01:25 +00:00
|
|
|
|
|
|
|
/* release allocated PMCs. */
|
2016-10-07 21:03:12 +00:00
|
|
|
STAILQ_FOREACH(ev, &args.pa_events, ev_next)
|
|
|
|
if (ev->ev_pmcid != PMC_ID_INVALID) {
|
|
|
|
if (pmc_stop(ev->ev_pmcid) < 0)
|
|
|
|
err(EX_OSERR,
|
|
|
|
"ERROR: cannot stop pmc 0x%x \"%s\"",
|
|
|
|
ev->ev_pmcid, ev->ev_name);
|
|
|
|
if (pmc_release(ev->ev_pmcid) < 0)
|
|
|
|
err(EX_OSERR,
|
|
|
|
"ERROR: cannot release pmc 0x%x \"%s\"",
|
|
|
|
ev->ev_pmcid, ev->ev_name);
|
|
|
|
}
|
2005-06-09 19:45:09 +00:00
|
|
|
|
2005-07-09 17:08:46 +00:00
|
|
|
/* de-configure the log file if present. */
|
2010-02-11 22:51:44 +00:00
|
|
|
if (args.pa_flags & (FLAG_HAS_PIPE | FLAG_HAS_OUTPUT_LOGFILE))
|
2005-07-09 17:08:46 +00:00
|
|
|
(void) pmc_configure_logfile(-1);
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
if (args.pa_logparser) {
|
|
|
|
pmclog_close(args.pa_logparser);
|
|
|
|
args.pa_logparser = NULL;
|
2005-06-09 19:45:09 +00:00
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2017-10-24 16:28:00 +00:00
|
|
|
pmcstat_log_shutdown_logging();
|
2007-04-22 15:00:39 +00:00
|
|
|
}
|
|
|
|
|
2007-04-27 12:09:31 +00:00
|
|
|
void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_find_targets(const char *spec)
|
2007-04-27 12:09:31 +00:00
|
|
|
{
|
|
|
|
int n, nproc, pid, rv;
|
|
|
|
struct pmcstat_target *pt;
|
|
|
|
char errbuf[_POSIX2_LINE_MAX], *end;
|
|
|
|
static struct kinfo_proc *kp;
|
|
|
|
regex_t reg;
|
|
|
|
regmatch_t regmatch;
|
|
|
|
|
|
|
|
/* First check if we've been given a process id. */
|
|
|
|
pid = strtol(spec, &end, 0);
|
|
|
|
if (end != spec && pid >= 0) {
|
|
|
|
if ((pt = malloc(sizeof(*pt))) == NULL)
|
|
|
|
goto outofmemory;
|
|
|
|
pt->pt_pid = pid;
|
2010-02-11 22:51:44 +00:00
|
|
|
SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
|
2007-04-27 12:09:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise treat arg as a regular expression naming processes. */
|
|
|
|
if (pmcstat_kvm == NULL) {
|
|
|
|
if ((pmcstat_kvm = kvm_openfiles(NULL, "/dev/null", NULL, 0,
|
|
|
|
errbuf)) == NULL)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot open kernel \"%s\"",
|
|
|
|
errbuf);
|
|
|
|
if ((pmcstat_plist = kvm_getprocs(pmcstat_kvm, KERN_PROC_PROC,
|
|
|
|
0, &nproc)) == NULL)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot get process list: %s",
|
|
|
|
kvm_geterr(pmcstat_kvm));
|
2010-06-05 22:57:53 +00:00
|
|
|
} else
|
|
|
|
nproc = 0;
|
2007-04-27 12:09:31 +00:00
|
|
|
|
|
|
|
if ((rv = regcomp(®, spec, REG_EXTENDED|REG_NOSUB)) != 0) {
|
|
|
|
regerror(rv, ®, errbuf, sizeof(errbuf));
|
|
|
|
err(EX_DATAERR, "ERROR: Failed to compile regex \"%s\": %s",
|
|
|
|
spec, errbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0, kp = pmcstat_plist; n < nproc; n++, kp++) {
|
|
|
|
if ((rv = regexec(®, kp->ki_comm, 1, ®match, 0)) == 0) {
|
|
|
|
if ((pt = malloc(sizeof(*pt))) == NULL)
|
|
|
|
goto outofmemory;
|
|
|
|
pt->pt_pid = kp->ki_pid;
|
2010-02-11 22:51:44 +00:00
|
|
|
SLIST_INSERT_HEAD(&args.pa_targets, pt, pt_next);
|
2007-04-27 12:09:31 +00:00
|
|
|
} else if (rv != REG_NOMATCH) {
|
|
|
|
regerror(rv, ®, errbuf, sizeof(errbuf));
|
|
|
|
errx(EX_SOFTWARE, "ERROR: Regex evalation failed: %s",
|
|
|
|
errbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
regfree(®);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
outofmemory:
|
|
|
|
errx(EX_SOFTWARE, "Out of memory.");
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_kill_process(void)
|
2007-04-27 12:09:31 +00:00
|
|
|
{
|
|
|
|
struct pmcstat_target *pt;
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
assert(args.pa_flags & FLAG_HAS_COMMANDLINE);
|
2007-04-27 12:09:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If a command line was specified, it would be the very first
|
|
|
|
* in the list, before any other processes specified by -t.
|
|
|
|
*/
|
2010-02-11 22:51:44 +00:00
|
|
|
pt = SLIST_FIRST(&args.pa_targets);
|
2007-04-27 12:09:31 +00:00
|
|
|
assert(pt != NULL);
|
|
|
|
|
|
|
|
if (kill(pt->pt_pid, SIGINT) != 0)
|
|
|
|
err(EX_OSERR, "ERROR: cannot signal child process");
|
|
|
|
}
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_start_pmcs(void)
|
2005-04-19 04:01:25 +00:00
|
|
|
{
|
|
|
|
struct pmcstat_ev *ev;
|
|
|
|
|
2007-04-27 12:09:31 +00:00
|
|
|
STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
|
2005-04-19 04:01:25 +00:00
|
|
|
|
|
|
|
assert(ev->ev_pmcid != PMC_ID_INVALID);
|
|
|
|
|
|
|
|
if (pmc_start(ev->ev_pmcid) < 0) {
|
2005-06-09 19:45:09 +00:00
|
|
|
warn("ERROR: Cannot start pmc 0x%x \"%s\"",
|
2005-04-19 04:01:25 +00:00
|
|
|
ev->ev_pmcid, ev->ev_name);
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_cleanup();
|
2005-06-09 19:45:09 +00:00
|
|
|
exit(EX_OSERR);
|
2005-04-19 04:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_print_headers(void)
|
2005-04-19 04:01:25 +00:00
|
|
|
{
|
|
|
|
struct pmcstat_ev *ev;
|
2007-04-22 15:00:39 +00:00
|
|
|
int c, w;
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
(void) fprintf(args.pa_printfile, PRINT_HEADER_PREFIX);
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
|
2005-04-19 04:01:25 +00:00
|
|
|
if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
c = PMC_IS_SYSTEM_MODE(ev->ev_mode) ? 's' : 'p';
|
|
|
|
|
2007-04-22 15:00:39 +00:00
|
|
|
if (ev->ev_fieldskip != 0)
|
2010-02-11 22:51:44 +00:00
|
|
|
(void) fprintf(args.pa_printfile, "%*s",
|
2007-04-22 15:00:39 +00:00
|
|
|
ev->ev_fieldskip, "");
|
|
|
|
w = ev->ev_fieldwidth - ev->ev_fieldskip - 2;
|
|
|
|
|
|
|
|
if (c == 's')
|
2010-02-11 22:51:44 +00:00
|
|
|
(void) fprintf(args.pa_printfile, "s/%02d/%-*s ",
|
2007-04-22 15:00:39 +00:00
|
|
|
ev->ev_cpu, w-3, ev->ev_name);
|
|
|
|
else
|
2010-02-11 22:51:44 +00:00
|
|
|
(void) fprintf(args.pa_printfile, "p/%*s ", w,
|
2005-04-19 04:01:25 +00:00
|
|
|
ev->ev_name);
|
|
|
|
}
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
(void) fflush(args.pa_printfile);
|
2005-04-19 04:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_print_counters(void)
|
2005-04-19 04:01:25 +00:00
|
|
|
{
|
|
|
|
int extra_width;
|
|
|
|
struct pmcstat_ev *ev;
|
|
|
|
pmc_value_t value;
|
|
|
|
|
|
|
|
extra_width = sizeof(PRINT_HEADER_PREFIX) - 1;
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
|
2005-04-19 04:01:25 +00:00
|
|
|
|
|
|
|
/* skip sampling mode counters */
|
|
|
|
if (PMC_IS_SAMPLING_MODE(ev->ev_mode))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pmc_read(ev->ev_pmcid, &value) < 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
err(EX_OSERR, "ERROR: Cannot read pmc \"%s\"",
|
|
|
|
ev->ev_name);
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
(void) fprintf(args.pa_printfile, "%*ju ",
|
2005-06-30 19:01:26 +00:00
|
|
|
ev->ev_fieldwidth + extra_width,
|
|
|
|
(uintmax_t) ev->ev_cumulative ? value :
|
|
|
|
(value - ev->ev_saved));
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
if (ev->ev_cumulative == 0)
|
|
|
|
ev->ev_saved = value;
|
|
|
|
extra_width = 0;
|
|
|
|
}
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
(void) fflush(args.pa_printfile);
|
2005-04-19 04:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print output
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_print_pmcs(void)
|
2005-04-19 04:01:25 +00:00
|
|
|
{
|
|
|
|
static int linecount = 0;
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
/* check if we need to print a header line */
|
2005-04-19 04:01:25 +00:00
|
|
|
if (++linecount > pmcstat_displayheight) {
|
2010-02-11 22:51:44 +00:00
|
|
|
(void) fprintf(args.pa_printfile, "\n");
|
2005-04-19 04:01:25 +00:00
|
|
|
linecount = 1;
|
|
|
|
}
|
|
|
|
if (linecount == 1)
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_print_headers();
|
|
|
|
(void) fprintf(args.pa_printfile, "\n");
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_print_counters();
|
2005-04-19 04:01:25 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pmcstat_show_usage(void)
|
|
|
|
{
|
|
|
|
errx(EX_USAGE,
|
|
|
|
"[options] [commandline]\n"
|
|
|
|
"\t Measure process and/or system performance using hardware\n"
|
|
|
|
"\t performance monitoring counters.\n"
|
|
|
|
"\t Options include:\n"
|
2005-06-09 19:45:09 +00:00
|
|
|
"\t -C\t\t (toggle) show cumulative counts\n"
|
|
|
|
"\t -D path\t create profiles in directory \"path\"\n"
|
|
|
|
"\t -E\t\t (toggle) show counts at process exit\n"
|
2010-02-11 22:51:44 +00:00
|
|
|
"\t -F file\t write a system-wide callgraph (Kcachegrind format)"
|
|
|
|
" to \"file\"\n"
|
2007-12-07 08:26:21 +00:00
|
|
|
"\t -G file\t write a system-wide callgraph to \"file\"\n"
|
2018-05-26 19:29:19 +00:00
|
|
|
"\t -I\t\t don't resolve leaf function name, show address instead\n"
|
|
|
|
"\t -L\t\t list all counters available on this host\n"
|
2006-03-26 12:20:54 +00:00
|
|
|
"\t -M file\t print executable/gmon file map to \"file\"\n"
|
2007-12-07 08:26:21 +00:00
|
|
|
"\t -N\t\t (toggle) capture callchains\n"
|
2005-06-09 19:45:09 +00:00
|
|
|
"\t -O file\t send log output to \"file\"\n"
|
|
|
|
"\t -P spec\t allocate a process-private sampling PMC\n"
|
|
|
|
"\t -R file\t read events from \"file\"\n"
|
|
|
|
"\t -S spec\t allocate a system-wide sampling PMC\n"
|
2010-02-11 22:51:44 +00:00
|
|
|
"\t -T\t\t start in top mode\n"
|
2018-06-04 01:10:23 +00:00
|
|
|
"\t -U \t\n merged user kernel stack capture\n"
|
2005-06-09 19:45:09 +00:00
|
|
|
"\t -W\t\t (toggle) show counts per context switch\n"
|
2014-04-29 07:19:22 +00:00
|
|
|
"\t -a file\t print sampled PCs and callgraph to \"file\"\n"
|
2007-04-22 15:00:39 +00:00
|
|
|
"\t -c cpu-list\t set cpus for subsequent system-wide PMCs\n"
|
2005-06-09 19:45:09 +00:00
|
|
|
"\t -d\t\t (toggle) track descendants\n"
|
2015-11-18 14:52:01 +00:00
|
|
|
"\t -e\t\t use wide history counter for gprof(1) output\n"
|
2010-02-11 22:51:44 +00:00
|
|
|
"\t -f spec\t pass \"spec\" to as plugin option\n"
|
2005-06-09 19:45:09 +00:00
|
|
|
"\t -g\t\t produce gprof(1) compatible profiles\n"
|
2018-05-26 19:29:19 +00:00
|
|
|
"\t -i lwp\t\t filter on thread id \"lwp\" in post-processing\n"
|
2006-09-29 17:28:01 +00:00
|
|
|
"\t -k dir\t\t set the path to the kernel\n"
|
2014-05-16 03:18:09 +00:00
|
|
|
"\t -l secs\t set duration time\n"
|
2012-08-22 19:02:07 +00:00
|
|
|
"\t -m file\t print sampled PCs to \"file\"\n"
|
2005-04-19 04:01:25 +00:00
|
|
|
"\t -n rate\t set sampling rate\n"
|
|
|
|
"\t -o file\t send print output to \"file\"\n"
|
2005-06-09 19:45:09 +00:00
|
|
|
"\t -p spec\t allocate a process-private counting PMC\n"
|
2006-03-26 12:20:54 +00:00
|
|
|
"\t -q\t\t suppress verbosity\n"
|
|
|
|
"\t -r fsroot\t specify FS root directory\n"
|
2005-06-09 19:45:09 +00:00
|
|
|
"\t -s spec\t allocate a system-wide counting PMC\n"
|
2008-09-19 14:51:41 +00:00
|
|
|
"\t -t process-spec attach to running processes matching "
|
|
|
|
"\"process-spec\"\n"
|
2018-05-26 19:29:19 +00:00
|
|
|
"\t -u spec \t provide short description of counters matching spec\n"
|
2006-03-26 12:20:54 +00:00
|
|
|
"\t -v\t\t increase verbosity\n"
|
2007-12-07 08:26:21 +00:00
|
|
|
"\t -w secs\t set printing time interval\n"
|
|
|
|
"\t -z depth\t limit callchain display depth"
|
2005-04-19 04:01:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/*
|
|
|
|
* At exit handler for top mode
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
pmcstat_topexit(void)
|
|
|
|
{
|
|
|
|
if (!args.pa_toptty)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shutdown ncurses.
|
|
|
|
*/
|
|
|
|
clrtoeol();
|
|
|
|
refresh();
|
|
|
|
endwin();
|
|
|
|
}
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
/*
|
|
|
|
* Main
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2015-05-27 13:54:37 +00:00
|
|
|
cpuset_t cpumask, rootmask;
|
2005-04-19 04:01:25 +00:00
|
|
|
double interval;
|
2014-05-16 03:18:09 +00:00
|
|
|
double duration;
|
2015-05-27 13:54:37 +00:00
|
|
|
int option, npmc;
|
2018-05-24 04:43:40 +00:00
|
|
|
int c, check_driver_stats;
|
2007-12-07 08:26:21 +00:00
|
|
|
int do_callchain, do_descendants, do_logproccsw, do_logprocexit;
|
2018-06-04 01:10:23 +00:00
|
|
|
int do_print, do_read, do_listcounters, do_descr;
|
|
|
|
int do_userspace;
|
2015-05-08 16:02:31 +00:00
|
|
|
size_t len;
|
2007-12-07 08:26:21 +00:00
|
|
|
int graphdepth;
|
2010-08-03 09:23:53 +00:00
|
|
|
int pipefd[2], rfd;
|
2005-06-09 19:45:09 +00:00
|
|
|
int use_cumulative_counts;
|
2010-02-11 22:51:44 +00:00
|
|
|
short cf, cb;
|
2018-05-24 04:43:40 +00:00
|
|
|
uint64_t current_sampling_count;
|
2018-05-26 19:29:19 +00:00
|
|
|
char *end, *tmp, *event;
|
2007-12-07 08:26:21 +00:00
|
|
|
const char *errmsg, *graphfilename;
|
2005-06-09 19:45:09 +00:00
|
|
|
enum pmcstat_state runstate;
|
2005-10-21 18:59:59 +00:00
|
|
|
struct pmc_driverstats ds_start, ds_end;
|
2005-04-19 04:01:25 +00:00
|
|
|
struct pmcstat_ev *ev;
|
|
|
|
struct sigaction sa;
|
|
|
|
struct kevent kev;
|
|
|
|
struct winsize ws;
|
2005-06-30 19:01:26 +00:00
|
|
|
struct stat sb;
|
2006-03-26 12:20:54 +00:00
|
|
|
char buffer[PATH_MAX];
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2005-10-21 18:59:59 +00:00
|
|
|
check_driver_stats = 0;
|
2018-05-24 04:30:06 +00:00
|
|
|
current_sampling_count = 0;
|
2007-12-07 08:26:21 +00:00
|
|
|
do_callchain = 1;
|
2018-05-26 19:29:19 +00:00
|
|
|
do_descr = 0;
|
2005-04-19 04:01:25 +00:00
|
|
|
do_descendants = 0;
|
2018-06-04 01:10:23 +00:00
|
|
|
do_userspace = 0;
|
2005-06-09 19:45:09 +00:00
|
|
|
do_logproccsw = 0;
|
|
|
|
do_logprocexit = 0;
|
2018-05-26 19:29:19 +00:00
|
|
|
do_listcounters = 0;
|
2005-04-19 04:01:25 +00:00
|
|
|
use_cumulative_counts = 0;
|
2007-12-07 08:26:21 +00:00
|
|
|
graphfilename = "-";
|
2005-06-09 19:45:09 +00:00
|
|
|
args.pa_required = 0;
|
2005-04-19 04:01:25 +00:00
|
|
|
args.pa_flags = 0;
|
2006-03-26 12:20:54 +00:00
|
|
|
args.pa_verbosity = 1;
|
2005-06-30 19:01:26 +00:00
|
|
|
args.pa_logfd = -1;
|
2006-03-26 12:20:54 +00:00
|
|
|
args.pa_fsroot = "";
|
2005-06-30 19:01:26 +00:00
|
|
|
args.pa_samplesdir = ".";
|
|
|
|
args.pa_printfile = stderr;
|
2007-12-07 08:26:21 +00:00
|
|
|
args.pa_graphdepth = DEFAULT_CALLGRAPH_DEPTH;
|
|
|
|
args.pa_graphfile = NULL;
|
2005-04-19 04:01:25 +00:00
|
|
|
args.pa_interval = DEFAULT_WAIT_INTERVAL;
|
2006-03-26 12:20:54 +00:00
|
|
|
args.pa_mapfilename = NULL;
|
2007-12-07 08:26:21 +00:00
|
|
|
args.pa_inputpath = NULL;
|
|
|
|
args.pa_outputpath = NULL;
|
2010-02-11 22:51:44 +00:00
|
|
|
args.pa_pplugin = PMCSTAT_PL_NONE;
|
|
|
|
args.pa_plugin = PMCSTAT_PL_NONE;
|
|
|
|
args.pa_ctdumpinstr = 1;
|
|
|
|
args.pa_topmode = PMCSTAT_TOP_DELTA;
|
|
|
|
args.pa_toptty = 0;
|
|
|
|
args.pa_topcolor = 0;
|
|
|
|
args.pa_mergepmc = 0;
|
2014-05-16 03:18:09 +00:00
|
|
|
args.pa_duration = 0.0;
|
2007-04-27 12:09:31 +00:00
|
|
|
STAILQ_INIT(&args.pa_events);
|
|
|
|
SLIST_INIT(&args.pa_targets);
|
2005-12-24 17:00:33 +00:00
|
|
|
bzero(&ds_start, sizeof(ds_start));
|
|
|
|
bzero(&ds_end, sizeof(ds_end));
|
2005-04-19 04:01:25 +00:00
|
|
|
ev = NULL;
|
2018-05-27 17:49:27 +00:00
|
|
|
event = NULL;
|
2011-08-07 18:37:05 +00:00
|
|
|
CPU_ZERO(&cpumask);
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2015-05-08 16:02:31 +00:00
|
|
|
/* Default to using the running system kernel. */
|
|
|
|
len = 0;
|
|
|
|
if (sysctlbyname("kern.bootfile", NULL, &len, NULL, 0) == -1)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot determine path of running kernel");
|
2016-10-07 21:03:12 +00:00
|
|
|
args.pa_kernel = malloc(len);
|
|
|
|
if (args.pa_kernel == NULL)
|
|
|
|
errx(EX_SOFTWARE, "ERROR: Out of memory.");
|
2015-05-08 16:02:31 +00:00
|
|
|
if (sysctlbyname("kern.bootfile", args.pa_kernel, &len, NULL, 0) == -1)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot determine path of running kernel");
|
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
/*
|
2015-05-27 13:54:37 +00:00
|
|
|
* The initial CPU mask specifies the root mask of this process
|
|
|
|
* which is usually all CPUs in the system.
|
2007-12-07 08:26:21 +00:00
|
|
|
*/
|
2015-05-27 13:54:37 +00:00
|
|
|
if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1,
|
|
|
|
sizeof(rootmask), &rootmask) == -1)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot determine the root set of CPUs");
|
|
|
|
CPU_COPY(&rootmask, &cpumask);
|
2007-04-22 15:00:39 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
while ((option = getopt(argc, argv,
|
2018-06-04 01:10:23 +00:00
|
|
|
"CD:EF:G:ILM:NO:P:R:S:TUWZa:c:def:gi:k:l:m:n:o:p:qr:s:t:u:vw:z:")) != -1)
|
2005-04-19 04:01:25 +00:00
|
|
|
switch (option) {
|
2014-02-24 02:43:58 +00:00
|
|
|
case 'a': /* Annotate + callgraph */
|
|
|
|
args.pa_flags |= FLAG_DO_ANNOTATE;
|
|
|
|
args.pa_plugin = PMCSTAT_PL_ANNOTATE_CG;
|
|
|
|
graphfilename = optarg;
|
|
|
|
break;
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
case 'C': /* cumulative values */
|
|
|
|
use_cumulative_counts = !use_cumulative_counts;
|
2005-06-09 19:45:09 +00:00
|
|
|
args.pa_required |= FLAG_HAS_COUNTING_PMCS;
|
2005-04-19 04:01:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c': /* CPU */
|
2015-05-27 13:54:37 +00:00
|
|
|
if (optarg[0] == '*' && optarg[1] == '\0')
|
|
|
|
CPU_COPY(&rootmask, &cpumask);
|
|
|
|
else
|
2011-08-07 18:37:05 +00:00
|
|
|
pmcstat_get_cpumask(optarg, &cpumask);
|
2007-04-22 15:00:39 +00:00
|
|
|
|
2010-08-03 13:46:25 +00:00
|
|
|
args.pa_flags |= FLAGS_HAS_CPUMASK;
|
2005-06-09 19:45:09 +00:00
|
|
|
args.pa_required |= FLAG_HAS_SYSTEM_PMCS;
|
2005-04-19 04:01:25 +00:00
|
|
|
break;
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
case 'D':
|
|
|
|
if (stat(optarg, &sb) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
|
|
|
|
optarg);
|
|
|
|
if (!S_ISDIR(sb.st_mode))
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: \"%s\" is not a directory.",
|
|
|
|
optarg);
|
2005-06-30 19:01:26 +00:00
|
|
|
args.pa_samplesdir = optarg;
|
|
|
|
args.pa_flags |= FLAG_HAS_SAMPLESDIR;
|
|
|
|
args.pa_required |= FLAG_DO_GPROF;
|
|
|
|
break;
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
case 'd': /* toggle descendents */
|
|
|
|
do_descendants = !do_descendants;
|
2005-06-09 19:45:09 +00:00
|
|
|
args.pa_required |= FLAG_HAS_PROCESS_PMCS;
|
|
|
|
break;
|
|
|
|
|
2018-05-26 19:29:19 +00:00
|
|
|
case 'E': /* log process exit */
|
|
|
|
do_logprocexit = !do_logprocexit;
|
|
|
|
args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
|
|
|
|
FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
|
|
|
|
break;
|
|
|
|
|
2015-11-18 14:52:01 +00:00
|
|
|
case 'e': /* wide gprof metrics */
|
|
|
|
args.pa_flags |= FLAG_DO_WIDE_GPROF_HC;
|
|
|
|
break;
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
case 'F': /* produce a system-wide calltree */
|
|
|
|
args.pa_flags |= FLAG_DO_CALLGRAPHS;
|
|
|
|
args.pa_plugin = PMCSTAT_PL_CALLTREE;
|
|
|
|
graphfilename = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f': /* plugins options */
|
|
|
|
if (args.pa_plugin == PMCSTAT_PL_NONE)
|
|
|
|
err(EX_USAGE, "ERROR: Need -g/-G/-m/-T.");
|
|
|
|
pmcstat_pluginconfigure_log(optarg);
|
|
|
|
break;
|
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
case 'G': /* produce a system-wide callgraph */
|
|
|
|
args.pa_flags |= FLAG_DO_CALLGRAPHS;
|
2010-02-11 22:51:44 +00:00
|
|
|
args.pa_plugin = PMCSTAT_PL_CALLGRAPH;
|
2007-12-07 08:26:21 +00:00
|
|
|
graphfilename = optarg;
|
|
|
|
break;
|
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
case 'g': /* produce gprof compatible profiles */
|
|
|
|
args.pa_flags |= FLAG_DO_GPROF;
|
2010-02-11 22:51:44 +00:00
|
|
|
args.pa_pplugin = PMCSTAT_PL_CALLGRAPH;
|
|
|
|
args.pa_plugin = PMCSTAT_PL_GPROF;
|
2005-06-09 19:45:09 +00:00
|
|
|
break;
|
|
|
|
|
2018-05-22 04:45:46 +00:00
|
|
|
case 'I':
|
|
|
|
args.pa_flags |= FLAG_SKIP_TOP_FN_RES;
|
|
|
|
break;
|
2018-05-26 19:29:19 +00:00
|
|
|
case 'i':
|
|
|
|
args.pa_flags |= FLAG_FILTER_THREAD_ID;
|
|
|
|
args.pa_tid = strtol(optarg, &end, 0);
|
|
|
|
break;
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
case 'k': /* pathname to the kernel */
|
2006-03-26 12:20:54 +00:00
|
|
|
free(args.pa_kernel);
|
|
|
|
args.pa_kernel = strdup(optarg);
|
2016-10-07 21:03:12 +00:00
|
|
|
if (args.pa_kernel == NULL)
|
|
|
|
errx(EX_SOFTWARE, "ERROR: Out of memory");
|
2007-12-07 08:26:21 +00:00
|
|
|
args.pa_required |= FLAG_DO_ANALYSIS;
|
2005-06-30 19:01:26 +00:00
|
|
|
args.pa_flags |= FLAG_HAS_KERNELPATH;
|
2005-06-09 19:45:09 +00:00
|
|
|
break;
|
|
|
|
|
2018-05-23 17:25:00 +00:00
|
|
|
case 'L':
|
2018-05-26 19:29:19 +00:00
|
|
|
do_listcounters = 1;
|
2018-05-23 17:25:00 +00:00
|
|
|
break;
|
2018-05-26 19:29:19 +00:00
|
|
|
|
2014-05-16 03:18:09 +00:00
|
|
|
case 'l': /* time duration in seconds */
|
|
|
|
duration = strtod(optarg, &end);
|
|
|
|
if (*end != '\0' || duration <= 0)
|
|
|
|
errx(EX_USAGE, "ERROR: Illegal duration time "
|
|
|
|
"value \"%s\".", optarg);
|
|
|
|
args.pa_flags |= FLAG_HAS_DURATION;
|
|
|
|
args.pa_duration = duration;
|
|
|
|
break;
|
|
|
|
|
2008-11-25 23:24:29 +00:00
|
|
|
case 'm':
|
2010-02-11 22:51:44 +00:00
|
|
|
args.pa_flags |= FLAG_DO_ANNOTATE;
|
|
|
|
args.pa_plugin = PMCSTAT_PL_ANNOTATE;
|
|
|
|
graphfilename = optarg;
|
2008-11-25 23:24:29 +00:00
|
|
|
break;
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
case 'M': /* mapfile */
|
|
|
|
args.pa_mapfilename = optarg;
|
|
|
|
break;
|
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
case 'N':
|
|
|
|
do_callchain = !do_callchain;
|
|
|
|
args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
|
|
|
|
break;
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
case 'p': /* process virtual counting PMC */
|
|
|
|
case 's': /* system-wide counting PMC */
|
|
|
|
case 'P': /* process virtual sampling PMC */
|
|
|
|
case 'S': /* system-wide sampling PMC */
|
|
|
|
if ((ev = malloc(sizeof(*ev))) == NULL)
|
2005-06-09 19:45:09 +00:00
|
|
|
errx(EX_SOFTWARE, "ERROR: Out of memory.");
|
2005-04-19 04:01:25 +00:00
|
|
|
|
|
|
|
switch (option) {
|
|
|
|
case 'p': ev->ev_mode = PMC_MODE_TC; break;
|
|
|
|
case 's': ev->ev_mode = PMC_MODE_SC; break;
|
|
|
|
case 'P': ev->ev_mode = PMC_MODE_TS; break;
|
|
|
|
case 'S': ev->ev_mode = PMC_MODE_SS; break;
|
|
|
|
}
|
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
if (option == 'P' || option == 'p') {
|
|
|
|
args.pa_flags |= FLAG_HAS_PROCESS_PMCS;
|
2005-06-30 19:01:26 +00:00
|
|
|
args.pa_required |= (FLAG_HAS_COMMANDLINE |
|
2007-04-27 12:09:31 +00:00
|
|
|
FLAG_HAS_TARGET);
|
2005-06-09 19:45:09 +00:00
|
|
|
}
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
if (option == 'P' || option == 'S') {
|
|
|
|
args.pa_flags |= FLAG_HAS_SAMPLING_PMCS;
|
2005-06-30 19:01:26 +00:00
|
|
|
args.pa_required |= (FLAG_HAS_PIPE |
|
|
|
|
FLAG_HAS_OUTPUT_LOGFILE);
|
2005-06-09 19:45:09 +00:00
|
|
|
}
|
2005-04-19 04:01:25 +00:00
|
|
|
|
|
|
|
if (option == 'p' || option == 's')
|
2005-06-09 19:45:09 +00:00
|
|
|
args.pa_flags |= FLAG_HAS_COUNTING_PMCS;
|
|
|
|
|
|
|
|
if (option == 's' || option == 'S')
|
|
|
|
args.pa_flags |= FLAG_HAS_SYSTEM_PMCS;
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2017-10-24 16:28:00 +00:00
|
|
|
ev->ev_spec = strdup(optarg);
|
2016-10-07 21:03:12 +00:00
|
|
|
if (ev->ev_spec == NULL)
|
|
|
|
errx(EX_SOFTWARE, "ERROR: Out of memory.");
|
2005-04-19 04:01:25 +00:00
|
|
|
|
|
|
|
if (option == 'S' || option == 'P')
|
2018-05-26 19:29:19 +00:00
|
|
|
ev->ev_count = current_sampling_count ? current_sampling_count : pmc_pmu_sample_rate_get(ev->ev_spec);
|
2005-04-19 04:01:25 +00:00
|
|
|
else
|
|
|
|
ev->ev_count = -1;
|
|
|
|
|
2015-05-27 13:54:37 +00:00
|
|
|
if (option == 'S' || option == 's')
|
2015-09-01 17:52:43 +00:00
|
|
|
ev->ev_cpu = CPU_FFS(&cpumask) - 1;
|
2015-05-27 13:54:37 +00:00
|
|
|
else
|
2005-04-19 04:01:25 +00:00
|
|
|
ev->ev_cpu = PMC_CPU_ANY;
|
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
ev->ev_flags = 0;
|
2018-06-04 01:10:23 +00:00
|
|
|
if (do_callchain) {
|
2007-12-07 08:26:21 +00:00
|
|
|
ev->ev_flags |= PMC_F_CALLCHAIN;
|
2018-06-04 01:10:23 +00:00
|
|
|
if (do_userspace)
|
|
|
|
ev->ev_flags |= PMC_F_USERCALLCHAIN;
|
|
|
|
}
|
2005-06-09 19:45:09 +00:00
|
|
|
if (do_descendants)
|
|
|
|
ev->ev_flags |= PMC_F_DESCENDANTS;
|
|
|
|
if (do_logprocexit)
|
|
|
|
ev->ev_flags |= PMC_F_LOG_PROCEXIT;
|
|
|
|
if (do_logproccsw)
|
|
|
|
ev->ev_flags |= PMC_F_LOG_PROCCSW;
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
ev->ev_cumulative = use_cumulative_counts;
|
|
|
|
|
|
|
|
ev->ev_saved = 0LL;
|
|
|
|
ev->ev_pmcid = PMC_ID_INVALID;
|
|
|
|
|
|
|
|
/* extract event name */
|
|
|
|
c = strcspn(optarg, ", \t");
|
|
|
|
ev->ev_name = malloc(c + 1);
|
2016-10-07 21:03:12 +00:00
|
|
|
if (ev->ev_name == NULL)
|
|
|
|
errx(EX_SOFTWARE, "ERROR: Out of memory.");
|
2005-04-19 04:01:25 +00:00
|
|
|
(void) strncpy(ev->ev_name, optarg, c);
|
|
|
|
*(ev->ev_name + c) = '\0';
|
|
|
|
|
2007-04-27 12:09:31 +00:00
|
|
|
STAILQ_INSERT_TAIL(&args.pa_events, ev, ev_next);
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2011-08-07 18:37:05 +00:00
|
|
|
if (option == 's' || option == 'S') {
|
|
|
|
CPU_CLR(ev->ev_cpu, &cpumask);
|
2017-10-24 16:28:00 +00:00
|
|
|
pmcstat_clone_event_descriptor(ev, &cpumask, &args);
|
2015-05-27 13:54:37 +00:00
|
|
|
CPU_SET(ev->ev_cpu, &cpumask);
|
2011-08-07 18:37:05 +00:00
|
|
|
}
|
2007-04-22 15:00:39 +00:00
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n': /* sampling count */
|
|
|
|
current_sampling_count = strtol(optarg, &end, 0);
|
|
|
|
if (*end != '\0' || current_sampling_count <= 0)
|
|
|
|
errx(EX_USAGE,
|
2005-06-09 19:45:09 +00:00
|
|
|
"ERROR: Illegal count value \"%s\".",
|
2005-04-19 04:01:25 +00:00
|
|
|
optarg);
|
2005-06-09 19:45:09 +00:00
|
|
|
args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
|
2005-04-19 04:01:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o': /* outputfile */
|
2011-11-01 09:28:47 +00:00
|
|
|
if (args.pa_printfile != NULL &&
|
|
|
|
args.pa_printfile != stdout &&
|
|
|
|
args.pa_printfile != stderr)
|
2005-06-30 19:01:26 +00:00
|
|
|
(void) fclose(args.pa_printfile);
|
|
|
|
if ((args.pa_printfile = fopen(optarg, "w")) == NULL)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_OSERR,
|
|
|
|
"ERROR: cannot open \"%s\" for writing.",
|
|
|
|
optarg);
|
2005-06-30 19:01:26 +00:00
|
|
|
args.pa_flags |= FLAG_DO_PRINT;
|
2005-06-09 19:45:09 +00:00
|
|
|
break;
|
2005-04-19 04:01:25 +00:00
|
|
|
|
|
|
|
case 'O': /* sampling output */
|
2005-06-30 19:01:26 +00:00
|
|
|
if (args.pa_outputpath)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: option -O may only be specified once.");
|
2005-06-30 19:01:26 +00:00
|
|
|
args.pa_outputpath = optarg;
|
|
|
|
args.pa_flags |= FLAG_HAS_OUTPUT_LOGFILE;
|
|
|
|
break;
|
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
case 'q': /* quiet mode */
|
|
|
|
args.pa_verbosity = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r': /* root FS path */
|
|
|
|
args.pa_fsroot = optarg;
|
|
|
|
break;
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
case 'R': /* read an existing log file */
|
2007-12-07 08:26:21 +00:00
|
|
|
if (args.pa_inputpath != NULL)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: option -R may only be specified once.");
|
2005-06-30 19:01:26 +00:00
|
|
|
args.pa_inputpath = optarg;
|
|
|
|
if (args.pa_printfile == stderr)
|
|
|
|
args.pa_printfile = stdout;
|
|
|
|
args.pa_flags |= FLAG_READ_LOGFILE;
|
2005-04-19 04:01:25 +00:00
|
|
|
break;
|
|
|
|
|
2007-04-27 12:09:31 +00:00
|
|
|
case 't': /* target pid or process name */
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_find_targets(optarg);
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2007-04-27 12:09:31 +00:00
|
|
|
args.pa_flags |= FLAG_HAS_TARGET;
|
2005-06-09 19:45:09 +00:00
|
|
|
args.pa_required |= FLAG_HAS_PROCESS_PMCS;
|
2005-04-19 04:01:25 +00:00
|
|
|
break;
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
case 'T': /* top mode */
|
|
|
|
args.pa_flags |= FLAG_DO_TOP;
|
|
|
|
args.pa_plugin = PMCSTAT_PL_CALLGRAPH;
|
|
|
|
args.pa_ctdumpinstr = 0;
|
|
|
|
args.pa_mergepmc = 1;
|
|
|
|
if (args.pa_printfile == stderr)
|
|
|
|
args.pa_printfile = stdout;
|
|
|
|
break;
|
|
|
|
|
2018-05-26 19:29:19 +00:00
|
|
|
case 'u':
|
|
|
|
do_descr = 1;
|
|
|
|
event = optarg;
|
|
|
|
break;
|
2018-06-04 01:10:23 +00:00
|
|
|
case 'U': /* toggle user-space callchain capture */
|
|
|
|
do_userspace = !do_userspace;
|
|
|
|
args.pa_required |= FLAG_HAS_SAMPLING_PMCS;
|
2018-05-26 19:29:19 +00:00
|
|
|
break;
|
2006-03-26 12:20:54 +00:00
|
|
|
case 'v': /* verbose */
|
|
|
|
args.pa_verbosity++;
|
|
|
|
break;
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
case 'w': /* wait interval */
|
|
|
|
interval = strtod(optarg, &end);
|
|
|
|
if (*end != '\0' || interval <= 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: Illegal wait interval value \"%s\".",
|
|
|
|
optarg);
|
2005-04-19 04:01:25 +00:00
|
|
|
args.pa_flags |= FLAG_HAS_WAIT_INTERVAL;
|
|
|
|
args.pa_interval = interval;
|
2005-06-09 19:45:09 +00:00
|
|
|
break;
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
case 'W': /* toggle LOG_CSW */
|
|
|
|
do_logproccsw = !do_logproccsw;
|
|
|
|
args.pa_required |= (FLAG_HAS_PROCESS_PMCS |
|
2005-06-30 19:01:26 +00:00
|
|
|
FLAG_HAS_COUNTING_PMCS | FLAG_HAS_OUTPUT_LOGFILE);
|
2005-04-19 04:01:25 +00:00
|
|
|
break;
|
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
case 'z':
|
|
|
|
graphdepth = strtod(optarg, &end);
|
|
|
|
if (*end != '\0' || graphdepth <= 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: Illegal callchain depth \"%s\".",
|
|
|
|
optarg);
|
2007-12-07 08:26:21 +00:00
|
|
|
args.pa_graphdepth = graphdepth;
|
|
|
|
args.pa_required |= FLAG_DO_CALLGRAPHS;
|
|
|
|
break;
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
pmcstat_show_usage();
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2018-06-04 01:10:23 +00:00
|
|
|
if ((do_listcounters | do_descr) &&
|
2018-05-26 19:29:19 +00:00
|
|
|
pmc_pmu_enabled() == 0)
|
|
|
|
errx(EX_USAGE, "pmu features not supported on host or hwpmc not loaded");
|
|
|
|
if (do_listcounters) {
|
2018-06-01 00:45:53 +00:00
|
|
|
pmc_pmu_print_counters(NULL);
|
2018-05-26 19:29:19 +00:00
|
|
|
} else if (do_descr) {
|
|
|
|
pmc_pmu_print_counter_desc(event);
|
|
|
|
}
|
2018-06-04 01:10:23 +00:00
|
|
|
if (do_listcounters | do_descr)
|
2018-05-26 19:29:19 +00:00
|
|
|
exit(0);
|
2005-04-19 04:01:25 +00:00
|
|
|
|
|
|
|
args.pa_argc = (argc -= optind);
|
|
|
|
args.pa_argv = (argv += optind);
|
|
|
|
|
2010-08-03 13:46:25 +00:00
|
|
|
/* If we read from logfile and no specified CPU mask use
|
|
|
|
* the maximum CPU count.
|
|
|
|
*/
|
|
|
|
if ((args.pa_flags & FLAG_READ_LOGFILE) &&
|
|
|
|
(args.pa_flags & FLAGS_HAS_CPUMASK) == 0)
|
2011-08-07 18:37:05 +00:00
|
|
|
CPU_FILL(&cpumask);
|
2010-08-03 13:46:25 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
args.pa_cpumask = cpumask; /* For selecting CPUs using -R. */
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
if (argc) /* command line present */
|
|
|
|
args.pa_flags |= FLAG_HAS_COMMANDLINE;
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2008-11-25 23:24:29 +00:00
|
|
|
if (args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS |
|
2010-02-11 22:51:44 +00:00
|
|
|
FLAG_DO_ANNOTATE | FLAG_DO_TOP))
|
2007-12-07 08:26:21 +00:00
|
|
|
args.pa_flags |= FLAG_DO_ANALYSIS;
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
/*
|
|
|
|
* Check invocation syntax.
|
|
|
|
*/
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
/* disallow -O and -R together */
|
|
|
|
if (args.pa_outputpath && args.pa_inputpath)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: options -O and -R are mutually exclusive.");
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2014-05-16 03:18:09 +00:00
|
|
|
/* disallow -T and -l together */
|
|
|
|
if ((args.pa_flags & FLAG_HAS_DURATION) &&
|
|
|
|
(args.pa_flags & FLAG_DO_TOP))
|
|
|
|
errx(EX_USAGE, "ERROR: options -T and -l are mutually "
|
|
|
|
"exclusive.");
|
|
|
|
|
2015-03-28 21:28:22 +00:00
|
|
|
/* -a and -m require -R */
|
2010-02-11 22:51:44 +00:00
|
|
|
if (args.pa_flags & FLAG_DO_ANNOTATE && args.pa_inputpath == NULL)
|
2014-02-24 02:43:58 +00:00
|
|
|
errx(EX_USAGE, "ERROR: option %s requires an input file",
|
|
|
|
args.pa_plugin == PMCSTAT_PL_ANNOTATE ? "-m" : "-a");
|
2008-11-25 23:24:29 +00:00
|
|
|
|
|
|
|
/* -m option is not allowed combined with -g or -G. */
|
2010-02-11 22:51:44 +00:00
|
|
|
if (args.pa_flags & FLAG_DO_ANNOTATE &&
|
2008-11-25 23:24:29 +00:00
|
|
|
args.pa_flags & (FLAG_DO_GPROF | FLAG_DO_CALLGRAPHS))
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: option -m and -g | -G are mutually exclusive");
|
2008-11-25 23:24:29 +00:00
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
if (args.pa_flags & FLAG_READ_LOGFILE) {
|
2005-06-09 19:45:09 +00:00
|
|
|
errmsg = NULL;
|
2005-06-30 19:01:26 +00:00
|
|
|
if (args.pa_flags & FLAG_HAS_COMMANDLINE)
|
2005-06-09 19:45:09 +00:00
|
|
|
errmsg = "a command line specification";
|
2007-04-27 12:09:31 +00:00
|
|
|
else if (args.pa_flags & FLAG_HAS_TARGET)
|
2005-06-09 19:45:09 +00:00
|
|
|
errmsg = "option -t";
|
2007-04-27 12:09:31 +00:00
|
|
|
else if (!STAILQ_EMPTY(&args.pa_events))
|
2005-06-09 19:45:09 +00:00
|
|
|
errmsg = "a PMC event specification";
|
|
|
|
if (errmsg)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: option -R may not be used with %s.",
|
|
|
|
errmsg);
|
2007-04-27 12:09:31 +00:00
|
|
|
} else if (STAILQ_EMPTY(&args.pa_events))
|
2005-06-30 19:01:26 +00:00
|
|
|
/* All other uses require a PMC spec. */
|
2005-04-19 04:01:25 +00:00
|
|
|
pmcstat_show_usage();
|
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
/* check for -t pid without a process PMC spec */
|
2007-04-27 12:09:31 +00:00
|
|
|
if ((args.pa_required & FLAG_HAS_TARGET) &&
|
2005-06-09 19:45:09 +00:00
|
|
|
(args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: option -t requires a process mode PMC to be specified."
|
|
|
|
);
|
2005-06-09 19:45:09 +00:00
|
|
|
|
|
|
|
/* check for process-mode options without a command or -t pid */
|
|
|
|
if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
|
2007-04-27 12:09:31 +00:00
|
|
|
(args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: options -d, -E, -p, -P, and -W require a command line or target process."
|
|
|
|
);
|
2005-06-09 19:45:09 +00:00
|
|
|
|
|
|
|
/* check for -p | -P without a target process of some sort */
|
2007-04-27 12:09:31 +00:00
|
|
|
if ((args.pa_required & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) &&
|
|
|
|
(args.pa_flags & (FLAG_HAS_COMMANDLINE | FLAG_HAS_TARGET)) == 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: options -P and -p require a target process or a command line."
|
|
|
|
);
|
2005-06-09 19:45:09 +00:00
|
|
|
|
|
|
|
/* check for process-mode options without a process-mode PMC */
|
|
|
|
if ((args.pa_required & FLAG_HAS_PROCESS_PMCS) &&
|
|
|
|
(args.pa_flags & FLAG_HAS_PROCESS_PMCS) == 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: options -d, -E, and -W require a process mode PMC to be specified."
|
|
|
|
);
|
2005-06-09 19:45:09 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
/* check for -c cpu with no system mode PMCs or logfile. */
|
2005-06-09 19:45:09 +00:00
|
|
|
if ((args.pa_required & FLAG_HAS_SYSTEM_PMCS) &&
|
2007-12-07 08:26:21 +00:00
|
|
|
(args.pa_flags & FLAG_HAS_SYSTEM_PMCS) == 0 &&
|
|
|
|
(args.pa_flags & FLAG_READ_LOGFILE) == 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: option -c requires at least one system mode PMC to be specified."
|
|
|
|
);
|
2005-06-09 19:45:09 +00:00
|
|
|
|
|
|
|
/* check for counting mode options without a counting PMC */
|
|
|
|
if ((args.pa_required & FLAG_HAS_COUNTING_PMCS) &&
|
|
|
|
(args.pa_flags & FLAG_HAS_COUNTING_PMCS) == 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: options -C, -W and -o require at least one counting mode PMC to be specified."
|
|
|
|
);
|
2005-06-09 19:45:09 +00:00
|
|
|
|
|
|
|
/* check for sampling mode options without a sampling PMC spec */
|
|
|
|
if ((args.pa_required & FLAG_HAS_SAMPLING_PMCS) &&
|
|
|
|
(args.pa_flags & FLAG_HAS_SAMPLING_PMCS) == 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: options -N, -n and -O require at least one sampling mode PMC to be specified."
|
|
|
|
);
|
2005-06-09 19:45:09 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* check if -g/-G/-m/-T are being used correctly */
|
2007-12-07 08:26:21 +00:00
|
|
|
if ((args.pa_flags & FLAG_DO_ANALYSIS) &&
|
2005-06-30 19:01:26 +00:00
|
|
|
!(args.pa_flags & (FLAG_HAS_SAMPLING_PMCS|FLAG_READ_LOGFILE)))
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: options -g/-G/-m/-T require sampling PMCs or -R to be specified."
|
|
|
|
);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2015-11-18 14:52:01 +00:00
|
|
|
/* check if -e was specified without -g */
|
|
|
|
if ((args.pa_flags & FLAG_DO_WIDE_GPROF_HC) &&
|
|
|
|
!(args.pa_flags & FLAG_DO_GPROF))
|
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: option -e requires gprof mode to be specified."
|
|
|
|
);
|
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
/* check if -O was spuriously specified */
|
2005-06-30 19:01:26 +00:00
|
|
|
if ((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) &&
|
|
|
|
(args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0)
|
2005-06-09 19:45:09 +00:00
|
|
|
errx(EX_USAGE,
|
2011-11-15 06:44:07 +00:00
|
|
|
"ERROR: option -O is used only with options -E, -P, -S and -W."
|
|
|
|
);
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/* -k kernel path require -g/-G/-m/-T or -R */
|
2005-06-30 19:01:26 +00:00
|
|
|
if ((args.pa_flags & FLAG_HAS_KERNELPATH) &&
|
2007-12-07 08:26:21 +00:00
|
|
|
(args.pa_flags & FLAG_DO_ANALYSIS) == 0 &&
|
2006-03-26 12:20:54 +00:00
|
|
|
(args.pa_flags & FLAG_READ_LOGFILE) == 0)
|
2010-02-11 22:51:44 +00:00
|
|
|
errx(EX_USAGE, "ERROR: option -k is only used with -g/-R/-m/-T.");
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
/* -D only applies to gprof output mode (-g) */
|
2005-06-30 19:01:26 +00:00
|
|
|
if ((args.pa_flags & FLAG_HAS_SAMPLESDIR) &&
|
2007-12-07 08:26:21 +00:00
|
|
|
(args.pa_flags & FLAG_DO_GPROF) == 0)
|
|
|
|
errx(EX_USAGE, "ERROR: option -D is only used with -g.");
|
2006-03-26 12:20:54 +00:00
|
|
|
|
|
|
|
/* -M mapfile requires -g or -R */
|
|
|
|
if (args.pa_mapfilename != NULL &&
|
|
|
|
(args.pa_flags & FLAG_DO_GPROF) == 0 &&
|
|
|
|
(args.pa_flags & FLAG_READ_LOGFILE) == 0)
|
|
|
|
errx(EX_USAGE, "ERROR: option -M is only used with -g/-R.");
|
2005-06-30 19:01:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Disallow textual output of sampling PMCs if counting PMCs
|
|
|
|
* have also been asked for, mostly because the combined output
|
|
|
|
* is difficult to make sense of.
|
|
|
|
*/
|
|
|
|
if ((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
|
|
|
|
(args.pa_flags & FLAG_HAS_SAMPLING_PMCS) &&
|
2007-04-21 12:04:03 +00:00
|
|
|
((args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE) == 0))
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: option -O is required if counting and sampling PMCs are specified together."
|
|
|
|
);
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2006-03-26 12:20:54 +00:00
|
|
|
/*
|
2015-05-08 16:02:31 +00:00
|
|
|
* Check if 'kerneldir' refers to a file rather than a
|
|
|
|
* directory. If so, use `dirname path` to determine the
|
|
|
|
* kernel directory.
|
2006-03-26 12:20:54 +00:00
|
|
|
*/
|
2015-05-08 16:02:31 +00:00
|
|
|
(void) snprintf(buffer, sizeof(buffer), "%s%s", args.pa_fsroot,
|
|
|
|
args.pa_kernel);
|
|
|
|
if (stat(buffer, &sb) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot locate kernel \"%s\"",
|
|
|
|
buffer);
|
|
|
|
if (!S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode))
|
|
|
|
errx(EX_USAGE, "ERROR: \"%s\": Unsupported file type.",
|
|
|
|
buffer);
|
|
|
|
if (!S_ISDIR(sb.st_mode)) {
|
|
|
|
tmp = args.pa_kernel;
|
|
|
|
args.pa_kernel = strdup(dirname(args.pa_kernel));
|
2016-10-07 21:03:12 +00:00
|
|
|
if (args.pa_kernel == NULL)
|
|
|
|
errx(EX_SOFTWARE, "ERROR: Out of memory");
|
2015-05-08 16:02:31 +00:00
|
|
|
free(tmp);
|
|
|
|
(void) snprintf(buffer, sizeof(buffer), "%s%s",
|
|
|
|
args.pa_fsroot, args.pa_kernel);
|
2006-03-26 12:20:54 +00:00
|
|
|
if (stat(buffer, &sb) < 0)
|
2015-05-08 16:02:31 +00:00
|
|
|
err(EX_OSERR, "ERROR: Cannot stat \"%s\"",
|
2006-03-26 12:20:54 +00:00
|
|
|
buffer);
|
2015-05-08 16:02:31 +00:00
|
|
|
if (!S_ISDIR(sb.st_mode))
|
|
|
|
errx(EX_USAGE,
|
|
|
|
"ERROR: \"%s\" is not a directory.",
|
2006-03-26 12:20:54 +00:00
|
|
|
buffer);
|
|
|
|
}
|
2007-04-22 15:00:39 +00:00
|
|
|
|
2007-12-07 08:26:21 +00:00
|
|
|
/*
|
|
|
|
* If we have a callgraph be created, select the outputfile.
|
|
|
|
*/
|
|
|
|
if (args.pa_flags & FLAG_DO_CALLGRAPHS) {
|
|
|
|
if (strcmp(graphfilename, "-") == 0)
|
|
|
|
args.pa_graphfile = args.pa_printfile;
|
|
|
|
else {
|
|
|
|
args.pa_graphfile = fopen(graphfilename, "w");
|
|
|
|
if (args.pa_graphfile == NULL)
|
2011-11-15 06:44:07 +00:00
|
|
|
err(EX_OSERR,
|
|
|
|
"ERROR: cannot open \"%s\" for writing",
|
|
|
|
graphfilename);
|
2007-12-07 08:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
2010-02-11 22:51:44 +00:00
|
|
|
if (args.pa_flags & FLAG_DO_ANNOTATE) {
|
2008-11-25 23:24:29 +00:00
|
|
|
args.pa_graphfile = fopen(graphfilename, "w");
|
|
|
|
if (args.pa_graphfile == NULL)
|
|
|
|
err(EX_OSERR, "ERROR: cannot open \"%s\" for writing",
|
|
|
|
graphfilename);
|
|
|
|
}
|
2007-12-07 08:26:21 +00:00
|
|
|
|
2010-08-03 09:23:53 +00:00
|
|
|
/* if we've been asked to process a log file, skip init */
|
|
|
|
if ((args.pa_flags & FLAG_READ_LOGFILE) == 0) {
|
|
|
|
if (pmc_init() < 0)
|
|
|
|
err(EX_UNAVAILABLE,
|
2011-11-15 06:44:07 +00:00
|
|
|
"ERROR: Initialization of the pmc(3) library failed"
|
|
|
|
);
|
2010-08-03 09:23:53 +00:00
|
|
|
|
|
|
|
if ((npmc = pmc_npmc(0)) < 0) /* assume all CPUs are identical */
|
2011-11-15 06:44:07 +00:00
|
|
|
err(EX_OSERR,
|
|
|
|
"ERROR: Cannot determine the number of PMCs on CPU %d",
|
|
|
|
0);
|
2010-08-03 09:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a kqueue */
|
|
|
|
if ((pmcstat_kq = kqueue()) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot allocate kqueue");
|
|
|
|
|
|
|
|
/* Setup the logfile as the source. */
|
2005-06-30 19:01:26 +00:00
|
|
|
if (args.pa_flags & FLAG_READ_LOGFILE) {
|
|
|
|
/*
|
|
|
|
* Print the log in textual form if we haven't been
|
2007-12-07 08:26:21 +00:00
|
|
|
* asked to generate profiling information.
|
2005-06-30 19:01:26 +00:00
|
|
|
*/
|
2007-12-07 08:26:21 +00:00
|
|
|
if ((args.pa_flags & FLAG_DO_ANALYSIS) == 0)
|
2005-06-30 19:01:26 +00:00
|
|
|
args.pa_flags |= FLAG_DO_PRINT;
|
|
|
|
|
2017-10-24 16:28:00 +00:00
|
|
|
pmcstat_log_initialize_logging();
|
2010-08-03 09:23:53 +00:00
|
|
|
rfd = pmcstat_open_log(args.pa_inputpath,
|
2006-04-02 12:52:16 +00:00
|
|
|
PMCSTAT_OPEN_FOR_READ);
|
2010-08-03 09:23:53 +00:00
|
|
|
if ((args.pa_logparser = pmclog_open(rfd)) == NULL)
|
2005-06-30 19:01:26 +00:00
|
|
|
err(EX_OSERR, "ERROR: Cannot create parser");
|
2010-08-03 09:23:53 +00:00
|
|
|
if (fcntl(rfd, F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: fcntl(2) failed");
|
|
|
|
EV_SET(&kev, rfd, EVFILT_READ, EV_ADD,
|
|
|
|
0, 0, NULL);
|
|
|
|
if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot register kevent");
|
2005-06-09 19:45:09 +00:00
|
|
|
}
|
2005-06-30 19:01:26 +00:00
|
|
|
/*
|
|
|
|
* Configure the specified log file or setup a default log
|
|
|
|
* consumer via a pipe.
|
|
|
|
*/
|
|
|
|
if (args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) {
|
2006-04-02 12:52:16 +00:00
|
|
|
if (args.pa_outputpath)
|
|
|
|
args.pa_logfd = pmcstat_open_log(args.pa_outputpath,
|
|
|
|
PMCSTAT_OPEN_FOR_WRITE);
|
|
|
|
else {
|
2005-06-30 19:01:26 +00:00
|
|
|
/*
|
|
|
|
* process the log on the fly by reading it in
|
|
|
|
* through a pipe.
|
|
|
|
*/
|
|
|
|
if (pipe(pipefd) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: pipe(2) failed");
|
|
|
|
|
|
|
|
if (fcntl(pipefd[READPIPEFD], F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: fcntl(2) failed");
|
|
|
|
|
|
|
|
EV_SET(&kev, pipefd[READPIPEFD], EVFILT_READ, EV_ADD,
|
|
|
|
0, 0, NULL);
|
|
|
|
|
|
|
|
if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot register kevent");
|
|
|
|
|
|
|
|
args.pa_logfd = pipefd[WRITEPIPEFD];
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
args.pa_flags |= FLAG_HAS_PIPE;
|
|
|
|
if ((args.pa_flags & FLAG_DO_TOP) == 0)
|
|
|
|
args.pa_flags |= FLAG_DO_PRINT;
|
2005-06-30 19:01:26 +00:00
|
|
|
args.pa_logparser = pmclog_open(pipefd[READPIPEFD]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pmc_configure_logfile(args.pa_logfd) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot configure log file");
|
|
|
|
}
|
|
|
|
|
2005-10-21 18:59:59 +00:00
|
|
|
/* remember to check for driver errors if we are sampling or logging */
|
|
|
|
check_driver_stats = (args.pa_flags & FLAG_HAS_SAMPLING_PMCS) ||
|
|
|
|
(args.pa_flags & FLAG_HAS_OUTPUT_LOGFILE);
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
/*
|
2010-08-03 09:23:53 +00:00
|
|
|
if (args.pa_flags & FLAG_READ_LOGFILE) {
|
2005-04-19 04:01:25 +00:00
|
|
|
* Allocate PMCs.
|
|
|
|
*/
|
|
|
|
|
2007-04-27 12:09:31 +00:00
|
|
|
STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
|
2011-11-15 06:44:07 +00:00
|
|
|
if (pmc_allocate(ev->ev_spec, ev->ev_mode,
|
2018-06-06 02:48:09 +00:00
|
|
|
ev->ev_flags, ev->ev_cpu, &ev->ev_pmcid,
|
|
|
|
ev->ev_count) < 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
err(EX_OSERR,
|
|
|
|
"ERROR: Cannot allocate %s-mode pmc with specification \"%s\"",
|
|
|
|
PMC_IS_SYSTEM_MODE(ev->ev_mode) ?
|
|
|
|
"system" : "process", ev->ev_spec);
|
|
|
|
|
|
|
|
if (PMC_IS_SAMPLING_MODE(ev->ev_mode) &&
|
|
|
|
pmc_set(ev->ev_pmcid, ev->ev_count) < 0)
|
|
|
|
err(EX_OSERR,
|
|
|
|
"ERROR: Cannot set sampling count for PMC \"%s\"",
|
|
|
|
ev->ev_name);
|
2005-06-30 19:01:26 +00:00
|
|
|
}
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
/* compute printout widths */
|
2007-04-27 12:09:31 +00:00
|
|
|
STAILQ_FOREACH(ev, &args.pa_events, ev_next) {
|
2005-05-01 14:11:49 +00:00
|
|
|
int counter_width;
|
|
|
|
int display_width;
|
|
|
|
int header_width;
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2005-05-01 14:11:49 +00:00
|
|
|
(void) pmc_width(ev->ev_pmcid, &counter_width);
|
2007-04-22 15:00:39 +00:00
|
|
|
header_width = strlen(ev->ev_name) + 2; /* prefix '%c/' */
|
2005-05-01 14:11:49 +00:00
|
|
|
display_width = (int) floor(counter_width / 3.32193) + 1;
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2007-04-22 15:00:39 +00:00
|
|
|
if (PMC_IS_SYSTEM_MODE(ev->ev_mode))
|
|
|
|
header_width += 3; /* 2 digit CPU number + '/' */
|
|
|
|
|
2005-05-01 14:11:49 +00:00
|
|
|
if (header_width > display_width) {
|
2005-04-19 04:01:25 +00:00
|
|
|
ev->ev_fieldskip = 0;
|
2005-05-01 14:11:49 +00:00
|
|
|
ev->ev_fieldwidth = header_width;
|
2005-04-19 04:01:25 +00:00
|
|
|
} else {
|
2005-05-01 14:11:49 +00:00
|
|
|
ev->ev_fieldskip = display_width -
|
|
|
|
header_width;
|
|
|
|
ev->ev_fieldwidth = display_width;
|
2005-04-19 04:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If our output is being set to a terminal, register a handler
|
|
|
|
* for window size changes.
|
|
|
|
*/
|
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
if (isatty(fileno(args.pa_printfile))) {
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2005-06-30 19:01:26 +00:00
|
|
|
if (ioctl(fileno(args.pa_printfile), TIOCGWINSZ, &ws) < 0)
|
2005-04-19 04:01:25 +00:00
|
|
|
err(EX_OSERR, "ERROR: Cannot determine window size");
|
|
|
|
|
|
|
|
pmcstat_displayheight = ws.ws_row - 1;
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_displaywidth = ws.ws_col - 1;
|
2005-04-19 04:01:25 +00:00
|
|
|
|
|
|
|
EV_SET(&kev, SIGWINCH, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
|
|
|
|
|
|
|
|
if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
err(EX_OSERR,
|
|
|
|
"ERROR: Cannot register kevent for SIGWINCH");
|
2010-02-11 22:51:44 +00:00
|
|
|
|
|
|
|
args.pa_toptty = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Listen to key input in top mode.
|
|
|
|
*/
|
|
|
|
if (args.pa_flags & FLAG_DO_TOP) {
|
|
|
|
EV_SET(&kev, fileno(stdin), EVFILT_READ, EV_ADD, 0, 0, NULL);
|
|
|
|
if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot register kevent");
|
2005-04-19 04:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EV_SET(&kev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
|
|
|
|
if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot register kevent for SIGINT");
|
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
EV_SET(&kev, SIGIO, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
|
|
|
|
if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot register kevent for SIGIO");
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
/*
|
|
|
|
* An exec() failure of a forked child is signalled by the
|
|
|
|
* child sending the parent a SIGCHLD. We don't register an
|
|
|
|
* actual signal handler for SIGCHLD, but instead use our
|
|
|
|
* kqueue to pick up the signal.
|
|
|
|
*/
|
|
|
|
EV_SET(&kev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL);
|
|
|
|
if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot register kevent for SIGCHLD");
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/*
|
|
|
|
* Setup a timer if we have counting mode PMCs needing to be printed or
|
|
|
|
* top mode plugin is active.
|
|
|
|
*/
|
|
|
|
if (((args.pa_flags & FLAG_HAS_COUNTING_PMCS) &&
|
|
|
|
(args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) ||
|
|
|
|
(args.pa_flags & FLAG_DO_TOP)) {
|
2005-04-19 04:01:25 +00:00
|
|
|
EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0,
|
|
|
|
args.pa_interval * 1000, NULL);
|
|
|
|
|
|
|
|
if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
err(EX_OSERR,
|
|
|
|
"ERROR: Cannot register kevent for timer");
|
2005-04-19 04:01:25 +00:00
|
|
|
}
|
|
|
|
|
2014-05-16 03:18:09 +00:00
|
|
|
/*
|
|
|
|
* Setup a duration timer if we have sampling mode PMCs and
|
|
|
|
* a duration time is set
|
|
|
|
*/
|
|
|
|
if ((args.pa_flags & FLAG_HAS_SAMPLING_PMCS) &&
|
|
|
|
(args.pa_flags & FLAG_HAS_DURATION)) {
|
|
|
|
EV_SET(&kev, 0, EVFILT_TIMER, EV_ADD, 0,
|
|
|
|
args.pa_duration * 1000, NULL);
|
|
|
|
|
|
|
|
if (kevent(pmcstat_kq, &kev, 1, NULL, 0, NULL) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot register kevent for "
|
|
|
|
"time duration");
|
|
|
|
}
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
/* attach PMCs to the target process, starting it if specified */
|
2007-04-22 15:00:39 +00:00
|
|
|
if (args.pa_flags & FLAG_HAS_COMMANDLINE)
|
2017-10-24 16:28:00 +00:00
|
|
|
pmcstat_create_process(pmcstat_sockpair, &args, pmcstat_kq);
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2005-10-21 18:59:59 +00:00
|
|
|
if (check_driver_stats && pmc_get_driver_stats(&ds_start) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot retrieve driver statistics");
|
|
|
|
|
2007-04-22 15:00:39 +00:00
|
|
|
/* Attach process pmcs to the target process. */
|
2008-07-01 15:20:15 +00:00
|
|
|
if (args.pa_flags & (FLAG_HAS_TARGET | FLAG_HAS_COMMANDLINE)) {
|
2007-04-27 12:09:31 +00:00
|
|
|
if (SLIST_EMPTY(&args.pa_targets))
|
2011-11-15 06:44:07 +00:00
|
|
|
errx(EX_DATAERR,
|
|
|
|
"ERROR: No matching target processes.");
|
2008-10-07 17:28:52 +00:00
|
|
|
if (args.pa_flags & FLAG_HAS_PROCESS_PMCS)
|
2017-10-24 16:28:00 +00:00
|
|
|
pmcstat_attach_pmcs(&args);
|
2007-04-27 12:09:31 +00:00
|
|
|
|
|
|
|
if (pmcstat_kvm) {
|
|
|
|
kvm_close(pmcstat_kvm);
|
|
|
|
pmcstat_kvm = NULL;
|
|
|
|
}
|
|
|
|
}
|
2007-04-22 15:00:39 +00:00
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
/* start the pmcs */
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_start_pmcs();
|
2005-04-19 04:01:25 +00:00
|
|
|
|
|
|
|
/* start the (commandline) process if needed */
|
2005-06-30 19:01:26 +00:00
|
|
|
if (args.pa_flags & FLAG_HAS_COMMANDLINE)
|
2017-10-24 16:28:00 +00:00
|
|
|
pmcstat_start_process(pmcstat_sockpair);
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2010-08-03 09:23:53 +00:00
|
|
|
/* initialize logging */
|
2017-10-24 16:28:00 +00:00
|
|
|
pmcstat_log_initialize_logging();
|
2005-06-30 19:01:26 +00:00
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
/* Handle SIGINT using the kqueue loop */
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
(void) sigemptyset(&sa.sa_mask);
|
|
|
|
|
|
|
|
if (sigaction(SIGINT, &sa, NULL) < 0)
|
|
|
|
err(EX_OSERR, "ERROR: Cannot install signal handler");
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
/*
|
|
|
|
* Setup the top mode display.
|
|
|
|
*/
|
|
|
|
if (args.pa_flags & FLAG_DO_TOP) {
|
|
|
|
args.pa_flags &= ~FLAG_DO_PRINT;
|
|
|
|
|
|
|
|
if (args.pa_toptty) {
|
|
|
|
/*
|
|
|
|
* Init ncurses.
|
|
|
|
*/
|
|
|
|
initscr();
|
|
|
|
if(has_colors() == TRUE) {
|
|
|
|
args.pa_topcolor = 1;
|
|
|
|
start_color();
|
|
|
|
use_default_colors();
|
|
|
|
pair_content(0, &cf, &cb);
|
|
|
|
init_pair(1, COLOR_RED, cb);
|
|
|
|
init_pair(2, COLOR_YELLOW, cb);
|
|
|
|
init_pair(3, COLOR_GREEN, cb);
|
|
|
|
}
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
nonl();
|
|
|
|
nodelay(stdscr, 1);
|
|
|
|
intrflush(stdscr, FALSE);
|
|
|
|
keypad(stdscr, TRUE);
|
|
|
|
clear();
|
2010-03-05 23:08:19 +00:00
|
|
|
/* Get terminal width / height with ncurses. */
|
2011-11-15 06:44:07 +00:00
|
|
|
getmaxyx(stdscr,
|
|
|
|
pmcstat_displayheight, pmcstat_displaywidth);
|
2010-03-05 23:08:19 +00:00
|
|
|
pmcstat_displayheight--; pmcstat_displaywidth--;
|
2010-02-11 22:51:44 +00:00
|
|
|
atexit(pmcstat_topexit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
/*
|
|
|
|
* loop till either the target process (if any) exits, or we
|
2014-05-16 03:18:09 +00:00
|
|
|
* are killed by a SIGINT or we reached the time duration.
|
2005-04-19 04:01:25 +00:00
|
|
|
*/
|
2005-06-09 19:45:09 +00:00
|
|
|
runstate = PMCSTAT_RUNNING;
|
2011-10-18 15:25:43 +00:00
|
|
|
do_print = do_read = 0;
|
2005-04-19 04:01:25 +00:00
|
|
|
do {
|
|
|
|
if ((c = kevent(pmcstat_kq, NULL, 0, &kev, 1, NULL)) <= 0) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
err(EX_OSERR, "ERROR: kevent failed");
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kev.flags & EV_ERROR)
|
|
|
|
errc(EX_OSERR, kev.data, "ERROR: kevent failed");
|
|
|
|
|
|
|
|
switch (kev.filter) {
|
2005-06-09 19:45:09 +00:00
|
|
|
case EVFILT_PROC: /* target has exited */
|
2017-10-24 16:28:00 +00:00
|
|
|
runstate = pmcstat_close_log(&args);
|
2005-08-04 03:12:55 +00:00
|
|
|
do_print = 1;
|
2005-06-09 19:45:09 +00:00
|
|
|
break;
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
case EVFILT_READ: /* log file data is present */
|
2010-08-03 09:23:53 +00:00
|
|
|
if (kev.ident == (unsigned)fileno(stdin) &&
|
|
|
|
(args.pa_flags & FLAG_DO_TOP)) {
|
2010-02-11 22:51:44 +00:00
|
|
|
if (pmcstat_keypress_log())
|
2017-10-24 16:28:00 +00:00
|
|
|
runstate = pmcstat_close_log(&args);
|
2011-10-18 15:25:43 +00:00
|
|
|
} else {
|
|
|
|
do_read = 0;
|
2010-02-11 22:51:44 +00:00
|
|
|
runstate = pmcstat_process_log();
|
2011-10-18 15:25:43 +00:00
|
|
|
}
|
2005-04-19 04:01:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EVFILT_SIGNAL:
|
2005-06-09 19:45:09 +00:00
|
|
|
if (kev.ident == SIGCHLD) {
|
|
|
|
/*
|
|
|
|
* The child process sends us a
|
|
|
|
* SIGCHLD if its exec() failed. We
|
|
|
|
* wait for it to exit and then exit
|
|
|
|
* ourselves.
|
|
|
|
*/
|
|
|
|
(void) wait(&c);
|
|
|
|
runstate = PMCSTAT_FINISHED;
|
|
|
|
} else if (kev.ident == SIGIO) {
|
|
|
|
/*
|
|
|
|
* We get a SIGIO if a PMC loses all
|
|
|
|
* of its targets, or if logfile
|
|
|
|
* writes encounter an error.
|
|
|
|
*/
|
2017-10-24 16:28:00 +00:00
|
|
|
runstate = pmcstat_close_log(&args);
|
2005-06-09 19:45:09 +00:00
|
|
|
do_print = 1; /* print PMCs at exit */
|
|
|
|
} else if (kev.ident == SIGINT) {
|
2005-06-30 19:01:26 +00:00
|
|
|
/* Kill the child process if we started it */
|
|
|
|
if (args.pa_flags & FLAG_HAS_COMMANDLINE)
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_kill_process();
|
2017-10-24 16:28:00 +00:00
|
|
|
runstate = pmcstat_close_log(&args);
|
2005-04-19 04:01:25 +00:00
|
|
|
} else if (kev.ident == SIGWINCH) {
|
2005-06-30 19:01:26 +00:00
|
|
|
if (ioctl(fileno(args.pa_printfile),
|
2005-04-19 04:01:25 +00:00
|
|
|
TIOCGWINSZ, &ws) < 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
err(EX_OSERR,
|
|
|
|
"ERROR: Cannot determine window size");
|
2005-04-19 04:01:25 +00:00
|
|
|
pmcstat_displayheight = ws.ws_row - 1;
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_displaywidth = ws.ws_col - 1;
|
2005-04-19 04:01:25 +00:00
|
|
|
} else
|
|
|
|
assert(0);
|
|
|
|
|
|
|
|
break;
|
2005-06-09 19:45:09 +00:00
|
|
|
|
2014-05-16 03:18:09 +00:00
|
|
|
case EVFILT_TIMER:
|
|
|
|
/* time duration reached, exit */
|
|
|
|
if (args.pa_flags & FLAG_HAS_DURATION) {
|
|
|
|
runstate = PMCSTAT_FINISHED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* print out counting PMCs */
|
2011-10-18 15:25:43 +00:00
|
|
|
if ((args.pa_flags & FLAG_DO_TOP) &&
|
2018-01-17 16:55:35 +00:00
|
|
|
(args.pa_flags & FLAG_HAS_PIPE) &&
|
2011-11-01 09:28:47 +00:00
|
|
|
pmc_flush_logfile() == 0)
|
2011-10-18 15:25:43 +00:00
|
|
|
do_read = 1;
|
2005-06-09 19:45:09 +00:00
|
|
|
do_print = 1;
|
|
|
|
break;
|
|
|
|
|
2005-04-19 04:01:25 +00:00
|
|
|
}
|
|
|
|
|
2011-10-18 15:25:43 +00:00
|
|
|
if (do_print && !do_read) {
|
2010-02-11 22:51:44 +00:00
|
|
|
if ((args.pa_required & FLAG_HAS_OUTPUT_LOGFILE) == 0) {
|
|
|
|
pmcstat_print_pmcs();
|
2011-11-15 06:44:07 +00:00
|
|
|
if (runstate == PMCSTAT_FINISHED &&
|
|
|
|
/* final newline */
|
2010-02-11 22:51:44 +00:00
|
|
|
(args.pa_flags & FLAG_DO_PRINT) == 0)
|
|
|
|
(void) fprintf(args.pa_printfile, "\n");
|
|
|
|
}
|
|
|
|
if (args.pa_flags & FLAG_DO_TOP)
|
|
|
|
pmcstat_display_log();
|
2005-06-09 19:45:09 +00:00
|
|
|
do_print = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (runstate != PMCSTAT_FINISHED);
|
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
if ((args.pa_flags & FLAG_DO_TOP) && args.pa_toptty) {
|
|
|
|
pmcstat_topexit();
|
|
|
|
args.pa_toptty = 0;
|
|
|
|
}
|
|
|
|
|
2005-06-09 19:45:09 +00:00
|
|
|
/* flush any pending log entries */
|
2005-06-30 19:01:26 +00:00
|
|
|
if (args.pa_flags & (FLAG_HAS_OUTPUT_LOGFILE | FLAG_HAS_PIPE))
|
2011-10-18 15:25:43 +00:00
|
|
|
pmc_close_logfile();
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2010-02-11 22:51:44 +00:00
|
|
|
pmcstat_cleanup();
|
2005-04-19 04:01:25 +00:00
|
|
|
|
2005-10-21 18:59:59 +00:00
|
|
|
/* check if the driver lost any samples or events */
|
|
|
|
if (check_driver_stats) {
|
|
|
|
if (pmc_get_driver_stats(&ds_end) < 0)
|
2011-11-15 06:44:07 +00:00
|
|
|
err(EX_OSERR,
|
|
|
|
"ERROR: Cannot retrieve driver statistics");
|
2006-03-26 12:20:54 +00:00
|
|
|
if (ds_start.pm_intr_bufferfull != ds_end.pm_intr_bufferfull &&
|
|
|
|
args.pa_verbosity > 0)
|
2015-11-16 15:16:09 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING: sampling was paused at least %u time%s.\n"
|
|
|
|
"Please consider tuning the \"kern.hwpmc.nsamples\" tunable.",
|
|
|
|
ds_end.pm_intr_bufferfull -
|
|
|
|
ds_start.pm_intr_bufferfull,
|
|
|
|
((ds_end.pm_intr_bufferfull -
|
|
|
|
ds_start.pm_intr_bufferfull) != 1) ? "s" : ""
|
2011-11-15 06:44:07 +00:00
|
|
|
);
|
2005-10-21 18:59:59 +00:00
|
|
|
if (ds_start.pm_buffer_requests_failed !=
|
2006-03-26 12:20:54 +00:00
|
|
|
ds_end.pm_buffer_requests_failed &&
|
|
|
|
args.pa_verbosity > 0)
|
2015-11-16 15:16:09 +00:00
|
|
|
warnx(
|
|
|
|
"WARNING: at least %u event%s were discarded while running.\n"
|
|
|
|
"Please consider tuning the \"kern.hwpmc.nbuffers\" tunable.",
|
|
|
|
ds_end.pm_buffer_requests_failed -
|
|
|
|
ds_start.pm_buffer_requests_failed,
|
|
|
|
((ds_end.pm_buffer_requests_failed -
|
|
|
|
ds_start.pm_buffer_requests_failed) != 1) ? "s" : ""
|
2011-11-15 06:44:07 +00:00
|
|
|
);
|
2005-10-21 18:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(EX_OK);
|
2005-04-19 04:01:25 +00:00
|
|
|
}
|