builtin-report.c 35 KB

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