builtin-report.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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. #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
  31. static int verbose;
  32. static int full_paths;
  33. static unsigned long page_size;
  34. static unsigned long mmap_window = 32;
  35. static char *call = "^sys_";
  36. static regex_t call_regex;
  37. struct ip_chain_event {
  38. __u16 nr;
  39. __u16 hv;
  40. __u16 kernel;
  41. __u16 user;
  42. __u64 ips[];
  43. };
  44. struct ip_event {
  45. struct perf_event_header header;
  46. __u64 ip;
  47. __u32 pid, tid;
  48. unsigned char __more_data[];
  49. };
  50. struct mmap_event {
  51. struct perf_event_header header;
  52. __u32 pid, tid;
  53. __u64 start;
  54. __u64 len;
  55. __u64 pgoff;
  56. char filename[PATH_MAX];
  57. };
  58. struct comm_event {
  59. struct perf_event_header header;
  60. __u32 pid, tid;
  61. char comm[16];
  62. };
  63. struct fork_event {
  64. struct perf_event_header header;
  65. __u32 pid, ppid;
  66. };
  67. struct period_event {
  68. struct perf_event_header header;
  69. __u64 time;
  70. __u64 id;
  71. __u64 sample_period;
  72. };
  73. typedef union event_union {
  74. struct perf_event_header header;
  75. struct ip_event ip;
  76. struct mmap_event mmap;
  77. struct comm_event comm;
  78. struct fork_event fork;
  79. struct period_event period;
  80. } event_t;
  81. static LIST_HEAD(dsos);
  82. static struct dso *kernel_dso;
  83. static struct dso *vdso;
  84. static void dsos__add(struct dso *dso)
  85. {
  86. list_add_tail(&dso->node, &dsos);
  87. }
  88. static struct dso *dsos__find(const char *name)
  89. {
  90. struct dso *pos;
  91. list_for_each_entry(pos, &dsos, node)
  92. if (strcmp(pos->name, name) == 0)
  93. return pos;
  94. return NULL;
  95. }
  96. static struct dso *dsos__findnew(const char *name)
  97. {
  98. struct dso *dso = dsos__find(name);
  99. int nr;
  100. if (dso)
  101. return dso;
  102. dso = dso__new(name, 0);
  103. if (!dso)
  104. goto out_delete_dso;
  105. nr = dso__load(dso, NULL, verbose);
  106. if (nr < 0) {
  107. if (verbose)
  108. fprintf(stderr, "Failed to open: %s\n", name);
  109. goto out_delete_dso;
  110. }
  111. if (!nr && verbose) {
  112. fprintf(stderr,
  113. "No symbols found in: %s, maybe install a debug package?\n",
  114. name);
  115. }
  116. dsos__add(dso);
  117. return dso;
  118. out_delete_dso:
  119. dso__delete(dso);
  120. return NULL;
  121. }
  122. static void dsos__fprintf(FILE *fp)
  123. {
  124. struct dso *pos;
  125. list_for_each_entry(pos, &dsos, node)
  126. dso__fprintf(pos, fp);
  127. }
  128. static struct symbol *vdso__find_symbol(struct dso *dso, __u64 ip)
  129. {
  130. return dso__find_symbol(kernel_dso, ip);
  131. }
  132. static int load_kernel(void)
  133. {
  134. int err;
  135. kernel_dso = dso__new("[kernel]", 0);
  136. if (!kernel_dso)
  137. return -1;
  138. err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose);
  139. if (err) {
  140. dso__delete(kernel_dso);
  141. kernel_dso = NULL;
  142. } else
  143. dsos__add(kernel_dso);
  144. vdso = dso__new("[vdso]", 0);
  145. if (!vdso)
  146. return -1;
  147. vdso->find_symbol = vdso__find_symbol;
  148. dsos__add(vdso);
  149. return err;
  150. }
  151. static char __cwd[PATH_MAX];
  152. static char *cwd = __cwd;
  153. static int cwdlen;
  154. static int strcommon(const char *pathname)
  155. {
  156. int n = 0;
  157. while (pathname[n] == cwd[n] && n < cwdlen)
  158. ++n;
  159. return n;
  160. }
  161. struct map {
  162. struct list_head node;
  163. __u64 start;
  164. __u64 end;
  165. __u64 pgoff;
  166. __u64 (*map_ip)(struct map *, __u64);
  167. struct dso *dso;
  168. };
  169. static __u64 map__map_ip(struct map *map, __u64 ip)
  170. {
  171. return ip - map->start + map->pgoff;
  172. }
  173. static __u64 vdso__map_ip(struct map *map, __u64 ip)
  174. {
  175. return ip;
  176. }
  177. static inline int is_anon_memory(const char *filename)
  178. {
  179. return strcmp(filename, "//anon") == 0;
  180. }
  181. static struct map *map__new(struct mmap_event *event)
  182. {
  183. struct map *self = malloc(sizeof(*self));
  184. if (self != NULL) {
  185. const char *filename = event->filename;
  186. char newfilename[PATH_MAX];
  187. int anon;
  188. if (cwd) {
  189. int n = strcommon(filename);
  190. if (n == cwdlen) {
  191. snprintf(newfilename, sizeof(newfilename),
  192. ".%s", filename + n);
  193. filename = newfilename;
  194. }
  195. }
  196. anon = is_anon_memory(filename);
  197. if (anon) {
  198. snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
  199. filename = newfilename;
  200. }
  201. self->start = event->start;
  202. self->end = event->start + event->len;
  203. self->pgoff = event->pgoff;
  204. self->dso = dsos__findnew(filename);
  205. if (self->dso == NULL)
  206. goto out_delete;
  207. if (self->dso == vdso || anon)
  208. self->map_ip = vdso__map_ip;
  209. else
  210. self->map_ip = map__map_ip;
  211. }
  212. return self;
  213. out_delete:
  214. free(self);
  215. return NULL;
  216. }
  217. static struct map *map__clone(struct map *self)
  218. {
  219. struct map *map = malloc(sizeof(*self));
  220. if (!map)
  221. return NULL;
  222. memcpy(map, self, sizeof(*self));
  223. return map;
  224. }
  225. static int map__overlap(struct map *l, struct map *r)
  226. {
  227. if (l->start > r->start) {
  228. struct map *t = l;
  229. l = r;
  230. r = t;
  231. }
  232. if (l->end > r->start)
  233. return 1;
  234. return 0;
  235. }
  236. static size_t map__fprintf(struct map *self, FILE *fp)
  237. {
  238. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  239. self->start, self->end, self->pgoff, self->dso->name);
  240. }
  241. struct thread {
  242. struct rb_node rb_node;
  243. struct list_head maps;
  244. pid_t pid;
  245. char *comm;
  246. };
  247. static struct thread *thread__new(pid_t pid)
  248. {
  249. struct thread *self = malloc(sizeof(*self));
  250. if (self != NULL) {
  251. self->pid = pid;
  252. self->comm = malloc(32);
  253. if (self->comm)
  254. snprintf(self->comm, 32, ":%d", self->pid);
  255. INIT_LIST_HEAD(&self->maps);
  256. }
  257. return self;
  258. }
  259. static int thread__set_comm(struct thread *self, const char *comm)
  260. {
  261. if (self->comm)
  262. free(self->comm);
  263. self->comm = strdup(comm);
  264. return self->comm ? 0 : -ENOMEM;
  265. }
  266. static size_t thread__fprintf(struct thread *self, FILE *fp)
  267. {
  268. struct map *pos;
  269. size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
  270. list_for_each_entry(pos, &self->maps, node)
  271. ret += map__fprintf(pos, fp);
  272. return ret;
  273. }
  274. static struct rb_root threads;
  275. static struct thread *last_match;
  276. static struct thread *threads__findnew(pid_t pid)
  277. {
  278. struct rb_node **p = &threads.rb_node;
  279. struct rb_node *parent = NULL;
  280. struct thread *th;
  281. /*
  282. * Font-end cache - PID lookups come in blocks,
  283. * so most of the time we dont have to look up
  284. * the full rbtree:
  285. */
  286. if (last_match && last_match->pid == pid)
  287. return last_match;
  288. while (*p != NULL) {
  289. parent = *p;
  290. th = rb_entry(parent, struct thread, rb_node);
  291. if (th->pid == pid) {
  292. last_match = th;
  293. return th;
  294. }
  295. if (pid < th->pid)
  296. p = &(*p)->rb_left;
  297. else
  298. p = &(*p)->rb_right;
  299. }
  300. th = thread__new(pid);
  301. if (th != NULL) {
  302. rb_link_node(&th->rb_node, parent, p);
  303. rb_insert_color(&th->rb_node, &threads);
  304. last_match = th;
  305. }
  306. return th;
  307. }
  308. static void thread__insert_map(struct thread *self, struct map *map)
  309. {
  310. struct map *pos, *tmp;
  311. list_for_each_entry_safe(pos, tmp, &self->maps, node) {
  312. if (map__overlap(pos, map)) {
  313. list_del_init(&pos->node);
  314. /* XXX leaks dsos */
  315. free(pos);
  316. }
  317. }
  318. list_add_tail(&map->node, &self->maps);
  319. }
  320. static int thread__fork(struct thread *self, struct thread *parent)
  321. {
  322. struct map *map;
  323. if (self->comm)
  324. free(self->comm);
  325. self->comm = strdup(parent->comm);
  326. if (!self->comm)
  327. return -ENOMEM;
  328. list_for_each_entry(map, &parent->maps, node) {
  329. struct map *new = map__clone(map);
  330. if (!new)
  331. return -ENOMEM;
  332. thread__insert_map(self, new);
  333. }
  334. return 0;
  335. }
  336. static struct map *thread__find_map(struct thread *self, __u64 ip)
  337. {
  338. struct map *pos;
  339. if (self == NULL)
  340. return NULL;
  341. list_for_each_entry(pos, &self->maps, node)
  342. if (ip >= pos->start && ip <= pos->end)
  343. return pos;
  344. return NULL;
  345. }
  346. static size_t threads__fprintf(FILE *fp)
  347. {
  348. size_t ret = 0;
  349. struct rb_node *nd;
  350. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  351. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  352. ret += thread__fprintf(pos, fp);
  353. }
  354. return ret;
  355. }
  356. /*
  357. * histogram, sorted on item, collects counts
  358. */
  359. static struct rb_root hist;
  360. struct hist_entry {
  361. struct rb_node rb_node;
  362. struct thread *thread;
  363. struct map *map;
  364. struct dso *dso;
  365. struct symbol *sym;
  366. struct symbol *call;
  367. __u64 ip;
  368. char level;
  369. __u64 count;
  370. };
  371. /*
  372. * configurable sorting bits
  373. */
  374. struct sort_entry {
  375. struct list_head list;
  376. char *header;
  377. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  378. int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
  379. size_t (*print)(FILE *fp, struct hist_entry *);
  380. };
  381. static int64_t cmp_null(void *l, void *r)
  382. {
  383. if (!l && !r)
  384. return 0;
  385. else if (!l)
  386. return -1;
  387. else
  388. return 1;
  389. }
  390. /* --sort pid */
  391. static int64_t
  392. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  393. {
  394. return right->thread->pid - left->thread->pid;
  395. }
  396. static size_t
  397. sort__thread_print(FILE *fp, struct hist_entry *self)
  398. {
  399. return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
  400. }
  401. static struct sort_entry sort_thread = {
  402. .header = " Command: Pid",
  403. .cmp = sort__thread_cmp,
  404. .print = sort__thread_print,
  405. };
  406. /* --sort comm */
  407. static int64_t
  408. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  409. {
  410. return right->thread->pid - left->thread->pid;
  411. }
  412. static int64_t
  413. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  414. {
  415. char *comm_l = left->thread->comm;
  416. char *comm_r = right->thread->comm;
  417. if (!comm_l || !comm_r)
  418. return cmp_null(comm_l, comm_r);
  419. return strcmp(comm_l, comm_r);
  420. }
  421. static size_t
  422. sort__comm_print(FILE *fp, struct hist_entry *self)
  423. {
  424. return fprintf(fp, "%16s", self->thread->comm);
  425. }
  426. static struct sort_entry sort_comm = {
  427. .header = " Command",
  428. .cmp = sort__comm_cmp,
  429. .collapse = sort__comm_collapse,
  430. .print = sort__comm_print,
  431. };
  432. /* --sort dso */
  433. static int64_t
  434. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  435. {
  436. struct dso *dso_l = left->dso;
  437. struct dso *dso_r = right->dso;
  438. if (!dso_l || !dso_r)
  439. return cmp_null(dso_l, dso_r);
  440. return strcmp(dso_l->name, dso_r->name);
  441. }
  442. static size_t
  443. sort__dso_print(FILE *fp, struct hist_entry *self)
  444. {
  445. if (self->dso)
  446. return fprintf(fp, "%-25s", self->dso->name);
  447. return fprintf(fp, "%016llx ", (__u64)self->ip);
  448. }
  449. static struct sort_entry sort_dso = {
  450. .header = "Shared Object ",
  451. .cmp = sort__dso_cmp,
  452. .print = sort__dso_print,
  453. };
  454. /* --sort symbol */
  455. static int64_t
  456. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  457. {
  458. __u64 ip_l, ip_r;
  459. if (left->sym == right->sym)
  460. return 0;
  461. ip_l = left->sym ? left->sym->start : left->ip;
  462. ip_r = right->sym ? right->sym->start : right->ip;
  463. return (int64_t)(ip_r - ip_l);
  464. }
  465. static size_t
  466. sort__sym_print(FILE *fp, struct hist_entry *self)
  467. {
  468. size_t ret = 0;
  469. if (verbose)
  470. ret += fprintf(fp, "%#018llx ", (__u64)self->ip);
  471. if (self->sym) {
  472. ret += fprintf(fp, "[%c] %s",
  473. self->dso == kernel_dso ? 'k' : '.', self->sym->name);
  474. } else {
  475. ret += fprintf(fp, "%#016llx", (__u64)self->ip);
  476. }
  477. return ret;
  478. }
  479. static struct sort_entry sort_sym = {
  480. .header = "Symbol",
  481. .cmp = sort__sym_cmp,
  482. .print = sort__sym_print,
  483. };
  484. /* --sort call */
  485. static int64_t
  486. sort__call_cmp(struct hist_entry *left, struct hist_entry *right)
  487. {
  488. struct symbol *sym_l = left->call;
  489. struct symbol *sym_r = right->call;
  490. if (!sym_l || !sym_r)
  491. return cmp_null(sym_l, sym_r);
  492. return strcmp(sym_l->name, sym_r->name);
  493. }
  494. static size_t
  495. sort__call_print(FILE *fp, struct hist_entry *self)
  496. {
  497. size_t ret = 0;
  498. ret += fprintf(fp, "%-20s", self->call ? self->call->name : "[unmatched]");
  499. return ret;
  500. }
  501. static struct sort_entry sort_call = {
  502. .header = "Callchain symbol ",
  503. .cmp = sort__call_cmp,
  504. .print = sort__call_print,
  505. };
  506. static int sort__need_collapse = 0;
  507. static int sort__has_call = 0;
  508. struct sort_dimension {
  509. char *name;
  510. struct sort_entry *entry;
  511. int taken;
  512. };
  513. static struct sort_dimension sort_dimensions[] = {
  514. { .name = "pid", .entry = &sort_thread, },
  515. { .name = "comm", .entry = &sort_comm, },
  516. { .name = "dso", .entry = &sort_dso, },
  517. { .name = "symbol", .entry = &sort_sym, },
  518. { .name = "call", .entry = &sort_call, },
  519. };
  520. static LIST_HEAD(hist_entry__sort_list);
  521. static int sort_dimension__add(char *tok)
  522. {
  523. int i;
  524. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  525. struct sort_dimension *sd = &sort_dimensions[i];
  526. if (sd->taken)
  527. continue;
  528. if (strncasecmp(tok, sd->name, strlen(tok)))
  529. continue;
  530. if (sd->entry->collapse)
  531. sort__need_collapse = 1;
  532. if (sd->entry == &sort_call) {
  533. int ret = regcomp(&call_regex, call, REG_EXTENDED);
  534. if (ret) {
  535. char err[BUFSIZ];
  536. regerror(ret, &call_regex, err, sizeof(err));
  537. fprintf(stderr, "Invalid regex: %s\n%s", call, err);
  538. exit(-1);
  539. }
  540. sort__has_call = 1;
  541. }
  542. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  543. sd->taken = 1;
  544. return 0;
  545. }
  546. return -ESRCH;
  547. }
  548. static int64_t
  549. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  550. {
  551. struct sort_entry *se;
  552. int64_t cmp = 0;
  553. list_for_each_entry(se, &hist_entry__sort_list, list) {
  554. cmp = se->cmp(left, right);
  555. if (cmp)
  556. break;
  557. }
  558. return cmp;
  559. }
  560. static int64_t
  561. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  562. {
  563. struct sort_entry *se;
  564. int64_t cmp = 0;
  565. list_for_each_entry(se, &hist_entry__sort_list, list) {
  566. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  567. f = se->collapse ?: se->cmp;
  568. cmp = f(left, right);
  569. if (cmp)
  570. break;
  571. }
  572. return cmp;
  573. }
  574. static size_t
  575. hist_entry__fprintf(FILE *fp, struct hist_entry *self, __u64 total_samples)
  576. {
  577. struct sort_entry *se;
  578. size_t ret;
  579. if (total_samples) {
  580. double percent = self->count * 100.0 / total_samples;
  581. char *color = PERF_COLOR_NORMAL;
  582. /*
  583. * We color high-overhead entries in red, mid-overhead
  584. * entries in green - and keep the low overhead places
  585. * normal:
  586. */
  587. if (percent >= 5.0) {
  588. color = PERF_COLOR_RED;
  589. } else {
  590. if (percent >= 0.5)
  591. color = PERF_COLOR_GREEN;
  592. }
  593. ret = color_fprintf(fp, color, " %6.2f%%",
  594. (self->count * 100.0) / total_samples);
  595. } else
  596. ret = fprintf(fp, "%12Ld ", self->count);
  597. list_for_each_entry(se, &hist_entry__sort_list, list) {
  598. fprintf(fp, " ");
  599. ret += se->print(fp, self);
  600. }
  601. ret += fprintf(fp, "\n");
  602. return ret;
  603. }
  604. /*
  605. *
  606. */
  607. static struct symbol *
  608. resolve_symbol(struct thread *thread, struct map **mapp,
  609. struct dso **dsop, __u64 *ipp)
  610. {
  611. struct dso *dso = dsop ? *dsop : NULL;
  612. struct map *map = mapp ? *mapp : NULL;
  613. uint64_t ip = *ipp;
  614. if (!thread)
  615. return NULL;
  616. if (dso)
  617. goto got_dso;
  618. if (map)
  619. goto got_map;
  620. map = thread__find_map(thread, ip);
  621. if (map != NULL) {
  622. if (mapp)
  623. *mapp = map;
  624. got_map:
  625. ip = map->map_ip(map, ip);
  626. *ipp = ip;
  627. dso = map->dso;
  628. } else {
  629. /*
  630. * If this is outside of all known maps,
  631. * and is a negative address, try to look it
  632. * up in the kernel dso, as it might be a
  633. * vsyscall (which executes in user-mode):
  634. */
  635. if ((long long)ip < 0)
  636. dso = kernel_dso;
  637. }
  638. dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  639. if (dsop)
  640. *dsop = dso;
  641. if (!dso)
  642. return NULL;
  643. got_dso:
  644. return dso->find_symbol(dso, ip);
  645. }
  646. static struct symbol *call__match(struct symbol *sym)
  647. {
  648. if (!sym)
  649. return NULL;
  650. if (sym->name && !regexec(&call_regex, sym->name, 0, NULL, 0))
  651. return sym;
  652. return NULL;
  653. }
  654. /*
  655. * collect histogram counts
  656. */
  657. static int
  658. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  659. struct symbol *sym, __u64 ip, struct ip_chain_event *chain,
  660. char level, __u64 count)
  661. {
  662. struct rb_node **p = &hist.rb_node;
  663. struct rb_node *parent = NULL;
  664. struct hist_entry *he;
  665. struct hist_entry entry = {
  666. .thread = thread,
  667. .map = map,
  668. .dso = dso,
  669. .sym = sym,
  670. .ip = ip,
  671. .level = level,
  672. .count = count,
  673. };
  674. int cmp;
  675. if (sort__has_call && chain) {
  676. int i, nr = chain->hv;
  677. struct symbol *sym;
  678. struct dso *dso;
  679. __u64 ip;
  680. for (i = 0; i < chain->kernel; i++) {
  681. ip = chain->ips[nr + i];
  682. dso = kernel_dso;
  683. sym = resolve_symbol(thread, NULL, &dso, &ip);
  684. entry.call = call__match(sym);
  685. if (entry.call)
  686. goto got_call;
  687. }
  688. nr += i;
  689. for (i = 0; i < chain->user; i++) {
  690. ip = chain->ips[nr + i];
  691. sym = resolve_symbol(thread, NULL, NULL, &ip);
  692. entry.call = call__match(sym);
  693. if (entry.call)
  694. goto got_call;
  695. }
  696. nr += i;
  697. }
  698. got_call:
  699. while (*p != NULL) {
  700. parent = *p;
  701. he = rb_entry(parent, struct hist_entry, rb_node);
  702. cmp = hist_entry__cmp(&entry, he);
  703. if (!cmp) {
  704. he->count += count;
  705. return 0;
  706. }
  707. if (cmp < 0)
  708. p = &(*p)->rb_left;
  709. else
  710. p = &(*p)->rb_right;
  711. }
  712. he = malloc(sizeof(*he));
  713. if (!he)
  714. return -ENOMEM;
  715. *he = entry;
  716. rb_link_node(&he->rb_node, parent, p);
  717. rb_insert_color(&he->rb_node, &hist);
  718. return 0;
  719. }
  720. static void hist_entry__free(struct hist_entry *he)
  721. {
  722. free(he);
  723. }
  724. /*
  725. * collapse the histogram
  726. */
  727. static struct rb_root collapse_hists;
  728. static void collapse__insert_entry(struct hist_entry *he)
  729. {
  730. struct rb_node **p = &collapse_hists.rb_node;
  731. struct rb_node *parent = NULL;
  732. struct hist_entry *iter;
  733. int64_t cmp;
  734. while (*p != NULL) {
  735. parent = *p;
  736. iter = rb_entry(parent, struct hist_entry, rb_node);
  737. cmp = hist_entry__collapse(iter, he);
  738. if (!cmp) {
  739. iter->count += he->count;
  740. hist_entry__free(he);
  741. return;
  742. }
  743. if (cmp < 0)
  744. p = &(*p)->rb_left;
  745. else
  746. p = &(*p)->rb_right;
  747. }
  748. rb_link_node(&he->rb_node, parent, p);
  749. rb_insert_color(&he->rb_node, &collapse_hists);
  750. }
  751. static void collapse__resort(void)
  752. {
  753. struct rb_node *next;
  754. struct hist_entry *n;
  755. if (!sort__need_collapse)
  756. return;
  757. next = rb_first(&hist);
  758. while (next) {
  759. n = rb_entry(next, struct hist_entry, rb_node);
  760. next = rb_next(&n->rb_node);
  761. rb_erase(&n->rb_node, &hist);
  762. collapse__insert_entry(n);
  763. }
  764. }
  765. /*
  766. * reverse the map, sort on count.
  767. */
  768. static struct rb_root output_hists;
  769. static void output__insert_entry(struct hist_entry *he)
  770. {
  771. struct rb_node **p = &output_hists.rb_node;
  772. struct rb_node *parent = NULL;
  773. struct hist_entry *iter;
  774. while (*p != NULL) {
  775. parent = *p;
  776. iter = rb_entry(parent, struct hist_entry, rb_node);
  777. if (he->count > iter->count)
  778. p = &(*p)->rb_left;
  779. else
  780. p = &(*p)->rb_right;
  781. }
  782. rb_link_node(&he->rb_node, parent, p);
  783. rb_insert_color(&he->rb_node, &output_hists);
  784. }
  785. static void output__resort(void)
  786. {
  787. struct rb_node *next;
  788. struct hist_entry *n;
  789. struct rb_root *tree = &hist;
  790. if (sort__need_collapse)
  791. tree = &collapse_hists;
  792. next = rb_first(tree);
  793. while (next) {
  794. n = rb_entry(next, struct hist_entry, rb_node);
  795. next = rb_next(&n->rb_node);
  796. rb_erase(&n->rb_node, tree);
  797. output__insert_entry(n);
  798. }
  799. }
  800. static size_t output__fprintf(FILE *fp, __u64 total_samples)
  801. {
  802. struct hist_entry *pos;
  803. struct sort_entry *se;
  804. struct rb_node *nd;
  805. size_t ret = 0;
  806. fprintf(fp, "\n");
  807. fprintf(fp, "#\n");
  808. fprintf(fp, "# (%Ld samples)\n", (__u64)total_samples);
  809. fprintf(fp, "#\n");
  810. fprintf(fp, "# Overhead");
  811. list_for_each_entry(se, &hist_entry__sort_list, list)
  812. fprintf(fp, " %s", se->header);
  813. fprintf(fp, "\n");
  814. fprintf(fp, "# ........");
  815. list_for_each_entry(se, &hist_entry__sort_list, list) {
  816. int i;
  817. fprintf(fp, " ");
  818. for (i = 0; i < strlen(se->header); i++)
  819. fprintf(fp, ".");
  820. }
  821. fprintf(fp, "\n");
  822. fprintf(fp, "#\n");
  823. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  824. pos = rb_entry(nd, struct hist_entry, rb_node);
  825. ret += hist_entry__fprintf(fp, pos, total_samples);
  826. }
  827. if (!strcmp(sort_order, default_sort_order)) {
  828. fprintf(fp, "#\n");
  829. fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
  830. fprintf(fp, "#\n");
  831. }
  832. fprintf(fp, "\n");
  833. return ret;
  834. }
  835. static void register_idle_thread(void)
  836. {
  837. struct thread *thread = threads__findnew(0);
  838. if (thread == NULL ||
  839. thread__set_comm(thread, "[idle]")) {
  840. fprintf(stderr, "problem inserting idle task.\n");
  841. exit(-1);
  842. }
  843. }
  844. static unsigned long total = 0,
  845. total_mmap = 0,
  846. total_comm = 0,
  847. total_fork = 0,
  848. total_unknown = 0;
  849. static int
  850. process_overflow_event(event_t *event, unsigned long offset, unsigned long head)
  851. {
  852. char level;
  853. int show = 0;
  854. struct dso *dso = NULL;
  855. struct thread *thread = threads__findnew(event->ip.pid);
  856. __u64 ip = event->ip.ip;
  857. __u64 period = 1;
  858. struct map *map = NULL;
  859. void *more_data = event->ip.__more_data;
  860. struct ip_chain_event *chain = NULL;
  861. if (event->header.type & PERF_SAMPLE_PERIOD) {
  862. period = *(__u64 *)more_data;
  863. more_data += sizeof(__u64);
  864. }
  865. dprintf("%p [%p]: PERF_EVENT (IP, %d): %d: %p period: %Ld\n",
  866. (void *)(offset + head),
  867. (void *)(long)(event->header.size),
  868. event->header.misc,
  869. event->ip.pid,
  870. (void *)(long)ip,
  871. (long long)period);
  872. if (event->header.type & PERF_SAMPLE_CALLCHAIN) {
  873. int i;
  874. chain = (void *)more_data;
  875. if (dump_trace) {
  876. dprintf("... chain: u:%d, k:%d, nr:%d\n",
  877. chain->user,
  878. chain->kernel,
  879. chain->nr);
  880. for (i = 0; i < chain->nr; i++)
  881. dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
  882. }
  883. }
  884. dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  885. if (thread == NULL) {
  886. fprintf(stderr, "problem processing %d event, skipping it.\n",
  887. event->header.type);
  888. return -1;
  889. }
  890. if (event->header.misc & PERF_EVENT_MISC_KERNEL) {
  891. show = SHOW_KERNEL;
  892. level = 'k';
  893. dso = kernel_dso;
  894. dprintf(" ...... dso: %s\n", dso->name);
  895. } else if (event->header.misc & PERF_EVENT_MISC_USER) {
  896. show = SHOW_USER;
  897. level = '.';
  898. } else {
  899. show = SHOW_HV;
  900. level = 'H';
  901. dprintf(" ...... dso: [hypervisor]\n");
  902. }
  903. if (show & show_mask) {
  904. struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
  905. if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
  906. fprintf(stderr,
  907. "problem incrementing symbol count, skipping event\n");
  908. return -1;
  909. }
  910. }
  911. total += period;
  912. return 0;
  913. }
  914. static int
  915. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  916. {
  917. struct thread *thread = threads__findnew(event->mmap.pid);
  918. struct map *map = map__new(&event->mmap);
  919. dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
  920. (void *)(offset + head),
  921. (void *)(long)(event->header.size),
  922. event->mmap.pid,
  923. (void *)(long)event->mmap.start,
  924. (void *)(long)event->mmap.len,
  925. (void *)(long)event->mmap.pgoff,
  926. event->mmap.filename);
  927. if (thread == NULL || map == NULL) {
  928. dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
  929. return 0;
  930. }
  931. thread__insert_map(thread, map);
  932. total_mmap++;
  933. return 0;
  934. }
  935. static int
  936. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  937. {
  938. struct thread *thread = threads__findnew(event->comm.pid);
  939. dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  940. (void *)(offset + head),
  941. (void *)(long)(event->header.size),
  942. event->comm.comm, event->comm.pid);
  943. if (thread == NULL ||
  944. thread__set_comm(thread, event->comm.comm)) {
  945. dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
  946. return -1;
  947. }
  948. total_comm++;
  949. return 0;
  950. }
  951. static int
  952. process_fork_event(event_t *event, unsigned long offset, unsigned long head)
  953. {
  954. struct thread *thread = threads__findnew(event->fork.pid);
  955. struct thread *parent = threads__findnew(event->fork.ppid);
  956. dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
  957. (void *)(offset + head),
  958. (void *)(long)(event->header.size),
  959. event->fork.pid, event->fork.ppid);
  960. if (!thread || !parent || thread__fork(thread, parent)) {
  961. dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
  962. return -1;
  963. }
  964. total_fork++;
  965. return 0;
  966. }
  967. static int
  968. process_period_event(event_t *event, unsigned long offset, unsigned long head)
  969. {
  970. dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
  971. (void *)(offset + head),
  972. (void *)(long)(event->header.size),
  973. event->period.time,
  974. event->period.id,
  975. event->period.sample_period);
  976. return 0;
  977. }
  978. static void trace_event(event_t *event)
  979. {
  980. unsigned char *raw_event = (void *)event;
  981. char *color = PERF_COLOR_BLUE;
  982. int i, j;
  983. if (!dump_trace)
  984. return;
  985. dprintf(".");
  986. cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
  987. for (i = 0; i < event->header.size; i++) {
  988. if ((i & 15) == 0) {
  989. dprintf(".");
  990. cdprintf(" %04x: ", i);
  991. }
  992. cdprintf(" %02x", raw_event[i]);
  993. if (((i & 15) == 15) || i == event->header.size-1) {
  994. cdprintf(" ");
  995. for (j = 0; j < 15-(i & 15); j++)
  996. cdprintf(" ");
  997. for (j = 0; j < (i & 15); j++) {
  998. if (isprint(raw_event[i-15+j]))
  999. cdprintf("%c", raw_event[i-15+j]);
  1000. else
  1001. cdprintf(".");
  1002. }
  1003. cdprintf("\n");
  1004. }
  1005. }
  1006. dprintf(".\n");
  1007. }
  1008. static int
  1009. process_event(event_t *event, unsigned long offset, unsigned long head)
  1010. {
  1011. trace_event(event);
  1012. if (event->header.misc & PERF_EVENT_MISC_OVERFLOW)
  1013. return process_overflow_event(event, offset, head);
  1014. switch (event->header.type) {
  1015. case PERF_EVENT_MMAP:
  1016. return process_mmap_event(event, offset, head);
  1017. case PERF_EVENT_COMM:
  1018. return process_comm_event(event, offset, head);
  1019. case PERF_EVENT_FORK:
  1020. return process_fork_event(event, offset, head);
  1021. case PERF_EVENT_PERIOD:
  1022. return process_period_event(event, offset, head);
  1023. /*
  1024. * We dont process them right now but they are fine:
  1025. */
  1026. case PERF_EVENT_THROTTLE:
  1027. case PERF_EVENT_UNTHROTTLE:
  1028. return 0;
  1029. default:
  1030. return -1;
  1031. }
  1032. return 0;
  1033. }
  1034. static int __cmd_report(void)
  1035. {
  1036. int ret, rc = EXIT_FAILURE;
  1037. unsigned long offset = 0;
  1038. unsigned long head = 0;
  1039. struct stat stat;
  1040. event_t *event;
  1041. uint32_t size;
  1042. char *buf;
  1043. register_idle_thread();
  1044. input = open(input_name, O_RDONLY);
  1045. if (input < 0) {
  1046. fprintf(stderr, " failed to open file: %s", input_name);
  1047. if (!strcmp(input_name, "perf.data"))
  1048. fprintf(stderr, " (try 'perf record' first)");
  1049. fprintf(stderr, "\n");
  1050. exit(-1);
  1051. }
  1052. ret = fstat(input, &stat);
  1053. if (ret < 0) {
  1054. perror("failed to stat file");
  1055. exit(-1);
  1056. }
  1057. if (!stat.st_size) {
  1058. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1059. exit(0);
  1060. }
  1061. if (load_kernel() < 0) {
  1062. perror("failed to load kernel symbols");
  1063. return EXIT_FAILURE;
  1064. }
  1065. if (!full_paths) {
  1066. if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
  1067. perror("failed to get the current directory");
  1068. return EXIT_FAILURE;
  1069. }
  1070. cwdlen = strlen(cwd);
  1071. } else {
  1072. cwd = NULL;
  1073. cwdlen = 0;
  1074. }
  1075. remap:
  1076. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  1077. MAP_SHARED, input, offset);
  1078. if (buf == MAP_FAILED) {
  1079. perror("failed to mmap file");
  1080. exit(-1);
  1081. }
  1082. more:
  1083. event = (event_t *)(buf + head);
  1084. size = event->header.size;
  1085. if (!size)
  1086. size = 8;
  1087. if (head + event->header.size >= page_size * mmap_window) {
  1088. unsigned long shift = page_size * (head / page_size);
  1089. int ret;
  1090. ret = munmap(buf, page_size * mmap_window);
  1091. assert(ret == 0);
  1092. offset += shift;
  1093. head -= shift;
  1094. goto remap;
  1095. }
  1096. size = event->header.size;
  1097. dprintf("\n%p [%p]: event: %d\n",
  1098. (void *)(offset + head),
  1099. (void *)(long)event->header.size,
  1100. event->header.type);
  1101. if (!size || process_event(event, offset, head) < 0) {
  1102. dprintf("%p [%p]: skipping unknown header type: %d\n",
  1103. (void *)(offset + head),
  1104. (void *)(long)(event->header.size),
  1105. event->header.type);
  1106. total_unknown++;
  1107. /*
  1108. * assume we lost track of the stream, check alignment, and
  1109. * increment a single u64 in the hope to catch on again 'soon'.
  1110. */
  1111. if (unlikely(head & 7))
  1112. head &= ~7ULL;
  1113. size = 8;
  1114. }
  1115. head += size;
  1116. if (offset + head < stat.st_size)
  1117. goto more;
  1118. rc = EXIT_SUCCESS;
  1119. close(input);
  1120. dprintf(" IP events: %10ld\n", total);
  1121. dprintf(" mmap events: %10ld\n", total_mmap);
  1122. dprintf(" comm events: %10ld\n", total_comm);
  1123. dprintf(" fork events: %10ld\n", total_fork);
  1124. dprintf(" unknown events: %10ld\n", total_unknown);
  1125. if (dump_trace)
  1126. return 0;
  1127. if (verbose >= 3)
  1128. threads__fprintf(stdout);
  1129. if (verbose >= 2)
  1130. dsos__fprintf(stdout);
  1131. collapse__resort();
  1132. output__resort();
  1133. output__fprintf(stdout, total);
  1134. return rc;
  1135. }
  1136. static const char * const report_usage[] = {
  1137. "perf report [<options>] <command>",
  1138. NULL
  1139. };
  1140. static const struct option options[] = {
  1141. OPT_STRING('i', "input", &input_name, "file",
  1142. "input file name"),
  1143. OPT_BOOLEAN('v', "verbose", &verbose,
  1144. "be more verbose (show symbol address, etc)"),
  1145. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1146. "dump raw trace in ASCII"),
  1147. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  1148. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1149. "sort by key(s): pid, comm, dso, symbol. Default: pid,symbol"),
  1150. OPT_BOOLEAN('P', "full-paths", &full_paths,
  1151. "Don't shorten the pathnames taking into account the cwd"),
  1152. OPT_STRING('c', "call", &call, "regex",
  1153. "regex to use for --sort call"),
  1154. OPT_END()
  1155. };
  1156. static void setup_sorting(void)
  1157. {
  1158. char *tmp, *tok, *str = strdup(sort_order);
  1159. for (tok = strtok_r(str, ", ", &tmp);
  1160. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1161. if (sort_dimension__add(tok) < 0) {
  1162. error("Unknown --sort key: `%s'", tok);
  1163. usage_with_options(report_usage, options);
  1164. }
  1165. }
  1166. free(str);
  1167. }
  1168. int cmd_report(int argc, const char **argv, const char *prefix)
  1169. {
  1170. symbol__init();
  1171. page_size = getpagesize();
  1172. argc = parse_options(argc, argv, options, report_usage, 0);
  1173. setup_sorting();
  1174. /*
  1175. * Any (unrecognized) arguments left?
  1176. */
  1177. if (argc)
  1178. usage_with_options(report_usage, options);
  1179. setup_pager();
  1180. return __cmd_report();
  1181. }