builtin-report.c 34 KB

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