hist.c 25 KB

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