builtin-report.c 40 KB

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