builtin-report.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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/list.h"
  11. #include "util/cache.h"
  12. #include "util/rbtree.h"
  13. #include "util/symbol.h"
  14. #include "util/string.h"
  15. #include "perf.h"
  16. #include "util/parse-options.h"
  17. #include "util/parse-events.h"
  18. #define SHOW_KERNEL 1
  19. #define SHOW_USER 2
  20. #define SHOW_HV 4
  21. static char const *input_name = "perf.data";
  22. static char *vmlinux = NULL;
  23. static char *sort_order = "comm,dso";
  24. static int input;
  25. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  26. static int dump_trace = 0;
  27. static int verbose;
  28. static int full_paths;
  29. static unsigned long page_size;
  30. static unsigned long mmap_window = 32;
  31. const char *perf_event_names[] = {
  32. [PERF_EVENT_MMAP] = " PERF_EVENT_MMAP",
  33. [PERF_EVENT_MUNMAP] = " PERF_EVENT_MUNMAP",
  34. [PERF_EVENT_COMM] = " PERF_EVENT_COMM",
  35. };
  36. struct ip_event {
  37. struct perf_event_header header;
  38. __u64 ip;
  39. __u32 pid, tid;
  40. };
  41. struct mmap_event {
  42. struct perf_event_header header;
  43. __u32 pid, tid;
  44. __u64 start;
  45. __u64 len;
  46. __u64 pgoff;
  47. char filename[PATH_MAX];
  48. };
  49. struct comm_event {
  50. struct perf_event_header header;
  51. __u32 pid,tid;
  52. char comm[16];
  53. };
  54. typedef union event_union {
  55. struct perf_event_header header;
  56. struct ip_event ip;
  57. struct mmap_event mmap;
  58. struct comm_event comm;
  59. } event_t;
  60. static LIST_HEAD(dsos);
  61. static struct dso *kernel_dso;
  62. static void dsos__add(struct dso *dso)
  63. {
  64. list_add_tail(&dso->node, &dsos);
  65. }
  66. static struct dso *dsos__find(const char *name)
  67. {
  68. struct dso *pos;
  69. list_for_each_entry(pos, &dsos, node)
  70. if (strcmp(pos->name, name) == 0)
  71. return pos;
  72. return NULL;
  73. }
  74. static struct dso *dsos__findnew(const char *name)
  75. {
  76. struct dso *dso = dsos__find(name);
  77. int nr;
  78. if (dso)
  79. return dso;
  80. dso = dso__new(name, 0);
  81. if (!dso)
  82. goto out_delete_dso;
  83. nr = dso__load(dso, NULL);
  84. if (nr < 0) {
  85. fprintf(stderr, "Failed to open: %s\n", name);
  86. goto out_delete_dso;
  87. }
  88. if (!nr && verbose) {
  89. fprintf(stderr,
  90. "No symbols found in: %s, maybe install a debug package?\n",
  91. name);
  92. }
  93. dsos__add(dso);
  94. return dso;
  95. out_delete_dso:
  96. dso__delete(dso);
  97. return NULL;
  98. }
  99. static void dsos__fprintf(FILE *fp)
  100. {
  101. struct dso *pos;
  102. list_for_each_entry(pos, &dsos, node)
  103. dso__fprintf(pos, fp);
  104. }
  105. static int load_kernel(void)
  106. {
  107. int err;
  108. kernel_dso = dso__new("[kernel]", 0);
  109. if (!kernel_dso)
  110. return -1;
  111. err = dso__load_kernel(kernel_dso, vmlinux, NULL);
  112. if (err) {
  113. dso__delete(kernel_dso);
  114. kernel_dso = NULL;
  115. } else
  116. dsos__add(kernel_dso);
  117. return err;
  118. }
  119. static int strcommon(const char *pathname, const char *cwd, int cwdlen)
  120. {
  121. int n = 0;
  122. while (pathname[n] == cwd[n] && n < cwdlen)
  123. ++n;
  124. return n;
  125. }
  126. struct map {
  127. struct list_head node;
  128. uint64_t start;
  129. uint64_t end;
  130. uint64_t pgoff;
  131. struct dso *dso;
  132. };
  133. static struct map *map__new(struct mmap_event *event, char *cwd, int cwdlen)
  134. {
  135. struct map *self = malloc(sizeof(*self));
  136. if (self != NULL) {
  137. const char *filename = event->filename;
  138. char newfilename[PATH_MAX];
  139. if (cwd) {
  140. int n = strcommon(filename, cwd, cwdlen);
  141. if (n == cwdlen) {
  142. snprintf(newfilename, sizeof(newfilename),
  143. ".%s", filename + n);
  144. filename = newfilename;
  145. }
  146. }
  147. self->start = event->start;
  148. self->end = event->start + event->len;
  149. self->pgoff = event->pgoff;
  150. self->dso = dsos__findnew(filename);
  151. if (self->dso == NULL)
  152. goto out_delete;
  153. }
  154. return self;
  155. out_delete:
  156. free(self);
  157. return NULL;
  158. }
  159. struct thread;
  160. struct thread {
  161. struct rb_node rb_node;
  162. struct list_head maps;
  163. pid_t pid;
  164. char *comm;
  165. };
  166. static struct thread *thread__new(pid_t pid)
  167. {
  168. struct thread *self = malloc(sizeof(*self));
  169. if (self != NULL) {
  170. self->pid = pid;
  171. self->comm = malloc(30);
  172. if (self->comm)
  173. sprintf(self->comm, ":%d", pid);
  174. INIT_LIST_HEAD(&self->maps);
  175. }
  176. return self;
  177. }
  178. static int thread__set_comm(struct thread *self, const char *comm)
  179. {
  180. self->comm = strdup(comm);
  181. return self->comm ? 0 : -ENOMEM;
  182. }
  183. static struct rb_root threads;
  184. static struct thread *threads__findnew(pid_t pid)
  185. {
  186. struct rb_node **p = &threads.rb_node;
  187. struct rb_node *parent = NULL;
  188. struct thread *th;
  189. while (*p != NULL) {
  190. parent = *p;
  191. th = rb_entry(parent, struct thread, rb_node);
  192. if (th->pid == pid)
  193. return th;
  194. if (pid < th->pid)
  195. p = &(*p)->rb_left;
  196. else
  197. p = &(*p)->rb_right;
  198. }
  199. th = thread__new(pid);
  200. if (th != NULL) {
  201. rb_link_node(&th->rb_node, parent, p);
  202. rb_insert_color(&th->rb_node, &threads);
  203. }
  204. return th;
  205. }
  206. static void thread__insert_map(struct thread *self, struct map *map)
  207. {
  208. list_add_tail(&map->node, &self->maps);
  209. }
  210. static struct map *thread__find_map(struct thread *self, uint64_t ip)
  211. {
  212. struct map *pos;
  213. if (self == NULL)
  214. return NULL;
  215. list_for_each_entry(pos, &self->maps, node)
  216. if (ip >= pos->start && ip <= pos->end)
  217. return pos;
  218. return NULL;
  219. }
  220. /*
  221. * histogram, sorted on item, collects counts
  222. */
  223. static struct rb_root hist;
  224. struct hist_entry {
  225. struct rb_node rb_node;
  226. struct thread *thread;
  227. struct map *map;
  228. struct dso *dso;
  229. struct symbol *sym;
  230. uint64_t ip;
  231. char level;
  232. uint32_t count;
  233. };
  234. /*
  235. * configurable sorting bits
  236. */
  237. struct sort_entry {
  238. struct list_head list;
  239. char *header;
  240. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  241. size_t (*print)(FILE *fp, struct hist_entry *);
  242. };
  243. static int64_t
  244. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  245. {
  246. return right->thread->pid - left->thread->pid;
  247. }
  248. static size_t
  249. sort__thread_print(FILE *fp, struct hist_entry *self)
  250. {
  251. return fprintf(fp, " %16s:%5d", self->thread->comm ?: "", self->thread->pid);
  252. }
  253. static struct sort_entry sort_thread = {
  254. .header = " Command: Pid ",
  255. .cmp = sort__thread_cmp,
  256. .print = sort__thread_print,
  257. };
  258. static int64_t
  259. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  260. {
  261. char *comm_l = left->thread->comm;
  262. char *comm_r = right->thread->comm;
  263. if (!comm_l || !comm_r) {
  264. if (!comm_l && !comm_r)
  265. return 0;
  266. else if (!comm_l)
  267. return -1;
  268. else
  269. return 1;
  270. }
  271. return strcmp(comm_l, comm_r);
  272. }
  273. static size_t
  274. sort__comm_print(FILE *fp, struct hist_entry *self)
  275. {
  276. return fprintf(fp, " %16s", self->thread->comm);
  277. }
  278. static struct sort_entry sort_comm = {
  279. .header = " Command",
  280. .cmp = sort__comm_cmp,
  281. .print = sort__comm_print,
  282. };
  283. static int64_t
  284. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  285. {
  286. struct dso *dso_l = left->dso;
  287. struct dso *dso_r = right->dso;
  288. if (!dso_l || !dso_r) {
  289. if (!dso_l && !dso_r)
  290. return 0;
  291. else if (!dso_l)
  292. return -1;
  293. else
  294. return 1;
  295. }
  296. return strcmp(dso_l->name, dso_r->name);
  297. }
  298. static size_t
  299. sort__dso_print(FILE *fp, struct hist_entry *self)
  300. {
  301. if (self->dso)
  302. return fprintf(fp, " %-25s", self->dso->name);
  303. return fprintf(fp, " %016llx", (__u64)self->ip);
  304. }
  305. static struct sort_entry sort_dso = {
  306. .header = " Shared Object ",
  307. .cmp = sort__dso_cmp,
  308. .print = sort__dso_print,
  309. };
  310. static int64_t
  311. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  312. {
  313. uint64_t ip_l, ip_r;
  314. if (left->sym == right->sym)
  315. return 0;
  316. ip_l = left->sym ? left->sym->start : left->ip;
  317. ip_r = right->sym ? right->sym->start : right->ip;
  318. return (int64_t)(ip_r - ip_l);
  319. }
  320. static size_t
  321. sort__sym_print(FILE *fp, struct hist_entry *self)
  322. {
  323. size_t ret = 0;
  324. if (verbose)
  325. ret += fprintf(fp, " %#018llx", (__u64)self->ip);
  326. if (self->dso)
  327. ret += fprintf(fp, " %s: ", self->dso->name);
  328. else
  329. ret += fprintf(fp, " %#016llx: ", (__u64)self->ip);
  330. if (self->sym)
  331. ret += fprintf(fp, "%s", self->sym->name);
  332. else
  333. ret += fprintf(fp, "%#016llx", (__u64)self->ip);
  334. return ret;
  335. }
  336. static struct sort_entry sort_sym = {
  337. .header = " Shared Object: Symbol",
  338. .cmp = sort__sym_cmp,
  339. .print = sort__sym_print,
  340. };
  341. struct sort_dimension {
  342. char *name;
  343. struct sort_entry *entry;
  344. int taken;
  345. };
  346. static struct sort_dimension sort_dimensions[] = {
  347. { .name = "pid", .entry = &sort_thread, },
  348. { .name = "comm", .entry = &sort_comm, },
  349. { .name = "dso", .entry = &sort_dso, },
  350. { .name = "symbol", .entry = &sort_sym, },
  351. };
  352. static LIST_HEAD(hist_entry__sort_list);
  353. static int sort_dimension__add(char *tok)
  354. {
  355. int i;
  356. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  357. struct sort_dimension *sd = &sort_dimensions[i];
  358. if (sd->taken)
  359. continue;
  360. if (strcmp(tok, sd->name))
  361. continue;
  362. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  363. sd->taken = 1;
  364. return 0;
  365. }
  366. return -ESRCH;
  367. }
  368. static void setup_sorting(void)
  369. {
  370. char *tmp, *tok, *str = strdup(sort_order);
  371. for (tok = strtok_r(str, ", ", &tmp);
  372. tok; tok = strtok_r(NULL, ", ", &tmp))
  373. sort_dimension__add(tok);
  374. free(str);
  375. }
  376. static int64_t
  377. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  378. {
  379. struct sort_entry *se;
  380. int64_t cmp = 0;
  381. list_for_each_entry(se, &hist_entry__sort_list, list) {
  382. cmp = se->cmp(left, right);
  383. if (cmp)
  384. break;
  385. }
  386. return cmp;
  387. }
  388. static size_t
  389. hist_entry__fprintf(FILE *fp, struct hist_entry *self, uint64_t total_samples)
  390. {
  391. struct sort_entry *se;
  392. size_t ret;
  393. if (total_samples) {
  394. ret = fprintf(fp, " %5.2f%%",
  395. (self->count * 100.0) / total_samples);
  396. } else
  397. ret = fprintf(fp, "%12d ", self->count);
  398. list_for_each_entry(se, &hist_entry__sort_list, list)
  399. ret += se->print(fp, self);
  400. ret += fprintf(fp, "\n");
  401. return ret;
  402. }
  403. /*
  404. * collect histogram counts
  405. */
  406. static int
  407. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  408. struct symbol *sym, uint64_t ip, char level)
  409. {
  410. struct rb_node **p = &hist.rb_node;
  411. struct rb_node *parent = NULL;
  412. struct hist_entry *he;
  413. struct hist_entry entry = {
  414. .thread = thread,
  415. .map = map,
  416. .dso = dso,
  417. .sym = sym,
  418. .ip = ip,
  419. .level = level,
  420. .count = 1,
  421. };
  422. int cmp;
  423. while (*p != NULL) {
  424. parent = *p;
  425. he = rb_entry(parent, struct hist_entry, rb_node);
  426. cmp = hist_entry__cmp(&entry, he);
  427. if (!cmp) {
  428. he->count++;
  429. return 0;
  430. }
  431. if (cmp < 0)
  432. p = &(*p)->rb_left;
  433. else
  434. p = &(*p)->rb_right;
  435. }
  436. he = malloc(sizeof(*he));
  437. if (!he)
  438. return -ENOMEM;
  439. *he = entry;
  440. rb_link_node(&he->rb_node, parent, p);
  441. rb_insert_color(&he->rb_node, &hist);
  442. return 0;
  443. }
  444. /*
  445. * reverse the map, sort on count.
  446. */
  447. static struct rb_root output_hists;
  448. static void output__insert_entry(struct hist_entry *he)
  449. {
  450. struct rb_node **p = &output_hists.rb_node;
  451. struct rb_node *parent = NULL;
  452. struct hist_entry *iter;
  453. while (*p != NULL) {
  454. parent = *p;
  455. iter = rb_entry(parent, struct hist_entry, rb_node);
  456. if (he->count > iter->count)
  457. p = &(*p)->rb_left;
  458. else
  459. p = &(*p)->rb_right;
  460. }
  461. rb_link_node(&he->rb_node, parent, p);
  462. rb_insert_color(&he->rb_node, &output_hists);
  463. }
  464. static void output__resort(void)
  465. {
  466. struct rb_node *next = rb_first(&hist);
  467. struct hist_entry *n;
  468. while (next) {
  469. n = rb_entry(next, struct hist_entry, rb_node);
  470. next = rb_next(&n->rb_node);
  471. rb_erase(&n->rb_node, &hist);
  472. output__insert_entry(n);
  473. }
  474. }
  475. static size_t output__fprintf(FILE *fp, uint64_t total_samples)
  476. {
  477. struct hist_entry *pos;
  478. struct sort_entry *se;
  479. struct rb_node *nd;
  480. size_t ret = 0;
  481. fprintf(fp, "#\n");
  482. fprintf(fp, "# Overhead");
  483. list_for_each_entry(se, &hist_entry__sort_list, list)
  484. fprintf(fp, " %s", se->header);
  485. fprintf(fp, "\n");
  486. fprintf(fp, "# ........");
  487. list_for_each_entry(se, &hist_entry__sort_list, list) {
  488. int i;
  489. fprintf(fp, " ");
  490. for (i = 0; i < strlen(se->header)-1; i++)
  491. fprintf(fp, ".");
  492. }
  493. fprintf(fp, "\n");
  494. fprintf(fp, "#\n");
  495. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  496. pos = rb_entry(nd, struct hist_entry, rb_node);
  497. ret += hist_entry__fprintf(fp, pos, total_samples);
  498. }
  499. return ret;
  500. }
  501. static void register_idle_thread(void)
  502. {
  503. struct thread *thread = threads__findnew(0);
  504. if (thread == NULL ||
  505. thread__set_comm(thread, "[idle]")) {
  506. fprintf(stderr, "problem inserting idle task.\n");
  507. exit(-1);
  508. }
  509. }
  510. static int __cmd_report(void)
  511. {
  512. unsigned long offset = 0;
  513. unsigned long head = 0;
  514. struct stat stat;
  515. char *buf;
  516. event_t *event;
  517. int ret, rc = EXIT_FAILURE;
  518. uint32_t size;
  519. unsigned long total = 0, total_mmap = 0, total_comm = 0, total_unknown = 0;
  520. char cwd[PATH_MAX], *cwdp = cwd;
  521. int cwdlen;
  522. register_idle_thread();
  523. input = open(input_name, O_RDONLY);
  524. if (input < 0) {
  525. perror("failed to open file");
  526. exit(-1);
  527. }
  528. ret = fstat(input, &stat);
  529. if (ret < 0) {
  530. perror("failed to stat file");
  531. exit(-1);
  532. }
  533. if (!stat.st_size) {
  534. fprintf(stderr, "zero-sized file, nothing to do!\n");
  535. exit(0);
  536. }
  537. if (load_kernel() < 0) {
  538. perror("failed to load kernel symbols");
  539. return EXIT_FAILURE;
  540. }
  541. if (!full_paths) {
  542. if (getcwd(cwd, sizeof(cwd)) == NULL) {
  543. perror("failed to get the current directory");
  544. return EXIT_FAILURE;
  545. }
  546. cwdlen = strlen(cwd);
  547. } else {
  548. cwdp = NULL;
  549. cwdlen = 0;
  550. }
  551. remap:
  552. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  553. MAP_SHARED, input, offset);
  554. if (buf == MAP_FAILED) {
  555. perror("failed to mmap file");
  556. exit(-1);
  557. }
  558. more:
  559. event = (event_t *)(buf + head);
  560. size = event->header.size;
  561. if (!size)
  562. size = 8;
  563. if (head + event->header.size >= page_size * mmap_window) {
  564. unsigned long shift = page_size * (head / page_size);
  565. int ret;
  566. ret = munmap(buf, page_size * mmap_window);
  567. assert(ret == 0);
  568. offset += shift;
  569. head -= shift;
  570. goto remap;
  571. }
  572. size = event->header.size;
  573. if (!size)
  574. goto broken_event;
  575. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW) {
  576. char level;
  577. int show = 0;
  578. struct dso *dso = NULL;
  579. struct thread *thread = threads__findnew(event->ip.pid);
  580. uint64_t ip = event->ip.ip;
  581. struct map *map = NULL;
  582. if (dump_trace) {
  583. fprintf(stderr, "%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
  584. (void *)(offset + head),
  585. (void *)(long)(event->header.size),
  586. event->header.misc,
  587. event->ip.pid,
  588. (void *)(long)ip);
  589. }
  590. if (thread == NULL) {
  591. fprintf(stderr, "problem processing %d event, skipping it.\n",
  592. event->header.type);
  593. goto broken_event;
  594. }
  595. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  596. show = SHOW_KERNEL;
  597. level = 'k';
  598. dso = kernel_dso;
  599. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  600. show = SHOW_USER;
  601. level = '.';
  602. map = thread__find_map(thread, ip);
  603. if (map != NULL) {
  604. dso = map->dso;
  605. ip -= map->start + map->pgoff;
  606. }
  607. } else {
  608. show = SHOW_HV;
  609. level = 'H';
  610. }
  611. if (show & show_mask) {
  612. struct symbol *sym = dso__find_symbol(dso, ip);
  613. if (hist_entry__add(thread, map, dso, sym, ip, level)) {
  614. fprintf(stderr,
  615. "problem incrementing symbol count, skipping event\n");
  616. goto broken_event;
  617. }
  618. }
  619. total++;
  620. } else switch (event->header.type) {
  621. case PERF_EVENT_MMAP: {
  622. struct thread *thread = threads__findnew(event->mmap.pid);
  623. struct map *map = map__new(&event->mmap, cwdp, cwdlen);
  624. if (dump_trace) {
  625. fprintf(stderr, "%p [%p]: PERF_EVENT_MMAP: [%p(%p) @ %p]: %s\n",
  626. (void *)(offset + head),
  627. (void *)(long)(event->header.size),
  628. (void *)(long)event->mmap.start,
  629. (void *)(long)event->mmap.len,
  630. (void *)(long)event->mmap.pgoff,
  631. event->mmap.filename);
  632. }
  633. if (thread == NULL || map == NULL) {
  634. if (verbose)
  635. fprintf(stderr, "problem processing PERF_EVENT_MMAP, skipping event.\n");
  636. goto broken_event;
  637. }
  638. thread__insert_map(thread, map);
  639. total_mmap++;
  640. break;
  641. }
  642. case PERF_EVENT_COMM: {
  643. struct thread *thread = threads__findnew(event->comm.pid);
  644. if (dump_trace) {
  645. fprintf(stderr, "%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  646. (void *)(offset + head),
  647. (void *)(long)(event->header.size),
  648. event->comm.comm, event->comm.pid);
  649. }
  650. if (thread == NULL ||
  651. thread__set_comm(thread, event->comm.comm)) {
  652. fprintf(stderr, "problem processing PERF_EVENT_COMM, skipping event.\n");
  653. goto broken_event;
  654. }
  655. total_comm++;
  656. break;
  657. }
  658. default: {
  659. broken_event:
  660. if (dump_trace)
  661. fprintf(stderr, "%p [%p]: skipping unknown header type: %d\n",
  662. (void *)(offset + head),
  663. (void *)(long)(event->header.size),
  664. event->header.type);
  665. total_unknown++;
  666. /*
  667. * assume we lost track of the stream, check alignment, and
  668. * increment a single u64 in the hope to catch on again 'soon'.
  669. */
  670. if (unlikely(head & 7))
  671. head &= ~7ULL;
  672. size = 8;
  673. }
  674. }
  675. head += size;
  676. if (offset + head < stat.st_size)
  677. goto more;
  678. rc = EXIT_SUCCESS;
  679. close(input);
  680. if (dump_trace) {
  681. fprintf(stderr, " IP events: %10ld\n", total);
  682. fprintf(stderr, " mmap events: %10ld\n", total_mmap);
  683. fprintf(stderr, " comm events: %10ld\n", total_comm);
  684. fprintf(stderr, " unknown events: %10ld\n", total_unknown);
  685. return 0;
  686. }
  687. if (verbose >= 2)
  688. dsos__fprintf(stdout);
  689. output__resort();
  690. output__fprintf(stdout, total);
  691. return rc;
  692. }
  693. static const char * const report_usage[] = {
  694. "perf report [<options>] <command>",
  695. NULL
  696. };
  697. static const struct option options[] = {
  698. OPT_STRING('i', "input", &input_name, "file",
  699. "input file name"),
  700. OPT_BOOLEAN('v', "verbose", &verbose,
  701. "be more verbose (show symbol address, etc)"),
  702. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  703. "dump raw trace in ASCII"),
  704. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  705. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  706. "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
  707. OPT_BOOLEAN('P', "full-paths", &full_paths,
  708. "Don't shorten the pathnames taking into account the cwd"),
  709. OPT_END()
  710. };
  711. int cmd_report(int argc, const char **argv, const char *prefix)
  712. {
  713. symbol__init();
  714. page_size = getpagesize();
  715. parse_options(argc, argv, options, report_usage, 0);
  716. setup_sorting();
  717. setup_pager();
  718. return __cmd_report();
  719. }