builtin-annotate.c 25 KB

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