hist.c 27 KB

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