hist.c 27 KB

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