/*-
* Copyright (c) 2006, 2007, 2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Andrew Doran.
*
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*
* Find the name list for resolving symbol names, and load it into
* memory.
*/
if (nlistf == NULL) {
nlfd = open(_PATH_KSYMS, O_RDONLY);
nlistf = getbootfile();
} else
nlfd = -1;
if (nlfd == -1) {
if ((nlfd = open(nlistf, O_RDONLY)) < 0)
err(EXIT_FAILURE, "cannot open " _PATH_KSYMS " or %s",
nlistf);
}
if (loadsym32(nlfd) != 0) {
if (loadsym64(nlfd) != 0)
errx(EXIT_FAILURE, "unable to load symbol table");
bin64 = 1;
}
close(nlfd);
memset(&le, 0, sizeof(le));
le.le_nbufs = nbufs;
/*
* Set up initial filtering.
*/
if (lockname != NULL) {
findsym(LOCK_BYNAME, lockname, &le.le_lockstart,
&le.le_lockend, true);
le.le_flags |= LE_ONE_LOCK;
}
if (!lflag)
le.le_flags |= LE_CALLSITE;
if (!fflag)
le.le_flags |= LE_LOCK;
if (funcname != NULL) {
if (lflag)
usage();
findsym(FUNC_BYNAME, funcname, &le.le_csstart, &le.le_csend, true);
le.le_flags |= LE_ONE_CALLSITE;
}
le.le_mask = (eventtype & LB_EVENT_MASK) | (locktype & LB_LOCK_MASK);
/*
* Start tracing.
*/
if ((lsfd = open(_PATH_DEV_LOCKSTAT, O_RDONLY)) < 0)
err(EXIT_FAILURE, "cannot open " _PATH_DEV_LOCKSTAT);
if (ioctl(lsfd, IOC_LOCKSTAT_GVERSION, &ch) < 0)
err(EXIT_FAILURE, "ioctl");
if (ch != LS_VERSION)
errx(EXIT_FAILURE,
"incompatible lockstat interface version (%d, kernel %d)",
LS_VERSION, ch);
if (dflag) {
goto disable;
}
if (ioctl(lsfd, IOC_LOCKSTAT_ENABLE, &le))
err(EXIT_FAILURE, "cannot enable tracing");
/*
* Execute the traced program.
*/
spawn(argc, argv);
disable:
/*
* Stop tracing, and read the trace buffers from the kernel.
*/
if (ioctl(lsfd, IOC_LOCKSTAT_DISABLE, &ld) == -1) {
if (errno == EOVERFLOW) {
warnx("overflowed available kernel trace buffers");
exit(EXIT_FAILURE);
}
err(EXIT_FAILURE, "cannot disable tracing");
}
if ((bufs = malloc(ld.ld_size)) == NULL)
err(EXIT_FAILURE, "cannot allocate memory for user buffers");
if ((size_t)read(lsfd, bufs, ld.ld_size) != ld.ld_size)
err(EXIT_FAILURE, "reading from " _PATH_DEV_LOCKSTAT);
if (close(lsfd))
err(EXIT_FAILURE, "close(" _PATH_DEV_LOCKSTAT ")");
/*
* Figure out how to scale the results. For internal use we convert
* all times from CPU frequency based to picoseconds, and values are
* eventually displayed in ms.
*/
for (i = 0; i < sizeof(ld.ld_freq) / sizeof(ld.ld_freq[0]); i++)
if (ld.ld_freq[i] != 0)
cpuscale[i] = PICO / ld.ld_freq[i];
ms = ld.ld_time.tv_sec * MILLI + ld.ld_time.tv_nsec / MICRO;
if (pflag)
cscale = 1.0 / ncpu();
else
cscale = 1.0;
cscale *= (sflag ? MILLI / ms : 1.0);
tscale = cscale / NANO;
nbufs = (int)(ld.ld_size / sizeof(lsbuf_t));
if (displayed == 0)
fprintf(outfp, "None of the selected events were recorded.\n");
exit(EXIT_SUCCESS);
}
static void
usage(void)
{
fprintf(stderr,
"%s: usage:\n"
"%s [options] <command>\n\n"
"-b nbuf\t\tset number of event buffers to allocate\n"
"-c\t\treport percentage of total events by count, not time\n"
"-d\t\tdisable lockstat\n"
"-E event\tdisplay only one type of event\n"
"-e\t\tlist event types\n"
"-F func\t\tlimit trace to one function\n"
"-f\t\ttrace only by function\n"
"-L lock\t\tlimit trace to one lock (name, or address)\n"
"-l\t\ttrace only by lock\n"
"-M\t\tmerge lock addresses within unique objects\n"
"-m\t\tmerge call sites within unique functions\n"
"-N nlist\tspecify name list file\n"
"-o file\t\tsend output to named file, not stdout\n"
"-p\t\tshow average count/time per CPU, not total\n"
"-s\t\tshow average count/time per second, not total\n"
"-T type\t\tdisplay only one type of lock\n"
"-t\t\tlist lock types\n"
"-x\t\tdon't differentiate event types\n",
getprogname(), getprogname());
exit(EXIT_FAILURE);
}
static void
nullsig(int junk)
{
(void)junk;
}
static void
listnames(const name_t *name)
{
for (; name->name != NULL; name++)
printf("%s\n", name->name);
exit(EXIT_SUCCESS);
}
static int
matchname(const name_t *name, char *string)
{
int empty, mask;
char *sp;
empty = 1;
mask = 0;
while ((sp = strsep(&string, ",")) != NULL) {
if (*sp == '\0')
usage();
/*
* Call into the ELF parser and look up a symbol by name or by address.
*/
static void
findsym(findsym_t find, char *name, uintptr_t *start, uintptr_t *end, bool chg)
{
uintptr_t tend, sa, ea;
char *p;
int rv;
if (!chg) {
sa = *start;
start = &sa;
end = &ea;
}
if (end == NULL)
end = &tend;
if (find == LOCK_BYNAME) {
if (isdigit((u_int)name[0])) {
*start = (uintptr_t)strtoul(name, &p, 0);
if (*p == '\0')
return;
}
}
if (find == FUNC_BYNAME || find == LOCK_BYNAME) {
if (rv == -1)
errx(EXIT_FAILURE, "unable to find symbol `%s'", name);
return;
}
if (rv == -1)
snprintf(name, NAME_SIZE, "%016lx", (long)*start);
}
/*
* Fork off the child process and wait for it to complete. We trap SIGINT
* so that the caller can use Ctrl-C to stop tracing early and still get
* useful results.
*/
static void
spawn(int argc, char **argv)
{
pid_t pid;
switch (pid = fork()) {
case 0:
close(lsfd);
if (execvp(argv[0], argv) == -1)
err(EXIT_FAILURE, "cannot exec");
break;
case -1:
err(EXIT_FAILURE, "cannot fork to exec");
break;
default:
signal(SIGINT, nullsig);
wait(NULL);
signal(SIGINT, SIG_DFL);
break;
}
}
/*
* Allocate a new block of lock_t structures.
*/
static lock_t *
morelocks(void)
{
const int batch = 32;
lock_t *l, *lp, *max;
l = (lock_t *)malloc(sizeof(*l) * batch);
for (lp = l, max = l + batch; lp < max; lp++)
TAILQ_INSERT_TAIL(&freelist, lp, chain);
/*
* From the kernel supplied data, construct two dimensional lists of locks
* and event buffers, indexed by lock type and sorted by event type.
*/
static void
makelists(int mask, int event)
{
lsbuf_t *lb, *lb2, *max;
lock_t *l, *l2;
bucket_t *bp;
int type;
size_t i;
/*
* Recycle lock_t structures from the last run.
*/
TAILQ_CONCAT(&freelist, &locklist, chain);
for (i = 0; i < __arraycount(bucket); i++) {
SLIST_INIT(&bucket[i]);
}
type = mask & LB_LOCK_MASK;
for (lb = bufs, max = bufs + nbufs; lb < max; lb++) {
if (!xflag && (lb->lb_flags & LB_LOCK_MASK) != type)
continue;
if (lb->lb_counts[event] == 0)
continue;
/*
* Look for a record describing this lock, and allocate a
* new one if needed.
*/
bp = HASH(lb->lb_lock);
SLIST_FOREACH(l, bp, bucket) {
if (l->lock == lb->lb_lock)
break;
}
if (l == NULL) {
if ((l = TAILQ_FIRST(&freelist)) == NULL)
l = morelocks();
TAILQ_REMOVE(&freelist, l, chain);
l->flags = lb->lb_flags;
l->lock = lb->lb_lock;
l->nbufs = 0;
l->name[0] = '\0';
l->count = 0;
l->time = 0;
TAILQ_INIT(&l->tosort);
TAILQ_INIT(&l->bufs);
TAILQ_INSERT_TAIL(&sortlist, l, chain);
SLIST_INSERT_HEAD(bp, l, bucket);
}
/*
* Scale the time values per buffer and summarise
* times+counts per lock.
*/
lb->lb_times[event] *= cpuscale[lb->lb_cpu];
l->count += lb->lb_counts[event];
l->time += lb->lb_times[event];
/*
* Now sort the lists.
*/
while ((l = TAILQ_FIRST(&sortlist)) != NULL) {
TAILQ_REMOVE(&sortlist, l, chain);
/*
* Sort the buffers into the per-lock list.
*/
while ((lb = TAILQ_FIRST(&l->tosort)) != NULL) {
TAILQ_REMOVE(&l->tosort, lb, lb_chain.tailq);
lb2 = TAILQ_FIRST(&l->bufs);
while (lb2 != NULL) {
if (cflag) {
if (lb->lb_counts[event] >
lb2->lb_counts[event])
break;
} else if (lb->lb_times[event] >
lb2->lb_times[event])
break;
lb2 = TAILQ_NEXT(lb2, lb_chain.tailq);
}
if (lb2 == NULL)
TAILQ_INSERT_TAIL(&l->bufs, lb,
lb_chain.tailq);
else
TAILQ_INSERT_BEFORE(lb2, lb, lb_chain.tailq);
}
/*
* Sort this lock into the per-type list, based on the
* totals per lock.
*/
l2 = TAILQ_FIRST(&locklist);
while (l2 != NULL) {
if (cflag) {
if (l->count > l2->count)
break;
} else if (l->time > l2->time)
break;
l2 = TAILQ_NEXT(l2, chain);
}
if (l2 == NULL)
TAILQ_INSERT_TAIL(&locklist, l, chain);
else
TAILQ_INSERT_BEFORE(l2, l, chain);
}
}
/*
* Display a summary table for one lock type / event type pair.
*/
static void
display(int mask, const char *name)
{
lock_t *l;
lsbuf_t *lb;
double pcscale, metric;
char fname[NAME_SIZE];
int event;
/*
* Sum up all events for this type of lock + event.
*/
pcscale = 0;
TAILQ_FOREACH(l, &locklist, chain) {
if (cflag)
pcscale += l->count;
else
pcscale += l->time;
displayed++;
}
if (pcscale == 0)
pcscale = 100;
else
pcscale = (100.0 / pcscale);
/*
* For each lock, print a summary total, followed by a breakdown by
* caller.
*/
TAILQ_FOREACH(l, &locklist, chain) {
if (cflag)
metric = l->count;
else
metric = l->time;
metric *= pcscale;
if (l->name[0] == '\0')
findsym(LOCK_BYADDR, l->name, &l->lock, NULL, false);