builtin-report.c 20 KB

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