builtin-report.c 35 KB

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