hist.c 26 KB

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