hist.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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, u64 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. u64 nr_events;
  485. const char *sep = symbol_conf.field_sep;
  486. int ret;
  487. if (symbol_conf.exclude_other && !self->parent)
  488. return 0;
  489. if (pair_hists) {
  490. period = self->pair ? self->pair->period : 0;
  491. nr_events = self->pair ? self->pair->nr_events : 0;
  492. total = pair_hists->stats.total_period;
  493. period_sys = self->pair ? self->pair->period_sys : 0;
  494. period_us = self->pair ? self->pair->period_us : 0;
  495. period_guest_sys = self->pair ? self->pair->period_guest_sys : 0;
  496. period_guest_us = self->pair ? self->pair->period_guest_us : 0;
  497. } else {
  498. period = self->period;
  499. nr_events = self->nr_events;
  500. total = session_total;
  501. period_sys = self->period_sys;
  502. period_us = self->period_us;
  503. period_guest_sys = self->period_guest_sys;
  504. period_guest_us = self->period_guest_us;
  505. }
  506. if (total) {
  507. if (color)
  508. ret = percent_color_snprintf(s, size,
  509. sep ? "%.2f" : " %6.2f%%",
  510. (period * 100.0) / total);
  511. else
  512. ret = snprintf(s, size, sep ? "%.2f" : " %6.2f%%",
  513. (period * 100.0) / total);
  514. if (symbol_conf.show_cpu_utilization) {
  515. ret += percent_color_snprintf(s + ret, size - ret,
  516. sep ? "%.2f" : " %6.2f%%",
  517. (period_sys * 100.0) / total);
  518. ret += percent_color_snprintf(s + ret, size - ret,
  519. sep ? "%.2f" : " %6.2f%%",
  520. (period_us * 100.0) / total);
  521. if (perf_guest) {
  522. ret += percent_color_snprintf(s + ret,
  523. size - ret,
  524. sep ? "%.2f" : " %6.2f%%",
  525. (period_guest_sys * 100.0) /
  526. total);
  527. ret += percent_color_snprintf(s + ret,
  528. size - ret,
  529. sep ? "%.2f" : " %6.2f%%",
  530. (period_guest_us * 100.0) /
  531. total);
  532. }
  533. }
  534. } else
  535. ret = snprintf(s, size, sep ? "%" PRIu64 : "%12" PRIu64 " ", period);
  536. if (symbol_conf.show_nr_samples) {
  537. if (sep)
  538. ret += snprintf(s + ret, size - ret, "%c%" PRIu64, *sep, nr_events);
  539. else
  540. ret += snprintf(s + ret, size - ret, "%11" PRIu64, nr_events);
  541. }
  542. if (pair_hists) {
  543. char bf[32];
  544. double old_percent = 0, new_percent = 0, diff;
  545. if (total > 0)
  546. old_percent = (period * 100.0) / total;
  547. if (session_total > 0)
  548. new_percent = (self->period * 100.0) / session_total;
  549. diff = new_percent - old_percent;
  550. if (fabs(diff) >= 0.01)
  551. snprintf(bf, sizeof(bf), "%+4.2F%%", diff);
  552. else
  553. snprintf(bf, sizeof(bf), " ");
  554. if (sep)
  555. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  556. else
  557. ret += snprintf(s + ret, size - ret, "%11.11s", bf);
  558. if (show_displacement) {
  559. if (displacement)
  560. snprintf(bf, sizeof(bf), "%+4ld", displacement);
  561. else
  562. snprintf(bf, sizeof(bf), " ");
  563. if (sep)
  564. ret += snprintf(s + ret, size - ret, "%c%s", *sep, bf);
  565. else
  566. ret += snprintf(s + ret, size - ret, "%6.6s", bf);
  567. }
  568. }
  569. list_for_each_entry(se, &hist_entry__sort_list, list) {
  570. if (se->elide)
  571. continue;
  572. ret += snprintf(s + ret, size - ret, "%s", sep ?: " ");
  573. ret += se->se_snprintf(self, s + ret, size - ret,
  574. hists__col_len(hists, se->se_width_idx));
  575. }
  576. return ret;
  577. }
  578. int hist_entry__fprintf(struct hist_entry *self, struct hists *hists,
  579. struct hists *pair_hists, bool show_displacement,
  580. long displacement, FILE *fp, u64 session_total)
  581. {
  582. char bf[512];
  583. hist_entry__snprintf(self, bf, sizeof(bf), hists, pair_hists,
  584. show_displacement, displacement,
  585. true, session_total);
  586. return fprintf(fp, "%s\n", bf);
  587. }
  588. static size_t hist_entry__fprintf_callchain(struct hist_entry *self,
  589. struct hists *hists, FILE *fp,
  590. u64 session_total)
  591. {
  592. int left_margin = 0;
  593. if (sort__first_dimension == SORT_COMM) {
  594. struct sort_entry *se = list_first_entry(&hist_entry__sort_list,
  595. typeof(*se), list);
  596. left_margin = hists__col_len(hists, se->se_width_idx);
  597. left_margin -= thread__comm_len(self->thread);
  598. }
  599. return hist_entry_callchain__fprintf(fp, self, session_total,
  600. left_margin);
  601. }
  602. size_t hists__fprintf(struct hists *self, struct hists *pair,
  603. bool show_displacement, FILE *fp)
  604. {
  605. struct sort_entry *se;
  606. struct rb_node *nd;
  607. size_t ret = 0;
  608. unsigned long position = 1;
  609. long displacement = 0;
  610. unsigned int width;
  611. const char *sep = symbol_conf.field_sep;
  612. const char *col_width = symbol_conf.col_width_list_str;
  613. init_rem_hits();
  614. fprintf(fp, "# %s", pair ? "Baseline" : "Overhead");
  615. if (symbol_conf.show_nr_samples) {
  616. if (sep)
  617. fprintf(fp, "%cSamples", *sep);
  618. else
  619. fputs(" Samples ", fp);
  620. }
  621. if (symbol_conf.show_cpu_utilization) {
  622. if (sep) {
  623. ret += fprintf(fp, "%csys", *sep);
  624. ret += fprintf(fp, "%cus", *sep);
  625. if (perf_guest) {
  626. ret += fprintf(fp, "%cguest sys", *sep);
  627. ret += fprintf(fp, "%cguest us", *sep);
  628. }
  629. } else {
  630. ret += fprintf(fp, " sys ");
  631. ret += fprintf(fp, " us ");
  632. if (perf_guest) {
  633. ret += fprintf(fp, " guest sys ");
  634. ret += fprintf(fp, " guest us ");
  635. }
  636. }
  637. }
  638. if (pair) {
  639. if (sep)
  640. ret += fprintf(fp, "%cDelta", *sep);
  641. else
  642. ret += fprintf(fp, " Delta ");
  643. if (show_displacement) {
  644. if (sep)
  645. ret += fprintf(fp, "%cDisplacement", *sep);
  646. else
  647. ret += fprintf(fp, " Displ");
  648. }
  649. }
  650. list_for_each_entry(se, &hist_entry__sort_list, list) {
  651. if (se->elide)
  652. continue;
  653. if (sep) {
  654. fprintf(fp, "%c%s", *sep, se->se_header);
  655. continue;
  656. }
  657. width = strlen(se->se_header);
  658. if (symbol_conf.col_width_list_str) {
  659. if (col_width) {
  660. hists__set_col_len(self, se->se_width_idx,
  661. atoi(col_width));
  662. col_width = strchr(col_width, ',');
  663. if (col_width)
  664. ++col_width;
  665. }
  666. }
  667. if (!hists__new_col_len(self, se->se_width_idx, width))
  668. width = hists__col_len(self, se->se_width_idx);
  669. fprintf(fp, " %*s", width, se->se_header);
  670. }
  671. fprintf(fp, "\n");
  672. if (sep)
  673. goto print_entries;
  674. fprintf(fp, "# ........");
  675. if (symbol_conf.show_nr_samples)
  676. fprintf(fp, " ..........");
  677. if (pair) {
  678. fprintf(fp, " ..........");
  679. if (show_displacement)
  680. fprintf(fp, " .....");
  681. }
  682. list_for_each_entry(se, &hist_entry__sort_list, list) {
  683. unsigned int i;
  684. if (se->elide)
  685. continue;
  686. fprintf(fp, " ");
  687. width = hists__col_len(self, se->se_width_idx);
  688. if (width == 0)
  689. width = strlen(se->se_header);
  690. for (i = 0; i < width; i++)
  691. fprintf(fp, ".");
  692. }
  693. fprintf(fp, "\n#\n");
  694. print_entries:
  695. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  696. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  697. if (show_displacement) {
  698. if (h->pair != NULL)
  699. displacement = ((long)h->pair->position -
  700. (long)position);
  701. else
  702. displacement = 0;
  703. ++position;
  704. }
  705. ret += hist_entry__fprintf(h, self, pair, show_displacement,
  706. displacement, fp, self->stats.total_period);
  707. if (symbol_conf.use_callchain)
  708. ret += hist_entry__fprintf_callchain(h, self, fp,
  709. self->stats.total_period);
  710. if (h->ms.map == NULL && verbose > 1) {
  711. __map_groups__fprintf_maps(&h->thread->mg,
  712. MAP__FUNCTION, verbose, fp);
  713. fprintf(fp, "%.10s end\n", graph_dotted_line);
  714. }
  715. }
  716. free(rem_sq_bracket);
  717. return ret;
  718. }
  719. /*
  720. * See hists__fprintf to match the column widths
  721. */
  722. unsigned int hists__sort_list_width(struct hists *self)
  723. {
  724. struct sort_entry *se;
  725. int ret = 9; /* total % */
  726. if (symbol_conf.show_cpu_utilization) {
  727. ret += 7; /* count_sys % */
  728. ret += 6; /* count_us % */
  729. if (perf_guest) {
  730. ret += 13; /* count_guest_sys % */
  731. ret += 12; /* count_guest_us % */
  732. }
  733. }
  734. if (symbol_conf.show_nr_samples)
  735. ret += 11;
  736. list_for_each_entry(se, &hist_entry__sort_list, list)
  737. if (!se->elide)
  738. ret += 2 + hists__col_len(self, se->se_width_idx);
  739. if (verbose) /* Addr + origin */
  740. ret += 3 + BITS_PER_LONG / 4;
  741. return ret;
  742. }
  743. static void hists__remove_entry_filter(struct hists *self, struct hist_entry *h,
  744. enum hist_filter filter)
  745. {
  746. h->filtered &= ~(1 << filter);
  747. if (h->filtered)
  748. return;
  749. ++self->nr_entries;
  750. if (h->ms.unfolded)
  751. self->nr_entries += h->nr_rows;
  752. h->row_offset = 0;
  753. self->stats.total_period += h->period;
  754. self->stats.nr_events[PERF_RECORD_SAMPLE] += h->nr_events;
  755. hists__calc_col_len(self, h);
  756. }
  757. void hists__filter_by_dso(struct hists *self, const struct dso *dso)
  758. {
  759. struct rb_node *nd;
  760. self->nr_entries = self->stats.total_period = 0;
  761. self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  762. hists__reset_col_len(self);
  763. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  764. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  765. if (symbol_conf.exclude_other && !h->parent)
  766. continue;
  767. if (dso != NULL && (h->ms.map == NULL || h->ms.map->dso != dso)) {
  768. h->filtered |= (1 << HIST_FILTER__DSO);
  769. continue;
  770. }
  771. hists__remove_entry_filter(self, h, HIST_FILTER__DSO);
  772. }
  773. }
  774. void hists__filter_by_thread(struct hists *self, const struct thread *thread)
  775. {
  776. struct rb_node *nd;
  777. self->nr_entries = self->stats.total_period = 0;
  778. self->stats.nr_events[PERF_RECORD_SAMPLE] = 0;
  779. hists__reset_col_len(self);
  780. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  781. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  782. if (thread != NULL && h->thread != thread) {
  783. h->filtered |= (1 << HIST_FILTER__THREAD);
  784. continue;
  785. }
  786. hists__remove_entry_filter(self, h, HIST_FILTER__THREAD);
  787. }
  788. }
  789. static int symbol__alloc_hist(struct symbol *self)
  790. {
  791. struct sym_priv *priv = symbol__priv(self);
  792. const int size = (sizeof(*priv->hist) +
  793. (self->end - self->start) * sizeof(u64));
  794. priv->hist = zalloc(size);
  795. return priv->hist == NULL ? -1 : 0;
  796. }
  797. int hist_entry__inc_addr_samples(struct hist_entry *self, u64 ip)
  798. {
  799. unsigned int sym_size, offset;
  800. struct symbol *sym = self->ms.sym;
  801. struct sym_priv *priv;
  802. struct sym_hist *h;
  803. if (!sym || !self->ms.map)
  804. return 0;
  805. priv = symbol__priv(sym);
  806. if (priv->hist == NULL && symbol__alloc_hist(sym) < 0)
  807. return -ENOMEM;
  808. sym_size = sym->end - sym->start;
  809. offset = ip - sym->start;
  810. pr_debug3("%s: ip=%#" PRIx64 "\n", __func__, self->ms.map->unmap_ip(self->ms.map, ip));
  811. if (offset >= sym_size)
  812. return 0;
  813. h = priv->hist;
  814. h->sum++;
  815. h->ip[offset]++;
  816. pr_debug3("%#" PRIx64 " %s: period++ [ip: %#" PRIx64 ", %#" PRIx64
  817. "] => %" PRIu64 "\n", self->ms.sym->start, self->ms.sym->name,
  818. ip, ip - self->ms.sym->start, h->ip[offset]);
  819. return 0;
  820. }
  821. static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
  822. {
  823. struct objdump_line *self = malloc(sizeof(*self) + privsize);
  824. if (self != NULL) {
  825. self->offset = offset;
  826. self->line = line;
  827. }
  828. return self;
  829. }
  830. void objdump_line__free(struct objdump_line *self)
  831. {
  832. free(self->line);
  833. free(self);
  834. }
  835. static void objdump__add_line(struct list_head *head, struct objdump_line *line)
  836. {
  837. list_add_tail(&line->node, head);
  838. }
  839. struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
  840. struct objdump_line *pos)
  841. {
  842. list_for_each_entry_continue(pos, head, node)
  843. if (pos->offset >= 0)
  844. return pos;
  845. return NULL;
  846. }
  847. static int hist_entry__parse_objdump_line(struct hist_entry *self, FILE *file,
  848. struct list_head *head, size_t privsize)
  849. {
  850. struct symbol *sym = self->ms.sym;
  851. struct objdump_line *objdump_line;
  852. char *line = NULL, *tmp, *tmp2, *c;
  853. size_t line_len;
  854. s64 line_ip, offset = -1;
  855. if (getline(&line, &line_len, file) < 0)
  856. return -1;
  857. if (!line)
  858. return -1;
  859. while (line_len != 0 && isspace(line[line_len - 1]))
  860. line[--line_len] = '\0';
  861. c = strchr(line, '\n');
  862. if (c)
  863. *c = 0;
  864. line_ip = -1;
  865. /*
  866. * Strip leading spaces:
  867. */
  868. tmp = line;
  869. while (*tmp) {
  870. if (*tmp != ' ')
  871. break;
  872. tmp++;
  873. }
  874. if (*tmp) {
  875. /*
  876. * Parse hexa addresses followed by ':'
  877. */
  878. line_ip = strtoull(tmp, &tmp2, 16);
  879. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  880. line_ip = -1;
  881. }
  882. if (line_ip != -1) {
  883. u64 start = map__rip_2objdump(self->ms.map, sym->start),
  884. end = map__rip_2objdump(self->ms.map, sym->end);
  885. offset = line_ip - start;
  886. if (offset < 0 || (u64)line_ip > end)
  887. offset = -1;
  888. }
  889. objdump_line = objdump_line__new(offset, line, privsize);
  890. if (objdump_line == NULL) {
  891. free(line);
  892. return -1;
  893. }
  894. objdump__add_line(head, objdump_line);
  895. return 0;
  896. }
  897. int hist_entry__annotate(struct hist_entry *self, struct list_head *head,
  898. size_t privsize)
  899. {
  900. struct symbol *sym = self->ms.sym;
  901. struct map *map = self->ms.map;
  902. struct dso *dso = map->dso;
  903. char *filename = dso__build_id_filename(dso, NULL, 0);
  904. bool free_filename = true;
  905. char command[PATH_MAX * 2];
  906. FILE *file;
  907. int err = 0;
  908. u64 len;
  909. char symfs_filename[PATH_MAX];
  910. if (filename) {
  911. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  912. symbol_conf.symfs, filename);
  913. }
  914. if (filename == NULL) {
  915. if (dso->has_build_id) {
  916. pr_err("Can't annotate %s: not enough memory\n",
  917. sym->name);
  918. return -ENOMEM;
  919. }
  920. goto fallback;
  921. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  922. strstr(command, "[kernel.kallsyms]") ||
  923. access(symfs_filename, R_OK)) {
  924. free(filename);
  925. fallback:
  926. /*
  927. * If we don't have build-ids or the build-id file isn't in the
  928. * cache, or is just a kallsyms file, well, lets hope that this
  929. * DSO is the same as when 'perf record' ran.
  930. */
  931. filename = dso->long_name;
  932. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  933. symbol_conf.symfs, filename);
  934. free_filename = false;
  935. }
  936. if (dso->origin == DSO__ORIG_KERNEL) {
  937. if (dso->annotate_warned)
  938. goto out_free_filename;
  939. err = -ENOENT;
  940. dso->annotate_warned = 1;
  941. pr_err("Can't annotate %s: No vmlinux file was found in the "
  942. "path\n", sym->name);
  943. goto out_free_filename;
  944. }
  945. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  946. filename, sym->name, map->unmap_ip(map, sym->start),
  947. map->unmap_ip(map, sym->end));
  948. len = sym->end - sym->start;
  949. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  950. dso, dso->long_name, sym, sym->name);
  951. snprintf(command, sizeof(command),
  952. "objdump --start-address=0x%016" PRIx64 " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand",
  953. map__rip_2objdump(map, sym->start),
  954. map__rip_2objdump(map, sym->end),
  955. symfs_filename, filename);
  956. pr_debug("Executing: %s\n", command);
  957. file = popen(command, "r");
  958. if (!file)
  959. goto out_free_filename;
  960. while (!feof(file))
  961. if (hist_entry__parse_objdump_line(self, file, head, privsize) < 0)
  962. break;
  963. pclose(file);
  964. out_free_filename:
  965. if (free_filename)
  966. free(filename);
  967. return err;
  968. }
  969. void hists__inc_nr_events(struct hists *self, u32 type)
  970. {
  971. ++self->stats.nr_events[0];
  972. ++self->stats.nr_events[type];
  973. }
  974. size_t hists__fprintf_nr_events(struct hists *self, FILE *fp)
  975. {
  976. int i;
  977. size_t ret = 0;
  978. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  979. const char *name = event__get_event_name(i);
  980. if (!strcmp(name, "UNKNOWN"))
  981. continue;
  982. ret += fprintf(fp, "%16s events: %10d\n", name,
  983. self->stats.nr_events[i]);
  984. }
  985. return ret;
  986. }