builtin-report.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. /*
  2. * builtin-report.c
  3. *
  4. * Builtin report command: Analyze the perf.data input file,
  5. * look up and read DSOs and symbol information and display
  6. * a histogram of results, along various sorting keys.
  7. */
  8. #include "builtin.h"
  9. #include "util/util.h"
  10. #include "util/color.h"
  11. #include "util/list.h"
  12. #include "util/cache.h"
  13. #include "util/rbtree.h"
  14. #include "util/symbol.h"
  15. #include "util/string.h"
  16. #include "perf.h"
  17. #include "util/parse-options.h"
  18. #include "util/parse-events.h"
  19. #define SHOW_KERNEL 1
  20. #define SHOW_USER 2
  21. #define SHOW_HV 4
  22. static char const *input_name = "perf.data";
  23. static char *vmlinux = NULL;
  24. static char default_sort_order[] = "comm,dso";
  25. static char *sort_order = default_sort_order;
  26. static int input;
  27. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  28. static int dump_trace = 0;
  29. #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
  30. #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
  31. static int verbose;
  32. #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
  33. static int full_paths;
  34. static unsigned long page_size;
  35. static unsigned long mmap_window = 32;
  36. static char default_parent_pattern[] = "^sys_|^do_page_fault";
  37. static char *parent_pattern = default_parent_pattern;
  38. static regex_t parent_regex;
  39. static int exclude_other = 1;
  40. struct ip_event {
  41. struct perf_event_header header;
  42. u64 ip;
  43. u32 pid, tid;
  44. unsigned char __more_data[];
  45. };
  46. struct ip_callchain {
  47. u64 nr;
  48. u64 ips[0];
  49. };
  50. struct mmap_event {
  51. struct perf_event_header header;
  52. u32 pid, tid;
  53. u64 start;
  54. u64 len;
  55. u64 pgoff;
  56. char filename[PATH_MAX];
  57. };
  58. struct comm_event {
  59. struct perf_event_header header;
  60. u32 pid, tid;
  61. char comm[16];
  62. };
  63. struct fork_event {
  64. struct perf_event_header header;
  65. u32 pid, ppid;
  66. };
  67. struct period_event {
  68. struct perf_event_header header;
  69. u64 time;
  70. u64 id;
  71. u64 sample_period;
  72. };
  73. struct lost_event {
  74. struct perf_event_header header;
  75. u64 id;
  76. u64 lost;
  77. };
  78. typedef union event_union {
  79. struct perf_event_header header;
  80. struct ip_event ip;
  81. struct mmap_event mmap;
  82. struct comm_event comm;
  83. struct fork_event fork;
  84. struct period_event period;
  85. struct lost_event lost;
  86. } event_t;
  87. static LIST_HEAD(dsos);
  88. static struct dso *kernel_dso;
  89. static struct dso *vdso;
  90. static void dsos__add(struct dso *dso)
  91. {
  92. list_add_tail(&dso->node, &dsos);
  93. }
  94. static struct dso *dsos__find(const char *name)
  95. {
  96. struct dso *pos;
  97. list_for_each_entry(pos, &dsos, node)
  98. if (strcmp(pos->name, name) == 0)
  99. return pos;
  100. return NULL;
  101. }
  102. static struct dso *dsos__findnew(const char *name)
  103. {
  104. struct dso *dso = dsos__find(name);
  105. int nr;
  106. if (dso)
  107. return dso;
  108. dso = dso__new(name, 0);
  109. if (!dso)
  110. goto out_delete_dso;
  111. nr = dso__load(dso, NULL, verbose);
  112. if (nr < 0) {
  113. eprintf("Failed to open: %s\n", name);
  114. goto out_delete_dso;
  115. }
  116. if (!nr)
  117. eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
  118. dsos__add(dso);
  119. return dso;
  120. out_delete_dso:
  121. dso__delete(dso);
  122. return NULL;
  123. }
  124. static void dsos__fprintf(FILE *fp)
  125. {
  126. struct dso *pos;
  127. list_for_each_entry(pos, &dsos, node)
  128. dso__fprintf(pos, fp);
  129. }
  130. static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
  131. {
  132. return dso__find_symbol(kernel_dso, ip);
  133. }
  134. static int load_kernel(void)
  135. {
  136. int err;
  137. kernel_dso = dso__new("[kernel]", 0);
  138. if (!kernel_dso)
  139. return -1;
  140. err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
  141. if (err) {
  142. dso__delete(kernel_dso);
  143. kernel_dso = NULL;
  144. } else
  145. dsos__add(kernel_dso);
  146. vdso = dso__new("[vdso]", 0);
  147. if (!vdso)
  148. return -1;
  149. vdso->find_symbol = vdso__find_symbol;
  150. dsos__add(vdso);
  151. return err;
  152. }
  153. static char __cwd[PATH_MAX];
  154. static char *cwd = __cwd;
  155. static int cwdlen;
  156. static int strcommon(const char *pathname)
  157. {
  158. int n = 0;
  159. while (pathname[n] == cwd[n] && n < cwdlen)
  160. ++n;
  161. return n;
  162. }
  163. struct map {
  164. struct list_head node;
  165. u64 start;
  166. u64 end;
  167. u64 pgoff;
  168. u64 (*map_ip)(struct map *, u64);
  169. struct dso *dso;
  170. };
  171. static u64 map__map_ip(struct map *map, u64 ip)
  172. {
  173. return ip - map->start + map->pgoff;
  174. }
  175. static u64 vdso__map_ip(struct map *map, u64 ip)
  176. {
  177. return ip;
  178. }
  179. static inline int is_anon_memory(const char *filename)
  180. {
  181. return strcmp(filename, "//anon") == 0;
  182. }
  183. static struct map *map__new(struct mmap_event *event)
  184. {
  185. struct map *self = malloc(sizeof(*self));
  186. if (self != NULL) {
  187. const char *filename = event->filename;
  188. char newfilename[PATH_MAX];
  189. int anon;
  190. if (cwd) {
  191. int n = strcommon(filename);
  192. if (n == cwdlen) {
  193. snprintf(newfilename, sizeof(newfilename),
  194. ".%s", filename + n);
  195. filename = newfilename;
  196. }
  197. }
  198. anon = is_anon_memory(filename);
  199. if (anon) {
  200. snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
  201. filename = newfilename;
  202. }
  203. self->start = event->start;
  204. self->end = event->start + event->len;
  205. self->pgoff = event->pgoff;
  206. self->dso = dsos__findnew(filename);
  207. if (self->dso == NULL)
  208. goto out_delete;
  209. if (self->dso == vdso || anon)
  210. self->map_ip = vdso__map_ip;
  211. else
  212. self->map_ip = map__map_ip;
  213. }
  214. return self;
  215. out_delete:
  216. free(self);
  217. return NULL;
  218. }
  219. static struct map *map__clone(struct map *self)
  220. {
  221. struct map *map = malloc(sizeof(*self));
  222. if (!map)
  223. return NULL;
  224. memcpy(map, self, sizeof(*self));
  225. return map;
  226. }
  227. static int map__overlap(struct map *l, struct map *r)
  228. {
  229. if (l->start > r->start) {
  230. struct map *t = l;
  231. l = r;
  232. r = t;
  233. }
  234. if (l->end > r->start)
  235. return 1;
  236. return 0;
  237. }
  238. static size_t map__fprintf(struct map *self, FILE *fp)
  239. {
  240. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  241. self->start, self->end, self->pgoff, self->dso->name);
  242. }
  243. struct thread {
  244. struct rb_node rb_node;
  245. struct list_head maps;
  246. pid_t pid;
  247. char *comm;
  248. };
  249. static struct thread *thread__new(pid_t pid)
  250. {
  251. struct thread *self = malloc(sizeof(*self));
  252. if (self != NULL) {
  253. self->pid = pid;
  254. self->comm = malloc(32);
  255. if (self->comm)
  256. snprintf(self->comm, 32, ":%d", self->pid);
  257. INIT_LIST_HEAD(&self->maps);
  258. }
  259. return self;
  260. }
  261. static int thread__set_comm(struct thread *self, const char *comm)
  262. {
  263. if (self->comm)
  264. free(self->comm);
  265. self->comm = strdup(comm);
  266. return self->comm ? 0 : -ENOMEM;
  267. }
  268. static size_t thread__fprintf(struct thread *self, FILE *fp)
  269. {
  270. struct map *pos;
  271. size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
  272. list_for_each_entry(pos, &self->maps, node)
  273. ret += map__fprintf(pos, fp);
  274. return ret;
  275. }
  276. static struct rb_root threads;
  277. static struct thread *last_match;
  278. static struct thread *threads__findnew(pid_t pid)
  279. {
  280. struct rb_node **p = &threads.rb_node;
  281. struct rb_node *parent = NULL;
  282. struct thread *th;
  283. /*
  284. * Font-end cache - PID lookups come in blocks,
  285. * so most of the time we dont have to look up
  286. * the full rbtree:
  287. */
  288. if (last_match && last_match->pid == pid)
  289. return last_match;
  290. while (*p != NULL) {
  291. parent = *p;
  292. th = rb_entry(parent, struct thread, rb_node);
  293. if (th->pid == pid) {
  294. last_match = th;
  295. return th;
  296. }
  297. if (pid < th->pid)
  298. p = &(*p)->rb_left;
  299. else
  300. p = &(*p)->rb_right;
  301. }
  302. th = thread__new(pid);
  303. if (th != NULL) {
  304. rb_link_node(&th->rb_node, parent, p);
  305. rb_insert_color(&th->rb_node, &threads);
  306. last_match = th;
  307. }
  308. return th;
  309. }
  310. static void thread__insert_map(struct thread *self, struct map *map)
  311. {
  312. struct map *pos, *tmp;
  313. list_for_each_entry_safe(pos, tmp, &self->maps, node) {
  314. if (map__overlap(pos, map)) {
  315. list_del_init(&pos->node);
  316. /* XXX leaks dsos */
  317. free(pos);
  318. }
  319. }
  320. list_add_tail(&map->node, &self->maps);
  321. }
  322. static int thread__fork(struct thread *self, struct thread *parent)
  323. {
  324. struct map *map;
  325. if (self->comm)
  326. free(self->comm);
  327. self->comm = strdup(parent->comm);
  328. if (!self->comm)
  329. return -ENOMEM;
  330. list_for_each_entry(map, &parent->maps, node) {
  331. struct map *new = map__clone(map);
  332. if (!new)
  333. return -ENOMEM;
  334. thread__insert_map(self, new);
  335. }
  336. return 0;
  337. }
  338. static struct map *thread__find_map(struct thread *self, u64 ip)
  339. {
  340. struct map *pos;
  341. if (self == NULL)
  342. return NULL;
  343. list_for_each_entry(pos, &self->maps, node)
  344. if (ip >= pos->start && ip <= pos->end)
  345. return pos;
  346. return NULL;
  347. }
  348. static size_t threads__fprintf(FILE *fp)
  349. {
  350. size_t ret = 0;
  351. struct rb_node *nd;
  352. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  353. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  354. ret += thread__fprintf(pos, fp);
  355. }
  356. return ret;
  357. }
  358. /*
  359. * histogram, sorted on item, collects counts
  360. */
  361. static struct rb_root hist;
  362. struct hist_entry {
  363. struct rb_node rb_node;
  364. struct thread *thread;
  365. struct map *map;
  366. struct dso *dso;
  367. struct symbol *sym;
  368. struct symbol *parent;
  369. u64 ip;
  370. char level;
  371. u64 count;
  372. };
  373. /*
  374. * configurable sorting bits
  375. */
  376. struct sort_entry {
  377. struct list_head list;
  378. char *header;
  379. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  380. int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
  381. size_t (*print)(FILE *fp, struct hist_entry *);
  382. };
  383. static int64_t cmp_null(void *l, void *r)
  384. {
  385. if (!l && !r)
  386. return 0;
  387. else if (!l)
  388. return -1;
  389. else
  390. return 1;
  391. }
  392. /* --sort pid */
  393. static int64_t
  394. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  395. {
  396. return right->thread->pid - left->thread->pid;
  397. }
  398. static size_t
  399. sort__thread_print(FILE *fp, struct hist_entry *self)
  400. {
  401. return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
  402. }
  403. static struct sort_entry sort_thread = {
  404. .header = " Command: Pid",
  405. .cmp = sort__thread_cmp,
  406. .print = sort__thread_print,
  407. };
  408. /* --sort comm */
  409. static int64_t
  410. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  411. {
  412. return right->thread->pid - left->thread->pid;
  413. }
  414. static int64_t
  415. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  416. {
  417. char *comm_l = left->thread->comm;
  418. char *comm_r = right->thread->comm;
  419. if (!comm_l || !comm_r)
  420. return cmp_null(comm_l, comm_r);
  421. return strcmp(comm_l, comm_r);
  422. }
  423. static size_t
  424. sort__comm_print(FILE *fp, struct hist_entry *self)
  425. {
  426. return fprintf(fp, "%16s", self->thread->comm);
  427. }
  428. static struct sort_entry sort_comm = {
  429. .header = " Command",
  430. .cmp = sort__comm_cmp,
  431. .collapse = sort__comm_collapse,
  432. .print = sort__comm_print,
  433. };
  434. /* --sort dso */
  435. static int64_t
  436. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  437. {
  438. struct dso *dso_l = left->dso;
  439. struct dso *dso_r = right->dso;
  440. if (!dso_l || !dso_r)
  441. return cmp_null(dso_l, dso_r);
  442. return strcmp(dso_l->name, dso_r->name);
  443. }
  444. static size_t
  445. sort__dso_print(FILE *fp, struct hist_entry *self)
  446. {
  447. if (self->dso)
  448. return fprintf(fp, "%-25s", self->dso->name);
  449. return fprintf(fp, "%016llx ", (u64)self->ip);
  450. }
  451. static struct sort_entry sort_dso = {
  452. .header = "Shared Object ",
  453. .cmp = sort__dso_cmp,
  454. .print = sort__dso_print,
  455. };
  456. /* --sort symbol */
  457. static int64_t
  458. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  459. {
  460. u64 ip_l, ip_r;
  461. if (left->sym == right->sym)
  462. return 0;
  463. ip_l = left->sym ? left->sym->start : left->ip;
  464. ip_r = right->sym ? right->sym->start : right->ip;
  465. return (int64_t)(ip_r - ip_l);
  466. }
  467. static size_t
  468. sort__sym_print(FILE *fp, struct hist_entry *self)
  469. {
  470. size_t ret = 0;
  471. if (verbose)
  472. ret += fprintf(fp, "%#018llx ", (u64)self->ip);
  473. if (self->sym) {
  474. ret += fprintf(fp, "[%c] %s",
  475. self->dso == kernel_dso ? 'k' : '.', self->sym->name);
  476. } else {
  477. ret += fprintf(fp, "%#016llx", (u64)self->ip);
  478. }
  479. return ret;
  480. }
  481. static struct sort_entry sort_sym = {
  482. .header = "Symbol",
  483. .cmp = sort__sym_cmp,
  484. .print = sort__sym_print,
  485. };
  486. /* --sort parent */
  487. static int64_t
  488. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  489. {
  490. struct symbol *sym_l = left->parent;
  491. struct symbol *sym_r = right->parent;
  492. if (!sym_l || !sym_r)
  493. return cmp_null(sym_l, sym_r);
  494. return strcmp(sym_l->name, sym_r->name);
  495. }
  496. static size_t
  497. sort__parent_print(FILE *fp, struct hist_entry *self)
  498. {
  499. size_t ret = 0;
  500. ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
  501. return ret;
  502. }
  503. static struct sort_entry sort_parent = {
  504. .header = "Parent symbol ",
  505. .cmp = sort__parent_cmp,
  506. .print = sort__parent_print,
  507. };
  508. static int sort__need_collapse = 0;
  509. static int sort__has_parent = 0;
  510. struct sort_dimension {
  511. char *name;
  512. struct sort_entry *entry;
  513. int taken;
  514. };
  515. static struct sort_dimension sort_dimensions[] = {
  516. { .name = "pid", .entry = &sort_thread, },
  517. { .name = "comm", .entry = &sort_comm, },
  518. { .name = "dso", .entry = &sort_dso, },
  519. { .name = "symbol", .entry = &sort_sym, },
  520. { .name = "parent", .entry = &sort_parent, },
  521. };
  522. static LIST_HEAD(hist_entry__sort_list);
  523. static int sort_dimension__add(char *tok)
  524. {
  525. int i;
  526. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  527. struct sort_dimension *sd = &sort_dimensions[i];
  528. if (sd->taken)
  529. continue;
  530. if (strncasecmp(tok, sd->name, strlen(tok)))
  531. continue;
  532. if (sd->entry->collapse)
  533. sort__need_collapse = 1;
  534. if (sd->entry == &sort_parent) {
  535. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  536. if (ret) {
  537. char err[BUFSIZ];
  538. regerror(ret, &parent_regex, err, sizeof(err));
  539. fprintf(stderr, "Invalid regex: %s\n%s",
  540. parent_pattern, err);
  541. exit(-1);
  542. }
  543. sort__has_parent = 1;
  544. }
  545. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  546. sd->taken = 1;
  547. return 0;
  548. }
  549. return -ESRCH;
  550. }
  551. static int64_t
  552. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  553. {
  554. struct sort_entry *se;
  555. int64_t cmp = 0;
  556. list_for_each_entry(se, &hist_entry__sort_list, list) {
  557. cmp = se->cmp(left, right);
  558. if (cmp)
  559. break;
  560. }
  561. return cmp;
  562. }
  563. static int64_t
  564. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  565. {
  566. struct sort_entry *se;
  567. int64_t cmp = 0;
  568. list_for_each_entry(se, &hist_entry__sort_list, list) {
  569. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  570. f = se->collapse ?: se->cmp;
  571. cmp = f(left, right);
  572. if (cmp)
  573. break;
  574. }
  575. return cmp;
  576. }
  577. static size_t
  578. hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
  579. {
  580. struct sort_entry *se;
  581. size_t ret;
  582. if (exclude_other && !self->parent)
  583. return 0;
  584. if (total_samples) {
  585. double percent = self->count * 100.0 / total_samples;
  586. char *color = PERF_COLOR_NORMAL;
  587. /*
  588. * We color high-overhead entries in red, mid-overhead
  589. * entries in green - and keep the low overhead places
  590. * normal:
  591. */
  592. if (percent >= 5.0) {
  593. color = PERF_COLOR_RED;
  594. } else {
  595. if (percent >= 0.5)
  596. color = PERF_COLOR_GREEN;
  597. }
  598. ret = color_fprintf(fp, color, " %6.2f%%",
  599. (self->count * 100.0) / total_samples);
  600. } else
  601. ret = fprintf(fp, "%12Ld ", self->count);
  602. list_for_each_entry(se, &hist_entry__sort_list, list) {
  603. if (exclude_other && (se == &sort_parent))
  604. continue;
  605. fprintf(fp, " ");
  606. ret += se->print(fp, self);
  607. }
  608. ret += fprintf(fp, "\n");
  609. return ret;
  610. }
  611. /*
  612. *
  613. */
  614. static struct symbol *
  615. resolve_symbol(struct thread *thread, struct map **mapp,
  616. struct dso **dsop, u64 *ipp)
  617. {
  618. struct dso *dso = dsop ? *dsop : NULL;
  619. struct map *map = mapp ? *mapp : NULL;
  620. u64 ip = *ipp;
  621. if (!thread)
  622. return NULL;
  623. if (dso)
  624. goto got_dso;
  625. if (map)
  626. goto got_map;
  627. map = thread__find_map(thread, ip);
  628. if (map != NULL) {
  629. if (mapp)
  630. *mapp = map;
  631. got_map:
  632. ip = map->map_ip(map, ip);
  633. dso = map->dso;
  634. } else {
  635. /*
  636. * If this is outside of all known maps,
  637. * and is a negative address, try to look it
  638. * up in the kernel dso, as it might be a
  639. * vsyscall (which executes in user-mode):
  640. */
  641. if ((long long)ip < 0)
  642. dso = kernel_dso;
  643. }
  644. dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  645. dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
  646. *ipp = ip;
  647. if (dsop)
  648. *dsop = dso;
  649. if (!dso)
  650. return NULL;
  651. got_dso:
  652. return dso->find_symbol(dso, ip);
  653. }
  654. static int call__match(struct symbol *sym)
  655. {
  656. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  657. return 1;
  658. return 0;
  659. }
  660. /*
  661. * collect histogram counts
  662. */
  663. static int
  664. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  665. struct symbol *sym, u64 ip, struct ip_callchain *chain,
  666. char level, u64 count)
  667. {
  668. struct rb_node **p = &hist.rb_node;
  669. struct rb_node *parent = NULL;
  670. struct hist_entry *he;
  671. struct hist_entry entry = {
  672. .thread = thread,
  673. .map = map,
  674. .dso = dso,
  675. .sym = sym,
  676. .ip = ip,
  677. .level = level,
  678. .count = count,
  679. .parent = NULL,
  680. };
  681. int cmp;
  682. if (sort__has_parent && chain) {
  683. u64 context = PERF_CONTEXT_MAX;
  684. int i;
  685. for (i = 0; i < chain->nr; i++) {
  686. u64 ip = chain->ips[i];
  687. struct dso *dso = NULL;
  688. struct symbol *sym;
  689. if (ip >= PERF_CONTEXT_MAX) {
  690. context = ip;
  691. continue;
  692. }
  693. switch (context) {
  694. case PERF_CONTEXT_KERNEL:
  695. dso = kernel_dso;
  696. break;
  697. default:
  698. break;
  699. }
  700. sym = resolve_symbol(thread, NULL, &dso, &ip);
  701. if (sym && call__match(sym)) {
  702. entry.parent = sym;
  703. break;
  704. }
  705. }
  706. }
  707. while (*p != NULL) {
  708. parent = *p;
  709. he = rb_entry(parent, struct hist_entry, rb_node);
  710. cmp = hist_entry__cmp(&entry, he);
  711. if (!cmp) {
  712. he->count += count;
  713. return 0;
  714. }
  715. if (cmp < 0)
  716. p = &(*p)->rb_left;
  717. else
  718. p = &(*p)->rb_right;
  719. }
  720. he = malloc(sizeof(*he));
  721. if (!he)
  722. return -ENOMEM;
  723. *he = entry;
  724. rb_link_node(&he->rb_node, parent, p);
  725. rb_insert_color(&he->rb_node, &hist);
  726. return 0;
  727. }
  728. static void hist_entry__free(struct hist_entry *he)
  729. {
  730. free(he);
  731. }
  732. /*
  733. * collapse the histogram
  734. */
  735. static struct rb_root collapse_hists;
  736. static void collapse__insert_entry(struct hist_entry *he)
  737. {
  738. struct rb_node **p = &collapse_hists.rb_node;
  739. struct rb_node *parent = NULL;
  740. struct hist_entry *iter;
  741. int64_t cmp;
  742. while (*p != NULL) {
  743. parent = *p;
  744. iter = rb_entry(parent, struct hist_entry, rb_node);
  745. cmp = hist_entry__collapse(iter, he);
  746. if (!cmp) {
  747. iter->count += he->count;
  748. hist_entry__free(he);
  749. return;
  750. }
  751. if (cmp < 0)
  752. p = &(*p)->rb_left;
  753. else
  754. p = &(*p)->rb_right;
  755. }
  756. rb_link_node(&he->rb_node, parent, p);
  757. rb_insert_color(&he->rb_node, &collapse_hists);
  758. }
  759. static void collapse__resort(void)
  760. {
  761. struct rb_node *next;
  762. struct hist_entry *n;
  763. if (!sort__need_collapse)
  764. return;
  765. next = rb_first(&hist);
  766. while (next) {
  767. n = rb_entry(next, struct hist_entry, rb_node);
  768. next = rb_next(&n->rb_node);
  769. rb_erase(&n->rb_node, &hist);
  770. collapse__insert_entry(n);
  771. }
  772. }
  773. /*
  774. * reverse the map, sort on count.
  775. */
  776. static struct rb_root output_hists;
  777. static void output__insert_entry(struct hist_entry *he)
  778. {
  779. struct rb_node **p = &output_hists.rb_node;
  780. struct rb_node *parent = NULL;
  781. struct hist_entry *iter;
  782. while (*p != NULL) {
  783. parent = *p;
  784. iter = rb_entry(parent, struct hist_entry, rb_node);
  785. if (he->count > iter->count)
  786. p = &(*p)->rb_left;
  787. else
  788. p = &(*p)->rb_right;
  789. }
  790. rb_link_node(&he->rb_node, parent, p);
  791. rb_insert_color(&he->rb_node, &output_hists);
  792. }
  793. static void output__resort(void)
  794. {
  795. struct rb_node *next;
  796. struct hist_entry *n;
  797. struct rb_root *tree = &hist;
  798. if (sort__need_collapse)
  799. tree = &collapse_hists;
  800. next = rb_first(tree);
  801. while (next) {
  802. n = rb_entry(next, struct hist_entry, rb_node);
  803. next = rb_next(&n->rb_node);
  804. rb_erase(&n->rb_node, tree);
  805. output__insert_entry(n);
  806. }
  807. }
  808. static size_t output__fprintf(FILE *fp, u64 total_samples)
  809. {
  810. struct hist_entry *pos;
  811. struct sort_entry *se;
  812. struct rb_node *nd;
  813. size_t ret = 0;
  814. fprintf(fp, "\n");
  815. fprintf(fp, "#\n");
  816. fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
  817. fprintf(fp, "#\n");
  818. fprintf(fp, "# Overhead");
  819. list_for_each_entry(se, &hist_entry__sort_list, list) {
  820. if (exclude_other && (se == &sort_parent))
  821. continue;
  822. fprintf(fp, " %s", se->header);
  823. }
  824. fprintf(fp, "\n");
  825. fprintf(fp, "# ........");
  826. list_for_each_entry(se, &hist_entry__sort_list, list) {
  827. int i;
  828. if (exclude_other && (se == &sort_parent))
  829. continue;
  830. fprintf(fp, " ");
  831. for (i = 0; i < strlen(se->header); i++)
  832. fprintf(fp, ".");
  833. }
  834. fprintf(fp, "\n");
  835. fprintf(fp, "#\n");
  836. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  837. pos = rb_entry(nd, struct hist_entry, rb_node);
  838. ret += hist_entry__fprintf(fp, pos, total_samples);
  839. }
  840. if (sort_order == default_sort_order &&
  841. parent_pattern == default_parent_pattern) {
  842. fprintf(fp, "#\n");
  843. fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
  844. fprintf(fp, "#\n");
  845. }
  846. fprintf(fp, "\n");
  847. return ret;
  848. }
  849. static void register_idle_thread(void)
  850. {
  851. struct thread *thread = threads__findnew(0);
  852. if (thread == NULL ||
  853. thread__set_comm(thread, "[idle]")) {
  854. fprintf(stderr, "problem inserting idle task.\n");
  855. exit(-1);
  856. }
  857. }
  858. static unsigned long total = 0,
  859. total_mmap = 0,
  860. total_comm = 0,
  861. total_fork = 0,
  862. total_unknown = 0,
  863. total_lost = 0;
  864. static int validate_chain(struct ip_callchain *chain, event_t *event)
  865. {
  866. unsigned int chain_size;
  867. chain_size = event->header.size;
  868. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  869. if (chain->nr*sizeof(u64) > chain_size)
  870. return -1;
  871. return 0;
  872. }
  873. static int
  874. process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
  875. {
  876. char level;
  877. int show = 0;
  878. struct dso *dso = NULL;
  879. struct thread *thread = threads__findnew(event->ip.pid);
  880. u64 ip = event->ip.ip;
  881. u64 period = 1;
  882. struct map *map = NULL;
  883. void *more_data = event->ip.__more_data;
  884. struct ip_callchain *chain = NULL;
  885. if (event->header.type & PERF_SAMPLE_PERIOD) {
  886. period = *(u64 *)more_data;
  887. more_data += sizeof(u64);
  888. }
  889. dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
  890. (void *)(offset + head),
  891. (void *)(long)(event->header.size),
  892. event->header.misc,
  893. event->ip.pid,
  894. (void *)(long)ip,
  895. (long long)period);
  896. if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
  897. int i;
  898. chain = (void *)more_data;
  899. dprintf("... chain: nr:%Lu\n", chain->nr);
  900. if (validate_chain(chain, event) < 0) {
  901. eprintf("call-chain problem with event, skipping it.\n");
  902. return 0;
  903. }
  904. if (dump_trace) {
  905. for (i = 0; i < chain->nr; i++)
  906. dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
  907. }
  908. }
  909. dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  910. if (thread == NULL) {
  911. eprintf("problem processing %d event, skipping it.\n",
  912. event->header.type);
  913. return -1;
  914. }
  915. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  916. show = SHOW_KERNEL;
  917. level = 'k';
  918. dso = kernel_dso;
  919. dprintf(" ...... dso: %s\n", dso->name);
  920. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  921. show = SHOW_USER;
  922. level = '.';
  923. } else {
  924. show = SHOW_HV;
  925. level = 'H';
  926. dprintf(" ...... dso: [hypervisor]\n");
  927. }
  928. if (show & show_mask) {
  929. struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
  930. if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
  931. eprintf("problem incrementing symbol count, skipping event\n");
  932. return -1;
  933. }
  934. }
  935. total += period;
  936. return 0;
  937. }
  938. static int
  939. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  940. {
  941. struct thread *thread = threads__findnew(event->mmap.pid);
  942. struct map *map = map__new(&event->mmap);
  943. dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
  944. (void *)(offset + head),
  945. (void *)(long)(event->header.size),
  946. event->mmap.pid,
  947. (void *)(long)event->mmap.start,
  948. (void *)(long)event->mmap.len,
  949. (void *)(long)event->mmap.pgoff,
  950. event->mmap.filename);
  951. if (thread == NULL || map == NULL) {
  952. dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
  953. return 0;
  954. }
  955. thread__insert_map(thread, map);
  956. total_mmap++;
  957. return 0;
  958. }
  959. static int
  960. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  961. {
  962. struct thread *thread = threads__findnew(event->comm.pid);
  963. dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  964. (void *)(offset + head),
  965. (void *)(long)(event->header.size),
  966. event->comm.comm, event->comm.pid);
  967. if (thread == NULL ||
  968. thread__set_comm(thread, event->comm.comm)) {
  969. dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
  970. return -1;
  971. }
  972. total_comm++;
  973. return 0;
  974. }
  975. static int
  976. process_fork_event(event_t *event, unsigned long offset, unsigned long head)
  977. {
  978. struct thread *thread = threads__findnew(event->fork.pid);
  979. struct thread *parent = threads__findnew(event->fork.ppid);
  980. dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
  981. (void *)(offset + head),
  982. (void *)(long)(event->header.size),
  983. event->fork.pid, event->fork.ppid);
  984. if (!thread || !parent || thread__fork(thread, parent)) {
  985. dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
  986. return -1;
  987. }
  988. total_fork++;
  989. return 0;
  990. }
  991. static int
  992. process_period_event(event_t *event, unsigned long offset, unsigned long head)
  993. {
  994. dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
  995. (void *)(offset + head),
  996. (void *)(long)(event->header.size),
  997. event->period.time,
  998. event->period.id,
  999. event->period.sample_period);
  1000. return 0;
  1001. }
  1002. static int
  1003. process_lost_event(event_t *event, unsigned long offset, unsigned long head)
  1004. {
  1005. dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
  1006. (void *)(offset + head),
  1007. (void *)(long)(event->header.size),
  1008. event->lost.id,
  1009. event->lost.lost);
  1010. total_lost += event->lost.lost;
  1011. return 0;
  1012. }
  1013. static void trace_event(event_t *event)
  1014. {
  1015. unsigned char *raw_event = (void *)event;
  1016. char *color = PERF_COLOR_BLUE;
  1017. int i, j;
  1018. if (!dump_trace)
  1019. return;
  1020. dprintf(".");
  1021. cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
  1022. for (i = 0; i < event->header.size; i++) {
  1023. if ((i & 15) == 0) {
  1024. dprintf(".");
  1025. cdprintf(" %04x: ", i);
  1026. }
  1027. cdprintf(" %02x", raw_event[i]);
  1028. if (((i & 15) == 15) || i == event->header.size-1) {
  1029. cdprintf(" ");
  1030. for (j = 0; j < 15-(i & 15); j++)
  1031. cdprintf(" ");
  1032. for (j = 0; j < (i & 15); j++) {
  1033. if (isprint(raw_event[i-15+j]))
  1034. cdprintf("%c", raw_event[i-15+j]);
  1035. else
  1036. cdprintf(".");
  1037. }
  1038. cdprintf("\n");
  1039. }
  1040. }
  1041. dprintf(".\n");
  1042. }
  1043. static int
  1044. process_event(event_t *event, unsigned long offset, unsigned long head)
  1045. {
  1046. trace_event(event);
  1047. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
  1048. return process_overflow_event(event, offset, head);
  1049. switch (event->header.type) {
  1050. case PERF_EVENT_MMAP:
  1051. return process_mmap_event(event, offset, head);
  1052. case PERF_EVENT_COMM:
  1053. return process_comm_event(event, offset, head);
  1054. case PERF_EVENT_FORK:
  1055. return process_fork_event(event, offset, head);
  1056. case PERF_EVENT_PERIOD:
  1057. return process_period_event(event, offset, head);
  1058. case PERF_EVENT_LOST:
  1059. return process_lost_event(event, offset, head);
  1060. /*
  1061. * We dont process them right now but they are fine:
  1062. */
  1063. case PERF_EVENT_THROTTLE:
  1064. case PERF_EVENT_UNTHROTTLE:
  1065. return 0;
  1066. default:
  1067. return -1;
  1068. }
  1069. return 0;
  1070. }
  1071. static struct perf_file_header file_header;
  1072. static int __cmd_report(void)
  1073. {
  1074. int ret, rc = EXIT_FAILURE;
  1075. unsigned long offset = 0;
  1076. unsigned long head = sizeof(file_header);
  1077. struct stat stat;
  1078. event_t *event;
  1079. uint32_t size;
  1080. char *buf;
  1081. register_idle_thread();
  1082. input = open(input_name, O_RDONLY);
  1083. if (input < 0) {
  1084. fprintf(stderr, " failed to open file: %s", input_name);
  1085. if (!strcmp(input_name, "perf.data"))
  1086. fprintf(stderr, " (try 'perf record' first)");
  1087. fprintf(stderr, "\n");
  1088. exit(-1);
  1089. }
  1090. ret = fstat(input, &stat);
  1091. if (ret < 0) {
  1092. perror("failed to stat file");
  1093. exit(-1);
  1094. }
  1095. if (!stat.st_size) {
  1096. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1097. exit(0);
  1098. }
  1099. if (read(input, &file_header, sizeof(file_header)) == -1) {
  1100. perror("failed to read file headers");
  1101. exit(-1);
  1102. }
  1103. if (sort__has_parent &&
  1104. !(file_header.sample_type & PERF_SAMPLE_CALLCHAIN)) {
  1105. fprintf(stderr, "selected --sort parent, but no callchain data\n");
  1106. exit(-1);
  1107. }
  1108. if (load_kernel() < 0) {
  1109. perror("failed to load kernel symbols");
  1110. return EXIT_FAILURE;
  1111. }
  1112. if (!full_paths) {
  1113. if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
  1114. perror("failed to get the current directory");
  1115. return EXIT_FAILURE;
  1116. }
  1117. cwdlen = strlen(cwd);
  1118. } else {
  1119. cwd = NULL;
  1120. cwdlen = 0;
  1121. }
  1122. remap:
  1123. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  1124. MAP_SHARED, input, offset);
  1125. if (buf == MAP_FAILED) {
  1126. perror("failed to mmap file");
  1127. exit(-1);
  1128. }
  1129. more:
  1130. event = (event_t *)(buf + head);
  1131. size = event->header.size;
  1132. if (!size)
  1133. size = 8;
  1134. if (head + event->header.size >= page_size * mmap_window) {
  1135. unsigned long shift = page_size * (head / page_size);
  1136. int ret;
  1137. ret = munmap(buf, page_size * mmap_window);
  1138. assert(ret == 0);
  1139. offset += shift;
  1140. head -= shift;
  1141. goto remap;
  1142. }
  1143. size = event->header.size;
  1144. dprintf("\n%p [%p]: event: %d\n",
  1145. (void *)(offset + head),
  1146. (void *)(long)event->header.size,
  1147. event->header.type);
  1148. if (!size || process_event(event, offset, head) < 0) {
  1149. dprintf("%p [%p]: skipping unknown header type: %d\n",
  1150. (void *)(offset + head),
  1151. (void *)(long)(event->header.size),
  1152. event->header.type);
  1153. total_unknown++;
  1154. /*
  1155. * assume we lost track of the stream, check alignment, and
  1156. * increment a single u64 in the hope to catch on again 'soon'.
  1157. */
  1158. if (unlikely(head & 7))
  1159. head &= ~7ULL;
  1160. size = 8;
  1161. }
  1162. head += size;
  1163. if (offset + head >= sizeof(file_header) + file_header.data_size)
  1164. goto done;
  1165. if (offset + head < stat.st_size)
  1166. goto more;
  1167. done:
  1168. rc = EXIT_SUCCESS;
  1169. close(input);
  1170. dprintf(" IP events: %10ld\n", total);
  1171. dprintf(" mmap events: %10ld\n", total_mmap);
  1172. dprintf(" comm events: %10ld\n", total_comm);
  1173. dprintf(" fork events: %10ld\n", total_fork);
  1174. dprintf(" lost events: %10ld\n", total_lost);
  1175. dprintf(" unknown events: %10ld\n", total_unknown);
  1176. if (dump_trace)
  1177. return 0;
  1178. if (verbose >= 3)
  1179. threads__fprintf(stdout);
  1180. if (verbose >= 2)
  1181. dsos__fprintf(stdout);
  1182. collapse__resort();
  1183. output__resort();
  1184. output__fprintf(stdout, total);
  1185. return rc;
  1186. }
  1187. static const char * const report_usage[] = {
  1188. "perf report [<options>] <command>",
  1189. NULL
  1190. };
  1191. static const struct option options[] = {
  1192. OPT_STRING('i', "input", &input_name, "file",
  1193. "input file name"),
  1194. OPT_BOOLEAN('v', "verbose", &verbose,
  1195. "be more verbose (show symbol address, etc)"),
  1196. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1197. "dump raw trace in ASCII"),
  1198. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  1199. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1200. "sort by key(s): pid, comm, dso, symbol, parent"),
  1201. OPT_BOOLEAN('P', "full-paths", &full_paths,
  1202. "Don't shorten the pathnames taking into account the cwd"),
  1203. OPT_STRING('p', "parent", &parent_pattern, "regex",
  1204. "regex filter to identify parent, see: '--sort parent'"),
  1205. OPT_BOOLEAN('x', "exclude-other", &exclude_other,
  1206. "Only display entries with parent-match"),
  1207. OPT_END()
  1208. };
  1209. static void setup_sorting(void)
  1210. {
  1211. char *tmp, *tok, *str = strdup(sort_order);
  1212. for (tok = strtok_r(str, ", ", &tmp);
  1213. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1214. if (sort_dimension__add(tok) < 0) {
  1215. error("Unknown --sort key: `%s'", tok);
  1216. usage_with_options(report_usage, options);
  1217. }
  1218. }
  1219. free(str);
  1220. }
  1221. int cmd_report(int argc, const char **argv, const char *prefix)
  1222. {
  1223. symbol__init();
  1224. page_size = getpagesize();
  1225. argc = parse_options(argc, argv, options, report_usage, 0);
  1226. setup_sorting();
  1227. if (parent_pattern != default_parent_pattern)
  1228. sort_dimension__add("parent");
  1229. else
  1230. exclude_other = 0;
  1231. /*
  1232. * Any (unrecognized) arguments left?
  1233. */
  1234. if (argc)
  1235. usage_with_options(report_usage, options);
  1236. setup_pager();
  1237. return __cmd_report();
  1238. }