builtin-report.c 21 KB

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