builtin-report.c 32 KB

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