builtin-report.c 27 KB

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