1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
* task_attach_timeout.c - attach to another task for monitoring for a short while
*
* Copyright (c) 2009 Google, Inc
* Contributed by Stephane Eranian <eranian@gmail.com>
*
* Based on:
* Copyright (c) 2002-2006 Hewlett-Packard Development Company, L.P.
* Contributed by Stephane Eranian <eranian@hpl.hp.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <sys/wait.h>
#include <err.h>
#include <sys/poll.h>
#include "perf_util.h"
typedef struct {
char *events;
int delay;
int print;
int group;
int pinned;
} options_t;
static options_t options;
static void
print_counts(perf_event_desc_t *fds, int num, int do_delta)
{
ssize_t ret;
int i;
/*
* now simply read the results.
*/
for(i=0; i < num; i++) {
uint64_t val;
double ratio;
ret = read(fds[i].fd, fds[i].values, sizeof(fds[i].values));
if (ret < (ssize_t)sizeof(fds[i].values)) {
if (ret == -1)
err(1, "cannot read values event %s", fds[i].name);
else
warnx("could not read event%d", i);
}
val = perf_scale(fds[i].values);
ratio = perf_scale_ratio(fds[i].values);
val = do_delta ? perf_scale_delta(fds[i].values, fds[i].prev_values) : val;
fds[i].prev_values[0] = fds[i].values[0];
fds[i].prev_values[1] = fds[i].values[1];
fds[i].prev_values[2] = fds[i].values[2];
if (ratio == 1.0)
printf("%20"PRIu64" %s\n", val, fds[i].name);
else
if (ratio == 0.0)
printf("%20"PRIu64" %s (did not run: incompatible events, too many events in a group, competing session)\n", val, fds[i].name);
else
printf("%20"PRIu64" %s (scaled from %.2f%% of time)\n", val, fds[i].name, ratio*100.0);
}
}
int
measure(pid_t pid)
{
perf_event_desc_t *fds = NULL;
int i, ret, num_fds = 0;
char fn[32];
if (pfm_initialize() != PFM_SUCCESS)
errx(1, "libpfm initialization failed\n");
ret = perf_setup_list_events(options.events, &fds, &num_fds);
if (ret || (num_fds == 0))
exit(1);
fds[0].fd = -1;
for(i=0; i < num_fds; i++) {
fds[i].hw.disabled = 0; /* start immediately */
/* request timing information necessary for scaling counts */
fds[i].hw.read_format = PERF_FORMAT_SCALE;
fds[i].hw.pinned = !i && options.pinned;
fds[i].fd = perf_event_open(&fds[i].hw, pid, -1, (options.group? fds[0].fd : -1), 0);
if (fds[i].fd == -1)
errx(1, "cannot attach event %s", fds[i].name);
}
/*
* no notification is generated by perf_counters
* when the monitored thread exits. Thus we need
* to poll /proc/ to detect it has disappeared,
* otherwise we have to wait until the end of the
* timeout
*/
sprintf(fn, "/proc/%d/status", pid);
while(access(fn, F_OK) == 0 && options.delay) {
sleep(1);
options.delay--;
if (options.print)
print_counts(fds, num_fds, 1);
}
if (options.delay)
warn("thread %d terminated before timeout", pid);
if (!options.print)
print_counts(fds, num_fds, 0);
for(i=0; i < num_fds; i++)
close(fds[i].fd);
perf_free_fds(fds, num_fds);
/* free libpfm resources cleanly */
pfm_terminate();
return 0;
}
static void
usage(void)
{
printf("usage: task_attach_timeout [-h] [-p] [-P] [-g] [-d delay] [-e event1,event2,...] pid\n");
}
int
main(int argc, char **argv)
{
int c;
while ((c=getopt(argc, argv,"he:vd:pgP")) != -1) {
switch(c) {
case 'e':
options.events = optarg;
break;
case 'p':
options.print = 1;
break;
case 'P':
options.pinned = 1;
break;
case 'g':
options.group = 1;
break;
case 'd':
options.delay = atoi(optarg);
break;
case 'h':
usage();
exit(0);
default:
errx(1, "unknown error");
}
}
if (!options.events)
options.events = strdup("cycles:u,instructions:u");
if (options.delay < 1)
options.delay = 10;
if (!argv[optind])
errx(1, "you must specify pid to attach to\n");
return measure(atoi(argv[optind]));
}
|