builtin-report.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929
  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 <linux/list.h>
  12. #include "util/cache.h"
  13. #include <linux/rbtree.h>
  14. #include "util/symbol.h"
  15. #include "util/string.h"
  16. #include "util/callchain.h"
  17. #include "util/strlist.h"
  18. #include "perf.h"
  19. #include "util/header.h"
  20. #include "util/parse-options.h"
  21. #include "util/parse-events.h"
  22. #define SHOW_KERNEL 1
  23. #define SHOW_USER 2
  24. #define SHOW_HV 4
  25. static char const *input_name = "perf.data";
  26. static char *vmlinux = NULL;
  27. static char default_sort_order[] = "comm,dso";
  28. static char *sort_order = default_sort_order;
  29. static char *dso_list_str, *comm_list_str, *sym_list_str;
  30. static struct strlist *dso_list, *comm_list, *sym_list;
  31. static int input;
  32. static int show_mask = SHOW_KERNEL | SHOW_USER | SHOW_HV;
  33. static int dump_trace = 0;
  34. #define dprintf(x...) do { if (dump_trace) printf(x); } while (0)
  35. #define cdprintf(x...) do { if (dump_trace) color_fprintf(stdout, color, x); } while (0)
  36. static int verbose;
  37. #define eprintf(x...) do { if (verbose) fprintf(stderr, x); } while (0)
  38. static int modules;
  39. static int full_paths;
  40. static unsigned long page_size;
  41. static unsigned long mmap_window = 32;
  42. static char default_parent_pattern[] = "^sys_|^do_page_fault";
  43. static char *parent_pattern = default_parent_pattern;
  44. static regex_t parent_regex;
  45. static int exclude_other = 1;
  46. static int callchain;
  47. static enum chain_mode callchain_mode;
  48. static double callchain_min_percent = 0.0;
  49. static u64 sample_type;
  50. struct ip_event {
  51. struct perf_event_header header;
  52. u64 ip;
  53. u32 pid, tid;
  54. unsigned char __more_data[];
  55. };
  56. struct mmap_event {
  57. struct perf_event_header header;
  58. u32 pid, tid;
  59. u64 start;
  60. u64 len;
  61. u64 pgoff;
  62. char filename[PATH_MAX];
  63. };
  64. struct comm_event {
  65. struct perf_event_header header;
  66. u32 pid, tid;
  67. char comm[16];
  68. };
  69. struct fork_event {
  70. struct perf_event_header header;
  71. u32 pid, ppid;
  72. };
  73. struct period_event {
  74. struct perf_event_header header;
  75. u64 time;
  76. u64 id;
  77. u64 sample_period;
  78. };
  79. struct lost_event {
  80. struct perf_event_header header;
  81. u64 id;
  82. u64 lost;
  83. };
  84. struct read_event {
  85. struct perf_event_header header;
  86. u32 pid,tid;
  87. u64 value;
  88. u64 format[3];
  89. };
  90. typedef union event_union {
  91. struct perf_event_header header;
  92. struct ip_event ip;
  93. struct mmap_event mmap;
  94. struct comm_event comm;
  95. struct fork_event fork;
  96. struct period_event period;
  97. struct lost_event lost;
  98. struct read_event read;
  99. } event_t;
  100. static LIST_HEAD(dsos);
  101. static struct dso *kernel_dso;
  102. static struct dso *vdso;
  103. static struct dso *hypervisor_dso;
  104. static void dsos__add(struct dso *dso)
  105. {
  106. list_add_tail(&dso->node, &dsos);
  107. }
  108. static struct dso *dsos__find(const char *name)
  109. {
  110. struct dso *pos;
  111. list_for_each_entry(pos, &dsos, node)
  112. if (strcmp(pos->name, name) == 0)
  113. return pos;
  114. return NULL;
  115. }
  116. static struct dso *dsos__findnew(const char *name)
  117. {
  118. struct dso *dso = dsos__find(name);
  119. int nr;
  120. if (dso)
  121. return dso;
  122. dso = dso__new(name, 0);
  123. if (!dso)
  124. goto out_delete_dso;
  125. nr = dso__load(dso, NULL, verbose);
  126. if (nr < 0) {
  127. eprintf("Failed to open: %s\n", name);
  128. goto out_delete_dso;
  129. }
  130. if (!nr)
  131. eprintf("No symbols found in: %s, maybe install a debug package?\n", name);
  132. dsos__add(dso);
  133. return dso;
  134. out_delete_dso:
  135. dso__delete(dso);
  136. return NULL;
  137. }
  138. static void dsos__fprintf(FILE *fp)
  139. {
  140. struct dso *pos;
  141. list_for_each_entry(pos, &dsos, node)
  142. dso__fprintf(pos, fp);
  143. }
  144. static struct symbol *vdso__find_symbol(struct dso *dso, u64 ip)
  145. {
  146. return dso__find_symbol(dso, ip);
  147. }
  148. static int load_kernel(void)
  149. {
  150. int err;
  151. kernel_dso = dso__new("[kernel]", 0);
  152. if (!kernel_dso)
  153. return -1;
  154. err = dso__load_kernel(kernel_dso, vmlinux, NULL, verbose, modules);
  155. if (err <= 0) {
  156. dso__delete(kernel_dso);
  157. kernel_dso = NULL;
  158. } else
  159. dsos__add(kernel_dso);
  160. vdso = dso__new("[vdso]", 0);
  161. if (!vdso)
  162. return -1;
  163. vdso->find_symbol = vdso__find_symbol;
  164. dsos__add(vdso);
  165. hypervisor_dso = dso__new("[hypervisor]", 0);
  166. if (!hypervisor_dso)
  167. return -1;
  168. dsos__add(hypervisor_dso);
  169. return err;
  170. }
  171. static char __cwd[PATH_MAX];
  172. static char *cwd = __cwd;
  173. static int cwdlen;
  174. static int strcommon(const char *pathname)
  175. {
  176. int n = 0;
  177. while (pathname[n] == cwd[n] && n < cwdlen)
  178. ++n;
  179. return n;
  180. }
  181. struct map {
  182. struct list_head node;
  183. u64 start;
  184. u64 end;
  185. u64 pgoff;
  186. u64 (*map_ip)(struct map *, u64);
  187. struct dso *dso;
  188. };
  189. static u64 map__map_ip(struct map *map, u64 ip)
  190. {
  191. return ip - map->start + map->pgoff;
  192. }
  193. static u64 vdso__map_ip(struct map *map __used, u64 ip)
  194. {
  195. return ip;
  196. }
  197. static inline int is_anon_memory(const char *filename)
  198. {
  199. return strcmp(filename, "//anon") == 0;
  200. }
  201. static struct map *map__new(struct mmap_event *event)
  202. {
  203. struct map *self = malloc(sizeof(*self));
  204. if (self != NULL) {
  205. const char *filename = event->filename;
  206. char newfilename[PATH_MAX];
  207. int anon;
  208. if (cwd) {
  209. int n = strcommon(filename);
  210. if (n == cwdlen) {
  211. snprintf(newfilename, sizeof(newfilename),
  212. ".%s", filename + n);
  213. filename = newfilename;
  214. }
  215. }
  216. anon = is_anon_memory(filename);
  217. if (anon) {
  218. snprintf(newfilename, sizeof(newfilename), "/tmp/perf-%d.map", event->pid);
  219. filename = newfilename;
  220. }
  221. self->start = event->start;
  222. self->end = event->start + event->len;
  223. self->pgoff = event->pgoff;
  224. self->dso = dsos__findnew(filename);
  225. if (self->dso == NULL)
  226. goto out_delete;
  227. if (self->dso == vdso || anon)
  228. self->map_ip = vdso__map_ip;
  229. else
  230. self->map_ip = map__map_ip;
  231. }
  232. return self;
  233. out_delete:
  234. free(self);
  235. return NULL;
  236. }
  237. static struct map *map__clone(struct map *self)
  238. {
  239. struct map *map = malloc(sizeof(*self));
  240. if (!map)
  241. return NULL;
  242. memcpy(map, self, sizeof(*self));
  243. return map;
  244. }
  245. static int map__overlap(struct map *l, struct map *r)
  246. {
  247. if (l->start > r->start) {
  248. struct map *t = l;
  249. l = r;
  250. r = t;
  251. }
  252. if (l->end > r->start)
  253. return 1;
  254. return 0;
  255. }
  256. static size_t map__fprintf(struct map *self, FILE *fp)
  257. {
  258. return fprintf(fp, " %Lx-%Lx %Lx %s\n",
  259. self->start, self->end, self->pgoff, self->dso->name);
  260. }
  261. struct thread {
  262. struct rb_node rb_node;
  263. struct list_head maps;
  264. pid_t pid;
  265. char *comm;
  266. };
  267. static struct thread *thread__new(pid_t pid)
  268. {
  269. struct thread *self = malloc(sizeof(*self));
  270. if (self != NULL) {
  271. self->pid = pid;
  272. self->comm = malloc(32);
  273. if (self->comm)
  274. snprintf(self->comm, 32, ":%d", self->pid);
  275. INIT_LIST_HEAD(&self->maps);
  276. }
  277. return self;
  278. }
  279. static int thread__set_comm(struct thread *self, const char *comm)
  280. {
  281. if (self->comm)
  282. free(self->comm);
  283. self->comm = strdup(comm);
  284. return self->comm ? 0 : -ENOMEM;
  285. }
  286. static size_t thread__fprintf(struct thread *self, FILE *fp)
  287. {
  288. struct map *pos;
  289. size_t ret = fprintf(fp, "Thread %d %s\n", self->pid, self->comm);
  290. list_for_each_entry(pos, &self->maps, node)
  291. ret += map__fprintf(pos, fp);
  292. return ret;
  293. }
  294. static struct rb_root threads;
  295. static struct thread *last_match;
  296. static struct thread *threads__findnew(pid_t pid)
  297. {
  298. struct rb_node **p = &threads.rb_node;
  299. struct rb_node *parent = NULL;
  300. struct thread *th;
  301. /*
  302. * Font-end cache - PID lookups come in blocks,
  303. * so most of the time we dont have to look up
  304. * the full rbtree:
  305. */
  306. if (last_match && last_match->pid == pid)
  307. return last_match;
  308. while (*p != NULL) {
  309. parent = *p;
  310. th = rb_entry(parent, struct thread, rb_node);
  311. if (th->pid == pid) {
  312. last_match = th;
  313. return th;
  314. }
  315. if (pid < th->pid)
  316. p = &(*p)->rb_left;
  317. else
  318. p = &(*p)->rb_right;
  319. }
  320. th = thread__new(pid);
  321. if (th != NULL) {
  322. rb_link_node(&th->rb_node, parent, p);
  323. rb_insert_color(&th->rb_node, &threads);
  324. last_match = th;
  325. }
  326. return th;
  327. }
  328. static void thread__insert_map(struct thread *self, struct map *map)
  329. {
  330. struct map *pos, *tmp;
  331. list_for_each_entry_safe(pos, tmp, &self->maps, node) {
  332. if (map__overlap(pos, map)) {
  333. if (verbose >= 2) {
  334. printf("overlapping maps:\n");
  335. map__fprintf(map, stdout);
  336. map__fprintf(pos, stdout);
  337. }
  338. if (map->start <= pos->start && map->end > pos->start)
  339. pos->start = map->end;
  340. if (map->end >= pos->end && map->start < pos->end)
  341. pos->end = map->start;
  342. if (verbose >= 2) {
  343. printf("after collision:\n");
  344. map__fprintf(pos, stdout);
  345. }
  346. if (pos->start >= pos->end) {
  347. list_del_init(&pos->node);
  348. free(pos);
  349. }
  350. }
  351. }
  352. list_add_tail(&map->node, &self->maps);
  353. }
  354. static int thread__fork(struct thread *self, struct thread *parent)
  355. {
  356. struct map *map;
  357. if (self->comm)
  358. free(self->comm);
  359. self->comm = strdup(parent->comm);
  360. if (!self->comm)
  361. return -ENOMEM;
  362. list_for_each_entry(map, &parent->maps, node) {
  363. struct map *new = map__clone(map);
  364. if (!new)
  365. return -ENOMEM;
  366. thread__insert_map(self, new);
  367. }
  368. return 0;
  369. }
  370. static struct map *thread__find_map(struct thread *self, u64 ip)
  371. {
  372. struct map *pos;
  373. if (self == NULL)
  374. return NULL;
  375. list_for_each_entry(pos, &self->maps, node)
  376. if (ip >= pos->start && ip <= pos->end)
  377. return pos;
  378. return NULL;
  379. }
  380. static size_t threads__fprintf(FILE *fp)
  381. {
  382. size_t ret = 0;
  383. struct rb_node *nd;
  384. for (nd = rb_first(&threads); nd; nd = rb_next(nd)) {
  385. struct thread *pos = rb_entry(nd, struct thread, rb_node);
  386. ret += thread__fprintf(pos, fp);
  387. }
  388. return ret;
  389. }
  390. /*
  391. * histogram, sorted on item, collects counts
  392. */
  393. static struct rb_root hist;
  394. struct hist_entry {
  395. struct rb_node rb_node;
  396. struct thread *thread;
  397. struct map *map;
  398. struct dso *dso;
  399. struct symbol *sym;
  400. struct symbol *parent;
  401. u64 ip;
  402. char level;
  403. struct callchain_node callchain;
  404. struct rb_root sorted_chain;
  405. u64 count;
  406. };
  407. /*
  408. * configurable sorting bits
  409. */
  410. struct sort_entry {
  411. struct list_head list;
  412. char *header;
  413. int64_t (*cmp)(struct hist_entry *, struct hist_entry *);
  414. int64_t (*collapse)(struct hist_entry *, struct hist_entry *);
  415. size_t (*print)(FILE *fp, struct hist_entry *);
  416. };
  417. static int64_t cmp_null(void *l, void *r)
  418. {
  419. if (!l && !r)
  420. return 0;
  421. else if (!l)
  422. return -1;
  423. else
  424. return 1;
  425. }
  426. /* --sort pid */
  427. static int64_t
  428. sort__thread_cmp(struct hist_entry *left, struct hist_entry *right)
  429. {
  430. return right->thread->pid - left->thread->pid;
  431. }
  432. static size_t
  433. sort__thread_print(FILE *fp, struct hist_entry *self)
  434. {
  435. return fprintf(fp, "%16s:%5d", self->thread->comm ?: "", self->thread->pid);
  436. }
  437. static struct sort_entry sort_thread = {
  438. .header = " Command: Pid",
  439. .cmp = sort__thread_cmp,
  440. .print = sort__thread_print,
  441. };
  442. /* --sort comm */
  443. static int64_t
  444. sort__comm_cmp(struct hist_entry *left, struct hist_entry *right)
  445. {
  446. return right->thread->pid - left->thread->pid;
  447. }
  448. static int64_t
  449. sort__comm_collapse(struct hist_entry *left, struct hist_entry *right)
  450. {
  451. char *comm_l = left->thread->comm;
  452. char *comm_r = right->thread->comm;
  453. if (!comm_l || !comm_r)
  454. return cmp_null(comm_l, comm_r);
  455. return strcmp(comm_l, comm_r);
  456. }
  457. static size_t
  458. sort__comm_print(FILE *fp, struct hist_entry *self)
  459. {
  460. return fprintf(fp, "%16s", self->thread->comm);
  461. }
  462. static struct sort_entry sort_comm = {
  463. .header = " Command",
  464. .cmp = sort__comm_cmp,
  465. .collapse = sort__comm_collapse,
  466. .print = sort__comm_print,
  467. };
  468. /* --sort dso */
  469. static int64_t
  470. sort__dso_cmp(struct hist_entry *left, struct hist_entry *right)
  471. {
  472. struct dso *dso_l = left->dso;
  473. struct dso *dso_r = right->dso;
  474. if (!dso_l || !dso_r)
  475. return cmp_null(dso_l, dso_r);
  476. return strcmp(dso_l->name, dso_r->name);
  477. }
  478. static size_t
  479. sort__dso_print(FILE *fp, struct hist_entry *self)
  480. {
  481. if (self->dso)
  482. return fprintf(fp, "%-25s", self->dso->name);
  483. return fprintf(fp, "%016llx ", (u64)self->ip);
  484. }
  485. static struct sort_entry sort_dso = {
  486. .header = "Shared Object ",
  487. .cmp = sort__dso_cmp,
  488. .print = sort__dso_print,
  489. };
  490. /* --sort symbol */
  491. static int64_t
  492. sort__sym_cmp(struct hist_entry *left, struct hist_entry *right)
  493. {
  494. u64 ip_l, ip_r;
  495. if (left->sym == right->sym)
  496. return 0;
  497. ip_l = left->sym ? left->sym->start : left->ip;
  498. ip_r = right->sym ? right->sym->start : right->ip;
  499. return (int64_t)(ip_r - ip_l);
  500. }
  501. static size_t
  502. sort__sym_print(FILE *fp, struct hist_entry *self)
  503. {
  504. size_t ret = 0;
  505. if (verbose)
  506. ret += fprintf(fp, "%#018llx ", (u64)self->ip);
  507. if (self->sym) {
  508. ret += fprintf(fp, "[%c] %s",
  509. self->dso == kernel_dso ? 'k' :
  510. self->dso == hypervisor_dso ? 'h' : '.', self->sym->name);
  511. if (self->sym->module)
  512. ret += fprintf(fp, "\t[%s]", self->sym->module->name);
  513. } else {
  514. ret += fprintf(fp, "%#016llx", (u64)self->ip);
  515. }
  516. return ret;
  517. }
  518. static struct sort_entry sort_sym = {
  519. .header = "Symbol",
  520. .cmp = sort__sym_cmp,
  521. .print = sort__sym_print,
  522. };
  523. /* --sort parent */
  524. static int64_t
  525. sort__parent_cmp(struct hist_entry *left, struct hist_entry *right)
  526. {
  527. struct symbol *sym_l = left->parent;
  528. struct symbol *sym_r = right->parent;
  529. if (!sym_l || !sym_r)
  530. return cmp_null(sym_l, sym_r);
  531. return strcmp(sym_l->name, sym_r->name);
  532. }
  533. static size_t
  534. sort__parent_print(FILE *fp, struct hist_entry *self)
  535. {
  536. size_t ret = 0;
  537. ret += fprintf(fp, "%-20s", self->parent ? self->parent->name : "[other]");
  538. return ret;
  539. }
  540. static struct sort_entry sort_parent = {
  541. .header = "Parent symbol ",
  542. .cmp = sort__parent_cmp,
  543. .print = sort__parent_print,
  544. };
  545. static int sort__need_collapse = 0;
  546. static int sort__has_parent = 0;
  547. struct sort_dimension {
  548. char *name;
  549. struct sort_entry *entry;
  550. int taken;
  551. };
  552. static struct sort_dimension sort_dimensions[] = {
  553. { .name = "pid", .entry = &sort_thread, },
  554. { .name = "comm", .entry = &sort_comm, },
  555. { .name = "dso", .entry = &sort_dso, },
  556. { .name = "symbol", .entry = &sort_sym, },
  557. { .name = "parent", .entry = &sort_parent, },
  558. };
  559. static LIST_HEAD(hist_entry__sort_list);
  560. static int sort_dimension__add(char *tok)
  561. {
  562. unsigned int i;
  563. for (i = 0; i < ARRAY_SIZE(sort_dimensions); i++) {
  564. struct sort_dimension *sd = &sort_dimensions[i];
  565. if (sd->taken)
  566. continue;
  567. if (strncasecmp(tok, sd->name, strlen(tok)))
  568. continue;
  569. if (sd->entry->collapse)
  570. sort__need_collapse = 1;
  571. if (sd->entry == &sort_parent) {
  572. int ret = regcomp(&parent_regex, parent_pattern, REG_EXTENDED);
  573. if (ret) {
  574. char err[BUFSIZ];
  575. regerror(ret, &parent_regex, err, sizeof(err));
  576. fprintf(stderr, "Invalid regex: %s\n%s",
  577. parent_pattern, err);
  578. exit(-1);
  579. }
  580. sort__has_parent = 1;
  581. }
  582. list_add_tail(&sd->entry->list, &hist_entry__sort_list);
  583. sd->taken = 1;
  584. return 0;
  585. }
  586. return -ESRCH;
  587. }
  588. static int64_t
  589. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  590. {
  591. struct sort_entry *se;
  592. int64_t cmp = 0;
  593. list_for_each_entry(se, &hist_entry__sort_list, list) {
  594. cmp = se->cmp(left, right);
  595. if (cmp)
  596. break;
  597. }
  598. return cmp;
  599. }
  600. static int64_t
  601. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  602. {
  603. struct sort_entry *se;
  604. int64_t cmp = 0;
  605. list_for_each_entry(se, &hist_entry__sort_list, list) {
  606. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  607. f = se->collapse ?: se->cmp;
  608. cmp = f(left, right);
  609. if (cmp)
  610. break;
  611. }
  612. return cmp;
  613. }
  614. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask)
  615. {
  616. int i;
  617. size_t ret = 0;
  618. ret += fprintf(fp, "%s", " ");
  619. for (i = 0; i < depth; i++)
  620. if (depth_mask & (1 << i))
  621. ret += fprintf(fp, "| ");
  622. else
  623. ret += fprintf(fp, " ");
  624. ret += fprintf(fp, "\n");
  625. return ret;
  626. }
  627. static size_t
  628. ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
  629. int depth_mask, int count, u64 total_samples,
  630. int hits)
  631. {
  632. int i;
  633. size_t ret = 0;
  634. ret += fprintf(fp, "%s", " ");
  635. for (i = 0; i < depth; i++) {
  636. if (depth_mask & (1 << i))
  637. ret += fprintf(fp, "|");
  638. else
  639. ret += fprintf(fp, " ");
  640. if (!count && i == depth - 1) {
  641. double percent;
  642. percent = hits * 100.0 / total_samples;
  643. ret += fprintf(fp, "--%2.2f%%-- ", percent);
  644. } else
  645. ret += fprintf(fp, "%s", " ");
  646. }
  647. if (chain->sym)
  648. ret += fprintf(fp, "%s\n", chain->sym->name);
  649. else
  650. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  651. return ret;
  652. }
  653. static size_t
  654. callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  655. u64 total_samples, int depth, int depth_mask)
  656. {
  657. struct rb_node *node, *next;
  658. struct callchain_node *child;
  659. struct callchain_list *chain;
  660. int new_depth_mask = depth_mask;
  661. size_t ret = 0;
  662. int i;
  663. node = rb_first(&self->rb_root);
  664. while (node) {
  665. child = rb_entry(node, struct callchain_node, rb_node);
  666. /*
  667. * The depth mask manages the output of pipes that show
  668. * the depth. We don't want to keep the pipes of the current
  669. * level for the last child of this depth
  670. */
  671. next = rb_next(node);
  672. if (!next)
  673. new_depth_mask &= ~(1 << (depth - 1));
  674. /*
  675. * But we keep the older depth mask for the line seperator
  676. * to keep the level link until we reach the last child
  677. */
  678. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask);
  679. i = 0;
  680. list_for_each_entry(chain, &child->val, list) {
  681. if (chain->ip >= PERF_CONTEXT_MAX)
  682. continue;
  683. ret += ipchain__fprintf_graph(fp, chain, depth,
  684. new_depth_mask, i++,
  685. total_samples,
  686. child->cumul_hit);
  687. }
  688. ret += callchain__fprintf_graph(fp, child, total_samples,
  689. depth + 1,
  690. new_depth_mask | (1 << depth));
  691. node = next;
  692. }
  693. return ret;
  694. }
  695. static size_t
  696. callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  697. u64 total_samples)
  698. {
  699. struct callchain_list *chain;
  700. size_t ret = 0;
  701. if (!self)
  702. return 0;
  703. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  704. list_for_each_entry(chain, &self->val, list) {
  705. if (chain->ip >= PERF_CONTEXT_MAX)
  706. continue;
  707. if (chain->sym)
  708. ret += fprintf(fp, " %s\n", chain->sym->name);
  709. else
  710. ret += fprintf(fp, " %p\n",
  711. (void *)(long)chain->ip);
  712. }
  713. return ret;
  714. }
  715. static size_t
  716. hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  717. u64 total_samples)
  718. {
  719. struct rb_node *rb_node;
  720. struct callchain_node *chain;
  721. size_t ret = 0;
  722. rb_node = rb_first(&self->sorted_chain);
  723. while (rb_node) {
  724. double percent;
  725. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  726. percent = chain->hit * 100.0 / total_samples;
  727. if (callchain_mode == FLAT) {
  728. ret += fprintf(fp, " %6.2f%%\n", percent);
  729. ret += callchain__fprintf_flat(fp, chain, total_samples);
  730. } else if (callchain_mode == GRAPH) {
  731. ret += callchain__fprintf_graph(fp, chain,
  732. total_samples, 1, 1);
  733. }
  734. ret += fprintf(fp, "\n");
  735. rb_node = rb_next(rb_node);
  736. }
  737. return ret;
  738. }
  739. static size_t
  740. hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
  741. {
  742. struct sort_entry *se;
  743. size_t ret;
  744. if (exclude_other && !self->parent)
  745. return 0;
  746. if (total_samples)
  747. ret = percent_color_fprintf(fp, " %6.2f%%",
  748. (self->count * 100.0) / total_samples);
  749. else
  750. ret = fprintf(fp, "%12Ld ", self->count);
  751. list_for_each_entry(se, &hist_entry__sort_list, list) {
  752. if (exclude_other && (se == &sort_parent))
  753. continue;
  754. fprintf(fp, " ");
  755. ret += se->print(fp, self);
  756. }
  757. ret += fprintf(fp, "\n");
  758. if (callchain)
  759. hist_entry_callchain__fprintf(fp, self, total_samples);
  760. return ret;
  761. }
  762. /*
  763. *
  764. */
  765. static struct symbol *
  766. resolve_symbol(struct thread *thread, struct map **mapp,
  767. struct dso **dsop, u64 *ipp)
  768. {
  769. struct dso *dso = dsop ? *dsop : NULL;
  770. struct map *map = mapp ? *mapp : NULL;
  771. u64 ip = *ipp;
  772. if (!thread)
  773. return NULL;
  774. if (dso)
  775. goto got_dso;
  776. if (map)
  777. goto got_map;
  778. map = thread__find_map(thread, ip);
  779. if (map != NULL) {
  780. if (mapp)
  781. *mapp = map;
  782. got_map:
  783. ip = map->map_ip(map, ip);
  784. dso = map->dso;
  785. } else {
  786. /*
  787. * If this is outside of all known maps,
  788. * and is a negative address, try to look it
  789. * up in the kernel dso, as it might be a
  790. * vsyscall (which executes in user-mode):
  791. */
  792. if ((long long)ip < 0)
  793. dso = kernel_dso;
  794. }
  795. dprintf(" ...... dso: %s\n", dso ? dso->name : "<not found>");
  796. dprintf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
  797. *ipp = ip;
  798. if (dsop)
  799. *dsop = dso;
  800. if (!dso)
  801. return NULL;
  802. got_dso:
  803. return dso->find_symbol(dso, ip);
  804. }
  805. static int call__match(struct symbol *sym)
  806. {
  807. if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
  808. return 1;
  809. return 0;
  810. }
  811. static struct symbol **
  812. resolve_callchain(struct thread *thread, struct map *map __used,
  813. struct ip_callchain *chain, struct hist_entry *entry)
  814. {
  815. u64 context = PERF_CONTEXT_MAX;
  816. struct symbol **syms;
  817. unsigned int i;
  818. if (callchain) {
  819. syms = calloc(chain->nr, sizeof(*syms));
  820. if (!syms) {
  821. fprintf(stderr, "Can't allocate memory for symbols\n");
  822. exit(-1);
  823. }
  824. }
  825. for (i = 0; i < chain->nr; i++) {
  826. u64 ip = chain->ips[i];
  827. struct dso *dso = NULL;
  828. struct symbol *sym;
  829. if (ip >= PERF_CONTEXT_MAX) {
  830. context = ip;
  831. continue;
  832. }
  833. switch (context) {
  834. case PERF_CONTEXT_HV:
  835. dso = hypervisor_dso;
  836. break;
  837. case PERF_CONTEXT_KERNEL:
  838. dso = kernel_dso;
  839. break;
  840. default:
  841. break;
  842. }
  843. sym = resolve_symbol(thread, NULL, &dso, &ip);
  844. if (sym) {
  845. if (sort__has_parent && call__match(sym) &&
  846. !entry->parent)
  847. entry->parent = sym;
  848. if (!callchain)
  849. break;
  850. syms[i] = sym;
  851. }
  852. }
  853. return syms;
  854. }
  855. /*
  856. * collect histogram counts
  857. */
  858. static int
  859. hist_entry__add(struct thread *thread, struct map *map, struct dso *dso,
  860. struct symbol *sym, u64 ip, struct ip_callchain *chain,
  861. char level, u64 count)
  862. {
  863. struct rb_node **p = &hist.rb_node;
  864. struct rb_node *parent = NULL;
  865. struct hist_entry *he;
  866. struct symbol **syms = NULL;
  867. struct hist_entry entry = {
  868. .thread = thread,
  869. .map = map,
  870. .dso = dso,
  871. .sym = sym,
  872. .ip = ip,
  873. .level = level,
  874. .count = count,
  875. .parent = NULL,
  876. .sorted_chain = RB_ROOT
  877. };
  878. int cmp;
  879. if ((sort__has_parent || callchain) && chain)
  880. syms = resolve_callchain(thread, map, chain, &entry);
  881. while (*p != NULL) {
  882. parent = *p;
  883. he = rb_entry(parent, struct hist_entry, rb_node);
  884. cmp = hist_entry__cmp(&entry, he);
  885. if (!cmp) {
  886. he->count += count;
  887. if (callchain) {
  888. append_chain(&he->callchain, chain, syms);
  889. free(syms);
  890. }
  891. return 0;
  892. }
  893. if (cmp < 0)
  894. p = &(*p)->rb_left;
  895. else
  896. p = &(*p)->rb_right;
  897. }
  898. he = malloc(sizeof(*he));
  899. if (!he)
  900. return -ENOMEM;
  901. *he = entry;
  902. if (callchain) {
  903. callchain_init(&he->callchain);
  904. append_chain(&he->callchain, chain, syms);
  905. free(syms);
  906. }
  907. rb_link_node(&he->rb_node, parent, p);
  908. rb_insert_color(&he->rb_node, &hist);
  909. return 0;
  910. }
  911. static void hist_entry__free(struct hist_entry *he)
  912. {
  913. free(he);
  914. }
  915. /*
  916. * collapse the histogram
  917. */
  918. static struct rb_root collapse_hists;
  919. static void collapse__insert_entry(struct hist_entry *he)
  920. {
  921. struct rb_node **p = &collapse_hists.rb_node;
  922. struct rb_node *parent = NULL;
  923. struct hist_entry *iter;
  924. int64_t cmp;
  925. while (*p != NULL) {
  926. parent = *p;
  927. iter = rb_entry(parent, struct hist_entry, rb_node);
  928. cmp = hist_entry__collapse(iter, he);
  929. if (!cmp) {
  930. iter->count += he->count;
  931. hist_entry__free(he);
  932. return;
  933. }
  934. if (cmp < 0)
  935. p = &(*p)->rb_left;
  936. else
  937. p = &(*p)->rb_right;
  938. }
  939. rb_link_node(&he->rb_node, parent, p);
  940. rb_insert_color(&he->rb_node, &collapse_hists);
  941. }
  942. static void collapse__resort(void)
  943. {
  944. struct rb_node *next;
  945. struct hist_entry *n;
  946. if (!sort__need_collapse)
  947. return;
  948. next = rb_first(&hist);
  949. while (next) {
  950. n = rb_entry(next, struct hist_entry, rb_node);
  951. next = rb_next(&n->rb_node);
  952. rb_erase(&n->rb_node, &hist);
  953. collapse__insert_entry(n);
  954. }
  955. }
  956. /*
  957. * reverse the map, sort on count.
  958. */
  959. static struct rb_root output_hists;
  960. static void output__insert_entry(struct hist_entry *he, u64 min_callchain_hits)
  961. {
  962. struct rb_node **p = &output_hists.rb_node;
  963. struct rb_node *parent = NULL;
  964. struct hist_entry *iter;
  965. if (callchain) {
  966. if (callchain_mode == FLAT)
  967. sort_chain_flat(&he->sorted_chain, &he->callchain,
  968. min_callchain_hits);
  969. else if (callchain_mode == GRAPH)
  970. sort_chain_graph(&he->sorted_chain, &he->callchain,
  971. min_callchain_hits);
  972. }
  973. while (*p != NULL) {
  974. parent = *p;
  975. iter = rb_entry(parent, struct hist_entry, rb_node);
  976. if (he->count > iter->count)
  977. p = &(*p)->rb_left;
  978. else
  979. p = &(*p)->rb_right;
  980. }
  981. rb_link_node(&he->rb_node, parent, p);
  982. rb_insert_color(&he->rb_node, &output_hists);
  983. }
  984. static void output__resort(u64 total_samples)
  985. {
  986. struct rb_node *next;
  987. struct hist_entry *n;
  988. struct rb_root *tree = &hist;
  989. u64 min_callchain_hits;
  990. min_callchain_hits = total_samples * (callchain_min_percent / 100);
  991. if (sort__need_collapse)
  992. tree = &collapse_hists;
  993. next = rb_first(tree);
  994. while (next) {
  995. n = rb_entry(next, struct hist_entry, rb_node);
  996. next = rb_next(&n->rb_node);
  997. rb_erase(&n->rb_node, tree);
  998. output__insert_entry(n, min_callchain_hits);
  999. }
  1000. }
  1001. static size_t output__fprintf(FILE *fp, u64 total_samples)
  1002. {
  1003. struct hist_entry *pos;
  1004. struct sort_entry *se;
  1005. struct rb_node *nd;
  1006. size_t ret = 0;
  1007. fprintf(fp, "\n");
  1008. fprintf(fp, "#\n");
  1009. fprintf(fp, "# (%Ld samples)\n", (u64)total_samples);
  1010. fprintf(fp, "#\n");
  1011. fprintf(fp, "# Overhead");
  1012. list_for_each_entry(se, &hist_entry__sort_list, list) {
  1013. if (exclude_other && (se == &sort_parent))
  1014. continue;
  1015. fprintf(fp, " %s", se->header);
  1016. }
  1017. fprintf(fp, "\n");
  1018. fprintf(fp, "# ........");
  1019. list_for_each_entry(se, &hist_entry__sort_list, list) {
  1020. unsigned int i;
  1021. if (exclude_other && (se == &sort_parent))
  1022. continue;
  1023. fprintf(fp, " ");
  1024. for (i = 0; i < strlen(se->header); i++)
  1025. fprintf(fp, ".");
  1026. }
  1027. fprintf(fp, "\n");
  1028. fprintf(fp, "#\n");
  1029. for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
  1030. pos = rb_entry(nd, struct hist_entry, rb_node);
  1031. ret += hist_entry__fprintf(fp, pos, total_samples);
  1032. }
  1033. if (sort_order == default_sort_order &&
  1034. parent_pattern == default_parent_pattern) {
  1035. fprintf(fp, "#\n");
  1036. fprintf(fp, "# (For more details, try: perf report --sort comm,dso,symbol)\n");
  1037. fprintf(fp, "#\n");
  1038. }
  1039. fprintf(fp, "\n");
  1040. return ret;
  1041. }
  1042. static void register_idle_thread(void)
  1043. {
  1044. struct thread *thread = threads__findnew(0);
  1045. if (thread == NULL ||
  1046. thread__set_comm(thread, "[idle]")) {
  1047. fprintf(stderr, "problem inserting idle task.\n");
  1048. exit(-1);
  1049. }
  1050. }
  1051. static unsigned long total = 0,
  1052. total_mmap = 0,
  1053. total_comm = 0,
  1054. total_fork = 0,
  1055. total_unknown = 0,
  1056. total_lost = 0;
  1057. static int validate_chain(struct ip_callchain *chain, event_t *event)
  1058. {
  1059. unsigned int chain_size;
  1060. chain_size = event->header.size;
  1061. chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
  1062. if (chain->nr*sizeof(u64) > chain_size)
  1063. return -1;
  1064. return 0;
  1065. }
  1066. static int
  1067. process_sample_event(event_t *event, unsigned long offset, unsigned long head)
  1068. {
  1069. char level;
  1070. int show = 0;
  1071. struct dso *dso = NULL;
  1072. struct thread *thread = threads__findnew(event->ip.pid);
  1073. u64 ip = event->ip.ip;
  1074. u64 period = 1;
  1075. struct map *map = NULL;
  1076. void *more_data = event->ip.__more_data;
  1077. struct ip_callchain *chain = NULL;
  1078. int cpumode;
  1079. if (sample_type & PERF_SAMPLE_PERIOD) {
  1080. period = *(u64 *)more_data;
  1081. more_data += sizeof(u64);
  1082. }
  1083. dprintf("%p [%p]: PERF_EVENT_SAMPLE (IP, %d): %d: %p period: %Ld\n",
  1084. (void *)(offset + head),
  1085. (void *)(long)(event->header.size),
  1086. event->header.misc,
  1087. event->ip.pid,
  1088. (void *)(long)ip,
  1089. (long long)period);
  1090. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  1091. unsigned int i;
  1092. chain = (void *)more_data;
  1093. dprintf("... chain: nr:%Lu\n", chain->nr);
  1094. if (validate_chain(chain, event) < 0) {
  1095. eprintf("call-chain problem with event, skipping it.\n");
  1096. return 0;
  1097. }
  1098. if (dump_trace) {
  1099. for (i = 0; i < chain->nr; i++)
  1100. dprintf("..... %2d: %016Lx\n", i, chain->ips[i]);
  1101. }
  1102. }
  1103. dprintf(" ... thread: %s:%d\n", thread->comm, thread->pid);
  1104. if (thread == NULL) {
  1105. eprintf("problem processing %d event, skipping it.\n",
  1106. event->header.type);
  1107. return -1;
  1108. }
  1109. if (comm_list && !strlist__has_entry(comm_list, thread->comm))
  1110. return 0;
  1111. cpumode = event->header.misc & PERF_EVENT_MISC_CPUMODE_MASK;
  1112. if (cpumode == PERF_EVENT_MISC_KERNEL) {
  1113. show = SHOW_KERNEL;
  1114. level = 'k';
  1115. dso = kernel_dso;
  1116. dprintf(" ...... dso: %s\n", dso->name);
  1117. } else if (cpumode == PERF_EVENT_MISC_USER) {
  1118. show = SHOW_USER;
  1119. level = '.';
  1120. } else {
  1121. show = SHOW_HV;
  1122. level = 'H';
  1123. dso = hypervisor_dso;
  1124. dprintf(" ...... dso: [hypervisor]\n");
  1125. }
  1126. if (show & show_mask) {
  1127. struct symbol *sym = resolve_symbol(thread, &map, &dso, &ip);
  1128. if (dso_list && dso && dso->name && !strlist__has_entry(dso_list, dso->name))
  1129. return 0;
  1130. if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
  1131. return 0;
  1132. if (hist_entry__add(thread, map, dso, sym, ip, chain, level, period)) {
  1133. eprintf("problem incrementing symbol count, skipping event\n");
  1134. return -1;
  1135. }
  1136. }
  1137. total += period;
  1138. return 0;
  1139. }
  1140. static int
  1141. process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
  1142. {
  1143. struct thread *thread = threads__findnew(event->mmap.pid);
  1144. struct map *map = map__new(&event->mmap);
  1145. dprintf("%p [%p]: PERF_EVENT_MMAP %d: [%p(%p) @ %p]: %s\n",
  1146. (void *)(offset + head),
  1147. (void *)(long)(event->header.size),
  1148. event->mmap.pid,
  1149. (void *)(long)event->mmap.start,
  1150. (void *)(long)event->mmap.len,
  1151. (void *)(long)event->mmap.pgoff,
  1152. event->mmap.filename);
  1153. if (thread == NULL || map == NULL) {
  1154. dprintf("problem processing PERF_EVENT_MMAP, skipping event.\n");
  1155. return 0;
  1156. }
  1157. thread__insert_map(thread, map);
  1158. total_mmap++;
  1159. return 0;
  1160. }
  1161. static int
  1162. process_comm_event(event_t *event, unsigned long offset, unsigned long head)
  1163. {
  1164. struct thread *thread = threads__findnew(event->comm.pid);
  1165. dprintf("%p [%p]: PERF_EVENT_COMM: %s:%d\n",
  1166. (void *)(offset + head),
  1167. (void *)(long)(event->header.size),
  1168. event->comm.comm, event->comm.pid);
  1169. if (thread == NULL ||
  1170. thread__set_comm(thread, event->comm.comm)) {
  1171. dprintf("problem processing PERF_EVENT_COMM, skipping event.\n");
  1172. return -1;
  1173. }
  1174. total_comm++;
  1175. return 0;
  1176. }
  1177. static int
  1178. process_fork_event(event_t *event, unsigned long offset, unsigned long head)
  1179. {
  1180. struct thread *thread = threads__findnew(event->fork.pid);
  1181. struct thread *parent = threads__findnew(event->fork.ppid);
  1182. dprintf("%p [%p]: PERF_EVENT_FORK: %d:%d\n",
  1183. (void *)(offset + head),
  1184. (void *)(long)(event->header.size),
  1185. event->fork.pid, event->fork.ppid);
  1186. if (!thread || !parent || thread__fork(thread, parent)) {
  1187. dprintf("problem processing PERF_EVENT_FORK, skipping event.\n");
  1188. return -1;
  1189. }
  1190. total_fork++;
  1191. return 0;
  1192. }
  1193. static int
  1194. process_period_event(event_t *event, unsigned long offset, unsigned long head)
  1195. {
  1196. dprintf("%p [%p]: PERF_EVENT_PERIOD: time:%Ld, id:%Ld: period:%Ld\n",
  1197. (void *)(offset + head),
  1198. (void *)(long)(event->header.size),
  1199. event->period.time,
  1200. event->period.id,
  1201. event->period.sample_period);
  1202. return 0;
  1203. }
  1204. static int
  1205. process_lost_event(event_t *event, unsigned long offset, unsigned long head)
  1206. {
  1207. dprintf("%p [%p]: PERF_EVENT_LOST: id:%Ld: lost:%Ld\n",
  1208. (void *)(offset + head),
  1209. (void *)(long)(event->header.size),
  1210. event->lost.id,
  1211. event->lost.lost);
  1212. total_lost += event->lost.lost;
  1213. return 0;
  1214. }
  1215. static void trace_event(event_t *event)
  1216. {
  1217. unsigned char *raw_event = (void *)event;
  1218. char *color = PERF_COLOR_BLUE;
  1219. int i, j;
  1220. if (!dump_trace)
  1221. return;
  1222. dprintf(".");
  1223. cdprintf("\n. ... raw event: size %d bytes\n", event->header.size);
  1224. for (i = 0; i < event->header.size; i++) {
  1225. if ((i & 15) == 0) {
  1226. dprintf(".");
  1227. cdprintf(" %04x: ", i);
  1228. }
  1229. cdprintf(" %02x", raw_event[i]);
  1230. if (((i & 15) == 15) || i == event->header.size-1) {
  1231. cdprintf(" ");
  1232. for (j = 0; j < 15-(i & 15); j++)
  1233. cdprintf(" ");
  1234. for (j = 0; j < (i & 15); j++) {
  1235. if (isprint(raw_event[i-15+j]))
  1236. cdprintf("%c", raw_event[i-15+j]);
  1237. else
  1238. cdprintf(".");
  1239. }
  1240. cdprintf("\n");
  1241. }
  1242. }
  1243. dprintf(".\n");
  1244. }
  1245. static int
  1246. process_read_event(event_t *event, unsigned long offset, unsigned long head)
  1247. {
  1248. dprintf("%p [%p]: PERF_EVENT_READ: %d %d %Lu\n",
  1249. (void *)(offset + head),
  1250. (void *)(long)(event->header.size),
  1251. event->read.pid,
  1252. event->read.tid,
  1253. event->read.value);
  1254. return 0;
  1255. }
  1256. static int
  1257. process_event(event_t *event, unsigned long offset, unsigned long head)
  1258. {
  1259. trace_event(event);
  1260. switch (event->header.type) {
  1261. case PERF_EVENT_SAMPLE:
  1262. return process_sample_event(event, offset, head);
  1263. case PERF_EVENT_MMAP:
  1264. return process_mmap_event(event, offset, head);
  1265. case PERF_EVENT_COMM:
  1266. return process_comm_event(event, offset, head);
  1267. case PERF_EVENT_FORK:
  1268. return process_fork_event(event, offset, head);
  1269. case PERF_EVENT_PERIOD:
  1270. return process_period_event(event, offset, head);
  1271. case PERF_EVENT_LOST:
  1272. return process_lost_event(event, offset, head);
  1273. case PERF_EVENT_READ:
  1274. return process_read_event(event, offset, head);
  1275. /*
  1276. * We dont process them right now but they are fine:
  1277. */
  1278. case PERF_EVENT_THROTTLE:
  1279. case PERF_EVENT_UNTHROTTLE:
  1280. return 0;
  1281. default:
  1282. return -1;
  1283. }
  1284. return 0;
  1285. }
  1286. static struct perf_header *header;
  1287. static u64 perf_header__sample_type(void)
  1288. {
  1289. u64 sample_type = 0;
  1290. int i;
  1291. for (i = 0; i < header->attrs; i++) {
  1292. struct perf_header_attr *attr = header->attr[i];
  1293. if (!sample_type)
  1294. sample_type = attr->attr.sample_type;
  1295. else if (sample_type != attr->attr.sample_type)
  1296. die("non matching sample_type");
  1297. }
  1298. return sample_type;
  1299. }
  1300. static int __cmd_report(void)
  1301. {
  1302. int ret, rc = EXIT_FAILURE;
  1303. unsigned long offset = 0;
  1304. unsigned long head, shift;
  1305. struct stat stat;
  1306. event_t *event;
  1307. uint32_t size;
  1308. char *buf;
  1309. register_idle_thread();
  1310. input = open(input_name, O_RDONLY);
  1311. if (input < 0) {
  1312. fprintf(stderr, " failed to open file: %s", input_name);
  1313. if (!strcmp(input_name, "perf.data"))
  1314. fprintf(stderr, " (try 'perf record' first)");
  1315. fprintf(stderr, "\n");
  1316. exit(-1);
  1317. }
  1318. ret = fstat(input, &stat);
  1319. if (ret < 0) {
  1320. perror("failed to stat file");
  1321. exit(-1);
  1322. }
  1323. if (!stat.st_size) {
  1324. fprintf(stderr, "zero-sized file, nothing to do!\n");
  1325. exit(0);
  1326. }
  1327. header = perf_header__read(input);
  1328. head = header->data_offset;
  1329. sample_type = perf_header__sample_type();
  1330. if (sort__has_parent && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
  1331. fprintf(stderr, "selected --sort parent, but no callchain data\n");
  1332. exit(-1);
  1333. }
  1334. if (load_kernel() < 0) {
  1335. perror("failed to load kernel symbols");
  1336. return EXIT_FAILURE;
  1337. }
  1338. if (!full_paths) {
  1339. if (getcwd(__cwd, sizeof(__cwd)) == NULL) {
  1340. perror("failed to get the current directory");
  1341. return EXIT_FAILURE;
  1342. }
  1343. cwdlen = strlen(cwd);
  1344. } else {
  1345. cwd = NULL;
  1346. cwdlen = 0;
  1347. }
  1348. shift = page_size * (head / page_size);
  1349. offset += shift;
  1350. head -= shift;
  1351. remap:
  1352. buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
  1353. MAP_SHARED, input, offset);
  1354. if (buf == MAP_FAILED) {
  1355. perror("failed to mmap file");
  1356. exit(-1);
  1357. }
  1358. more:
  1359. event = (event_t *)(buf + head);
  1360. size = event->header.size;
  1361. if (!size)
  1362. size = 8;
  1363. if (head + event->header.size >= page_size * mmap_window) {
  1364. int ret;
  1365. shift = page_size * (head / page_size);
  1366. ret = munmap(buf, page_size * mmap_window);
  1367. assert(ret == 0);
  1368. offset += shift;
  1369. head -= shift;
  1370. goto remap;
  1371. }
  1372. size = event->header.size;
  1373. dprintf("\n%p [%p]: event: %d\n",
  1374. (void *)(offset + head),
  1375. (void *)(long)event->header.size,
  1376. event->header.type);
  1377. if (!size || process_event(event, offset, head) < 0) {
  1378. dprintf("%p [%p]: skipping unknown header type: %d\n",
  1379. (void *)(offset + head),
  1380. (void *)(long)(event->header.size),
  1381. event->header.type);
  1382. total_unknown++;
  1383. /*
  1384. * assume we lost track of the stream, check alignment, and
  1385. * increment a single u64 in the hope to catch on again 'soon'.
  1386. */
  1387. if (unlikely(head & 7))
  1388. head &= ~7ULL;
  1389. size = 8;
  1390. }
  1391. head += size;
  1392. if (offset + head >= header->data_offset + header->data_size)
  1393. goto done;
  1394. if (offset + head < (unsigned long)stat.st_size)
  1395. goto more;
  1396. done:
  1397. rc = EXIT_SUCCESS;
  1398. close(input);
  1399. dprintf(" IP events: %10ld\n", total);
  1400. dprintf(" mmap events: %10ld\n", total_mmap);
  1401. dprintf(" comm events: %10ld\n", total_comm);
  1402. dprintf(" fork events: %10ld\n", total_fork);
  1403. dprintf(" lost events: %10ld\n", total_lost);
  1404. dprintf(" unknown events: %10ld\n", total_unknown);
  1405. if (dump_trace)
  1406. return 0;
  1407. if (verbose >= 3)
  1408. threads__fprintf(stdout);
  1409. if (verbose >= 2)
  1410. dsos__fprintf(stdout);
  1411. collapse__resort();
  1412. output__resort(total);
  1413. output__fprintf(stdout, total);
  1414. return rc;
  1415. }
  1416. static int
  1417. parse_callchain_opt(const struct option *opt __used, const char *arg,
  1418. int unset __used)
  1419. {
  1420. char *tok;
  1421. char *endptr;
  1422. callchain = 1;
  1423. if (!arg)
  1424. return 0;
  1425. tok = strtok((char *)arg, ",");
  1426. if (!tok)
  1427. return -1;
  1428. /* get the output mode */
  1429. if (!strncmp(tok, "graph", strlen(arg)))
  1430. callchain_mode = GRAPH;
  1431. else if (!strncmp(tok, "flat", strlen(arg)))
  1432. callchain_mode = FLAT;
  1433. else
  1434. return -1;
  1435. /* get the min percentage */
  1436. tok = strtok(NULL, ",");
  1437. if (!tok)
  1438. return 0;
  1439. callchain_min_percent = strtod(tok, &endptr);
  1440. if (tok == endptr)
  1441. return -1;
  1442. return 0;
  1443. }
  1444. static const char * const report_usage[] = {
  1445. "perf report [<options>] <command>",
  1446. NULL
  1447. };
  1448. static const struct option options[] = {
  1449. OPT_STRING('i', "input", &input_name, "file",
  1450. "input file name"),
  1451. OPT_BOOLEAN('v', "verbose", &verbose,
  1452. "be more verbose (show symbol address, etc)"),
  1453. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  1454. "dump raw trace in ASCII"),
  1455. OPT_STRING('k', "vmlinux", &vmlinux, "file", "vmlinux pathname"),
  1456. OPT_BOOLEAN('m', "modules", &modules,
  1457. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  1458. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  1459. "sort by key(s): pid, comm, dso, symbol, parent"),
  1460. OPT_BOOLEAN('P', "full-paths", &full_paths,
  1461. "Don't shorten the pathnames taking into account the cwd"),
  1462. OPT_STRING('p', "parent", &parent_pattern, "regex",
  1463. "regex filter to identify parent, see: '--sort parent'"),
  1464. OPT_BOOLEAN('x', "exclude-other", &exclude_other,
  1465. "Only display entries with parent-match"),
  1466. OPT_CALLBACK_DEFAULT('c', "callchain", NULL, "output_type,min_percent",
  1467. "Display callchains using output_type and min percent threshold. "
  1468. "Default: flat,0", &parse_callchain_opt, "flat,100"),
  1469. OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
  1470. "only consider symbols in these dsos"),
  1471. OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
  1472. "only consider symbols in these comms"),
  1473. OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
  1474. "only consider these symbols"),
  1475. OPT_END()
  1476. };
  1477. static void setup_sorting(void)
  1478. {
  1479. char *tmp, *tok, *str = strdup(sort_order);
  1480. for (tok = strtok_r(str, ", ", &tmp);
  1481. tok; tok = strtok_r(NULL, ", ", &tmp)) {
  1482. if (sort_dimension__add(tok) < 0) {
  1483. error("Unknown --sort key: `%s'", tok);
  1484. usage_with_options(report_usage, options);
  1485. }
  1486. }
  1487. free(str);
  1488. }
  1489. static void setup_list(struct strlist **list, const char *list_str,
  1490. const char *list_name)
  1491. {
  1492. if (list_str) {
  1493. *list = strlist__new(true, list_str);
  1494. if (!*list) {
  1495. fprintf(stderr, "problems parsing %s list\n",
  1496. list_name);
  1497. exit(129);
  1498. }
  1499. }
  1500. }
  1501. int cmd_report(int argc, const char **argv, const char *prefix __used)
  1502. {
  1503. symbol__init();
  1504. page_size = getpagesize();
  1505. argc = parse_options(argc, argv, options, report_usage, 0);
  1506. setup_sorting();
  1507. if (parent_pattern != default_parent_pattern)
  1508. sort_dimension__add("parent");
  1509. else
  1510. exclude_other = 0;
  1511. /*
  1512. * Any (unrecognized) arguments left?
  1513. */
  1514. if (argc)
  1515. usage_with_options(report_usage, options);
  1516. setup_list(&dso_list, dso_list_str, "dso");
  1517. setup_list(&comm_list, comm_list_str, "comm");
  1518. setup_list(&sym_list, sym_list_str, "symbol");
  1519. setup_pager();
  1520. return __cmd_report();
  1521. }