builtin-report.c 33 KB

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