builtin-report.c 30 KB

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