hist.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. #include "annotate.h"
  2. #include "util.h"
  3. #include "build-id.h"
  4. #include "hist.h"
  5. #include "session.h"
  6. #include "sort.h"
  7. #include <math.h>
  8. static bool hists__filter_entry_by_dso(struct hists *hists,
  9. struct hist_entry *he);
  10. static bool hists__filter_entry_by_thread(struct hists *hists,
  11. struct hist_entry *he);
  12. enum hist_filter {
  13. HIST_FILTER__DSO,
  14. HIST_FILTER__THREAD,
  15. HIST_FILTER__PARENT,
  16. };
  17. struct callchain_param callchain_param = {
  18. .mode = CHAIN_GRAPH_REL,
  19. .min_percent = 0.5,
  20. .order = ORDER_CALLEE
  21. };
  22. u16 hists__col_len(struct hists *hists, enum hist_column col)
  23. {
  24. return hists->col_len[col];
  25. }
  26. void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
  27. {
  28. hists->col_len[col] = len;
  29. }
  30. bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
  31. {
  32. if (len > hists__col_len(hists, col)) {
  33. hists__set_col_len(hists, col, len);
  34. return true;
  35. }
  36. return false;
  37. }
  38. static void hists__reset_col_len(struct hists *hists)
  39. {
  40. enum hist_column col;
  41. for (col = 0; col < HISTC_NR_COLS; ++col)
  42. hists__set_col_len(hists, col, 0);
  43. }
  44. static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
  45. {
  46. const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
  47. if (hists__col_len(hists, dso) < unresolved_col_width &&
  48. !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
  49. !symbol_conf.dso_list)
  50. hists__set_col_len(hists, dso, unresolved_col_width);
  51. }
  52. static void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
  53. {
  54. const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
  55. u16 len;
  56. if (h->ms.sym)
  57. hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4);
  58. else
  59. hists__set_unres_dso_col_len(hists, HISTC_DSO);
  60. len = thread__comm_len(h->thread);
  61. if (hists__new_col_len(hists, HISTC_COMM, len))
  62. hists__set_col_len(hists, HISTC_THREAD, len + 6);
  63. if (h->ms.map) {
  64. len = dso__name_len(h->ms.map->dso);
  65. hists__new_col_len(hists, HISTC_DSO, len);
  66. }
  67. if (h->branch_info) {
  68. int symlen;
  69. /*
  70. * +4 accounts for '[x] ' priv level info
  71. * +2 account of 0x prefix on raw addresses
  72. */
  73. if (h->branch_info->from.sym) {
  74. symlen = (int)h->branch_info->from.sym->namelen + 4;
  75. hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
  76. symlen = dso__name_len(h->branch_info->from.map->dso);
  77. hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
  78. } else {
  79. symlen = unresolved_col_width + 4 + 2;
  80. hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
  81. hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
  82. }
  83. if (h->branch_info->to.sym) {
  84. symlen = (int)h->branch_info->to.sym->namelen + 4;
  85. hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
  86. symlen = dso__name_len(h->branch_info->to.map->dso);
  87. hists__new_col_len(hists, HISTC_DSO_TO, symlen);
  88. } else {
  89. symlen = unresolved_col_width + 4 + 2;
  90. hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
  91. hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
  92. }
  93. }
  94. }
  95. static void hist_entry__add_cpumode_period(struct hist_entry *he,
  96. unsigned int cpumode, u64 period)
  97. {
  98. switch (cpumode) {
  99. case PERF_RECORD_MISC_KERNEL:
  100. he->period_sys += period;
  101. break;
  102. case PERF_RECORD_MISC_USER:
  103. he->period_us += period;
  104. break;
  105. case PERF_RECORD_MISC_GUEST_KERNEL:
  106. he->period_guest_sys += period;
  107. break;
  108. case PERF_RECORD_MISC_GUEST_USER:
  109. he->period_guest_us += period;
  110. break;
  111. default:
  112. break;
  113. }
  114. }
  115. static void hist_entry__decay(struct hist_entry *he)
  116. {
  117. he->period = (he->period * 7) / 8;
  118. he->nr_events = (he->nr_events * 7) / 8;
  119. }
  120. static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
  121. {
  122. u64 prev_period = he->period;
  123. if (prev_period == 0)
  124. return true;
  125. hist_entry__decay(he);
  126. if (!he->filtered)
  127. hists->stats.total_period -= prev_period - he->period;
  128. return he->period == 0;
  129. }
  130. static void __hists__decay_entries(struct hists *hists, bool zap_user,
  131. bool zap_kernel, bool threaded)
  132. {
  133. struct rb_node *next = rb_first(&hists->entries);
  134. struct hist_entry *n;
  135. while (next) {
  136. n = rb_entry(next, struct hist_entry, rb_node);
  137. next = rb_next(&n->rb_node);
  138. /*
  139. * We may be annotating this, for instance, so keep it here in
  140. * case some it gets new samples, we'll eventually free it when
  141. * the user stops browsing and it agains gets fully decayed.
  142. */
  143. if (((zap_user && n->level == '.') ||
  144. (zap_kernel && n->level != '.') ||
  145. hists__decay_entry(hists, n)) &&
  146. !n->used) {
  147. rb_erase(&n->rb_node, &hists->entries);
  148. if (sort__need_collapse || threaded)
  149. rb_erase(&n->rb_node_in, &hists->entries_collapsed);
  150. hist_entry__free(n);
  151. --hists->nr_entries;
  152. }
  153. }
  154. }
  155. void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
  156. {
  157. return __hists__decay_entries(hists, zap_user, zap_kernel, false);
  158. }
  159. void hists__decay_entries_threaded(struct hists *hists,
  160. bool zap_user, bool zap_kernel)
  161. {
  162. return __hists__decay_entries(hists, zap_user, zap_kernel, true);
  163. }
  164. /*
  165. * histogram, sorted on item, collects periods
  166. */
  167. static struct hist_entry *hist_entry__new(struct hist_entry *template)
  168. {
  169. size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0;
  170. struct hist_entry *he = malloc(sizeof(*he) + callchain_size);
  171. if (he != NULL) {
  172. *he = *template;
  173. he->nr_events = 1;
  174. if (he->ms.map)
  175. he->ms.map->referenced = true;
  176. if (symbol_conf.use_callchain)
  177. callchain_init(he->callchain);
  178. }
  179. return he;
  180. }
  181. static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h)
  182. {
  183. if (!h->filtered) {
  184. hists__calc_col_len(hists, h);
  185. ++hists->nr_entries;
  186. hists->stats.total_period += h->period;
  187. }
  188. }
  189. static u8 symbol__parent_filter(const struct symbol *parent)
  190. {
  191. if (symbol_conf.exclude_other && parent == NULL)
  192. return 1 << HIST_FILTER__PARENT;
  193. return 0;
  194. }
  195. static struct hist_entry *add_hist_entry(struct hists *hists,
  196. struct hist_entry *entry,
  197. struct addr_location *al,
  198. u64 period)
  199. {
  200. struct rb_node **p;
  201. struct rb_node *parent = NULL;
  202. struct hist_entry *he;
  203. int cmp;
  204. pthread_mutex_lock(&hists->lock);
  205. p = &hists->entries_in->rb_node;
  206. while (*p != NULL) {
  207. parent = *p;
  208. he = rb_entry(parent, struct hist_entry, rb_node_in);
  209. cmp = hist_entry__cmp(entry, he);
  210. if (!cmp) {
  211. he->period += period;
  212. ++he->nr_events;
  213. goto out;
  214. }
  215. if (cmp < 0)
  216. p = &(*p)->rb_left;
  217. else
  218. p = &(*p)->rb_right;
  219. }
  220. he = hist_entry__new(entry);
  221. if (!he)
  222. goto out_unlock;
  223. rb_link_node(&he->rb_node_in, parent, p);
  224. rb_insert_color(&he->rb_node_in, hists->entries_in);
  225. out:
  226. hist_entry__add_cpumode_period(he, al->cpumode, period);
  227. out_unlock:
  228. pthread_mutex_unlock(&hists->lock);
  229. return he;
  230. }
  231. struct hist_entry *__hists__add_branch_entry(struct hists *self,
  232. struct addr_location *al,
  233. struct symbol *sym_parent,
  234. struct branch_info *bi,
  235. u64 period)
  236. {
  237. struct hist_entry entry = {
  238. .thread = al->thread,
  239. .ms = {
  240. .map = bi->to.map,
  241. .sym = bi->to.sym,
  242. },
  243. .cpu = al->cpu,
  244. .ip = bi->to.addr,
  245. .level = al->level,
  246. .period = period,
  247. .parent = sym_parent,
  248. .filtered = symbol__parent_filter(sym_parent),
  249. .branch_info = bi,
  250. };
  251. return add_hist_entry(self, &entry, al, period);
  252. }
  253. struct hist_entry *__hists__add_entry(struct hists *self,
  254. struct addr_location *al,
  255. struct symbol *sym_parent, u64 period)
  256. {
  257. struct hist_entry entry = {
  258. .thread = al->thread,
  259. .ms = {
  260. .map = al->map,
  261. .sym = al->sym,
  262. },
  263. .cpu = al->cpu,
  264. .ip = al->addr,
  265. .level = al->level,
  266. .period = period,
  267. .parent = sym_parent,
  268. .filtered = symbol__parent_filter(sym_parent),
  269. };
  270. return add_hist_entry(self, &entry, al, period);
  271. }
  272. int64_t
  273. hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
  274. {
  275. struct sort_entry *se;
  276. int64_t cmp = 0;
  277. list_for_each_entry(se, &hist_entry__sort_list, list) {
  278. cmp = se->se_cmp(left, right);
  279. if (cmp)
  280. break;
  281. }
  282. return cmp;
  283. }
  284. int64_t
  285. hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
  286. {
  287. struct sort_entry *se;
  288. int64_t cmp = 0;
  289. list_for_each_entry(se, &hist_entry__sort_list, list) {
  290. int64_t (*f)(struct hist_entry *, struct hist_entry *);
  291. f = se->se_collapse ?: se->se_cmp;
  292. cmp = f(left, right);
  293. if (cmp)
  294. break;
  295. }
  296. return cmp;
  297. }
  298. void hist_entry__free(struct hist_entry *he)
  299. {
  300. free(he);
  301. }
  302. /*
  303. * collapse the histogram
  304. */
  305. static bool hists__collapse_insert_entry(struct hists *hists,
  306. struct rb_root *root,
  307. struct hist_entry *he)
  308. {
  309. struct rb_node **p = &root->rb_node;
  310. struct rb_node *parent = NULL;
  311. struct hist_entry *iter;
  312. int64_t cmp;
  313. while (*p != NULL) {
  314. parent = *p;
  315. iter = rb_entry(parent, struct hist_entry, rb_node_in);
  316. cmp = hist_entry__collapse(iter, he);
  317. if (!cmp) {
  318. iter->period += he->period;
  319. iter->nr_events += he->nr_events;
  320. if (symbol_conf.use_callchain) {
  321. callchain_cursor_reset(&hists->callchain_cursor);
  322. callchain_merge(&hists->callchain_cursor, iter->callchain,
  323. he->callchain);
  324. }
  325. hist_entry__free(he);
  326. return false;
  327. }
  328. if (cmp < 0)
  329. p = &(*p)->rb_left;
  330. else
  331. p = &(*p)->rb_right;
  332. }
  333. rb_link_node(&he->rb_node_in, parent, p);
  334. rb_insert_color(&he->rb_node_in, root);
  335. return true;
  336. }
  337. static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
  338. {
  339. struct rb_root *root;
  340. pthread_mutex_lock(&hists->lock);
  341. root = hists->entries_in;
  342. if (++hists->entries_in > &hists->entries_in_array[1])
  343. hists->entries_in = &hists->entries_in_array[0];
  344. pthread_mutex_unlock(&hists->lock);
  345. return root;
  346. }
  347. static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
  348. {
  349. hists__filter_entry_by_dso(hists, he);
  350. hists__filter_entry_by_thread(hists, he);
  351. }
  352. static void __hists__collapse_resort(struct hists *hists, bool threaded)
  353. {
  354. struct rb_root *root;
  355. struct rb_node *next;
  356. struct hist_entry *n;
  357. if (!sort__need_collapse && !threaded)
  358. return;
  359. root = hists__get_rotate_entries_in(hists);
  360. next = rb_first(root);
  361. while (next) {
  362. n = rb_entry(next, struct hist_entry, rb_node_in);
  363. next = rb_next(&n->rb_node_in);
  364. rb_erase(&n->rb_node_in, root);
  365. if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
  366. /*
  367. * If it wasn't combined with one of the entries already
  368. * collapsed, we need to apply the filters that may have
  369. * been set by, say, the hist_browser.
  370. */
  371. hists__apply_filters(hists, n);
  372. }
  373. }
  374. }
  375. void hists__collapse_resort(struct hists *hists)
  376. {
  377. return __hists__collapse_resort(hists, false);
  378. }
  379. void hists__collapse_resort_threaded(struct hists *hists)
  380. {
  381. return __hists__collapse_resort(hists, true);
  382. }
  383. /*
  384. * reverse the map, sort on period.
  385. */
  386. static void __hists__insert_output_entry(struct rb_root *entries,
  387. struct hist_entry *he,
  388. u64 min_callchain_hits)
  389. {
  390. struct rb_node **p = &entries->rb_node;
  391. struct rb_node *parent = NULL;
  392. struct hist_entry *iter;
  393. if (symbol_conf.use_callchain)
  394. callchain_param.sort(&he->sorted_chain, he->callchain,
  395. min_callchain_hits, &callchain_param);
  396. while (*p != NULL) {
  397. parent = *p;
  398. iter = rb_entry(parent, struct hist_entry, rb_node);
  399. if (he->period > iter->period)
  400. p = &(*p)->rb_left;
  401. else
  402. p = &(*p)->rb_right;
  403. }
  404. rb_link_node(&he->rb_node, parent, p);
  405. rb_insert_color(&he->rb_node, entries);
  406. }
  407. static void __hists__output_resort(struct hists *hists, bool threaded)
  408. {
  409. struct rb_root *root;
  410. struct rb_node *next;
  411. struct hist_entry *n;
  412. u64 min_callchain_hits;
  413. min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
  414. if (sort__need_collapse || threaded)
  415. root = &hists->entries_collapsed;
  416. else
  417. root = hists->entries_in;
  418. next = rb_first(root);
  419. hists->entries = RB_ROOT;
  420. hists->nr_entries = 0;
  421. hists->stats.total_period = 0;
  422. hists__reset_col_len(hists);
  423. while (next) {
  424. n = rb_entry(next, struct hist_entry, rb_node_in);
  425. next = rb_next(&n->rb_node_in);
  426. __hists__insert_output_entry(&hists->entries, n, min_callchain_hits);
  427. hists__inc_nr_entries(hists, n);
  428. }
  429. }
  430. void hists__output_resort(struct hists *hists)
  431. {
  432. return __hists__output_resort(hists, false);
  433. }
  434. void hists__output_resort_threaded(struct hists *hists)
  435. {
  436. return __hists__output_resort(hists, true);
  437. }
  438. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  439. {
  440. int i;
  441. int ret = fprintf(fp, " ");
  442. for (i = 0; i < left_margin; i++)
  443. ret += fprintf(fp, " ");
  444. return ret;
  445. }
  446. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  447. int left_margin)
  448. {
  449. int i;
  450. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  451. for (i = 0; i < depth; i++)
  452. if (depth_mask & (1 << i))
  453. ret += fprintf(fp, "| ");
  454. else
  455. ret += fprintf(fp, " ");
  456. ret += fprintf(fp, "\n");
  457. return ret;
  458. }
  459. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain,
  460. int depth, int depth_mask, int period,
  461. u64 total_samples, u64 hits,
  462. int left_margin)
  463. {
  464. int i;
  465. size_t ret = 0;
  466. ret += callchain__fprintf_left_margin(fp, left_margin);
  467. for (i = 0; i < depth; i++) {
  468. if (depth_mask & (1 << i))
  469. ret += fprintf(fp, "|");
  470. else
  471. ret += fprintf(fp, " ");
  472. if (!period && i == depth - 1) {
  473. double percent;
  474. percent = hits * 100.0 / total_samples;
  475. ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
  476. } else
  477. ret += fprintf(fp, "%s", " ");
  478. }
  479. if (chain->ms.sym)
  480. ret += fprintf(fp, "%s\n", chain->ms.sym->name);
  481. else
  482. ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
  483. return ret;
  484. }
  485. static struct symbol *rem_sq_bracket;
  486. static struct callchain_list rem_hits;
  487. static void init_rem_hits(void)
  488. {
  489. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  490. if (!rem_sq_bracket) {
  491. fprintf(stderr, "Not enough memory to display remaining hits\n");
  492. return;
  493. }
  494. strcpy(rem_sq_bracket->name, "[...]");
  495. rem_hits.ms.sym = rem_sq_bracket;
  496. }
  497. static size_t __callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  498. u64 total_samples, int depth,
  499. int depth_mask, int left_margin)
  500. {
  501. struct rb_node *node, *next;
  502. struct callchain_node *child;
  503. struct callchain_list *chain;
  504. int new_depth_mask = depth_mask;
  505. u64 new_total;
  506. u64 remaining;
  507. size_t ret = 0;
  508. int i;
  509. uint entries_printed = 0;
  510. if (callchain_param.mode == CHAIN_GRAPH_REL)
  511. new_total = self->children_hit;
  512. else
  513. new_total = total_samples;
  514. remaining = new_total;
  515. node = rb_first(&self->rb_root);
  516. while (node) {
  517. u64 cumul;
  518. child = rb_entry(node, struct callchain_node, rb_node);
  519. cumul = callchain_cumul_hits(child);
  520. remaining -= cumul;
  521. /*
  522. * The depth mask manages the output of pipes that show
  523. * the depth. We don't want to keep the pipes of the current
  524. * level for the last child of this depth.
  525. * Except if we have remaining filtered hits. They will
  526. * supersede the last child
  527. */
  528. next = rb_next(node);
  529. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  530. new_depth_mask &= ~(1 << (depth - 1));
  531. /*
  532. * But we keep the older depth mask for the line separator
  533. * to keep the level link until we reach the last child
  534. */
  535. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  536. left_margin);
  537. i = 0;
  538. list_for_each_entry(chain, &child->val, list) {
  539. ret += ipchain__fprintf_graph(fp, chain, depth,
  540. new_depth_mask, i++,
  541. new_total,
  542. cumul,
  543. left_margin);
  544. }
  545. ret += __callchain__fprintf_graph(fp, child, new_total,
  546. depth + 1,
  547. new_depth_mask | (1 << depth),
  548. left_margin);
  549. node = next;
  550. if (++entries_printed == callchain_param.print_limit)
  551. break;
  552. }
  553. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  554. remaining && remaining != new_total) {
  555. if (!rem_sq_bracket)
  556. return ret;
  557. new_depth_mask &= ~(1 << (depth - 1));
  558. ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
  559. new_depth_mask, 0, new_total,
  560. remaining, left_margin);
  561. }
  562. return ret;
  563. }
  564. static size_t callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
  565. u64 total_samples, int left_margin)
  566. {
  567. struct callchain_list *chain;
  568. bool printed = false;
  569. int i = 0;
  570. int ret = 0;
  571. u32 entries_printed = 0;
  572. list_for_each_entry(chain, &self->val, list) {
  573. if (!i++ && sort__first_dimension == SORT_SYM)
  574. continue;
  575. if (!printed) {
  576. ret += callchain__fprintf_left_margin(fp, left_margin);
  577. ret += fprintf(fp, "|\n");
  578. ret += callchain__fprintf_left_margin(fp, left_margin);
  579. ret += fprintf(fp, "---");
  580. left_margin += 3;
  581. printed = true;
  582. } else
  583. ret += callchain__fprintf_left_margin(fp, left_margin);
  584. if (chain->ms.sym)
  585. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  586. else
  587. ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
  588. if (++entries_printed == callchain_param.print_limit)
  589. break;
  590. }
  591. ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
  592. return ret;
  593. }
  594. static size_t callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
  595. u64 total_samples)
  596. {
  597. struct callchain_list *chain;
  598. size_t ret = 0;
  599. if (!self)
  600. return 0;
  601. ret += callchain__fprintf_flat(fp, self->parent, total_samples);
  602. list_for_each_entry(chain, &self->val, list) {
  603. if (chain->ip >= PERF_CONTEXT_MAX)
  604. continue;
  605. if (chain->ms.sym)
  606. ret += fprintf(fp, " %s\n", chain->ms.sym->name);
  607. else
  608. ret += fprintf(fp, " %p\n",
  609. (void *)(long)chain->ip);
  610. }
  611. return ret;
  612. }
  613. static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
  614. u64 total_samples, int left_margin,
  615. FILE *fp)
  616. {
  617. struct rb_node *rb_node;
  618. struct callchain_node *chain;
  619. size_t ret = 0;
  620. u32 entries_printed = 0;
  621. rb_node = rb_first(&he->sorted_chain);
  622. while (rb_node) {
  623. double percent;
  624. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  625. percent = chain->hit * 100.0 / total_samples;
  626. switch (callchain_param.mode) {
  627. case CHAIN_FLAT:
  628. ret += percent_color_fprintf(fp, " %6.2f%%\n",
  629. percent);
  630. ret += callchain__fprintf_flat(fp, chain, total_samples);
  631. break;
  632. case CHAIN_GRAPH_ABS: /* Falldown */
  633. case CHAIN_GRAPH_REL:
  634. ret += callchain__fprintf_graph(fp, chain, total_samples,
  635. left_margin);
  636. case CHAIN_NONE:
  637. default:
  638. break;
  639. }
  640. ret += fprintf(fp, "\n");
  641. if (++entries_printed == callchain_param.print_limit)
  642. break;
  643. rb_node = rb_next(rb_node);
  644. }
  645. return ret;
  646. }
  647. void hists__output_recalc_col_len(struct hists *hists, int max_rows)
  648. {
  649. struct rb_node *next = rb_first(&hists->entries);
  650. struct hist_entry *n;
  651. int row = 0;
  652. hists__reset_col_len(hists);
  653. while (next && row++ < max_rows) {
  654. n = rb_entry(next, struct hist_entry, rb_node);
  655. if (!n->filtered)
  656. hists__calc_col_len(hists, n);
  657. next = rb_next(&n->rb_node);
  658. }
  659. }
  660. static int hist_entry__pcnt_snprintf(struct hist_entry *he, char *s,
  661. size_t size, struct hists *pair_hists,
  662. bool show_displacement, long displacement,
  663. bool color, u64 total_period)
  664. {
  665. u64 period, total, period_sys, period_us, period_guest_sys, period_guest_us;
  666. u64 nr_events;
  667. const char *sep = symbol_conf.field_sep;
  668. int ret;
  669. if (symbol_conf.exclude_other && !he->parent)
  670. return 0;
  671. if (pair_hists) {
  672. period = he->pair ? he->pair->period : 0;
  673. nr_events = he->pair ? he->pair->nr_events : 0;
  674. total = pair_hists->stats.total_period;
  675. period_sys = he->pair ? he->pair->period_sys : 0;
  676. period_us = he->pair ? he->pair->period_us : 0;
  677. period_guest_sys = he->pair ? he->pair->period_guest_sys : 0;
  678. period_guest_us = he->pair ? he->pair->period_guest_us : 0;
  679. } else {
  680. period = he->period;
  681. nr_events = he->nr_events;
  682. total = total_period;
  683. period_sys = he->period_sys;
  684. period_us = he->period_us;
  685. period_guest_sys = he->period_guest_sys;
  686. period_guest_us = he->period_guest_us;
  687. }
  688. if (total) {
  689. if (color)
  690. ret = percent_color_snprintf(s, size,
  691. sep ? "%.2f" : " %6.2f%%",
  692. (period * 100.0) / total);
  693. else
  694. ret = scnprintf(s, size, sep ? "%.2f" : " %6.2f%%",
  695. (period * 100.0) / total);
  696. if (symbol_conf.show_cpu_utilization) {
  697. ret += percent_color_snprintf(s + ret, size - ret,
  698. sep ? "%.2f" : " %6.2f%%",
  699. (period_sys * 100.0) / total);
  700. ret += percent_color_snprintf(s + ret, size - ret,
  701. sep ? "%.2f" : " %6.2f%%",
  702. (period_us * 100.0) / total);
  703. if (perf_guest) {
  704. ret += percent_color_snprintf(s + ret,
  705. size - ret,
  706. sep ? "%.2f" : " %6.2f%%",
  707. (period_guest_sys * 100.0) /
  708. total);
  709. ret += percent_color_snprintf(s + ret,
  710. size - ret,
  711. sep ? "%.2f" : " %6.2f%%",
  712. (period_guest_us * 100.0) /
  713. total);
  714. }
  715. }
  716. } else
  717. ret = scnprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
  718. if (symbol_conf.show_nr_samples) {
  719. if (sep)
  720. ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
  721. else
  722. ret += scnprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
  723. }
  724. if (symbol_conf.show_total_period) {
  725. if (sep)
  726. ret += scnprintf(s + ret, size - ret, "%c%" PRIu64, *sep, period);
  727. else
  728. ret += scnprintf(s + ret, size - ret, " %12" PRIu64, period);
  729. }
  730. if (pair_hists) {
  731. char bf[32];
  732. double old_percent = 0, new_percent = 0, diff;
  733. if (total > 0)
  734. old_percent = (period * 100.0) / total;
  735. if (total_period > 0)
  736. new_percent = (he->period * 100.0) / total_period;
  737. diff = new_percent - old_percent;
  738. if (fabs(diff) >= 0.01)
  739. ret += scnprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  740. else
  741. ret += scnprintf(bf, sizeof(bf), " ");
  742. if (sep)
  743. ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
  744. else
  745. ret += scnprintf(s + ret, size - ret, "%11.11s", bf);
  746. if (show_displacement) {
  747. if (displacement)
  748. ret += scnprintf(bf, sizeof(bf), "%+4ld", displacement);
  749. else
  750. ret += scnprintf(bf, sizeof(bf), " ");
  751. if (sep)
  752. ret += scnprintf(s + ret, size - ret, "%c%s", *sep, bf);
  753. else
  754. ret += scnprintf(s + ret, size - ret, "%6.6s", bf);
  755. }
  756. }
  757. return ret;
  758. }
  759. int hist_entry__snprintf(struct hist_entry *he, char *s, size_t size,
  760. struct hists *hists)
  761. {
  762. const char *sep = symbol_conf.field_sep;
  763. struct sort_entry *se;
  764. int ret = 0;
  765. list_for_each_entry(se, &hist_entry__sort_list, list) {
  766. if (se->elide)
  767. continue;
  768. ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
  769. ret += se->se_snprintf(he, s + ret, size - ret,
  770. hists__col_len(hists, se->se_width_idx));
  771. }
  772. return ret;
  773. }
  774. static int hist_entry__fprintf(struct hist_entry *he, size_t size,
  775. struct hists *hists, struct hists *pair_hists,
  776. bool show_displacement, long displacement,
  777. u64 total_period, FILE *fp)
  778. {
  779. char bf[512];
  780. int ret;
  781. if (size == 0 || size > sizeof(bf))
  782. size = sizeof(bf);
  783. ret = hist_entry__pcnt_snprintf(he, bf, size, pair_hists,
  784. show_displacement, displacement,
  785. true, total_period);
  786. hist_entry__snprintf(he, bf + ret, size - ret, hists);
  787. return fprintf(fp, "%s\n", bf);
  788. }
  789. static size_t hist_entry__fprintf_callchain(struct hist_entry *he,
  790. struct hists *hists,
  791. u64 total_period, FILE *fp)
  792. {
  793. int left_margin = 0;
  794. if (sort__first_dimension == SORT_COMM) {
  795. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  796. typeof(*se), list);
  797. left_margin = hists__col_len(hists, se->se_width_idx);
  798. left_margin -= thread__comm_len(he->thread);
  799. }
  800. return hist_entry_callchain__fprintf(he, total_period, left_margin, fp);
  801. }
  802. size_t hists__fprintf(struct hists *hists, struct hists *pair,
  803. bool show_displacement, bool show_header, int max_rows,
  804. int max_cols, FILE *fp)
  805. {
  806. struct sort_entry *se;
  807. struct rb_node *nd;
  808. size_t ret = 0;
  809. u64 total_period;
  810. unsigned long position = 1;
  811. long displacement = 0;
  812. unsigned int width;
  813. const char *sep = symbol_conf.field_sep;
  814. const char *col_width = symbol_conf.col_width_list_str;
  815. int nr_rows = 0;
  816. init_rem_hits();
  817. if (!show_header)
  818. goto print_entries;
  819. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  820. if (symbol_conf.show_cpu_utilization) {
  821. if (sep) {
  822. ret += fprintf(fp, "%csys", *sep);
  823. ret += fprintf(fp, "%cus", *sep);
  824. if (perf_guest) {
  825. ret += fprintf(fp, "%cguest sys", *sep);
  826. ret += fprintf(fp, "%cguest us", *sep);
  827. }
  828. } else {
  829. ret += fprintf(fp, " sys ");
  830. ret += fprintf(fp, " us ");
  831. if (perf_guest) {
  832. ret += fprintf(fp, " guest sys ");
  833. ret += fprintf(fp, " guest us ");
  834. }
  835. }
  836. }
  837. if (symbol_conf.show_nr_samples) {
  838. if (sep)
  839. fprintf(fp, "%cSamples", *sep);
  840. else
  841. fputs(" Samples ", fp);
  842. }
  843. if (symbol_conf.show_total_period) {
  844. if (sep)
  845. ret += fprintf(fp, "%cPeriod", *sep);
  846. else
  847. ret += fprintf(fp, " Period ");
  848. }
  849. if (pair) {
  850. if (sep)
  851. ret += fprintf(fp, "%cDelta", *sep);
  852. else
  853. ret += fprintf(fp, " Delta ");
  854. if (show_displacement) {
  855. if (sep)
  856. ret += fprintf(fp, "%cDisplacement", *sep);
  857. else
  858. ret += fprintf(fp, " Displ");
  859. }
  860. }
  861. list_for_each_entry(se, &hist_entry__sort_list, list) {
  862. if (se->elide)
  863. continue;
  864. if (sep) {
  865. fprintf(fp, "%c%s", *sep, se->se_header);
  866. continue;
  867. }
  868. width = strlen(se->se_header);
  869. if (symbol_conf.col_width_list_str) {
  870. if (col_width) {
  871. hists__set_col_len(hists, se->se_width_idx,
  872. atoi(col_width));
  873. col_width = strchr(col_width, ',');
  874. if (col_width)
  875. ++col_width;
  876. }
  877. }
  878. if (!hists__new_col_len(hists, se->se_width_idx, width))
  879. width = hists__col_len(hists, se->se_width_idx);
  880. fprintf(fp, " %*s", width, se->se_header);
  881. }
  882. fprintf(fp, "\n");
  883. if (max_rows && ++nr_rows >= max_rows)
  884. goto out;
  885. if (sep)
  886. goto print_entries;
  887. fprintf(fp, "# ........");
  888. if (symbol_conf.show_cpu_utilization)
  889. fprintf(fp, " ....... .......");
  890. if (symbol_conf.show_nr_samples)
  891. fprintf(fp, " ..........");
  892. if (symbol_conf.show_total_period)
  893. fprintf(fp, " ............");
  894. if (pair) {
  895. fprintf(fp, " ..........");
  896. if (show_displacement)
  897. fprintf(fp, " .....");
  898. }
  899. list_for_each_entry(se, &hist_entry__sort_list, list) {
  900. unsigned int i;
  901. if (se->elide)
  902. continue;
  903. fprintf(fp, " ");
  904. width = hists__col_len(hists, se->se_width_idx);
  905. if (width == 0)
  906. width = strlen(se->se_header);
  907. for (i = 0; i < width; i++)
  908. fprintf(fp, ".");
  909. }
  910. fprintf(fp, "\n");
  911. if (max_rows && ++nr_rows >= max_rows)
  912. goto out;
  913. fprintf(fp, "#\n");
  914. if (max_rows && ++nr_rows >= max_rows)
  915. goto out;
  916. print_entries:
  917. total_period = hists->stats.total_period;
  918. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  919. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  920. if (h->filtered)
  921. continue;
  922. if (show_displacement) {
  923. if (h->pair != NULL)
  924. displacement = ((long)h->pair->position -
  925. (long)position);
  926. else
  927. displacement = 0;
  928. ++position;
  929. }
  930. ret += hist_entry__fprintf(h, max_cols, hists, pair, show_displacement,
  931. displacement, total_period, fp);
  932. if (symbol_conf.use_callchain)
  933. ret += hist_entry__fprintf_callchain(h, hists, total_period, fp);
  934. if (max_rows && ++nr_rows >= max_rows)
  935. goto out;
  936. if (h->ms.map == NULL && verbose > 1) {
  937. __map_groups__fprintf_maps(&h->thread->mg,
  938. MAP__FUNCTION, verbose, fp);
  939. fprintf(fp, "%.10s end\n", graph_dotted_line);
  940. }
  941. }
  942. out:
  943. free(rem_sq_bracket);
  944. return ret;
  945. }
  946. /*
  947. * See hists__fprintf to match the column widths
  948. */
  949. unsigned int hists__sort_list_width(struct hists *hists)
  950. {
  951. struct sort_entry *se;
  952. int ret = 9; /* total % */
  953. if (symbol_conf.show_cpu_utilization) {
  954. ret += 7; /* count_sys % */
  955. ret += 6; /* count_us % */
  956. if (perf_guest) {
  957. ret += 13; /* count_guest_sys % */
  958. ret += 12; /* count_guest_us % */
  959. }
  960. }
  961. if (symbol_conf.show_nr_samples)
  962. ret += 11;
  963. if (symbol_conf.show_total_period)
  964. ret += 13;
  965. list_for_each_entry(se, &hist_entry__sort_list, list)
  966. if (!se->elide)
  967. ret += 2 + hists__col_len(hists, se->se_width_idx);
  968. if (verbose) /* Addr + origin */
  969. ret += 3 + BITS_PER_LONG / 4;
  970. return ret;
  971. }
  972. static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
  973. enum hist_filter filter)
  974. {
  975. h->filtered &= ~(1 << filter);
  976. if (h->filtered)
  977. return;
  978. ++hists->nr_entries;
  979. if (h->ms.unfolded)
  980. hists->nr_entries += h->nr_rows;
  981. h->row_offset = 0;
  982. hists->stats.total_period += h->period;
  983. hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
  984. hists__calc_col_len(hists, h);
  985. }
  986. static bool hists__filter_entry_by_dso(struct hists *hists,
  987. struct hist_entry *he)
  988. {
  989. if (hists->dso_filter != NULL &&
  990. (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
  991. he->filtered |= (1 << HIST_FILTER__DSO);
  992. return true;
  993. }
  994. return false;
  995. }
  996. void hists__filter_by_dso(struct hists *hists)
  997. {
  998. struct rb_node *nd;
  999. hists->nr_entries = hists->stats.total_period = 0;
  1000. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  1001. hists__reset_col_len(hists);
  1002. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1003. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1004. if (symbol_conf.exclude_other && !h->parent)
  1005. continue;
  1006. if (hists__filter_entry_by_dso(hists, h))
  1007. continue;
  1008. hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
  1009. }
  1010. }
  1011. static bool hists__filter_entry_by_thread(struct hists *hists,
  1012. struct hist_entry *he)
  1013. {
  1014. if (hists->thread_filter != NULL &&
  1015. he->thread != hists->thread_filter) {
  1016. he->filtered |= (1 << HIST_FILTER__THREAD);
  1017. return true;
  1018. }
  1019. return false;
  1020. }
  1021. void hists__filter_by_thread(struct hists *hists)
  1022. {
  1023. struct rb_node *nd;
  1024. hists->nr_entries = hists->stats.total_period = 0;
  1025. hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  1026. hists__reset_col_len(hists);
  1027. for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
  1028. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  1029. if (hists__filter_entry_by_thread(hists, h))
  1030. continue;
  1031. hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
  1032. }
  1033. }
  1034. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  1035. {
  1036. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  1037. }
  1038. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  1039. {
  1040. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  1041. }
  1042. void hists__inc_nr_events(struct hists *hists, u32 type)
  1043. {
  1044. ++hists->stats.nr_events[0];
  1045. ++hists->stats.nr_events[type];
  1046. }
  1047. size_t hists__fprintf_nr_events(struct hists *hists, FILE *fp)
  1048. {
  1049. int i;
  1050. size_t ret = 0;
  1051. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  1052. const char *name;
  1053. if (hists->stats.nr_events[i] == 0)
  1054. continue;
  1055. name = perf_event__name(i);
  1056. if (!strcmp(name, "UNKNOWN"))
  1057. continue;
  1058. ret += fprintf(fp, "%16s events: %10d\n", name,
  1059. hists->stats.nr_events[i]);
  1060. }
  1061. return ret;
  1062. }