hist.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. #include <stdio.h>
  2. #include "../../util/util.h"
  3. #include "../../util/hist.h"
  4. #include "../../util/sort.h"
  5. #include "../../util/evsel.h"
  6. #include "../../util/srcline.h"
  7. #include "../../util/string2.h"
  8. #include "../../util/thread.h"
  9. #include "../../util/sane_ctype.h"
  10. static size_t callchain__fprintf_left_margin(FILE *fp, int left_margin)
  11. {
  12. int i;
  13. int ret = fprintf(fp, " ");
  14. for (i = 0; i < left_margin; i++)
  15. ret += fprintf(fp, " ");
  16. return ret;
  17. }
  18. static size_t inline__fprintf(struct map *map, u64 ip, int left_margin,
  19. int depth, int depth_mask, FILE *fp)
  20. {
  21. struct dso *dso;
  22. struct inline_node *node;
  23. struct inline_list *ilist;
  24. int ret = 0, i;
  25. if (map == NULL)
  26. return 0;
  27. dso = map->dso;
  28. if (dso == NULL)
  29. return 0;
  30. if (dso->kernel != DSO_TYPE_USER)
  31. return 0;
  32. node = dso__parse_addr_inlines(dso,
  33. map__rip_2objdump(map, ip));
  34. if (node == NULL)
  35. return 0;
  36. list_for_each_entry(ilist, &node->val, list) {
  37. if ((ilist->filename != NULL) || (ilist->funcname != NULL)) {
  38. ret += callchain__fprintf_left_margin(fp, left_margin);
  39. for (i = 0; i < depth; i++) {
  40. if (depth_mask & (1 << i))
  41. ret += fprintf(fp, "|");
  42. else
  43. ret += fprintf(fp, " ");
  44. ret += fprintf(fp, " ");
  45. }
  46. if (callchain_param.key == CCKEY_ADDRESS ||
  47. callchain_param.key == CCKEY_SRCLINE) {
  48. if (ilist->filename != NULL)
  49. ret += fprintf(fp, "%s:%d (inline)",
  50. ilist->filename,
  51. ilist->line_nr);
  52. else
  53. ret += fprintf(fp, "??");
  54. } else if (ilist->funcname != NULL)
  55. ret += fprintf(fp, "%s (inline)",
  56. ilist->funcname);
  57. else if (ilist->filename != NULL)
  58. ret += fprintf(fp, "%s:%d (inline)",
  59. ilist->filename,
  60. ilist->line_nr);
  61. else
  62. ret += fprintf(fp, "??");
  63. ret += fprintf(fp, "\n");
  64. }
  65. }
  66. inline_node__delete(node);
  67. return ret;
  68. }
  69. static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
  70. int left_margin)
  71. {
  72. int i;
  73. size_t ret = callchain__fprintf_left_margin(fp, left_margin);
  74. for (i = 0; i < depth; i++)
  75. if (depth_mask & (1 << i))
  76. ret += fprintf(fp, "| ");
  77. else
  78. ret += fprintf(fp, " ");
  79. ret += fprintf(fp, "\n");
  80. return ret;
  81. }
  82. static size_t ipchain__fprintf_graph(FILE *fp, struct callchain_node *node,
  83. struct callchain_list *chain,
  84. int depth, int depth_mask, int period,
  85. u64 total_samples, int left_margin)
  86. {
  87. int i;
  88. size_t ret = 0;
  89. char bf[1024], *alloc_str = NULL;
  90. char buf[64];
  91. const char *str;
  92. ret += callchain__fprintf_left_margin(fp, left_margin);
  93. for (i = 0; i < depth; i++) {
  94. if (depth_mask & (1 << i))
  95. ret += fprintf(fp, "|");
  96. else
  97. ret += fprintf(fp, " ");
  98. if (!period && i == depth - 1) {
  99. ret += fprintf(fp, "--");
  100. ret += callchain_node__fprintf_value(node, fp, total_samples);
  101. ret += fprintf(fp, "--");
  102. } else
  103. ret += fprintf(fp, "%s", " ");
  104. }
  105. str = callchain_list__sym_name(chain, bf, sizeof(bf), false);
  106. if (symbol_conf.show_branchflag_count) {
  107. if (!period)
  108. callchain_list_counts__printf_value(node, chain, NULL,
  109. buf, sizeof(buf));
  110. else
  111. callchain_list_counts__printf_value(NULL, chain, NULL,
  112. buf, sizeof(buf));
  113. if (asprintf(&alloc_str, "%s%s", str, buf) < 0)
  114. str = "Not enough memory!";
  115. else
  116. str = alloc_str;
  117. }
  118. fputs(str, fp);
  119. fputc('\n', fp);
  120. free(alloc_str);
  121. if (symbol_conf.inline_name)
  122. ret += inline__fprintf(chain->ms.map, chain->ip,
  123. left_margin, depth, depth_mask, fp);
  124. return ret;
  125. }
  126. static struct symbol *rem_sq_bracket;
  127. static struct callchain_list rem_hits;
  128. static void init_rem_hits(void)
  129. {
  130. rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
  131. if (!rem_sq_bracket) {
  132. fprintf(stderr, "Not enough memory to display remaining hits\n");
  133. return;
  134. }
  135. strcpy(rem_sq_bracket->name, "[...]");
  136. rem_hits.ms.sym = rem_sq_bracket;
  137. }
  138. static size_t __callchain__fprintf_graph(FILE *fp, struct rb_root *root,
  139. u64 total_samples, int depth,
  140. int depth_mask, int left_margin)
  141. {
  142. struct rb_node *node, *next;
  143. struct callchain_node *child = NULL;
  144. struct callchain_list *chain;
  145. int new_depth_mask = depth_mask;
  146. u64 remaining;
  147. size_t ret = 0;
  148. int i;
  149. uint entries_printed = 0;
  150. int cumul_count = 0;
  151. remaining = total_samples;
  152. node = rb_first(root);
  153. while (node) {
  154. u64 new_total;
  155. u64 cumul;
  156. child = rb_entry(node, struct callchain_node, rb_node);
  157. cumul = callchain_cumul_hits(child);
  158. remaining -= cumul;
  159. cumul_count += callchain_cumul_counts(child);
  160. /*
  161. * The depth mask manages the output of pipes that show
  162. * the depth. We don't want to keep the pipes of the current
  163. * level for the last child of this depth.
  164. * Except if we have remaining filtered hits. They will
  165. * supersede the last child
  166. */
  167. next = rb_next(node);
  168. if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
  169. new_depth_mask &= ~(1 << (depth - 1));
  170. /*
  171. * But we keep the older depth mask for the line separator
  172. * to keep the level link until we reach the last child
  173. */
  174. ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
  175. left_margin);
  176. i = 0;
  177. list_for_each_entry(chain, &child->val, list) {
  178. ret += ipchain__fprintf_graph(fp, child, chain, depth,
  179. new_depth_mask, i++,
  180. total_samples,
  181. left_margin);
  182. }
  183. if (callchain_param.mode == CHAIN_GRAPH_REL)
  184. new_total = child->children_hit;
  185. else
  186. new_total = total_samples;
  187. ret += __callchain__fprintf_graph(fp, &child->rb_root, new_total,
  188. depth + 1,
  189. new_depth_mask | (1 << depth),
  190. left_margin);
  191. node = next;
  192. if (++entries_printed == callchain_param.print_limit)
  193. break;
  194. }
  195. if (callchain_param.mode == CHAIN_GRAPH_REL &&
  196. remaining && remaining != total_samples) {
  197. struct callchain_node rem_node = {
  198. .hit = remaining,
  199. };
  200. if (!rem_sq_bracket)
  201. return ret;
  202. if (callchain_param.value == CCVAL_COUNT && child && child->parent) {
  203. rem_node.count = child->parent->children_count - cumul_count;
  204. if (rem_node.count <= 0)
  205. return ret;
  206. }
  207. new_depth_mask &= ~(1 << (depth - 1));
  208. ret += ipchain__fprintf_graph(fp, &rem_node, &rem_hits, depth,
  209. new_depth_mask, 0, total_samples,
  210. left_margin);
  211. }
  212. return ret;
  213. }
  214. /*
  215. * If have one single callchain root, don't bother printing
  216. * its percentage (100 % in fractal mode and the same percentage
  217. * than the hist in graph mode). This also avoid one level of column.
  218. *
  219. * However when percent-limit applied, it's possible that single callchain
  220. * node have different (non-100% in fractal mode) percentage.
  221. */
  222. static bool need_percent_display(struct rb_node *node, u64 parent_samples)
  223. {
  224. struct callchain_node *cnode;
  225. if (rb_next(node))
  226. return true;
  227. cnode = rb_entry(node, struct callchain_node, rb_node);
  228. return callchain_cumul_hits(cnode) != parent_samples;
  229. }
  230. static size_t callchain__fprintf_graph(FILE *fp, struct rb_root *root,
  231. u64 total_samples, u64 parent_samples,
  232. int left_margin)
  233. {
  234. struct callchain_node *cnode;
  235. struct callchain_list *chain;
  236. u32 entries_printed = 0;
  237. bool printed = false;
  238. struct rb_node *node;
  239. int i = 0;
  240. int ret = 0;
  241. char bf[1024];
  242. node = rb_first(root);
  243. if (node && !need_percent_display(node, parent_samples)) {
  244. cnode = rb_entry(node, struct callchain_node, rb_node);
  245. list_for_each_entry(chain, &cnode->val, list) {
  246. /*
  247. * If we sort by symbol, the first entry is the same than
  248. * the symbol. No need to print it otherwise it appears as
  249. * displayed twice.
  250. */
  251. if (!i++ && field_order == NULL &&
  252. sort_order && !prefixcmp(sort_order, "sym"))
  253. continue;
  254. if (!printed) {
  255. ret += callchain__fprintf_left_margin(fp, left_margin);
  256. ret += fprintf(fp, "|\n");
  257. ret += callchain__fprintf_left_margin(fp, left_margin);
  258. ret += fprintf(fp, "---");
  259. left_margin += 3;
  260. printed = true;
  261. } else
  262. ret += callchain__fprintf_left_margin(fp, left_margin);
  263. ret += fprintf(fp, "%s",
  264. callchain_list__sym_name(chain, bf,
  265. sizeof(bf),
  266. false));
  267. if (symbol_conf.show_branchflag_count)
  268. ret += callchain_list_counts__printf_value(
  269. NULL, chain, fp, NULL, 0);
  270. ret += fprintf(fp, "\n");
  271. if (++entries_printed == callchain_param.print_limit)
  272. break;
  273. if (symbol_conf.inline_name)
  274. ret += inline__fprintf(chain->ms.map,
  275. chain->ip,
  276. left_margin,
  277. 0, 0,
  278. fp);
  279. }
  280. root = &cnode->rb_root;
  281. }
  282. if (callchain_param.mode == CHAIN_GRAPH_REL)
  283. total_samples = parent_samples;
  284. ret += __callchain__fprintf_graph(fp, root, total_samples,
  285. 1, 1, left_margin);
  286. if (ret) {
  287. /* do not add a blank line if it printed nothing */
  288. ret += fprintf(fp, "\n");
  289. }
  290. return ret;
  291. }
  292. static size_t __callchain__fprintf_flat(FILE *fp, struct callchain_node *node,
  293. u64 total_samples)
  294. {
  295. struct callchain_list *chain;
  296. size_t ret = 0;
  297. char bf[1024];
  298. if (!node)
  299. return 0;
  300. ret += __callchain__fprintf_flat(fp, node->parent, total_samples);
  301. list_for_each_entry(chain, &node->val, list) {
  302. if (chain->ip >= PERF_CONTEXT_MAX)
  303. continue;
  304. ret += fprintf(fp, " %s\n", callchain_list__sym_name(chain,
  305. bf, sizeof(bf), false));
  306. }
  307. return ret;
  308. }
  309. static size_t callchain__fprintf_flat(FILE *fp, struct rb_root *tree,
  310. u64 total_samples)
  311. {
  312. size_t ret = 0;
  313. u32 entries_printed = 0;
  314. struct callchain_node *chain;
  315. struct rb_node *rb_node = rb_first(tree);
  316. while (rb_node) {
  317. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  318. ret += fprintf(fp, " ");
  319. ret += callchain_node__fprintf_value(chain, fp, total_samples);
  320. ret += fprintf(fp, "\n");
  321. ret += __callchain__fprintf_flat(fp, chain, total_samples);
  322. ret += fprintf(fp, "\n");
  323. if (++entries_printed == callchain_param.print_limit)
  324. break;
  325. rb_node = rb_next(rb_node);
  326. }
  327. return ret;
  328. }
  329. static size_t __callchain__fprintf_folded(FILE *fp, struct callchain_node *node)
  330. {
  331. const char *sep = symbol_conf.field_sep ?: ";";
  332. struct callchain_list *chain;
  333. size_t ret = 0;
  334. char bf[1024];
  335. bool first;
  336. if (!node)
  337. return 0;
  338. ret += __callchain__fprintf_folded(fp, node->parent);
  339. first = (ret == 0);
  340. list_for_each_entry(chain, &node->val, list) {
  341. if (chain->ip >= PERF_CONTEXT_MAX)
  342. continue;
  343. ret += fprintf(fp, "%s%s", first ? "" : sep,
  344. callchain_list__sym_name(chain,
  345. bf, sizeof(bf), false));
  346. first = false;
  347. }
  348. return ret;
  349. }
  350. static size_t callchain__fprintf_folded(FILE *fp, struct rb_root *tree,
  351. u64 total_samples)
  352. {
  353. size_t ret = 0;
  354. u32 entries_printed = 0;
  355. struct callchain_node *chain;
  356. struct rb_node *rb_node = rb_first(tree);
  357. while (rb_node) {
  358. chain = rb_entry(rb_node, struct callchain_node, rb_node);
  359. ret += callchain_node__fprintf_value(chain, fp, total_samples);
  360. ret += fprintf(fp, " ");
  361. ret += __callchain__fprintf_folded(fp, chain);
  362. ret += fprintf(fp, "\n");
  363. if (++entries_printed == callchain_param.print_limit)
  364. break;
  365. rb_node = rb_next(rb_node);
  366. }
  367. return ret;
  368. }
  369. static size_t hist_entry_callchain__fprintf(struct hist_entry *he,
  370. u64 total_samples, int left_margin,
  371. FILE *fp)
  372. {
  373. u64 parent_samples = he->stat.period;
  374. if (symbol_conf.cumulate_callchain)
  375. parent_samples = he->stat_acc->period;
  376. switch (callchain_param.mode) {
  377. case CHAIN_GRAPH_REL:
  378. return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
  379. parent_samples, left_margin);
  380. break;
  381. case CHAIN_GRAPH_ABS:
  382. return callchain__fprintf_graph(fp, &he->sorted_chain, total_samples,
  383. parent_samples, left_margin);
  384. break;
  385. case CHAIN_FLAT:
  386. return callchain__fprintf_flat(fp, &he->sorted_chain, total_samples);
  387. break;
  388. case CHAIN_FOLDED:
  389. return callchain__fprintf_folded(fp, &he->sorted_chain, total_samples);
  390. break;
  391. case CHAIN_NONE:
  392. break;
  393. default:
  394. pr_err("Bad callchain mode\n");
  395. }
  396. return 0;
  397. }
  398. int __hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp,
  399. struct perf_hpp_list *hpp_list)
  400. {
  401. const char *sep = symbol_conf.field_sep;
  402. struct perf_hpp_fmt *fmt;
  403. char *start = hpp->buf;
  404. int ret;
  405. bool first = true;
  406. if (symbol_conf.exclude_other && !he->parent)
  407. return 0;
  408. perf_hpp_list__for_each_format(hpp_list, fmt) {
  409. if (perf_hpp__should_skip(fmt, he->hists))
  410. continue;
  411. /*
  412. * If there's no field_sep, we still need
  413. * to display initial ' '.
  414. */
  415. if (!sep || !first) {
  416. ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
  417. advance_hpp(hpp, ret);
  418. } else
  419. first = false;
  420. if (perf_hpp__use_color() && fmt->color)
  421. ret = fmt->color(fmt, hpp, he);
  422. else
  423. ret = fmt->entry(fmt, hpp, he);
  424. ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
  425. advance_hpp(hpp, ret);
  426. }
  427. return hpp->buf - start;
  428. }
  429. static int hist_entry__snprintf(struct hist_entry *he, struct perf_hpp *hpp)
  430. {
  431. return __hist_entry__snprintf(he, hpp, he->hists->hpp_list);
  432. }
  433. static int hist_entry__hierarchy_fprintf(struct hist_entry *he,
  434. struct perf_hpp *hpp,
  435. struct hists *hists,
  436. FILE *fp)
  437. {
  438. const char *sep = symbol_conf.field_sep;
  439. struct perf_hpp_fmt *fmt;
  440. struct perf_hpp_list_node *fmt_node;
  441. char *buf = hpp->buf;
  442. size_t size = hpp->size;
  443. int ret, printed = 0;
  444. bool first = true;
  445. if (symbol_conf.exclude_other && !he->parent)
  446. return 0;
  447. ret = scnprintf(hpp->buf, hpp->size, "%*s", he->depth * HIERARCHY_INDENT, "");
  448. advance_hpp(hpp, ret);
  449. /* the first hpp_list_node is for overhead columns */
  450. fmt_node = list_first_entry(&hists->hpp_formats,
  451. struct perf_hpp_list_node, list);
  452. perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
  453. /*
  454. * If there's no field_sep, we still need
  455. * to display initial ' '.
  456. */
  457. if (!sep || !first) {
  458. ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
  459. advance_hpp(hpp, ret);
  460. } else
  461. first = false;
  462. if (perf_hpp__use_color() && fmt->color)
  463. ret = fmt->color(fmt, hpp, he);
  464. else
  465. ret = fmt->entry(fmt, hpp, he);
  466. ret = hist_entry__snprintf_alignment(he, hpp, fmt, ret);
  467. advance_hpp(hpp, ret);
  468. }
  469. if (!sep)
  470. ret = scnprintf(hpp->buf, hpp->size, "%*s",
  471. (hists->nr_hpp_node - 2) * HIERARCHY_INDENT, "");
  472. advance_hpp(hpp, ret);
  473. printed += fprintf(fp, "%s", buf);
  474. perf_hpp_list__for_each_format(he->hpp_list, fmt) {
  475. hpp->buf = buf;
  476. hpp->size = size;
  477. /*
  478. * No need to call hist_entry__snprintf_alignment() since this
  479. * fmt is always the last column in the hierarchy mode.
  480. */
  481. if (perf_hpp__use_color() && fmt->color)
  482. fmt->color(fmt, hpp, he);
  483. else
  484. fmt->entry(fmt, hpp, he);
  485. /*
  486. * dynamic entries are right-aligned but we want left-aligned
  487. * in the hierarchy mode
  488. */
  489. printed += fprintf(fp, "%s%s", sep ?: " ", ltrim(buf));
  490. }
  491. printed += putc('\n', fp);
  492. if (symbol_conf.use_callchain && he->leaf) {
  493. u64 total = hists__total_period(hists);
  494. printed += hist_entry_callchain__fprintf(he, total, 0, fp);
  495. goto out;
  496. }
  497. out:
  498. return printed;
  499. }
  500. static int hist_entry__fprintf(struct hist_entry *he, size_t size,
  501. char *bf, size_t bfsz, FILE *fp,
  502. bool use_callchain)
  503. {
  504. int ret;
  505. int callchain_ret = 0;
  506. int inline_ret = 0;
  507. struct perf_hpp hpp = {
  508. .buf = bf,
  509. .size = size,
  510. };
  511. struct hists *hists = he->hists;
  512. u64 total_period = hists->stats.total_period;
  513. if (size == 0 || size > bfsz)
  514. size = hpp.size = bfsz;
  515. if (symbol_conf.report_hierarchy)
  516. return hist_entry__hierarchy_fprintf(he, &hpp, hists, fp);
  517. hist_entry__snprintf(he, &hpp);
  518. ret = fprintf(fp, "%s\n", bf);
  519. if (use_callchain)
  520. callchain_ret = hist_entry_callchain__fprintf(he, total_period,
  521. 0, fp);
  522. if (callchain_ret == 0 && symbol_conf.inline_name) {
  523. inline_ret = inline__fprintf(he->ms.map, he->ip, 0, 0, 0, fp);
  524. ret += inline_ret;
  525. if (inline_ret > 0)
  526. ret += fprintf(fp, "\n");
  527. } else
  528. ret += callchain_ret;
  529. return ret;
  530. }
  531. static int print_hierarchy_indent(const char *sep, int indent,
  532. const char *line, FILE *fp)
  533. {
  534. if (sep != NULL || indent < 2)
  535. return 0;
  536. return fprintf(fp, "%-.*s", (indent - 2) * HIERARCHY_INDENT, line);
  537. }
  538. static int hists__fprintf_hierarchy_headers(struct hists *hists,
  539. struct perf_hpp *hpp, FILE *fp)
  540. {
  541. bool first_node, first_col;
  542. int indent;
  543. int depth;
  544. unsigned width = 0;
  545. unsigned header_width = 0;
  546. struct perf_hpp_fmt *fmt;
  547. struct perf_hpp_list_node *fmt_node;
  548. const char *sep = symbol_conf.field_sep;
  549. indent = hists->nr_hpp_node;
  550. /* preserve max indent depth for column headers */
  551. print_hierarchy_indent(sep, indent, spaces, fp);
  552. /* the first hpp_list_node is for overhead columns */
  553. fmt_node = list_first_entry(&hists->hpp_formats,
  554. struct perf_hpp_list_node, list);
  555. perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
  556. fmt->header(fmt, hpp, hists, 0, NULL);
  557. fprintf(fp, "%s%s", hpp->buf, sep ?: " ");
  558. }
  559. /* combine sort headers with ' / ' */
  560. first_node = true;
  561. list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
  562. if (!first_node)
  563. header_width += fprintf(fp, " / ");
  564. first_node = false;
  565. first_col = true;
  566. perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
  567. if (perf_hpp__should_skip(fmt, hists))
  568. continue;
  569. if (!first_col)
  570. header_width += fprintf(fp, "+");
  571. first_col = false;
  572. fmt->header(fmt, hpp, hists, 0, NULL);
  573. header_width += fprintf(fp, "%s", trim(hpp->buf));
  574. }
  575. }
  576. fprintf(fp, "\n# ");
  577. /* preserve max indent depth for initial dots */
  578. print_hierarchy_indent(sep, indent, dots, fp);
  579. /* the first hpp_list_node is for overhead columns */
  580. fmt_node = list_first_entry(&hists->hpp_formats,
  581. struct perf_hpp_list_node, list);
  582. first_col = true;
  583. perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
  584. if (!first_col)
  585. fprintf(fp, "%s", sep ?: "..");
  586. first_col = false;
  587. width = fmt->width(fmt, hpp, hists);
  588. fprintf(fp, "%.*s", width, dots);
  589. }
  590. depth = 0;
  591. list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
  592. first_col = true;
  593. width = depth * HIERARCHY_INDENT;
  594. perf_hpp_list__for_each_format(&fmt_node->hpp, fmt) {
  595. if (perf_hpp__should_skip(fmt, hists))
  596. continue;
  597. if (!first_col)
  598. width++; /* for '+' sign between column header */
  599. first_col = false;
  600. width += fmt->width(fmt, hpp, hists);
  601. }
  602. if (width > header_width)
  603. header_width = width;
  604. depth++;
  605. }
  606. fprintf(fp, "%s%-.*s", sep ?: " ", header_width, dots);
  607. fprintf(fp, "\n#\n");
  608. return 2;
  609. }
  610. static void fprintf_line(struct hists *hists, struct perf_hpp *hpp,
  611. int line, FILE *fp)
  612. {
  613. struct perf_hpp_fmt *fmt;
  614. const char *sep = symbol_conf.field_sep;
  615. bool first = true;
  616. int span = 0;
  617. hists__for_each_format(hists, fmt) {
  618. if (perf_hpp__should_skip(fmt, hists))
  619. continue;
  620. if (!first && !span)
  621. fprintf(fp, "%s", sep ?: " ");
  622. else
  623. first = false;
  624. fmt->header(fmt, hpp, hists, line, &span);
  625. if (!span)
  626. fprintf(fp, "%s", hpp->buf);
  627. }
  628. }
  629. static int
  630. hists__fprintf_standard_headers(struct hists *hists,
  631. struct perf_hpp *hpp,
  632. FILE *fp)
  633. {
  634. struct perf_hpp_list *hpp_list = hists->hpp_list;
  635. struct perf_hpp_fmt *fmt;
  636. unsigned int width;
  637. const char *sep = symbol_conf.field_sep;
  638. bool first = true;
  639. int line;
  640. for (line = 0; line < hpp_list->nr_header_lines; line++) {
  641. /* first # is displayed one level up */
  642. if (line)
  643. fprintf(fp, "# ");
  644. fprintf_line(hists, hpp, line, fp);
  645. fprintf(fp, "\n");
  646. }
  647. if (sep)
  648. return hpp_list->nr_header_lines;
  649. first = true;
  650. fprintf(fp, "# ");
  651. hists__for_each_format(hists, fmt) {
  652. unsigned int i;
  653. if (perf_hpp__should_skip(fmt, hists))
  654. continue;
  655. if (!first)
  656. fprintf(fp, "%s", sep ?: " ");
  657. else
  658. first = false;
  659. width = fmt->width(fmt, hpp, hists);
  660. for (i = 0; i < width; i++)
  661. fprintf(fp, ".");
  662. }
  663. fprintf(fp, "\n");
  664. fprintf(fp, "#\n");
  665. return hpp_list->nr_header_lines + 2;
  666. }
  667. int hists__fprintf_headers(struct hists *hists, FILE *fp)
  668. {
  669. char bf[1024];
  670. struct perf_hpp dummy_hpp = {
  671. .buf = bf,
  672. .size = sizeof(bf),
  673. };
  674. fprintf(fp, "# ");
  675. if (symbol_conf.report_hierarchy)
  676. return hists__fprintf_hierarchy_headers(hists, &dummy_hpp, fp);
  677. else
  678. return hists__fprintf_standard_headers(hists, &dummy_hpp, fp);
  679. }
  680. size_t hists__fprintf(struct hists *hists, bool show_header, int max_rows,
  681. int max_cols, float min_pcnt, FILE *fp,
  682. bool use_callchain)
  683. {
  684. struct rb_node *nd;
  685. size_t ret = 0;
  686. const char *sep = symbol_conf.field_sep;
  687. int nr_rows = 0;
  688. size_t linesz;
  689. char *line = NULL;
  690. unsigned indent;
  691. init_rem_hits();
  692. hists__reset_column_width(hists);
  693. if (symbol_conf.col_width_list_str)
  694. perf_hpp__set_user_width(symbol_conf.col_width_list_str);
  695. if (show_header)
  696. nr_rows += hists__fprintf_headers(hists, fp);
  697. if (max_rows && nr_rows >= max_rows)
  698. goto out;
  699. linesz = hists__sort_list_width(hists) + 3 + 1;
  700. linesz += perf_hpp__color_overhead();
  701. line = malloc(linesz);
  702. if (line == NULL) {
  703. ret = -1;
  704. goto out;
  705. }
  706. indent = hists__overhead_width(hists) + 4;
  707. for (nd = rb_first(&hists->entries); nd; nd = __rb_hierarchy_next(nd, HMD_FORCE_CHILD)) {
  708. struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
  709. float percent;
  710. if (h->filtered)
  711. continue;
  712. percent = hist_entry__get_percent_limit(h);
  713. if (percent < min_pcnt)
  714. continue;
  715. ret += hist_entry__fprintf(h, max_cols, line, linesz, fp, use_callchain);
  716. if (max_rows && ++nr_rows >= max_rows)
  717. break;
  718. /*
  719. * If all children are filtered out or percent-limited,
  720. * display "no entry >= x.xx%" message.
  721. */
  722. if (!h->leaf && !hist_entry__has_hierarchy_children(h, min_pcnt)) {
  723. int depth = hists->nr_hpp_node + h->depth + 1;
  724. print_hierarchy_indent(sep, depth, spaces, fp);
  725. fprintf(fp, "%*sno entry >= %.2f%%\n", indent, "", min_pcnt);
  726. if (max_rows && ++nr_rows >= max_rows)
  727. break;
  728. }
  729. if (h->ms.map == NULL && verbose > 1) {
  730. __map_groups__fprintf_maps(h->thread->mg,
  731. MAP__FUNCTION, fp);
  732. fprintf(fp, "%.10s end\n", graph_dotted_line);
  733. }
  734. }
  735. free(line);
  736. out:
  737. zfree(&rem_sq_bracket);
  738. return ret;
  739. }
  740. size_t events_stats__fprintf(struct events_stats *stats, FILE *fp)
  741. {
  742. int i;
  743. size_t ret = 0;
  744. for (i = 0; i < PERF_RECORD_HEADER_MAX; ++i) {
  745. const char *name;
  746. if (stats->nr_events[i] == 0)
  747. continue;
  748. name = perf_event__name(i);
  749. if (!strcmp(name, "UNKNOWN"))
  750. continue;
  751. ret += fprintf(fp, "%16s events: %10d\n", name,
  752. stats->nr_events[i]);
  753. }
  754. return ret;
  755. }