hist.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. #include <math.h>
  2. #include <linux/compiler.h>
  3. #include "../util/hist.h"
  4. #include "../util/util.h"
  5. #include "../util/sort.h"
  6. #include "../util/evsel.h"
  7. #include "../util/evlist.h"
  8. /* hist period print (hpp) functions */
  9. #define hpp__call_print_fn(hpp, fn, fmt, ...) \
  10. ({ \
  11. int __ret = fn(hpp, fmt, ##__VA_ARGS__); \
  12. advance_hpp(hpp, __ret); \
  13. __ret; \
  14. })
  15. static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
  16. hpp_field_fn get_field, const char *fmt, int len,
  17. hpp_snprint_fn print_fn, bool fmt_percent)
  18. {
  19. int ret;
  20. struct hists *hists = he->hists;
  21. struct perf_evsel *evsel = hists_to_evsel(hists);
  22. char *buf = hpp->buf;
  23. size_t size = hpp->size;
  24. if (fmt_percent) {
  25. double percent = 0.0;
  26. u64 total = hists__total_period(hists);
  27. if (total)
  28. percent = 100.0 * get_field(he) / total;
  29. ret = hpp__call_print_fn(hpp, print_fn, fmt, len, percent);
  30. } else
  31. ret = hpp__call_print_fn(hpp, print_fn, fmt, len, get_field(he));
  32. if (perf_evsel__is_group_event(evsel)) {
  33. int prev_idx, idx_delta;
  34. struct hist_entry *pair;
  35. int nr_members = evsel->nr_members;
  36. prev_idx = perf_evsel__group_idx(evsel);
  37. list_for_each_entry(pair, &he->pairs.head, pairs.node) {
  38. u64 period = get_field(pair);
  39. u64 total = hists__total_period(pair->hists);
  40. if (!total)
  41. continue;
  42. evsel = hists_to_evsel(pair->hists);
  43. idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
  44. while (idx_delta--) {
  45. /*
  46. * zero-fill group members in the middle which
  47. * have no sample
  48. */
  49. if (fmt_percent) {
  50. ret += hpp__call_print_fn(hpp, print_fn,
  51. fmt, len, 0.0);
  52. } else {
  53. ret += hpp__call_print_fn(hpp, print_fn,
  54. fmt, len, 0ULL);
  55. }
  56. }
  57. if (fmt_percent) {
  58. ret += hpp__call_print_fn(hpp, print_fn, fmt, len,
  59. 100.0 * period / total);
  60. } else {
  61. ret += hpp__call_print_fn(hpp, print_fn, fmt,
  62. len, period);
  63. }
  64. prev_idx = perf_evsel__group_idx(evsel);
  65. }
  66. idx_delta = nr_members - prev_idx - 1;
  67. while (idx_delta--) {
  68. /*
  69. * zero-fill group members at last which have no sample
  70. */
  71. if (fmt_percent) {
  72. ret += hpp__call_print_fn(hpp, print_fn,
  73. fmt, len, 0.0);
  74. } else {
  75. ret += hpp__call_print_fn(hpp, print_fn,
  76. fmt, len, 0ULL);
  77. }
  78. }
  79. }
  80. /*
  81. * Restore original buf and size as it's where caller expects
  82. * the result will be saved.
  83. */
  84. hpp->buf = buf;
  85. hpp->size = size;
  86. return ret;
  87. }
  88. int hpp__fmt(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  89. struct hist_entry *he, hpp_field_fn get_field,
  90. const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
  91. {
  92. int len = fmt->user_len ?: fmt->len;
  93. if (symbol_conf.field_sep) {
  94. return __hpp__fmt(hpp, he, get_field, fmtstr, 1,
  95. print_fn, fmt_percent);
  96. }
  97. if (fmt_percent)
  98. len -= 2; /* 2 for a space and a % sign */
  99. else
  100. len -= 1;
  101. return __hpp__fmt(hpp, he, get_field, fmtstr, len, print_fn, fmt_percent);
  102. }
  103. int hpp__fmt_acc(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  104. struct hist_entry *he, hpp_field_fn get_field,
  105. const char *fmtstr, hpp_snprint_fn print_fn, bool fmt_percent)
  106. {
  107. if (!symbol_conf.cumulate_callchain) {
  108. int len = fmt->user_len ?: fmt->len;
  109. return snprintf(hpp->buf, hpp->size, " %*s", len - 1, "N/A");
  110. }
  111. return hpp__fmt(fmt, hpp, he, get_field, fmtstr, print_fn, fmt_percent);
  112. }
  113. static int field_cmp(u64 field_a, u64 field_b)
  114. {
  115. if (field_a > field_b)
  116. return 1;
  117. if (field_a < field_b)
  118. return -1;
  119. return 0;
  120. }
  121. static int __hpp__sort(struct hist_entry *a, struct hist_entry *b,
  122. hpp_field_fn get_field)
  123. {
  124. s64 ret;
  125. int i, nr_members;
  126. struct perf_evsel *evsel;
  127. struct hist_entry *pair;
  128. u64 *fields_a, *fields_b;
  129. ret = field_cmp(get_field(a), get_field(b));
  130. if (ret || !symbol_conf.event_group)
  131. return ret;
  132. evsel = hists_to_evsel(a->hists);
  133. if (!perf_evsel__is_group_event(evsel))
  134. return ret;
  135. nr_members = evsel->nr_members;
  136. fields_a = calloc(nr_members, sizeof(*fields_a));
  137. fields_b = calloc(nr_members, sizeof(*fields_b));
  138. if (!fields_a || !fields_b)
  139. goto out;
  140. list_for_each_entry(pair, &a->pairs.head, pairs.node) {
  141. evsel = hists_to_evsel(pair->hists);
  142. fields_a[perf_evsel__group_idx(evsel)] = get_field(pair);
  143. }
  144. list_for_each_entry(pair, &b->pairs.head, pairs.node) {
  145. evsel = hists_to_evsel(pair->hists);
  146. fields_b[perf_evsel__group_idx(evsel)] = get_field(pair);
  147. }
  148. for (i = 1; i < nr_members; i++) {
  149. ret = field_cmp(fields_a[i], fields_b[i]);
  150. if (ret)
  151. break;
  152. }
  153. out:
  154. free(fields_a);
  155. free(fields_b);
  156. return ret;
  157. }
  158. static int __hpp__sort_acc(struct hist_entry *a, struct hist_entry *b,
  159. hpp_field_fn get_field)
  160. {
  161. s64 ret = 0;
  162. if (symbol_conf.cumulate_callchain) {
  163. /*
  164. * Put caller above callee when they have equal period.
  165. */
  166. ret = field_cmp(get_field(a), get_field(b));
  167. if (ret)
  168. return ret;
  169. if (a->thread != b->thread || !symbol_conf.use_callchain)
  170. return 0;
  171. ret = b->callchain->max_depth - a->callchain->max_depth;
  172. }
  173. return ret;
  174. }
  175. static int hpp__width_fn(struct perf_hpp_fmt *fmt,
  176. struct perf_hpp *hpp __maybe_unused,
  177. struct perf_evsel *evsel)
  178. {
  179. int len = fmt->user_len ?: fmt->len;
  180. if (symbol_conf.event_group)
  181. len = max(len, evsel->nr_members * fmt->len);
  182. if (len < (int)strlen(fmt->name))
  183. len = strlen(fmt->name);
  184. return len;
  185. }
  186. static int hpp__header_fn(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  187. struct perf_evsel *evsel)
  188. {
  189. int len = hpp__width_fn(fmt, hpp, evsel);
  190. return scnprintf(hpp->buf, hpp->size, "%*s", len, fmt->name);
  191. }
  192. static int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
  193. {
  194. va_list args;
  195. ssize_t ssize = hpp->size;
  196. double percent;
  197. int ret, len;
  198. va_start(args, fmt);
  199. len = va_arg(args, int);
  200. percent = va_arg(args, double);
  201. ret = percent_color_len_snprintf(hpp->buf, hpp->size, fmt, len, percent);
  202. va_end(args);
  203. return (ret >= ssize) ? (ssize - 1) : ret;
  204. }
  205. static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
  206. {
  207. va_list args;
  208. ssize_t ssize = hpp->size;
  209. int ret;
  210. va_start(args, fmt);
  211. ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
  212. va_end(args);
  213. return (ret >= ssize) ? (ssize - 1) : ret;
  214. }
  215. #define __HPP_COLOR_PERCENT_FN(_type, _field) \
  216. static u64 he_get_##_field(struct hist_entry *he) \
  217. { \
  218. return he->stat._field; \
  219. } \
  220. \
  221. static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
  222. struct perf_hpp *hpp, struct hist_entry *he) \
  223. { \
  224. return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
  225. hpp_color_scnprintf, true); \
  226. }
  227. #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
  228. static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
  229. struct perf_hpp *hpp, struct hist_entry *he) \
  230. { \
  231. return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
  232. hpp_entry_scnprintf, true); \
  233. }
  234. #define __HPP_SORT_FN(_type, _field) \
  235. static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
  236. struct hist_entry *a, struct hist_entry *b) \
  237. { \
  238. return __hpp__sort(a, b, he_get_##_field); \
  239. }
  240. #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
  241. static u64 he_get_acc_##_field(struct hist_entry *he) \
  242. { \
  243. return he->stat_acc->_field; \
  244. } \
  245. \
  246. static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
  247. struct perf_hpp *hpp, struct hist_entry *he) \
  248. { \
  249. return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
  250. hpp_color_scnprintf, true); \
  251. }
  252. #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
  253. static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
  254. struct perf_hpp *hpp, struct hist_entry *he) \
  255. { \
  256. return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
  257. hpp_entry_scnprintf, true); \
  258. }
  259. #define __HPP_SORT_ACC_FN(_type, _field) \
  260. static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
  261. struct hist_entry *a, struct hist_entry *b) \
  262. { \
  263. return __hpp__sort_acc(a, b, he_get_acc_##_field); \
  264. }
  265. #define __HPP_ENTRY_RAW_FN(_type, _field) \
  266. static u64 he_get_raw_##_field(struct hist_entry *he) \
  267. { \
  268. return he->stat._field; \
  269. } \
  270. \
  271. static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
  272. struct perf_hpp *hpp, struct hist_entry *he) \
  273. { \
  274. return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, \
  275. hpp_entry_scnprintf, false); \
  276. }
  277. #define __HPP_SORT_RAW_FN(_type, _field) \
  278. static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
  279. struct hist_entry *a, struct hist_entry *b) \
  280. { \
  281. return __hpp__sort(a, b, he_get_raw_##_field); \
  282. }
  283. #define HPP_PERCENT_FNS(_type, _field) \
  284. __HPP_COLOR_PERCENT_FN(_type, _field) \
  285. __HPP_ENTRY_PERCENT_FN(_type, _field) \
  286. __HPP_SORT_FN(_type, _field)
  287. #define HPP_PERCENT_ACC_FNS(_type, _field) \
  288. __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
  289. __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
  290. __HPP_SORT_ACC_FN(_type, _field)
  291. #define HPP_RAW_FNS(_type, _field) \
  292. __HPP_ENTRY_RAW_FN(_type, _field) \
  293. __HPP_SORT_RAW_FN(_type, _field)
  294. HPP_PERCENT_FNS(overhead, period)
  295. HPP_PERCENT_FNS(overhead_sys, period_sys)
  296. HPP_PERCENT_FNS(overhead_us, period_us)
  297. HPP_PERCENT_FNS(overhead_guest_sys, period_guest_sys)
  298. HPP_PERCENT_FNS(overhead_guest_us, period_guest_us)
  299. HPP_PERCENT_ACC_FNS(overhead_acc, period)
  300. HPP_RAW_FNS(samples, nr_events)
  301. HPP_RAW_FNS(period, period)
  302. static int64_t hpp__nop_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  303. struct hist_entry *a __maybe_unused,
  304. struct hist_entry *b __maybe_unused)
  305. {
  306. return 0;
  307. }
  308. static bool perf_hpp__is_hpp_entry(struct perf_hpp_fmt *a)
  309. {
  310. return a->header == hpp__header_fn;
  311. }
  312. static bool hpp__equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
  313. {
  314. if (!perf_hpp__is_hpp_entry(a) || !perf_hpp__is_hpp_entry(b))
  315. return false;
  316. return a->idx == b->idx;
  317. }
  318. #define HPP__COLOR_PRINT_FNS(_name, _fn, _idx) \
  319. { \
  320. .name = _name, \
  321. .header = hpp__header_fn, \
  322. .width = hpp__width_fn, \
  323. .color = hpp__color_ ## _fn, \
  324. .entry = hpp__entry_ ## _fn, \
  325. .cmp = hpp__nop_cmp, \
  326. .collapse = hpp__nop_cmp, \
  327. .sort = hpp__sort_ ## _fn, \
  328. .idx = PERF_HPP__ ## _idx, \
  329. .equal = hpp__equal, \
  330. }
  331. #define HPP__COLOR_ACC_PRINT_FNS(_name, _fn, _idx) \
  332. { \
  333. .name = _name, \
  334. .header = hpp__header_fn, \
  335. .width = hpp__width_fn, \
  336. .color = hpp__color_ ## _fn, \
  337. .entry = hpp__entry_ ## _fn, \
  338. .cmp = hpp__nop_cmp, \
  339. .collapse = hpp__nop_cmp, \
  340. .sort = hpp__sort_ ## _fn, \
  341. .idx = PERF_HPP__ ## _idx, \
  342. .equal = hpp__equal, \
  343. }
  344. #define HPP__PRINT_FNS(_name, _fn, _idx) \
  345. { \
  346. .name = _name, \
  347. .header = hpp__header_fn, \
  348. .width = hpp__width_fn, \
  349. .entry = hpp__entry_ ## _fn, \
  350. .cmp = hpp__nop_cmp, \
  351. .collapse = hpp__nop_cmp, \
  352. .sort = hpp__sort_ ## _fn, \
  353. .idx = PERF_HPP__ ## _idx, \
  354. .equal = hpp__equal, \
  355. }
  356. struct perf_hpp_fmt perf_hpp__format[] = {
  357. HPP__COLOR_PRINT_FNS("Overhead", overhead, OVERHEAD),
  358. HPP__COLOR_PRINT_FNS("sys", overhead_sys, OVERHEAD_SYS),
  359. HPP__COLOR_PRINT_FNS("usr", overhead_us, OVERHEAD_US),
  360. HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys, OVERHEAD_GUEST_SYS),
  361. HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us, OVERHEAD_GUEST_US),
  362. HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc, OVERHEAD_ACC),
  363. HPP__PRINT_FNS("Samples", samples, SAMPLES),
  364. HPP__PRINT_FNS("Period", period, PERIOD)
  365. };
  366. struct perf_hpp_list perf_hpp_list = {
  367. .fields = LIST_HEAD_INIT(perf_hpp_list.fields),
  368. .sorts = LIST_HEAD_INIT(perf_hpp_list.sorts),
  369. };
  370. #undef HPP__COLOR_PRINT_FNS
  371. #undef HPP__COLOR_ACC_PRINT_FNS
  372. #undef HPP__PRINT_FNS
  373. #undef HPP_PERCENT_FNS
  374. #undef HPP_PERCENT_ACC_FNS
  375. #undef HPP_RAW_FNS
  376. #undef __HPP_HEADER_FN
  377. #undef __HPP_WIDTH_FN
  378. #undef __HPP_COLOR_PERCENT_FN
  379. #undef __HPP_ENTRY_PERCENT_FN
  380. #undef __HPP_COLOR_ACC_PERCENT_FN
  381. #undef __HPP_ENTRY_ACC_PERCENT_FN
  382. #undef __HPP_ENTRY_RAW_FN
  383. #undef __HPP_SORT_FN
  384. #undef __HPP_SORT_ACC_FN
  385. #undef __HPP_SORT_RAW_FN
  386. void perf_hpp__init(void)
  387. {
  388. int i;
  389. for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
  390. struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
  391. INIT_LIST_HEAD(&fmt->list);
  392. /* sort_list may be linked by setup_sorting() */
  393. if (fmt->sort_list.next == NULL)
  394. INIT_LIST_HEAD(&fmt->sort_list);
  395. }
  396. /*
  397. * If user specified field order, no need to setup default fields.
  398. */
  399. if (is_strict_order(field_order))
  400. return;
  401. if (symbol_conf.cumulate_callchain) {
  402. hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC);
  403. perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self";
  404. }
  405. hpp_dimension__add_output(PERF_HPP__OVERHEAD);
  406. if (symbol_conf.show_cpu_utilization) {
  407. hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS);
  408. hpp_dimension__add_output(PERF_HPP__OVERHEAD_US);
  409. if (perf_guest) {
  410. hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS);
  411. hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US);
  412. }
  413. }
  414. if (symbol_conf.show_nr_samples)
  415. hpp_dimension__add_output(PERF_HPP__SAMPLES);
  416. if (symbol_conf.show_total_period)
  417. hpp_dimension__add_output(PERF_HPP__PERIOD);
  418. }
  419. void perf_hpp_list__column_register(struct perf_hpp_list *list,
  420. struct perf_hpp_fmt *format)
  421. {
  422. list_add_tail(&format->list, &list->fields);
  423. }
  424. void perf_hpp_list__register_sort_field(struct perf_hpp_list *list,
  425. struct perf_hpp_fmt *format)
  426. {
  427. list_add_tail(&format->sort_list, &list->sorts);
  428. }
  429. void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
  430. {
  431. list_del(&format->list);
  432. }
  433. void perf_hpp__cancel_cumulate(void)
  434. {
  435. struct perf_hpp_fmt *fmt, *acc, *ovh, *tmp;
  436. if (is_strict_order(field_order))
  437. return;
  438. ovh = &perf_hpp__format[PERF_HPP__OVERHEAD];
  439. acc = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC];
  440. perf_hpp_list__for_each_format_safe(&perf_hpp_list, fmt, tmp) {
  441. if (acc->equal(acc, fmt)) {
  442. perf_hpp__column_unregister(fmt);
  443. continue;
  444. }
  445. if (ovh->equal(ovh, fmt))
  446. fmt->name = "Overhead";
  447. }
  448. }
  449. static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
  450. {
  451. return a->equal && a->equal(a, b);
  452. }
  453. void perf_hpp__setup_output_field(struct perf_hpp_list *list)
  454. {
  455. struct perf_hpp_fmt *fmt;
  456. /* append sort keys to output field */
  457. perf_hpp_list__for_each_sort_list(list, fmt) {
  458. struct perf_hpp_fmt *pos;
  459. perf_hpp_list__for_each_format(list, pos) {
  460. if (fmt_equal(fmt, pos))
  461. goto next;
  462. }
  463. perf_hpp__column_register(fmt);
  464. next:
  465. continue;
  466. }
  467. }
  468. void perf_hpp__append_sort_keys(struct perf_hpp_list *list)
  469. {
  470. struct perf_hpp_fmt *fmt;
  471. /* append output fields to sort keys */
  472. perf_hpp_list__for_each_format(list, fmt) {
  473. struct perf_hpp_fmt *pos;
  474. perf_hpp_list__for_each_sort_list(list, pos) {
  475. if (fmt_equal(fmt, pos))
  476. goto next;
  477. }
  478. perf_hpp__register_sort_field(fmt);
  479. next:
  480. continue;
  481. }
  482. }
  483. static void fmt_free(struct perf_hpp_fmt *fmt)
  484. {
  485. if (fmt->free)
  486. fmt->free(fmt);
  487. }
  488. void perf_hpp__reset_output_field(struct perf_hpp_list *list)
  489. {
  490. struct perf_hpp_fmt *fmt, *tmp;
  491. /* reset output fields */
  492. perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
  493. list_del_init(&fmt->list);
  494. list_del_init(&fmt->sort_list);
  495. fmt_free(fmt);
  496. }
  497. /* reset sort keys */
  498. perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
  499. list_del_init(&fmt->list);
  500. list_del_init(&fmt->sort_list);
  501. fmt_free(fmt);
  502. }
  503. }
  504. /*
  505. * See hists__fprintf to match the column widths
  506. */
  507. unsigned int hists__sort_list_width(struct hists *hists)
  508. {
  509. struct perf_hpp_fmt *fmt;
  510. int ret = 0;
  511. bool first = true;
  512. struct perf_hpp dummy_hpp;
  513. hists__for_each_format(hists, fmt) {
  514. if (perf_hpp__should_skip(fmt, hists))
  515. continue;
  516. if (first)
  517. first = false;
  518. else
  519. ret += 2;
  520. ret += fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
  521. }
  522. if (verbose && hists__has(hists, sym)) /* Addr + origin */
  523. ret += 3 + BITS_PER_LONG / 4;
  524. return ret;
  525. }
  526. unsigned int hists__overhead_width(struct hists *hists)
  527. {
  528. struct perf_hpp_fmt *fmt;
  529. int ret = 0;
  530. bool first = true;
  531. struct perf_hpp dummy_hpp;
  532. hists__for_each_format(hists, fmt) {
  533. if (perf_hpp__is_sort_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
  534. break;
  535. if (first)
  536. first = false;
  537. else
  538. ret += 2;
  539. ret += fmt->width(fmt, &dummy_hpp, hists_to_evsel(hists));
  540. }
  541. return ret;
  542. }
  543. void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists)
  544. {
  545. if (perf_hpp__is_sort_entry(fmt))
  546. return perf_hpp__reset_sort_width(fmt, hists);
  547. if (perf_hpp__is_dynamic_entry(fmt))
  548. return;
  549. BUG_ON(fmt->idx >= PERF_HPP__MAX_INDEX);
  550. switch (fmt->idx) {
  551. case PERF_HPP__OVERHEAD:
  552. case PERF_HPP__OVERHEAD_SYS:
  553. case PERF_HPP__OVERHEAD_US:
  554. case PERF_HPP__OVERHEAD_ACC:
  555. fmt->len = 8;
  556. break;
  557. case PERF_HPP__OVERHEAD_GUEST_SYS:
  558. case PERF_HPP__OVERHEAD_GUEST_US:
  559. fmt->len = 9;
  560. break;
  561. case PERF_HPP__SAMPLES:
  562. case PERF_HPP__PERIOD:
  563. fmt->len = 12;
  564. break;
  565. default:
  566. break;
  567. }
  568. }
  569. void perf_hpp__set_user_width(const char *width_list_str)
  570. {
  571. struct perf_hpp_fmt *fmt;
  572. const char *ptr = width_list_str;
  573. perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
  574. char *p;
  575. int len = strtol(ptr, &p, 10);
  576. fmt->user_len = len;
  577. if (*p == ',')
  578. ptr = p + 1;
  579. else
  580. break;
  581. }
  582. }
  583. static int add_hierarchy_fmt(struct hists *hists, struct perf_hpp_fmt *fmt)
  584. {
  585. struct perf_hpp_list_node *node = NULL;
  586. struct perf_hpp_fmt *fmt_copy;
  587. bool found = false;
  588. bool skip = perf_hpp__should_skip(fmt, hists);
  589. list_for_each_entry(node, &hists->hpp_formats, list) {
  590. if (node->level == fmt->level) {
  591. found = true;
  592. break;
  593. }
  594. }
  595. if (!found) {
  596. node = malloc(sizeof(*node));
  597. if (node == NULL)
  598. return -1;
  599. node->skip = skip;
  600. node->level = fmt->level;
  601. perf_hpp_list__init(&node->hpp);
  602. hists->nr_hpp_node++;
  603. list_add_tail(&node->list, &hists->hpp_formats);
  604. }
  605. fmt_copy = perf_hpp_fmt__dup(fmt);
  606. if (fmt_copy == NULL)
  607. return -1;
  608. if (!skip)
  609. node->skip = false;
  610. list_add_tail(&fmt_copy->list, &node->hpp.fields);
  611. list_add_tail(&fmt_copy->sort_list, &node->hpp.sorts);
  612. return 0;
  613. }
  614. int perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
  615. struct perf_evlist *evlist)
  616. {
  617. struct perf_evsel *evsel;
  618. struct perf_hpp_fmt *fmt;
  619. struct hists *hists;
  620. int ret;
  621. if (!symbol_conf.report_hierarchy)
  622. return 0;
  623. evlist__for_each(evlist, evsel) {
  624. hists = evsel__hists(evsel);
  625. perf_hpp_list__for_each_sort_list(list, fmt) {
  626. if (perf_hpp__is_dynamic_entry(fmt) &&
  627. !perf_hpp__defined_dynamic_entry(fmt, hists))
  628. continue;
  629. ret = add_hierarchy_fmt(hists, fmt);
  630. if (ret < 0)
  631. return ret;
  632. }
  633. }
  634. return 0;
  635. }