builtin-report.c 45 KB

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