builtin-report.c 24 KB

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