builtin-report.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  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. static int verbose;
  31. static int full_paths;
  32. static unsigned long page_size;
  33. static unsigned long mmap_window = 32;
  34. struct ip_event {
  35. struct perf_event_header header;
  36. __u64 ip;
  37. __u32 pid, tid;
  38. };
  39. struct mmap_event {
  40. struct perf_event_header header;
  41. __u32 pid, tid;
  42. __u64 start;
  43. __u64 len;
  44. __u64 pgoff;
  45. char filename[PATH_MAX];
  46. };
  47. struct comm_event {
  48. struct perf_event_header header;
  49. __u32 pid, tid;
  50. char comm[16];
  51. };
  52. struct fork_event {
  53. struct perf_event_header header;
  54. __u32 pid, ppid;
  55. };
  56. typedef union event_union {
  57. struct perf_event_header header;
  58. struct ip_event ip;
  59. struct mmap_event mmap;
  60. struct comm_event comm;
  61. struct fork_event fork;
  62. } event_t;
  63. static LIST_HEAD(dsos);
  64. static struct dso *kernel_dso;
  65. static struct dso *vdso;
  66. static void dsos__add(struct dso *dso)
  67. {
  68. list_add_tail(&dso->node, &dsos);
  69. }
  70. static struct dso *dsos__find(const char *name)
  71. {
  72. struct dso *pos;
  73. list_for_each_entry(pos, &dsos, node)
  74. if (strcmp(pos->name, name) == 0)
  75. return pos;
  76. return NULL;
  77. }
  78. static struct dso *dsos__findnew(const char *name)
  79. {
  80. struct dso *dso = dsos__find(name);
  81. int nr;
  82. if (dso)
  83. return dso;
  84. dso = dso__new(name, 0);
  85. if (!dso)
  86. goto out_delete_dso;
  87. nr = dso__load(dso, NULL, verbose);
  88. if (nr < 0) {
  89. if (verbose)
  90. fprintf(stderr, "Failed to open: %s\n", name);
  91. goto out_delete_dso;
  92. }
  93. if (!nr && verbose) {
  94. fprintf(stderr,
  95. "No symbols found in: %s, maybe install a debug package?\n",
  96. name);
  97. }
  98. dsos__add(dso);
  99. return dso;
  100. out_delete_dso:
  101. dso__delete(dso);
  102. return NULL;
  103. }
  104. static void dsos__fprintf(FILE *fp)
  105. {
  106. struct dso *pos;
  107. list_for_each_entry(pos, &dsos, node)
  108. dso__fprintf(pos, fp);
  109. }
  110. static struct symbol *vdso__find_symbol(struct dso *dso, uint64_t ip)
  111. {
  112. return dso__find_symbol(kernel_dso, ip);
  113. }
  114. static int load_kernel(void)
  115. {
  116. int err;
  117. kernel_dso = dso__new("[kernel]", 0);
  118. if (!kernel_dso)
  119. return -1;
  120. err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
  121. if (err) {
  122. dso__delete(kernel_dso);
  123. kernel_dso = NULL;
  124. } else
  125. dsos__add(kernel_dso);
  126. vdso = dso__new("[vdso]", 0);
  127. if (!vdso)
  128. return -1;
  129. vdso->find_symbol = vdso__find_symbol;
  130. dsos__add(vdso);
  131. return err;
  132. }
  133. static char __cwd[PATH_MAX];
  134. static char *cwd = __cwd;
  135. static int cwdlen;
  136. static int strcommon(const char *pathname)
  137. {
  138. int n = 0;
  139. while (pathname[n] == cwd[n] && n < cwdlen)
  140. ++n;
  141. return n;
  142. }
  143. struct map {
  144. struct list_head node;
  145. uint64_t start;
  146. uint64_t end;
  147. uint64_t pgoff;
  148. uint64_t (*map_ip)(struct map *, uint64_t);
  149. struct dso *dso;
  150. };
  151. static uint64_t map__map_ip(struct map *map, uint64_t ip)
  152. {
  153. return ip - map->start + map->pgoff;
  154. }
  155. static uint64_t vdso__map_ip(struct map *map, uint64_t ip)
  156. {
  157. return ip;
  158. }
  159. static struct map *map__new(struct mmap_event *event)
  160. {
  161. struct map *self = malloc(sizeof(*self));
  162. if (self != NULL) {
  163. const char *filename = event->filename;
  164. char newfilename[PATH_MAX];
  165. if (cwd) {
  166. int n = strcommon(filename);
  167. if (n == cwdlen) {
  168. snprintf(newfilename, sizeof(newfilename),
  169. ".%s", filename + n);
  170. filename = newfilename;
  171. }
  172. }
  173. self->start = event->start;
  174. self->end = event->start + event->len;
  175. self->pgoff = event->pgoff;
  176. self->dso = dsos__findnew(filename);
  177. if (self->dso == NULL)
  178. goto out_delete;
  179. if (self->dso == vdso)
  180. self->map_ip = vdso__map_ip;
  181. else
  182. self->map_ip = map__map_ip;
  183. }
  184. return self;
  185. out_delete:
  186. free(self);
  187. return NULL;
  188. }
  189. static struct map *map__clone(struct map *self)
  190. {
  191. struct map *map = malloc(sizeof(*self));
  192. if (!map)
  193. return NULL;
  194. memcpy(map, self, sizeof(*self));
  195. return map;
  196. }
  197. static int map__overlap(struct map *l, struct map *r)
  198. {
  199. if (l->start > r->start) {
  200. struct map *t = l;
  201. l = r;
  202. r = t;
  203. }
  204. if (l->end > r->start)
  205. return 1;
  206. return 0;
  207. }
  208. static size_t map__fprintf(struct map *self, FILE *fp)
  209. {
  210. return fprintf(fp, " %"PRIx64"-%"PRIx64" %"PRIx64" %s\n",
  211. self->start, self->end, self->pgoff, self->dso->name);
  212. }
  213. struct thread {
  214. struct rb_node rb_node;
  215. struct list_head maps;
  216. pid_t pid;
  217. char *comm;
  218. };
  219. static struct thread *thread__new(pid_t pid)
  220. {
  221. struct thread *self = malloc(sizeof(*self));
  222. if (self != NULL) {
  223. self->pid = pid;
  224. self->comm = malloc(32);
  225. if (self->comm)
  226. snprintf(self->comm, 32, ":%d", self->pid);
  227. INIT_LIST_HEAD(&self->maps);
  228. }
  229. return self;
  230. }
  231. static int thread__set_comm(struct thread *self, const char *comm)
  232. {
  233. if (self->comm)
  234. free(self->comm);
  235. self->comm = strdup(comm);
  236. return self->comm ? 0 : -ENOMEM;
  237. }
  238. static size_t thread__fprintf(struct thread *self, FILE *fp)
  239. {
  240. struct map *pos;
  241. size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
  242. list_for_each_entry(pos, &self->maps, node)
  243. ret += map__fprintf(pos, fp);
  244. return ret;
  245. }
  246. static struct rb_root threads;
  247. static struct thread *last_match;
  248. static struct thread *threads__findnew(pid_t pid)
  249. {
  250. struct rb_node **p = &threads.rb_node;
  251. struct rb_node *parent = NULL;
  252. struct thread *th;
  253. /*
  254. * Font-end cache - PID lookups come in blocks,
  255. * so most of the time we dont have to look up
  256. * the full rbtree:
  257. */
  258. if (last_match && last_match->pid == pid)
  259. return last_match;
  260. while (*p != NULL) {
  261. parent = *p;
  262. th = rb_entry(parent, struct thread, rb_node);
  263. if (th->pid == pid) {
  264. last_match = th;
  265. return th;
  266. }
  267. if (pid < th->pid)
  268. p = &(*p)->rb_left;
  269. else
  270. p = &(*p)->rb_right;
  271. }
  272. th = thread__new(pid);
  273. if (th != NULL) {
  274. rb_link_node(&th->rb_node, parent, p);
  275. rb_insert_color(&th->rb_node, &threads);
  276. last_match = th;
  277. }
  278. return th;
  279. }
  280. static void thread__insert_map(struct thread *self, struct map *map)
  281. {
  282. struct map *pos, *tmp;
  283. list_for_each_entry_safe(pos, tmp, &self->maps, node) {
  284. if (map__overlap(pos, map)) {
  285. list_del_init(&pos->node);
  286. /* XXX leaks dsos */
  287. free(pos);
  288. }
  289. }
  290. list_add_tail(&map->node, &self->maps);
  291. }
  292. static int thread__fork(struct thread *self, struct thread *parent)
  293. {
  294. struct map *map;
  295. if (self->comm)
  296. free(self->comm);
  297. self->comm = strdup(parent->comm);
  298. if (!self->comm)
  299. return -ENOMEM;
  300. list_for_each_entry(map, &parent->maps, node) {
  301. struct map *new = map__clone(map);
  302. if (!new)
  303. return -ENOMEM;
  304. thread__insert_map(self, new);
  305. }
  306. return 0;
  307. }
  308. static struct map *thread__find_map(struct thread *self, uint64_t ip)
  309. {
  310. struct map *pos;
  311. if (self == NULL)
  312. return NULL;
  313. list_for_each_entry(pos, &self->maps, node)
  314. if (ip >= pos->start && ip <= pos->end)
  315. return pos;
  316. return NULL;
  317. }
  318. static size_t threads__fprintf(FILE *fp)
  319. {
  320. size_t ret = 0;
  321. struct rb_node *nd;
  322. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  323. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  324. ret += thread__fprintf(pos, fp);
  325. }
  326. return ret;
  327. }
  328. /*
  329. * histogram, sorted on item, collects counts
  330. */
  331. static struct rb_root hist;
  332. struct hist_entry {
  333. struct rb_node rb_node;
  334. struct thread *thread;
  335. struct map *map;
  336. struct dso *dso;
  337. struct symbol *sym;
  338. uint64_t ip;
  339. char level;
  340. uint32_t count;
  341. };
  342. /*
  343. * configurable sorting bits
  344. */
  345. struct sort_entry {
  346. struct list_head list;
  347. char *header;
  348. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  349. int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
  350. size_t (*print)(FILE *fp, struct hist_entry *);
  351. };
  352. /* --sort pid */
  353. static int64_t
  354. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  355. {
  356. return right->thread->pid - left->thread->pid;
  357. }
  358. static size_t
  359. sort__thread_print(FILE *fp, struct hist_entry *self)
  360. {
  361. return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
  362. }
  363. static struct sort_entry sort_thread = {
  364. .header = " Command: Pid",
  365. .cmp = sort__thread_cmp,
  366. .print = sort__thread_print,
  367. };
  368. /* --sort comm */
  369. static int64_t
  370. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  371. {
  372. return right->thread->pid - left->thread->pid;
  373. }
  374. static int64_t
  375. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  376. {
  377. char *comm_l = left->thread->comm;
  378. char *comm_r = right->thread->comm;
  379. if (!comm_l || !comm_r) {
  380. if (!comm_l && !comm_r)
  381. return 0;
  382. else if (!comm_l)
  383. return -1;
  384. else
  385. return 1;
  386. }
  387. return strcmp(comm_l, comm_r);
  388. }
  389. static size_t
  390. sort__comm_print(FILE *fp, struct hist_entry *self)
  391. {
  392. return fprintf(fp, "%16s", self->thread->comm);
  393. }
  394. static struct sort_entry sort_comm = {
  395. .header = " Command",
  396. .cmp = sort__comm_cmp,
  397. .collapse = sort__comm_collapse,
  398. .print = sort__comm_print,
  399. };
  400. /* --sort dso */
  401. static int64_t
  402. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  403. {
  404. struct dso *dso_l = left->dso;
  405. struct dso *dso_r = right->dso;
  406. if (!dso_l || !dso_r) {
  407. if (!dso_l && !dso_r)
  408. return 0;
  409. else if (!dso_l)
  410. return -1;
  411. else
  412. return 1;
  413. }
  414. return strcmp(dso_l->name, dso_r->name);
  415. }
  416. static size_t
  417. sort__dso_print(FILE *fp, struct hist_entry *self)
  418. {
  419. if (self->dso)
  420. return fprintf(fp, "%-25s", self->dso->name);
  421. return fprintf(fp, "%016llx ", (__u64)self->ip);
  422. }
  423. static struct sort_entry sort_dso = {
  424. .header = "Shared Object ",
  425. .cmp = sort__dso_cmp,
  426. .print = sort__dso_print,
  427. };
  428. /* --sort symbol */
  429. static int64_t
  430. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  431. {
  432. uint64_t ip_l, ip_r;
  433. if (left->sym == right->sym)
  434. return 0;
  435. ip_l = left->sym ? left->sym->start : left->ip;
  436. ip_r = right->sym ? right->sym->start : right->ip;
  437. return (int64_t)(ip_r - ip_l);
  438. }
  439. static size_t
  440. sort__sym_print(FILE *fp, struct hist_entry *self)
  441. {
  442. size_t ret = 0;
  443. if (verbose)
  444. ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
  445. if (self->sym) {
  446. ret += fprintf(fp, "[%c] %s",
  447. self->dso == kernel_dso ? 'k' : '.', self->sym->name);
  448. } else {
  449. ret += fprintf(fp, "%#016llx", (__u64)self->ip);
  450. }
  451. return ret;
  452. }
  453. static struct sort_entry sort_sym = {
  454. .header = "Symbol",
  455. .cmp = sort__sym_cmp,
  456. .print = sort__sym_print,
  457. };
  458. static int sort__need_collapse = 0;
  459. struct sort_dimension {
  460. char *name;
  461. struct sort_entry *entry;
  462. int taken;
  463. };
  464. static struct sort_dimension sort_dimensions[] = {
  465. { .name = "pid", .entry = &sort_thread, },
  466. { .name = "comm", .entry = &sort_comm, },
  467. { .name = "dso", .entry = &sort_dso, },
  468. { .name = "symbol", .entry = &sort_sym, },
  469. };
  470. static LIST_HEAD(hist_entry__sort_list);
  471. static int sort_dimension__add(char *tok)
  472. {
  473. int i;
  474. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  475. struct sort_dimension *sd = &sort_dimensions[i];
  476. if (sd->taken)
  477. continue;
  478. if (strncasecmp(tok, sd->name, strlen(tok)))
  479. continue;
  480. if (sd->entry->collapse)
  481. sort__need_collapse = 1;
  482. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  483. sd->taken = 1;
  484. return 0;
  485. }
  486. return -ESRCH;
  487. }
  488. static int64_t
  489. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  490. {
  491. struct sort_entry *se;
  492. int64_t cmp = 0;
  493. list_for_each_entry(se, &hist_entry__sort_list, list) {
  494. cmp = se->cmp(left, right);
  495. if (cmp)
  496. break;
  497. }
  498. return cmp;
  499. }
  500. static int64_t
  501. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  502. {
  503. struct sort_entry *se;
  504. int64_t cmp = 0;
  505. list_for_each_entry(se, &hist_entry__sort_list, list) {
  506. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  507. f = se->collapse ?: se->cmp;
  508. cmp = f(left, right);
  509. if (cmp)
  510. break;
  511. }
  512. return cmp;
  513. }
  514. static size_t
  515. hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
  516. {
  517. struct sort_entry *se;
  518. size_t ret;
  519. if (total_samples) {
  520. double percent = self->count * 100.0 / total_samples;
  521. char *color = PERF_COLOR_NORMAL;
  522. /*
  523. * We color high-overhead entries in red, low-overhead
  524. * entries in green - and keep the middle ground normal:
  525. */
  526. if (percent >= 5.0)
  527. color = PERF_COLOR_RED;
  528. if (percent < 0.5)
  529. color = PERF_COLOR_GREEN;
  530. ret = color_fprintf(fp, color, " %6.2f%%",
  531. (self->count * 100.0) / total_samples);
  532. } else
  533. ret = fprintf(fp, "%12d ", self->count);
  534. list_for_each_entry(se, &hist_entry__sort_list, list) {
  535. fprintf(fp, " ");
  536. ret += se->print(fp, self);
  537. }
  538. ret += fprintf(fp, "\n");
  539. return ret;
  540. }
  541. /*
  542. * collect histogram counts
  543. */
  544. static int
  545. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  546. struct symbol *sym, uint64_t ip, char level)
  547. {
  548. struct rb_node **p = &hist.rb_node;
  549. struct rb_node *parent = NULL;
  550. struct hist_entry *he;
  551. struct hist_entry entry = {
  552. .thread = thread,
  553. .map = map,
  554. .dso = dso,
  555. .sym = sym,
  556. .ip = ip,
  557. .level = level,
  558. .count = 1,
  559. };
  560. int cmp;
  561. while (*p != NULL) {
  562. parent = *p;
  563. he = rb_entry(parent, struct hist_entry, rb_node);
  564. cmp = hist_entry__cmp(&entry, he);
  565. if (!cmp) {
  566. he->count++;
  567. return 0;
  568. }
  569. if (cmp < 0)
  570. p = &(*p)->rb_left;
  571. else
  572. p = &(*p)->rb_right;
  573. }
  574. he = malloc(sizeof(*he));
  575. if (!he)
  576. return -ENOMEM;
  577. *he = entry;
  578. rb_link_node(&he->rb_node, parent, p);
  579. rb_insert_color(&he->rb_node, &hist);
  580. return 0;
  581. }
  582. static void hist_entry__free(struct hist_entry *he)
  583. {
  584. free(he);
  585. }
  586. /*
  587. * collapse the histogram
  588. */
  589. static struct rb_root collapse_hists;
  590. static void collapse__insert_entry(struct hist_entry *he)
  591. {
  592. struct rb_node **p = &collapse_hists.rb_node;
  593. struct rb_node *parent = NULL;
  594. struct hist_entry *iter;
  595. int64_t cmp;
  596. while (*p != NULL) {
  597. parent = *p;
  598. iter = rb_entry(parent, struct hist_entry, rb_node);
  599. cmp = hist_entry__collapse(iter, he);
  600. if (!cmp) {
  601. iter->count += he->count;
  602. hist_entry__free(he);
  603. return;
  604. }
  605. if (cmp < 0)
  606. p = &(*p)->rb_left;
  607. else
  608. p = &(*p)->rb_right;
  609. }
  610. rb_link_node(&he->rb_node, parent, p);
  611. rb_insert_color(&he->rb_node, &collapse_hists);
  612. }
  613. static void collapse__resort(void)
  614. {
  615. struct rb_node *next;
  616. struct hist_entry *n;
  617. if (!sort__need_collapse)
  618. return;
  619. next = rb_first(&hist);
  620. while (next) {
  621. n = rb_entry(next, struct hist_entry, rb_node);
  622. next = rb_next(&n->rb_node);
  623. rb_erase(&n->rb_node, &hist);
  624. collapse__insert_entry(n);
  625. }
  626. }
  627. /*
  628. * reverse the map, sort on count.
  629. */
  630. static struct rb_root output_hists;
  631. static void output__insert_entry(struct hist_entry *he)
  632. {
  633. struct rb_node **p = &output_hists.rb_node;
  634. struct rb_node *parent = NULL;
  635. struct hist_entry *iter;
  636. while (*p != NULL) {
  637. parent = *p;
  638. iter = rb_entry(parent, struct hist_entry, rb_node);
  639. if (he->count > iter->count)
  640. p = &(*p)->rb_left;
  641. else
  642. p = &(*p)->rb_right;
  643. }
  644. rb_link_node(&he->rb_node, parent, p);
  645. rb_insert_color(&he->rb_node, &output_hists);
  646. }
  647. static void output__resort(void)
  648. {
  649. struct rb_node *next;
  650. struct hist_entry *n;
  651. struct rb_root *tree = &hist;
  652. if (sort__need_collapse)
  653. tree = &collapse_hists;
  654. next = rb_first(tree);
  655. while (next) {
  656. n = rb_entry(next, struct hist_entry, rb_node);
  657. next = rb_next(&n->rb_node);
  658. rb_erase(&n->rb_node, tree);
  659. output__insert_entry(n);
  660. }
  661. }
  662. static size_t output__fprintf(FILE *fp, uint64_t total_samples)
  663. {
  664. struct hist_entry *pos;
  665. struct sort_entry *se;
  666. struct rb_node *nd;
  667. size_t ret = 0;
  668. fprintf(fp, "\n");
  669. fprintf(fp, "#\n");
  670. fprintf(fp, "# (%Ld profiler events)\n", (__u64)total_samples);
  671. fprintf(fp, "#\n");
  672. fprintf(fp, "# Overhead");
  673. list_for_each_entry(se, &hist_entry__sort_list, list)
  674. fprintf(fp, " %s", se->header);
  675. fprintf(fp, "\n");
  676. fprintf(fp, "# ........");
  677. list_for_each_entry(se, &hist_entry__sort_list, list) {
  678. int i;
  679. fprintf(fp, " ");
  680. for (i = 0; i < strlen(se->header); i++)
  681. fprintf(fp, ".");
  682. }
  683. fprintf(fp, "\n");
  684. fprintf(fp, "#\n");
  685. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  686. pos = rb_entry(nd, struct hist_entry, rb_node);
  687. ret += hist_entry__fprintf(fp, pos, total_samples);
  688. }
  689. if (!strcmp(sort_order, default_sort_order)) {
  690. fprintf(fp, "#\n");
  691. fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
  692. fprintf(fp, "#\n");
  693. }
  694. fprintf(fp, "\n");
  695. return ret;
  696. }
  697. static void register_idle_thread(void)
  698. {
  699. struct thread *thread = threads__findnew(0);
  700. if (thread == NULL ||
  701. thread__set_comm(thread, "[idle]")) {
  702. fprintf(stderr, "problem inserting idle task.\n");
  703. exit(-1);
  704. }
  705. }
  706. static unsigned long total = 0,
  707. total_mmap = 0,
  708. total_comm = 0,
  709. total_fork = 0,
  710. total_unknown = 0;
  711. static int
  712. process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
  713. {
  714. char level;
  715. int show = 0;
  716. struct dso *dso = NULL;
  717. struct thread *thread = threads__findnew(event->ip.pid);
  718. uint64_t ip = event->ip.ip;
  719. struct map *map = NULL;
  720. dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  721. (void *)(offset + head),
  722. (void *)(long)(event->header.size),
  723. event->header.misc,
  724. event->ip.pid,
  725. (void *)(long)ip);
  726. dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  727. if (thread == NULL) {
  728. fprintf(stderr, "problem processing %d event, skipping it.\n",
  729. event->header.type);
  730. return -1;
  731. }
  732. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  733. show = SHOW_KERNEL;
  734. level = 'k';
  735. dso = kernel_dso;
  736. dprintf(" ...... dso: %s\n", dso->name);
  737. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  738. show = SHOW_USER;
  739. level = '.';
  740. map = thread__find_map(thread, ip);
  741. if (map != NULL) {
  742. ip = map->map_ip(map, ip);
  743. dso = map->dso;
  744. } else {
  745. /*
  746. * If this is outside of all known maps,
  747. * and is a negative address, try to look it
  748. * up in the kernel dso, as it might be a
  749. * vsyscall (which executes in user-mode):
  750. */
  751. if ((long long)ip < 0)
  752. dso = kernel_dso;
  753. }
  754. dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  755. } else {
  756. show = SHOW_HV;
  757. level = 'H';
  758. dprintf(" ...... dso: [hypervisor]\n");
  759. }
  760. if (show & show_mask) {
  761. struct symbol *sym = NULL;
  762. if (dso)
  763. sym = dso->find_symbol(dso, ip);
  764. if (hist_entry__add(thread, map, dso, sym, ip, level)) {
  765. fprintf(stderr,
  766. "problem incrementing symbol count, skipping event\n");
  767. return -1;
  768. }
  769. }
  770. total++;
  771. return 0;
  772. }
  773. static int
  774. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  775. {
  776. struct thread *thread = threads__findnew(event->mmap.pid);
  777. struct map *map = map__new(&event->mmap);
  778. dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
  779. (void *)(offset + head),
  780. (void *)(long)(event->header.size),
  781. event->mmap.pid,
  782. (void *)(long)event->mmap.start,
  783. (void *)(long)event->mmap.len,
  784. (void *)(long)event->mmap.pgoff,
  785. event->mmap.filename);
  786. if (thread == NULL || map == NULL) {
  787. dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
  788. return 0;
  789. }
  790. thread__insert_map(thread, map);
  791. total_mmap++;
  792. return 0;
  793. }
  794. static int
  795. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  796. {
  797. struct thread *thread = threads__findnew(event->comm.pid);
  798. dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  799. (void *)(offset + head),
  800. (void *)(long)(event->header.size),
  801. event->comm.comm, event->comm.pid);
  802. if (thread == NULL ||
  803. thread__set_comm(thread, event->comm.comm)) {
  804. dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
  805. return -1;
  806. }
  807. total_comm++;
  808. return 0;
  809. }
  810. static int
  811. process_fork_event(event_t *event, unsigned long offset, unsigned long head)
  812. {
  813. struct thread *thread = threads__findnew(event->fork.pid);
  814. struct thread *parent = threads__findnew(event->fork.ppid);
  815. dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
  816. (void *)(offset + head),
  817. (void *)(long)(event->header.size),
  818. event->fork.pid, event->fork.ppid);
  819. if (!thread || !parent || thread__fork(thread, parent)) {
  820. dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
  821. return -1;
  822. }
  823. total_fork++;
  824. return 0;
  825. }
  826. static int
  827. process_event(event_t *event, unsigned long offset, unsigned long head)
  828. {
  829. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
  830. return process_overflow_event(event, offset, head);
  831. switch (event->header.type) {
  832. case PERF_EVENT_MMAP:
  833. return process_mmap_event(event, offset, head);
  834. case PERF_EVENT_COMM:
  835. return process_comm_event(event, offset, head);
  836. case PERF_EVENT_FORK:
  837. return process_fork_event(event, offset, head);
  838. /*
  839. * We dont process them right now but they are fine:
  840. */
  841. case PERF_EVENT_PERIOD:
  842. case PERF_EVENT_THROTTLE:
  843. case PERF_EVENT_UNTHROTTLE:
  844. return 0;
  845. default:
  846. return -1;
  847. }
  848. return 0;
  849. }
  850. static int __cmd_report(void)
  851. {
  852. int ret, rc = EXIT_FAILURE;
  853. unsigned long offset = 0;
  854. unsigned long head = 0;
  855. struct stat stat;
  856. event_t *event;
  857. uint32_t size;
  858. char *buf;
  859. register_idle_thread();
  860. input = open(input_name, O_RDONLY);
  861. if (input < 0) {
  862. perror("failed to open file");
  863. exit(-1);
  864. }
  865. ret = fstat(input, &stat);
  866. if (ret < 0) {
  867. perror("failed to stat file");
  868. exit(-1);
  869. }
  870. if (!stat.st_size) {
  871. fprintf(stderr, "zero-sized file, nothing to do!\n");
  872. exit(0);
  873. }
  874. if (load_kernel() < 0) {
  875. perror("failed to load kernel symbols");
  876. return EXIT_FAILURE;
  877. }
  878. if (!full_paths) {
  879. if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
  880. perror("failed to get the current directory");
  881. return EXIT_FAILURE;
  882. }
  883. cwdlen = strlen(cwd);
  884. } else {
  885. cwd = NULL;
  886. cwdlen = 0;
  887. }
  888. remap:
  889. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  890. MAP_SHARED, input, offset);
  891. if (buf == MAP_FAILED) {
  892. perror("failed to mmap file");
  893. exit(-1);
  894. }
  895. more:
  896. event = (event_t *)(buf + head);
  897. size = event->header.size;
  898. if (!size)
  899. size = 8;
  900. if (head + event->header.size >= page_size * mmap_window) {
  901. unsigned long shift = page_size * (head / page_size);
  902. int ret;
  903. ret = munmap(buf, page_size * mmap_window);
  904. assert(ret == 0);
  905. offset += shift;
  906. head -= shift;
  907. goto remap;
  908. }
  909. size = event->header.size;
  910. if (!size || process_event(event, offset, head) < 0) {
  911. dprintf("%p [%p]: skipping unknown header type: %d\n",
  912. (void *)(offset + head),
  913. (void *)(long)(event->header.size),
  914. event->header.type);
  915. total_unknown++;
  916. /*
  917. * assume we lost track of the stream, check alignment, and
  918. * increment a single u64 in the hope to catch on again 'soon'.
  919. */
  920. if (unlikely(head & 7))
  921. head &= ~7ULL;
  922. size = 8;
  923. }
  924. head += size;
  925. if (offset + head < stat.st_size)
  926. goto more;
  927. rc = EXIT_SUCCESS;
  928. close(input);
  929. dprintf(" IP events: %10ld\n", total);
  930. dprintf(" mmap events: %10ld\n", total_mmap);
  931. dprintf(" comm events: %10ld\n", total_comm);
  932. dprintf(" fork events: %10ld\n", total_fork);
  933. dprintf(" unknown events: %10ld\n", total_unknown);
  934. if (dump_trace)
  935. return 0;
  936. if (verbose >= 3)
  937. threads__fprintf(stdout);
  938. if (verbose >= 2)
  939. dsos__fprintf(stdout);
  940. collapse__resort();
  941. output__resort();
  942. output__fprintf(stdout, total);
  943. return rc;
  944. }
  945. static const char * const report_usage[] = {
  946. "perf report [<options>] <command>",
  947. NULL
  948. };
  949. static const struct option options[] = {
  950. OPT_STRING('i', "input", &input_name, "file",
  951. "input file name"),
  952. OPT_BOOLEAN('v', "verbose", &verbose,
  953. "be more verbose (show symbol address, etc)"),
  954. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  955. "dump raw trace in ASCII"),
  956. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  957. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  958. "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
  959. OPT_BOOLEAN('P', "full-paths", &full_paths,
  960. "Don't shorten the pathnames taking into account the cwd"),
  961. OPT_END()
  962. };
  963. static void setup_sorting(void)
  964. {
  965. char *tmp, *tok, *str = strdup(sort_order);
  966. for (tok = strtok_r(str, ", ", &tmp);
  967. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  968. if (sort_dimension__add(tok) < 0) {
  969. error("Unknown --sort key: `%s'", tok);
  970. usage_with_options(report_usage, options);
  971. }
  972. }
  973. free(str);
  974. }
  975. int cmd_report(int argc, const char **argv, const char *prefix)
  976. {
  977. symbol__init();
  978. page_size = getpagesize();
  979. argc = parse_options(argc, argv, options, report_usage, 0);
  980. setup_sorting();
  981. /*
  982. * Any (unrecognized) arguments left?
  983. */
  984. if (argc)
  985. usage_with_options(report_usage, options);
  986. setup_pager();
  987. return __cmd_report();
  988. }