hist.c 27 KB

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