builtin-report.c 45 KB

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