|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* task_attach_timeout.c - attach to another task for monitoring for a short while
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Copyright (c) 2009 Google, Inc
|
|
Packit |
577717 |
* Contributed by Stephane Eranian <eranian@gmail.com>
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Based on:
|
|
Packit |
577717 |
* Copyright (c) 2002-2006 Hewlett-Packard Development Company, L.P.
|
|
Packit |
577717 |
* Contributed by Stephane Eranian <eranian@hpl.hp.com>
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
Packit |
577717 |
* of this software and associated documentation files (the "Software"), to deal
|
|
Packit |
577717 |
* in the Software without restriction, including without limitation the rights
|
|
Packit |
577717 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
Packit |
577717 |
* of the Software, and to permit persons to whom the Software is furnished to do so,
|
|
Packit |
577717 |
* subject to the following conditions:
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* The above copyright notice and this permission notice shall be included in all
|
|
Packit |
577717 |
* copies or substantial portions of the Software.
|
|
Packit |
577717 |
*
|
|
Packit |
577717 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
Packit |
577717 |
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
Packit |
577717 |
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
Packit |
577717 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
|
Packit |
577717 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
|
Packit |
577717 |
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
#include <sys/types.h>
|
|
Packit |
577717 |
#include <inttypes.h>
|
|
Packit |
577717 |
#include <stdio.h>
|
|
Packit |
577717 |
#include <stdlib.h>
|
|
Packit |
577717 |
#include <stdarg.h>
|
|
Packit |
577717 |
#include <errno.h>
|
|
Packit |
577717 |
#include <unistd.h>
|
|
Packit |
577717 |
#include <string.h>
|
|
Packit |
577717 |
#include <stdarg.h>
|
|
Packit |
577717 |
#include <sys/wait.h>
|
|
Packit |
577717 |
#include <err.h>
|
|
Packit |
577717 |
#include <sys/poll.h>
|
|
Packit |
577717 |
|
|
Packit |
577717 |
#include "perf_util.h"
|
|
Packit |
577717 |
|
|
Packit |
577717 |
typedef struct {
|
|
Packit |
577717 |
char *events;
|
|
Packit |
577717 |
int delay;
|
|
Packit |
577717 |
int print;
|
|
Packit |
577717 |
int group;
|
|
Packit |
577717 |
int pinned;
|
|
Packit |
577717 |
} options_t;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static options_t options;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
print_counts(perf_event_desc_t *fds, int num, int do_delta)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
ssize_t ret;
|
|
Packit |
577717 |
int i;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* now simply read the results.
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
for(i=0; i < num; i++) {
|
|
Packit |
577717 |
uint64_t val;
|
|
Packit |
577717 |
double ratio;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = read(fds[i].fd, fds[i].values, sizeof(fds[i].values));
|
|
Packit |
577717 |
if (ret < (ssize_t)sizeof(fds[i].values)) {
|
|
Packit |
577717 |
if (ret == -1)
|
|
Packit |
577717 |
err(1, "cannot read values event %s", fds[i].name);
|
|
Packit |
577717 |
else
|
|
Packit |
577717 |
warnx("could not read event%d", i);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
val = perf_scale(fds[i].values);
|
|
Packit |
577717 |
ratio = perf_scale_ratio(fds[i].values);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
val = do_delta ? perf_scale_delta(fds[i].values, fds[i].prev_values) : val;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[i].prev_values[0] = fds[i].values[0];
|
|
Packit |
577717 |
fds[i].prev_values[1] = fds[i].values[1];
|
|
Packit |
577717 |
fds[i].prev_values[2] = fds[i].values[2];
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (ratio == 1.0)
|
|
Packit |
577717 |
printf("%20"PRIu64" %s\n", val, fds[i].name);
|
|
Packit |
577717 |
else
|
|
Packit |
577717 |
if (ratio == 0.0)
|
|
Packit |
577717 |
printf("%20"PRIu64" %s (did not run: incompatible events, too many events in a group, competing session)\n", val, fds[i].name);
|
|
Packit |
577717 |
else
|
|
Packit |
577717 |
printf("%20"PRIu64" %s (scaled from %.2f%% of time)\n", val, fds[i].name, ratio*100.0);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
measure(pid_t pid)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
perf_event_desc_t *fds = NULL;
|
|
Packit |
577717 |
int i, ret, num_fds = 0;
|
|
Packit |
577717 |
char fn[32];
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (pfm_initialize() != PFM_SUCCESS)
|
|
Packit |
577717 |
errx(1, "libpfm initialization failed\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
ret = perf_setup_list_events(options.events, &fds, &num_fds);
|
|
Packit |
577717 |
if (ret || (num_fds == 0))
|
|
Packit |
577717 |
exit(1);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
fds[0].fd = -1;
|
|
Packit |
577717 |
for(i=0; i < num_fds; i++) {
|
|
Packit |
577717 |
fds[i].hw.disabled = 0; /* start immediately */
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* request timing information necessary for scaling counts */
|
|
Packit |
577717 |
fds[i].hw.read_format = PERF_FORMAT_SCALE;
|
|
Packit |
577717 |
fds[i].hw.pinned = !i && options.pinned;
|
|
Packit |
577717 |
fds[i].fd = perf_event_open(&fds[i].hw, pid, -1, (options.group? fds[0].fd : -1), 0);
|
|
Packit |
577717 |
if (fds[i].fd == -1)
|
|
Packit |
577717 |
errx(1, "cannot attach event %s", fds[i].name);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
/*
|
|
Packit |
577717 |
* no notification is generated by perf_counters
|
|
Packit |
577717 |
* when the monitored thread exits. Thus we need
|
|
Packit |
577717 |
* to poll /proc/ to detect it has disappeared,
|
|
Packit |
577717 |
* otherwise we have to wait until the end of the
|
|
Packit |
577717 |
* timeout
|
|
Packit |
577717 |
*/
|
|
Packit |
577717 |
sprintf(fn, "/proc/%d/status", pid);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
while(access(fn, F_OK) == 0 && options.delay) {
|
|
Packit |
577717 |
sleep(1);
|
|
Packit |
577717 |
options.delay--;
|
|
Packit |
577717 |
if (options.print)
|
|
Packit |
577717 |
print_counts(fds, num_fds, 1);
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
if (options.delay)
|
|
Packit |
577717 |
warn("thread %d terminated before timeout", pid);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (!options.print)
|
|
Packit |
577717 |
print_counts(fds, num_fds, 0);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
for(i=0; i < num_fds; i++)
|
|
Packit |
577717 |
close(fds[i].fd);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
perf_free_fds(fds, num_fds);
|
|
Packit |
577717 |
|
|
Packit |
577717 |
/* free libpfm resources cleanly */
|
|
Packit |
577717 |
pfm_terminate();
|
|
Packit |
577717 |
|
|
Packit |
577717 |
return 0;
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
static void
|
|
Packit |
577717 |
usage(void)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
printf("usage: task_attach_timeout [-h] [-p] [-P] [-g] [-d delay] [-e event1,event2,...] pid\n");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
|
|
Packit |
577717 |
int
|
|
Packit |
577717 |
main(int argc, char **argv)
|
|
Packit |
577717 |
{
|
|
Packit |
577717 |
int c;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
while ((c=getopt(argc, argv,"he:vd:pgP")) != -1) {
|
|
Packit |
577717 |
switch(c) {
|
|
Packit |
577717 |
case 'e':
|
|
Packit |
577717 |
options.events = optarg;
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case 'p':
|
|
Packit |
577717 |
options.print = 1;
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case 'P':
|
|
Packit |
577717 |
options.pinned = 1;
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case 'g':
|
|
Packit |
577717 |
options.group = 1;
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case 'd':
|
|
Packit |
577717 |
options.delay = atoi(optarg);
|
|
Packit |
577717 |
break;
|
|
Packit |
577717 |
case 'h':
|
|
Packit |
577717 |
usage();
|
|
Packit |
577717 |
exit(0);
|
|
Packit |
577717 |
default:
|
|
Packit |
577717 |
errx(1, "unknown error");
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
}
|
|
Packit |
577717 |
if (!options.events)
|
|
Packit |
577717 |
options.events = strdup("cycles,instructions");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (options.delay < 1)
|
|
Packit |
577717 |
options.delay = 10;
|
|
Packit |
577717 |
|
|
Packit |
577717 |
if (!argv[optind])
|
|
Packit |
577717 |
errx(1, "you must specify pid to attach to\n");
|
|
Packit |
577717 |
|
|
Packit |
577717 |
return measure(atoi(argv[optind]));
|
|
Packit |
577717 |
}
|