builtin-report.c 39 KB

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