hist.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. #include "util.h"
  2. #include "build-id.h"
  3. #include "hist.h"
  4. #include "session.h"
  5. #include "sort.h"
  6. #include <math.h>
  7. struct callchain_param callchain_param = {
  8. .mode = CHAIN_GRAPH_REL,
  9. .min_percent = 0.5
  10. };
  11. static void hist_entry__add_cpumode_period(struct hist_entry *self,
  12. unsigned int cpumode, u64 period)
  13. {
  14. switch (cpumode) {
  15. case PERF_RECORD_MISC_KERNEL:
  16. self->period_sys += period;
  17. break;
  18. case PERF_RECORD_MISC_USER:
  19. self->period_us += period;
  20. break;
  21. case PERF_RECORD_MISC_GUEST_KERNEL:
  22. self->period_guest_sys += period;
  23. break;
  24. case PERF_RECORD_MISC_GUEST_USER:
  25. self->period_guest_us += period;
  26. break;
  27. default:
  28. break;
  29. }
  30. }
  31. /*
  32. * histogram, sorted on item, collects periods
  33. */
  34. static struct hist_entry *hist_entry__new(struct hist_entry *template)
  35. {
  36. size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_node) : 0;
  37. struct hist_entry *self = malloc(sizeof(*self) + callchain_size);
  38. if (self != NULL) {
  39. *self = *template;
  40. self->nr_events = 1;
  41. if (symbol_conf.use_callchain)
  42. callchain_init(self->callchain);
  43. }
  44. return self;
  45. }
  46. static void hists__inc_nr_entries(struct hists *self, struct hist_entry *entry)
  47. {
  48. if (entry->ms.sym && self->max_sym_namelen < entry->ms.sym->namelen)
  49. self->max_sym_namelen = entry->ms.sym->namelen;
  50. ++self->nr_entries;
  51. }
  52. struct hist_entry *__hists__add_entry(struct hists *self,
  53. struct addr_location *al,
  54. struct symbol *sym_parent, u64 period)
  55. {
  56. struct rb_node **p = &self->entries.rb_node;
  57. struct rb_node *parent = NULL;
  58. struct hist_entry *he;
  59. struct hist_entry entry = {
  60. .thread = al->thread,
  61. .ms = {
  62. .map = al->map,
  63. .sym = al->sym,
  64. },
  65. .ip = al->addr,
  66. .level = al->level,
  67. .period = period,
  68. .parent = sym_parent,
  69. };
  70. int cmp;
  71. while (*p != NULL) {
  72. parent = *p;
  73. he = rb_entry(parent, struct hist_entry, rb_node);
  74. cmp = hist_entry__cmp(&entry, he);
  75. if (!cmp) {
  76. he->period += period;
  77. ++he->nr_events;
  78. goto out;
  79. }
  80. if (cmp < 0)
  81. p = &(*p)->rb_left;
  82. else
  83. p = &(*p)->rb_right;
  84. }
  85. he = hist_entry__new(&entry);
  86. if (!he)
  87. return NULL;
  88. rb_link_node(&he->rb_node, parent, p);
  89. rb_insert_color(&he->rb_node, &self->entries);
  90. hists__inc_nr_entries(self, he);
  91. out:
  92. hist_entry__add_cpumode_period(he, al->cpumode, period);
  93. return he;
  94. }
  95. int64_t
  96. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  97. {
  98. struct sort_entry *se;
  99. int64_t cmp = 0;
  100. list_for_each_entry(se, &hist_entry__sort_list, list) {
  101. cmp = se->se_cmp(left, right);
  102. if (cmp)
  103. break;
  104. }
  105. return cmp;
  106. }
  107. int64_t
  108. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  109. {
  110. struct sort_entry *se;
  111. int64_t cmp = 0;
  112. list_for_each_entry(se, &hist_entry__sort_list, list) {
  113. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  114. f = se->se_collapse ?: se->se_cmp;
  115. cmp = f(left, right);
  116. if (cmp)
  117. break;
  118. }
  119. return cmp;
  120. }
  121. void hist_entry__free(struct hist_entry *he)
  122. {
  123. free(he);
  124. }
  125. /*
  126. * collapse the histogram
  127. */
  128. static bool collapse__insert_entry(struct rb_root *root, struct hist_entry *he)
  129. {
  130. struct rb_node **p = &root->rb_node;
  131. struct rb_node *parent = NULL;
  132. struct hist_entry *iter;
  133. int64_t cmp;
  134. while (*p != NULL) {
  135. parent = *p;
  136. iter = rb_entry(parent, struct hist_entry, rb_node);
  137. cmp = hist_entry__collapse(iter, he);
  138. if (!cmp) {
  139. iter->period += he->period;
  140. hist_entry__free(he);
  141. return false;
  142. }
  143. if (cmp < 0)
  144. p = &(*p)->rb_left;
  145. else
  146. p = &(*p)->rb_right;
  147. }
  148. rb_link_node(&he->rb_node, parent, p);
  149. rb_insert_color(&he->rb_node, root);
  150. return true;
  151. }
  152. void hists__collapse_resort(struct hists *self)
  153. {
  154. struct rb_root tmp;
  155. struct rb_node *next;
  156. struct hist_entry *n;
  157. if (!sort__need_collapse)
  158. return;
  159. tmp = RB_ROOT;
  160. next = rb_first(&self->entries);
  161. self->nr_entries = 0;
  162. self->max_sym_namelen = 0;
  163. while (next) {
  164. n = rb_entry(next, struct hist_entry, rb_node);
  165. next = rb_next(&n->rb_node);
  166. rb_erase(&n->rb_node, &self->entries);
  167. if (collapse__insert_entry(&tmp, n))
  168. hists__inc_nr_entries(self, n);
  169. }
  170. self->entries = tmp;
  171. }
  172. /*
  173. * reverse the map, sort on period.
  174. */
  175. static void __hists__insert_output_entry(struct rb_root *entries,
  176. struct hist_entry *he,
  177. u64 min_callchain_hits)
  178. {
  179. struct rb_node **p = &entries->rb_node;
  180. struct rb_node *parent = NULL;
  181. struct hist_entry *iter;
  182. if (symbol_conf.use_callchain)
  183. callchain_param.sort(&he->sorted_chain, he->callchain,
  184. min_callchain_hits, &callchain_param);
  185. while (*p != NULL) {
  186. parent = *p;
  187. iter = rb_entry(parent, struct hist_entry, rb_node);
  188. if (he->period > iter->period)
  189. p = &(*p)->rb_left;
  190. else
  191. p = &(*p)->rb_right;
  192. }
  193. rb_link_node(&he->rb_node, parent, p);
  194. rb_insert_color(&he->rb_node, entries);
  195. }
  196. void hists__output_resort(struct hists *self)
  197. {
  198. struct rb_root tmp;
  199. struct rb_node *next;
  200. struct hist_entry *n;
  201. u64 min_callchain_hits;
  202. min_callchain_hits = self->stats.total_period * (callchain_param.min_percent / 100);
  203. tmp = RB_ROOT;
  204. next = rb_first(&self->entries);
  205. self->nr_entries = 0;
  206. self->max_sym_namelen = 0;
  207. while (next) {
  208. n = rb_entry(next, struct hist_entry, rb_node);
  209. next = rb_next(&n->rb_node);
  210. rb_erase(&n->rb_node, &self->entries);
  211. __hists__insert_output_entry(&tmp, n, min_callchain_hits);
  212. hists__inc_nr_entries(self, n);
  213. }
  214. self->entries = tmp;
  215. }
  216. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  217. {
  218. int i;
  219. int ret = fprintf(fp, " ");
  220. for (i = 0; i < left_margin; i++)
  221. ret += fprintf(fp, " ");
  222. return ret;
  223. }
  224. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  225. int left_margin)
  226. {
  227. int i;
  228. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  229. for (i = 0; i < depth; i++)
  230. if (depth_mask & (1 << i))
  231. ret += fprintf(fp, "| ");
  232. else
  233. ret += fprintf(fp, " ");
  234. ret += fprintf(fp, "\n");
  235. return ret;
  236. }
  237. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
  238. int depth, int depth_mask, int period,
  239. u64 total_samples, int hits,
  240. int left_margin)
  241. {
  242. int i;
  243. size_t ret = 0;
  244. ret += callchain__fprintf_left_margin(fp, left_margin);
  245. for (i = 0; i < depth; i++) {
  246. if (depth_mask & (1 << i))
  247. ret += fprintf(fp, "|");
  248. else
  249. ret += fprintf(fp, " ");
  250. if (!period && i == depth - 1) {
  251. double percent;
  252. percent = hits * 100.0 / total_samples;
  253. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  254. } else
  255. ret += fprintf(fp, "%s", " ");
  256. }
  257. if (chain->ms.sym)
  258. ret += fprintf(fp, "%s\n", chain->ms.sym->name);
  259. else
  260. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  261. return ret;
  262. }
  263. static struct symbol *rem_sq_bracket;
  264. static struct callchain_list rem_hits;
  265. static void init_rem_hits(void)
  266. {
  267. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  268. if (!rem_sq_bracket) {
  269. fprintf(stderr, "Not enough memory to display remaining hits\n");
  270. return;
  271. }
  272. strcpy(rem_sq_bracket->name, "[...]");
  273. rem_hits.ms.sym = rem_sq_bracket;
  274. }
  275. static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  276. u64 total_samples, int depth,
  277. int depth_mask, int left_margin)
  278. {
  279. struct rb_node *node, *next;
  280. struct callchain_node *child;
  281. struct callchain_list *chain;
  282. int new_depth_mask = depth_mask;
  283. u64 new_total;
  284. u64 remaining;
  285. size_t ret = 0;
  286. int i;
  287. uint entries_printed = 0;
  288. if (callchain_param.mode == CHAIN_GRAPH_REL)
  289. new_total = self->children_hit;
  290. else
  291. new_total = total_samples;
  292. remaining = new_total;
  293. node = rb_first(&self->rb_root);
  294. while (node) {
  295. u64 cumul;
  296. child = rb_entry(node, struct callchain_node, rb_node);
  297. cumul = cumul_hits(child);
  298. remaining -= cumul;
  299. /*
  300. * The depth mask manages the output of pipes that show
  301. * the depth. We don't want to keep the pipes of the current
  302. * level for the last child of this depth.
  303. * Except if we have remaining filtered hits. They will
  304. * supersede the last child
  305. */
  306. next = rb_next(node);
  307. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  308. new_depth_mask &= ~(1 << (depth - 1));
  309. /*
  310. * But we keep the older depth mask for the line separator
  311. * to keep the level link until we reach the last child
  312. */
  313. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  314. left_margin);
  315. i = 0;
  316. list_for_each_entry(chain, &child->val, list) {
  317. ret += ipchain__fprintf_graph(fp, chain, depth,
  318. new_depth_mask, i++,
  319. new_total,
  320. cumul,
  321. left_margin);
  322. }
  323. ret += __callchain__fprintf_graph(fp, child, new_total,
  324. depth + 1,
  325. new_depth_mask | (1 << depth),
  326. left_margin);
  327. node = next;
  328. if (++entries_printed == callchain_param.print_limit)
  329. break;
  330. }
  331. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  332. remaining && remaining != new_total) {
  333. if (!rem_sq_bracket)
  334. return ret;
  335. new_depth_mask &= ~(1 << (depth - 1));
  336. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  337. new_depth_mask, 0, new_total,
  338. remaining, left_margin);
  339. }
  340. return ret;
  341. }
  342. static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  343. u64 total_samples, int left_margin)
  344. {
  345. struct callchain_list *chain;
  346. bool printed = false;
  347. int i = 0;
  348. int ret = 0;
  349. u32 entries_printed = 0;
  350. list_for_each_entry(chain, &self->val, list) {
  351. if (!i++ && sort__first_dimension == SORT_SYM)
  352. continue;
  353. if (!printed) {
  354. ret += callchain__fprintf_left_margin(fp, left_margin);
  355. ret += fprintf(fp, "|\n");
  356. ret += callchain__fprintf_left_margin(fp, left_margin);
  357. ret += fprintf(fp, "---");
  358. left_margin += 3;
  359. printed = true;
  360. } else
  361. ret += callchain__fprintf_left_margin(fp, left_margin);
  362. if (chain->ms.sym)
  363. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  364. else
  365. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  366. if (++entries_printed == callchain_param.print_limit)
  367. break;
  368. }
  369. ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
  370. return ret;
  371. }
  372. static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  373. u64 total_samples)
  374. {
  375. struct callchain_list *chain;
  376. size_t ret = 0;
  377. if (!self)
  378. return 0;
  379. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  380. list_for_each_entry(chain, &self->val, list) {
  381. if (chain->ip >= PERF_CONTEXT_MAX)
  382. continue;
  383. if (chain->ms.sym)
  384. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  385. else
  386. ret += fprintf(fp, " %p\n",
  387. (void *)(long)chain->ip);
  388. }
  389. return ret;
  390. }
  391. static size_t hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
  392. u64 total_samples, int left_margin)
  393. {
  394. struct rb_node *rb_node;
  395. struct callchain_node *chain;
  396. size_t ret = 0;
  397. u32 entries_printed = 0;
  398. rb_node = rb_first(&self->sorted_chain);
  399. while (rb_node) {
  400. double percent;
  401. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  402. percent = chain->hit * 100.0 / total_samples;
  403. switch (callchain_param.mode) {
  404. case CHAIN_FLAT:
  405. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  406. percent);
  407. ret += callchain__fprintf_flat(fp, chain, total_samples);
  408. break;
  409. case CHAIN_GRAPH_ABS: /* Falldown */
  410. case CHAIN_GRAPH_REL:
  411. ret += callchain__fprintf_graph(fp, chain, total_samples,
  412. left_margin);
  413. case CHAIN_NONE:
  414. default:
  415. break;
  416. }
  417. ret += fprintf(fp, "\n");
  418. if (++entries_printed == callchain_param.print_limit)
  419. break;
  420. rb_node = rb_next(rb_node);
  421. }
  422. return ret;
  423. }
  424. int hist_entry__snprintf(struct hist_entry *self, char *s, size_t size,
  425. struct hists *pair_hists, bool show_displacement,
  426. long displacement, bool color, u64 session_total)
  427. {
  428. struct sort_entry *se;
  429. u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
  430. const char *sep = symbol_conf.field_sep;
  431. int ret;
  432. if (symbol_conf.exclude_other && !self->parent)
  433. return 0;
  434. if (pair_hists) {
  435. period = self->pair ? self->pair->period : 0;
  436. total = pair_hists->stats.total_period;
  437. period_sys = self->pair ? self->pair->period_sys : 0;
  438. period_us = self->pair ? self->pair->period_us : 0;
  439. period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
  440. period_guest_us = self->pair ? self->pair->period_guest_us : 0;
  441. } else {
  442. period = self->period;
  443. total = session_total;
  444. period_sys = self->period_sys;
  445. period_us = self->period_us;
  446. period_guest_sys = self->period_guest_sys;
  447. period_guest_us = self->period_guest_us;
  448. }
  449. if (total) {
  450. if (color)
  451. ret = percent_color_snprintf(s, size,
  452. sep ? "%.2f" : " %6.2f%%",
  453. (period * 100.0) / total);
  454. else
  455. ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
  456. (period * 100.0) / total);
  457. if (symbol_conf.show_cpu_utilization) {
  458. ret += percent_color_snprintf(s + ret, size - ret,
  459. sep ? "%.2f" : " %6.2f%%",
  460. (period_sys * 100.0) / total);
  461. ret += percent_color_snprintf(s + ret, size - ret,
  462. sep ? "%.2f" : " %6.2f%%",
  463. (period_us * 100.0) / total);
  464. if (perf_guest) {
  465. ret += percent_color_snprintf(s + ret,
  466. size - ret,
  467. sep ? "%.2f" : " %6.2f%%",
  468. (period_guest_sys * 100.0) /
  469. total);
  470. ret += percent_color_snprintf(s + ret,
  471. size - ret,
  472. sep ? "%.2f" : " %6.2f%%",
  473. (period_guest_us * 100.0) /
  474. total);
  475. }
  476. }
  477. } else
  478. ret = snprintf(s, size, sep ? "%lld" : "%12lld ", period);
  479. if (symbol_conf.show_nr_samples) {
  480. if (sep)
  481. ret += snprintf(s + ret, size - ret, "%c%lld", *sep, period);
  482. else
  483. ret += snprintf(s + ret, size - ret, "%11lld", period);
  484. }
  485. if (pair_hists) {
  486. char bf[32];
  487. double old_percent = 0, new_percent = 0, diff;
  488. if (total > 0)
  489. old_percent = (period * 100.0) / total;
  490. if (session_total > 0)
  491. new_percent = (self->period * 100.0) / session_total;
  492. diff = new_percent - old_percent;
  493. if (fabs(diff) >= 0.01)
  494. snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  495. else
  496. snprintf(bf, sizeof(bf), " ");
  497. if (sep)
  498. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  499. else
  500. ret += snprintf(s + ret, size - ret, "%11.11s", bf);
  501. if (show_displacement) {
  502. if (displacement)
  503. snprintf(bf, sizeof(bf), "%+4ld", displacement);
  504. else
  505. snprintf(bf, sizeof(bf), " ");
  506. if (sep)
  507. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  508. else
  509. ret += snprintf(s + ret, size - ret, "%6.6s", bf);
  510. }
  511. }
  512. list_for_each_entry(se, &hist_entry__sort_list, list) {
  513. if (se->elide)
  514. continue;
  515. ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
  516. ret += se->se_snprintf(self, s + ret, size - ret,
  517. se->se_width ? *se->se_width : 0);
  518. }
  519. return ret;
  520. }
  521. int hist_entry__fprintf(struct hist_entry *self, struct hists *pair_hists,
  522. bool show_displacement, long displacement, FILE *fp,
  523. u64 session_total)
  524. {
  525. char bf[512];
  526. hist_entry__snprintf(self, bf, sizeof(bf), pair_hists,
  527. show_displacement, displacement,
  528. true, session_total);
  529. return fprintf(fp, "%s\n", bf);
  530. }
  531. static size_t hist_entry__fprintf_callchain(struct hist_entry *self, FILE *fp,
  532. u64 session_total)
  533. {
  534. int left_margin = 0;
  535. if (sort__first_dimension == SORT_COMM) {
  536. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  537. typeof(*se), list);
  538. left_margin = se->se_width ? *se->se_width : 0;
  539. left_margin -= thread__comm_len(self->thread);
  540. }
  541. return hist_entry_callchain__fprintf(fp, self, session_total,
  542. left_margin);
  543. }
  544. size_t hists__fprintf(struct hists *self, struct hists *pair,
  545. bool show_displacement, FILE *fp)
  546. {
  547. struct sort_entry *se;
  548. struct rb_node *nd;
  549. size_t ret = 0;
  550. unsigned long position = 1;
  551. long displacement = 0;
  552. unsigned int width;
  553. const char *sep = symbol_conf.field_sep;
  554. const char *col_width = symbol_conf.col_width_list_str;
  555. init_rem_hits();
  556. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  557. if (symbol_conf.show_nr_samples) {
  558. if (sep)
  559. fprintf(fp, "%cSamples", *sep);
  560. else
  561. fputs(" Samples ", fp);
  562. }
  563. if (symbol_conf.show_cpu_utilization) {
  564. if (sep) {
  565. ret += fprintf(fp, "%csys", *sep);
  566. ret += fprintf(fp, "%cus", *sep);
  567. if (perf_guest) {
  568. ret += fprintf(fp, "%cguest sys", *sep);
  569. ret += fprintf(fp, "%cguest us", *sep);
  570. }
  571. } else {
  572. ret += fprintf(fp, " sys ");
  573. ret += fprintf(fp, " us ");
  574. if (perf_guest) {
  575. ret += fprintf(fp, " guest sys ");
  576. ret += fprintf(fp, " guest us ");
  577. }
  578. }
  579. }
  580. if (pair) {
  581. if (sep)
  582. ret += fprintf(fp, "%cDelta", *sep);
  583. else
  584. ret += fprintf(fp, " Delta ");
  585. if (show_displacement) {
  586. if (sep)
  587. ret += fprintf(fp, "%cDisplacement", *sep);
  588. else
  589. ret += fprintf(fp, " Displ");
  590. }
  591. }
  592. list_for_each_entry(se, &hist_entry__sort_list, list) {
  593. if (se->elide)
  594. continue;
  595. if (sep) {
  596. fprintf(fp, "%c%s", *sep, se->se_header);
  597. continue;
  598. }
  599. width = strlen(se->se_header);
  600. if (se->se_width) {
  601. if (symbol_conf.col_width_list_str) {
  602. if (col_width) {
  603. *se->se_width = atoi(col_width);
  604. col_width = strchr(col_width, ',');
  605. if (col_width)
  606. ++col_width;
  607. }
  608. }
  609. width = *se->se_width = max(*se->se_width, width);
  610. }
  611. fprintf(fp, " %*s", width, se->se_header);
  612. }
  613. fprintf(fp, "\n");
  614. if (sep)
  615. goto print_entries;
  616. fprintf(fp, "# ........");
  617. if (symbol_conf.show_nr_samples)
  618. fprintf(fp, " ..........");
  619. if (pair) {
  620. fprintf(fp, " ..........");
  621. if (show_displacement)
  622. fprintf(fp, " .....");
  623. }
  624. list_for_each_entry(se, &hist_entry__sort_list, list) {
  625. unsigned int i;
  626. if (se->elide)
  627. continue;
  628. fprintf(fp, " ");
  629. if (se->se_width)
  630. width = *se->se_width;
  631. else
  632. width = strlen(se->se_header);
  633. for (i = 0; i < width; i++)
  634. fprintf(fp, ".");
  635. }
  636. fprintf(fp, "\n#\n");
  637. print_entries:
  638. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  639. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  640. if (show_displacement) {
  641. if (h->pair != NULL)
  642. displacement = ((long)h->pair->position -
  643. (long)position);
  644. else
  645. displacement = 0;
  646. ++position;
  647. }
  648. ret += hist_entry__fprintf(h, pair, show_displacement,
  649. displacement, fp, self->stats.total_period);
  650. if (symbol_conf.use_callchain)
  651. ret += hist_entry__fprintf_callchain(h, fp, self->stats.total_period);
  652. if (h->ms.map == NULL && verbose > 1) {
  653. __map_groups__fprintf_maps(&h->thread->mg,
  654. MAP__FUNCTION, verbose, fp);
  655. fprintf(fp, "%.10s end\n", graph_dotted_line);
  656. }
  657. }
  658. free(rem_sq_bracket);
  659. return ret;
  660. }
  661. enum hist_filter {
  662. HIST_FILTER__DSO,
  663. HIST_FILTER__THREAD,
  664. };
  665. void hists__filter_by_dso(struct hists *self, const struct dso *dso)
  666. {
  667. struct rb_node *nd;
  668. self->nr_entries = self->stats.total_period = 0;
  669. self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  670. self->max_sym_namelen = 0;
  671. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  672. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  673. if (symbol_conf.exclude_other && !h->parent)
  674. continue;
  675. if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
  676. h->filtered |= (1 << HIST_FILTER__DSO);
  677. continue;
  678. }
  679. h->filtered &= ~(1 << HIST_FILTER__DSO);
  680. if (!h->filtered) {
  681. ++self->nr_entries;
  682. self->stats.total_period += h->period;
  683. self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
  684. if (h->ms.sym &&
  685. self->max_sym_namelen < h->ms.sym->namelen)
  686. self->max_sym_namelen = h->ms.sym->namelen;
  687. }
  688. }
  689. }
  690. void hists__filter_by_thread(struct hists *self, const struct thread *thread)
  691. {
  692. struct rb_node *nd;
  693. self->nr_entries = self->stats.total_period = 0;
  694. self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  695. self->max_sym_namelen = 0;
  696. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  697. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  698. if (thread != NULL && h->thread != thread) {
  699. h->filtered |= (1 << HIST_FILTER__THREAD);
  700. continue;
  701. }
  702. h->filtered &= ~(1 << HIST_FILTER__THREAD);
  703. if (!h->filtered) {
  704. ++self->nr_entries;
  705. self->stats.total_period += h->period;
  706. self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
  707. if (h->ms.sym &&
  708. self->max_sym_namelen < h->ms.sym->namelen)
  709. self->max_sym_namelen = h->ms.sym->namelen;
  710. }
  711. }
  712. }
  713. static int symbol__alloc_hist(struct symbol *self)
  714. {
  715. struct sym_priv *priv = symbol__priv(self);
  716. const int size = (sizeof(*priv->hist) +
  717. (self->end - self->start) * sizeof(u64));
  718. priv->hist = zalloc(size);
  719. return priv->hist == NULL ? -1 : 0;
  720. }
  721. int hist_entry__inc_addr_samples(struct hist_entry *self, u64 ip)
  722. {
  723. unsigned int sym_size, offset;
  724. struct symbol *sym = self->ms.sym;
  725. struct sym_priv *priv;
  726. struct sym_hist *h;
  727. if (!sym || !self->ms.map)
  728. return 0;
  729. priv = symbol__priv(sym);
  730. if (priv->hist == NULL && symbol__alloc_hist(sym) < 0)
  731. return -ENOMEM;
  732. sym_size = sym->end - sym->start;
  733. offset = ip - sym->start;
  734. pr_debug3("%s: ip=%#Lx\n", __func__, self->ms.map->unmap_ip(self->ms.map, ip));
  735. if (offset >= sym_size)
  736. return 0;
  737. h = priv->hist;
  738. h->sum++;
  739. h->ip[offset]++;
  740. pr_debug3("%#Lx %s: period++ [ip: %#Lx, %#Lx] => %Ld\n", self->ms.sym->start,
  741. self->ms.sym->name, ip, ip - self->ms.sym->start, h->ip[offset]);
  742. return 0;
  743. }
  744. static struct objdump_line *objdump_line__new(s64 offset, char *line)
  745. {
  746. struct objdump_line *self = malloc(sizeof(*self));
  747. if (self != NULL) {
  748. self->offset = offset;
  749. self->line = line;
  750. }
  751. return self;
  752. }
  753. void objdump_line__free(struct objdump_line *self)
  754. {
  755. free(self->line);
  756. free(self);
  757. }
  758. static void objdump__add_line(struct list_head *head, struct objdump_line *line)
  759. {
  760. list_add_tail(&line->node, head);
  761. }
  762. struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
  763. struct objdump_line *pos)
  764. {
  765. list_for_each_entry_continue(pos, head, node)
  766. if (pos->offset >= 0)
  767. return pos;
  768. return NULL;
  769. }
  770. static int hist_entry__parse_objdump_line(struct hist_entry *self, FILE *file,
  771. struct list_head *head)
  772. {
  773. struct symbol *sym = self->ms.sym;
  774. struct objdump_line *objdump_line;
  775. char *line = NULL, *tmp, *tmp2, *c;
  776. size_t line_len;
  777. s64 line_ip, offset = -1;
  778. if (getline(&line, &line_len, file) < 0)
  779. return -1;
  780. if (!line)
  781. return -1;
  782. while (line_len != 0 && isspace(line[line_len - 1]))
  783. line[--line_len] = '\0';
  784. c = strchr(line, '\n');
  785. if (c)
  786. *c = 0;
  787. line_ip = -1;
  788. /*
  789. * Strip leading spaces:
  790. */
  791. tmp = line;
  792. while (*tmp) {
  793. if (*tmp != ' ')
  794. break;
  795. tmp++;
  796. }
  797. if (*tmp) {
  798. /*
  799. * Parse hexa addresses followed by ':'
  800. */
  801. line_ip = strtoull(tmp, &tmp2, 16);
  802. if (*tmp2 != ':')
  803. line_ip = -1;
  804. }
  805. if (line_ip != -1) {
  806. u64 start = map__rip_2objdump(self->ms.map, sym->start);
  807. offset = line_ip - start;
  808. }
  809. objdump_line = objdump_line__new(offset, line);
  810. if (objdump_line == NULL) {
  811. free(line);
  812. return -1;
  813. }
  814. objdump__add_line(head, objdump_line);
  815. return 0;
  816. }
  817. int hist_entry__annotate(struct hist_entry *self, struct list_head *head)
  818. {
  819. struct symbol *sym = self->ms.sym;
  820. struct map *map = self->ms.map;
  821. struct dso *dso = map->dso;
  822. char *filename = dso__build_id_filename(dso, NULL, 0);
  823. bool free_filename = true;
  824. char command[PATH_MAX * 2];
  825. FILE *file;
  826. int err = 0;
  827. u64 len;
  828. if (filename == NULL) {
  829. if (dso->has_build_id) {
  830. pr_err("Can't annotate %s: not enough memory\n",
  831. sym->name);
  832. return -ENOMEM;
  833. }
  834. goto fallback;
  835. } else if (readlink(filename, command, sizeof(command)) < 0 ||
  836. strstr(command, "[kernel.kallsyms]") ||
  837. access(filename, R_OK)) {
  838. free(filename);
  839. fallback:
  840. /*
  841. * If we don't have build-ids or the build-id file isn't in the
  842. * cache, or is just a kallsyms file, well, lets hope that this
  843. * DSO is the same as when 'perf record' ran.
  844. */
  845. filename = dso->long_name;
  846. free_filename = false;
  847. }
  848. if (dso->origin == DSO__ORIG_KERNEL) {
  849. if (dso->annotate_warned)
  850. goto out_free_filename;
  851. err = -ENOENT;
  852. dso->annotate_warned = 1;
  853. pr_err("Can't annotate %s: No vmlinux file was found in the "
  854. "path\n", sym->name);
  855. goto out_free_filename;
  856. }
  857. pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
  858. filename, sym->name, map->unmap_ip(map, sym->start),
  859. map->unmap_ip(map, sym->end));
  860. len = sym->end - sym->start;
  861. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  862. dso, dso->long_name, sym, sym->name);
  863. snprintf(command, sizeof(command),
  864. "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s|expand",
  865. map__rip_2objdump(map, sym->start),
  866. map__rip_2objdump(map, sym->end),
  867. filename, filename);
  868. pr_debug("Executing: %s\n", command);
  869. file = popen(command, "r");
  870. if (!file)
  871. goto out_free_filename;
  872. while (!feof(file))
  873. if (hist_entry__parse_objdump_line(self, file, head) < 0)
  874. break;
  875. pclose(file);
  876. out_free_filename:
  877. if (free_filename)
  878. free(filename);
  879. return err;
  880. }
  881. void hists__inc_nr_events(struct hists *self, u32 type)
  882. {
  883. ++self->stats.nr_events[0];
  884. ++self->stats.nr_events[type];
  885. }
  886. size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
  887. {
  888. int i;
  889. size_t ret = 0;
  890. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  891. if (!event__name[i])
  892. continue;
  893. ret += fprintf(fp, "%10s events: %10d\n",
  894. event__name[i], self->stats.nr_events[i]);
  895. }
  896. return ret;
  897. }