perf_counter: tools: update the tools to support process and inherited counters
[linux-2.6/btrfs-unstable.git] / Documentation / perf_counter / perf-report.cc
blob8855107fe6b3c38a4d895d45dd5e6212c46c4eb6
1 #define _GNU_SOURCE
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <sys/time.h>
5 #include <unistd.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <limits.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <errno.h>
13 #include <ctype.h>
14 #include <time.h>
15 #include <getopt.h>
16 #include <assert.h>
18 #include <sys/ioctl.h>
19 #include <sys/poll.h>
20 #include <sys/prctl.h>
21 #include <sys/wait.h>
22 #include <sys/mman.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
26 #include <linux/unistd.h>
27 #include <linux/types.h>
29 #include "../../include/linux/perf_counter.h"
31 #include <set>
32 #include <map>
33 #include <string>
36 #define SHOW_KERNEL 1
37 #define SHOW_USER 2
38 #define SHOW_HV 4
40 static char const *input_name = "output.perf";
41 static int input;
42 static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
44 static unsigned long page_size;
45 static unsigned long mmap_window = 32;
47 struct ip_event {
48 struct perf_event_header header;
49 __u64 ip;
50 __u32 pid, tid;
52 struct mmap_event {
53 struct perf_event_header header;
54 __u32 pid, tid;
55 __u64 start;
56 __u64 len;
57 __u64 pgoff;
58 char filename[PATH_MAX];
60 struct comm_event {
61 struct perf_event_header header;
62 __u32 pid,tid;
63 char comm[16];
66 typedef union event_union {
67 struct perf_event_header header;
68 struct ip_event ip;
69 struct mmap_event mmap;
70 struct comm_event comm;
71 } event_t;
73 struct section {
74 uint64_t start;
75 uint64_t end;
77 uint64_t offset;
79 std::string name;
81 section() { };
83 section(uint64_t stab) : end(stab) { };
85 section(uint64_t start, uint64_t size, uint64_t offset, std::string name) :
86 start(start), end(start + size), offset(offset), name(name)
87 { };
89 bool operator < (const struct section &s) const {
90 return end < s.end;
94 typedef std::set<struct section> sections_t;
96 struct symbol {
97 uint64_t start;
98 uint64_t end;
100 std::string name;
102 symbol() { };
104 symbol(uint64_t ip) : start(ip) { }
106 symbol(uint64_t start, uint64_t len, std::string name) :
107 start(start), end(start + len), name(name)
108 { };
110 bool operator < (const struct symbol &s) const {
111 return start < s.start;
115 typedef std::set<struct symbol> symbols_t;
117 struct dso {
118 sections_t sections;
119 symbols_t syms;
122 static std::map<std::string, struct dso> dsos;
124 static void load_dso_sections(std::string dso_name)
126 struct dso &dso = dsos[dso_name];
128 std::string cmd = "readelf -DSW " + dso_name;
130 FILE *file = popen(cmd.c_str(), "r");
131 if (!file) {
132 perror("failed to open pipe");
133 exit(-1);
136 char *line = NULL;
137 size_t n = 0;
139 while (!feof(file)) {
140 uint64_t addr, off, size;
141 char name[32];
143 if (getline(&line, &n, file) < 0)
144 break;
145 if (!line)
146 break;
148 if (sscanf(line, " [%*2d] %16s %*14s %Lx %Lx %Lx",
149 name, &addr, &off, &size) == 4) {
151 dso.sections.insert(section(addr, size, addr - off, name));
153 #if 0
155 * for reading readelf symbols (-s), however these don't seem
156 * to include nearly everything, so use nm for that.
158 if (sscanf(line, " %*4d %*3d: %Lx %5Lu %*7s %*6s %*7s %3d %s",
159 &start, &size, &section, sym) == 4) {
161 start -= dso.section_offsets[section];
163 dso.syms.insert(symbol(start, size, std::string(sym)));
165 #endif
167 pclose(file);
170 static void load_dso_symbols(std::string dso_name, std::string args)
172 struct dso &dso = dsos[dso_name];
174 std::string cmd = "nm -nSC " + args + " " + dso_name;
176 FILE *file = popen(cmd.c_str(), "r");
177 if (!file) {
178 perror("failed to open pipe");
179 exit(-1);
182 char *line = NULL;
183 size_t n = 0;
185 while (!feof(file)) {
186 uint64_t start, size;
187 char c;
188 char sym[1024];
190 if (getline(&line, &n, file) < 0)
191 break;
192 if (!line)
193 break;
196 if (sscanf(line, "%Lx %Lx %c %s", &start, &size, &c, sym) == 4) {
197 sections_t::const_iterator si =
198 dso.sections.upper_bound(section(start));
199 if (si == dso.sections.end()) {
200 printf("symbol in unknown section: %s\n", sym);
201 continue;
204 start -= si->offset;
206 dso.syms.insert(symbol(start, size, sym));
209 pclose(file);
212 static void load_dso(std::string dso_name)
214 load_dso_sections(dso_name);
215 load_dso_symbols(dso_name, "-D"); /* dynamic symbols */
216 load_dso_symbols(dso_name, ""); /* regular ones */
219 void load_kallsyms(void)
221 struct dso &dso = dsos["[kernel]"];
223 FILE *file = fopen("/proc/kallsyms", "r");
224 if (!file) {
225 perror("failed to open kallsyms");
226 exit(-1);
229 char *line;
230 size_t n;
232 while (!feof(file)) {
233 uint64_t start;
234 char c;
235 char sym[1024000];
237 if (getline(&line, &n, file) < 0)
238 break;
239 if (!line)
240 break;
242 if (sscanf(line, "%Lx %c %s", &start, &c, sym) == 3)
243 dso.syms.insert(symbol(start, 0x1000000, std::string(sym)));
245 fclose(file);
248 struct map {
249 uint64_t start;
250 uint64_t end;
251 uint64_t pgoff;
253 std::string dso;
255 map() { };
257 map(uint64_t ip) : end(ip) { }
259 map(mmap_event *mmap) {
260 start = mmap->start;
261 end = mmap->start + mmap->len;
262 pgoff = mmap->pgoff;
264 dso = std::string(mmap->filename);
266 if (dsos.find(dso) == dsos.end())
267 load_dso(dso);
270 bool operator < (const struct map &m) const {
271 return end < m.end;
275 typedef std::set<struct map> maps_t;
277 static std::map<int, maps_t> maps;
279 static std::map<int, std::string> comms;
281 static std::map<std::string, int> hist;
282 static std::multimap<int, std::string> rev_hist;
284 static std::string resolve_comm(int pid)
286 std::string comm;
288 std::map<int, std::string>::const_iterator ci = comms.find(pid);
289 if (ci != comms.end()) {
290 comm = ci->second;
291 } else {
292 char pid_str[30];
294 sprintf(pid_str, ":%d", pid);
295 comm = pid_str;
298 return comm;
301 static std::string resolve_user_symbol(int pid, uint64_t ip)
303 std::string sym = "<unknown>";
305 maps_t &m = maps[pid];
306 maps_t::const_iterator mi = m.upper_bound(map(ip));
307 if (mi == m.end())
308 return sym;
310 ip -= mi->start + mi->pgoff;
312 symbols_t &s = dsos[mi->dso].syms;
313 symbols_t::const_iterator si = s.upper_bound(symbol(ip));
315 sym = mi->dso + ": <unknown>";
317 if (si == s.begin())
318 return sym;
319 si--;
321 if (si->start <= ip && ip < si->end)
322 sym = mi->dso + ": " + si->name;
323 #if 0
324 else if (si->start <= ip)
325 sym = mi->dso + ": ?" + si->name;
326 #endif
328 return sym;
331 static std::string resolve_kernel_symbol(uint64_t ip)
333 std::string sym = "<unknown>";
335 symbols_t &s = dsos["[kernel]"].syms;
336 symbols_t::const_iterator si = s.upper_bound(symbol(ip));
338 if (si == s.begin())
339 return sym;
340 si--;
342 if (si->start <= ip && ip < si->end)
343 sym = si->name;
345 return sym;
348 static void display_help(void)
350 printf(
351 "Usage: perf-report [<options>]\n"
352 " -i file --input=<file> # input file\n"
355 exit(0);
358 static void process_options(int argc, char *argv[])
360 int error = 0;
362 for (;;) {
363 int option_index = 0;
364 /** Options for getopt */
365 static struct option long_options[] = {
366 {"input", required_argument, NULL, 'i'},
367 {"no-user", no_argument, NULL, 'u'},
368 {"no-kernel", no_argument, NULL, 'k'},
369 {"no-hv", no_argument, NULL, 'h'},
370 {NULL, 0, NULL, 0 }
372 int c = getopt_long(argc, argv, "+:i:kuh",
373 long_options, &option_index);
374 if (c == -1)
375 break;
377 switch (c) {
378 case 'i': input_name = strdup(optarg); break;
379 case 'k': show_mask &= ~SHOW_KERNEL; break;
380 case 'u': show_mask &= ~SHOW_USER; break;
381 case 'h': show_mask &= ~SHOW_HV; break;
382 default: error = 1; break;
386 if (error)
387 display_help();
390 int main(int argc, char *argv[])
392 unsigned long offset = 0;
393 unsigned long head = 0;
394 struct stat stat;
395 char *buf;
396 event_t *event;
397 int ret;
398 unsigned long total = 0;
400 page_size = getpagesize();
402 process_options(argc, argv);
404 input = open(input_name, O_RDONLY);
405 if (input < 0) {
406 perror("failed to open file");
407 exit(-1);
410 ret = fstat(input, &stat);
411 if (ret < 0) {
412 perror("failed to stat file");
413 exit(-1);
416 if (!stat.st_size) {
417 fprintf(stderr, "zero-sized file, nothing to do!\n");
418 exit(0);
421 load_kallsyms();
423 remap:
424 buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
425 MAP_SHARED, input, offset);
426 if (buf == MAP_FAILED) {
427 perror("failed to mmap file");
428 exit(-1);
431 more:
432 event = (event_t *)(buf + head);
434 if (head + event->header.size >= page_size * mmap_window) {
435 unsigned long shift = page_size * (head / page_size);
436 int ret;
438 ret = munmap(buf, page_size * mmap_window);
439 assert(ret == 0);
441 offset += shift;
442 head -= shift;
443 goto remap;
447 if (!event->header.size) {
448 fprintf(stderr, "zero-sized event at file offset %ld\n", offset + head);
449 fprintf(stderr, "skipping %ld bytes of events.\n", stat.st_size - offset - head);
450 goto done;
453 head += event->header.size;
455 if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
456 std::string comm, sym, level;
457 int show = 0;
458 char output[1024];
460 if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
461 show |= SHOW_KERNEL;
462 level = " [k] ";
463 sym = resolve_kernel_symbol(event->ip.ip);
464 } else if (event->header.misc & PERF_EVENT_MISC_USER) {
465 show |= SHOW_USER;
466 level = " [.] ";
467 sym = resolve_user_symbol(event->ip.pid, event->ip.ip);
468 } else {
469 show |= SHOW_HV;
470 level = " [H] ";
473 if (show & show_mask) {
474 comm = resolve_comm(event->ip.pid);
475 snprintf(output, sizeof(output), "%16s %s %s",
476 comm.c_str(), level.c_str(), sym.c_str());
477 hist[output]++;
480 total++;
482 } else switch (event->header.type) {
483 case PERF_EVENT_MMAP:
484 maps[event->mmap.pid].insert(map(&event->mmap));
485 break;
487 case PERF_EVENT_COMM:
488 comms[event->comm.pid] = std::string(event->comm.comm);
489 break;
492 if (offset + head < stat.st_size)
493 goto more;
495 done:
497 close(input);
499 std::map<std::string, int>::iterator hi = hist.begin();
501 while (hi != hist.end()) {
502 rev_hist.insert(std::pair<int, std::string>(hi->second, hi->first));
503 hist.erase(hi++);
506 std::multimap<int, std::string>::const_iterator ri = rev_hist.begin();
508 while (ri != rev_hist.end()) {
509 printf(" %5.2f %s\n", (100.0 * ri->first)/total, ri->second.c_str());
510 ri++;
513 return 0;