hist.c 28 KB

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