builtin-report.c 21 KB

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