builtin-report.c 27 KB

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