hist.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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 hists *hists)
  178. {
  179. int len = fmt->user_len ?: fmt->len;
  180. struct perf_evsel *evsel = hists_to_evsel(hists);
  181. if (symbol_conf.event_group)
  182. len = max(len, evsel->nr_members * fmt->len);
  183. if (len < (int)strlen(fmt->name))
  184. len = strlen(fmt->name);
  185. return len;
  186. }
  187. static int hpp__header_fn(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp,
  188. struct hists *hists)
  189. {
  190. int len = hpp__width_fn(fmt, hpp, hists);
  191. return scnprintf(hpp->buf, hpp->size, "%*s", len, fmt->name);
  192. }
  193. static int hpp_color_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
  194. {
  195. va_list args;
  196. ssize_t ssize = hpp->size;
  197. double percent;
  198. int ret, len;
  199. va_start(args, fmt);
  200. len = va_arg(args, int);
  201. percent = va_arg(args, double);
  202. ret = percent_color_len_snprintf(hpp->buf, hpp->size, fmt, len, percent);
  203. va_end(args);
  204. return (ret >= ssize) ? (ssize - 1) : ret;
  205. }
  206. static int hpp_entry_scnprintf(struct perf_hpp *hpp, const char *fmt, ...)
  207. {
  208. va_list args;
  209. ssize_t ssize = hpp->size;
  210. int ret;
  211. va_start(args, fmt);
  212. ret = vsnprintf(hpp->buf, hpp->size, fmt, args);
  213. va_end(args);
  214. return (ret >= ssize) ? (ssize - 1) : ret;
  215. }
  216. #define __HPP_COLOR_PERCENT_FN(_type, _field) \
  217. static u64 he_get_##_field(struct hist_entry *he) \
  218. { \
  219. return he->stat._field; \
  220. } \
  221. \
  222. static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
  223. struct perf_hpp *hpp, struct hist_entry *he) \
  224. { \
  225. return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
  226. hpp_color_scnprintf, true); \
  227. }
  228. #define __HPP_ENTRY_PERCENT_FN(_type, _field) \
  229. static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
  230. struct perf_hpp *hpp, struct hist_entry *he) \
  231. { \
  232. return hpp__fmt(fmt, hpp, he, he_get_##_field, " %*.2f%%", \
  233. hpp_entry_scnprintf, true); \
  234. }
  235. #define __HPP_SORT_FN(_type, _field) \
  236. static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
  237. struct hist_entry *a, struct hist_entry *b) \
  238. { \
  239. return __hpp__sort(a, b, he_get_##_field); \
  240. }
  241. #define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
  242. static u64 he_get_acc_##_field(struct hist_entry *he) \
  243. { \
  244. return he->stat_acc->_field; \
  245. } \
  246. \
  247. static int hpp__color_##_type(struct perf_hpp_fmt *fmt, \
  248. struct perf_hpp *hpp, struct hist_entry *he) \
  249. { \
  250. return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
  251. hpp_color_scnprintf, true); \
  252. }
  253. #define __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
  254. static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
  255. struct perf_hpp *hpp, struct hist_entry *he) \
  256. { \
  257. return hpp__fmt_acc(fmt, hpp, he, he_get_acc_##_field, " %*.2f%%", \
  258. hpp_entry_scnprintf, true); \
  259. }
  260. #define __HPP_SORT_ACC_FN(_type, _field) \
  261. static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
  262. struct hist_entry *a, struct hist_entry *b) \
  263. { \
  264. return __hpp__sort_acc(a, b, he_get_acc_##_field); \
  265. }
  266. #define __HPP_ENTRY_RAW_FN(_type, _field) \
  267. static u64 he_get_raw_##_field(struct hist_entry *he) \
  268. { \
  269. return he->stat._field; \
  270. } \
  271. \
  272. static int hpp__entry_##_type(struct perf_hpp_fmt *fmt, \
  273. struct perf_hpp *hpp, struct hist_entry *he) \
  274. { \
  275. return hpp__fmt(fmt, hpp, he, he_get_raw_##_field, " %*"PRIu64, \
  276. hpp_entry_scnprintf, false); \
  277. }
  278. #define __HPP_SORT_RAW_FN(_type, _field) \
  279. static int64_t hpp__sort_##_type(struct perf_hpp_fmt *fmt __maybe_unused, \
  280. struct hist_entry *a, struct hist_entry *b) \
  281. { \
  282. return __hpp__sort(a, b, he_get_raw_##_field); \
  283. }
  284. #define HPP_PERCENT_FNS(_type, _field) \
  285. __HPP_COLOR_PERCENT_FN(_type, _field) \
  286. __HPP_ENTRY_PERCENT_FN(_type, _field) \
  287. __HPP_SORT_FN(_type, _field)
  288. #define HPP_PERCENT_ACC_FNS(_type, _field) \
  289. __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
  290. __HPP_ENTRY_ACC_PERCENT_FN(_type, _field) \
  291. __HPP_SORT_ACC_FN(_type, _field)
  292. #define HPP_RAW_FNS(_type, _field) \
  293. __HPP_ENTRY_RAW_FN(_type, _field) \
  294. __HPP_SORT_RAW_FN(_type, _field)
  295. HPP_PERCENT_FNS(overhead, period)
  296. HPP_PERCENT_FNS(overhead_sys, period_sys)
  297. HPP_PERCENT_FNS(overhead_us, period_us)
  298. HPP_PERCENT_FNS(overhead_guest_sys, period_guest_sys)
  299. HPP_PERCENT_FNS(overhead_guest_us, period_guest_us)
  300. HPP_PERCENT_ACC_FNS(overhead_acc, period)
  301. HPP_RAW_FNS(samples, nr_events)
  302. HPP_RAW_FNS(period, period)
  303. static int64_t hpp__nop_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
  304. struct hist_entry *a __maybe_unused,
  305. struct hist_entry *b __maybe_unused)
  306. {
  307. return 0;
  308. }
  309. static bool perf_hpp__is_hpp_entry(struct perf_hpp_fmt *a)
  310. {
  311. return a->header == hpp__header_fn;
  312. }
  313. static bool hpp__equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
  314. {
  315. if (!perf_hpp__is_hpp_entry(a) || !perf_hpp__is_hpp_entry(b))
  316. return false;
  317. return a->idx == b->idx;
  318. }
  319. #define HPP__COLOR_PRINT_FNS(_name, _fn, _idx) \
  320. { \
  321. .name = _name, \
  322. .header = hpp__header_fn, \
  323. .width = hpp__width_fn, \
  324. .color = hpp__color_ ## _fn, \
  325. .entry = hpp__entry_ ## _fn, \
  326. .cmp = hpp__nop_cmp, \
  327. .collapse = hpp__nop_cmp, \
  328. .sort = hpp__sort_ ## _fn, \
  329. .idx = PERF_HPP__ ## _idx, \
  330. .equal = hpp__equal, \
  331. }
  332. #define HPP__COLOR_ACC_PRINT_FNS(_name, _fn, _idx) \
  333. { \
  334. .name = _name, \
  335. .header = hpp__header_fn, \
  336. .width = hpp__width_fn, \
  337. .color = hpp__color_ ## _fn, \
  338. .entry = hpp__entry_ ## _fn, \
  339. .cmp = hpp__nop_cmp, \
  340. .collapse = hpp__nop_cmp, \
  341. .sort = hpp__sort_ ## _fn, \
  342. .idx = PERF_HPP__ ## _idx, \
  343. .equal = hpp__equal, \
  344. }
  345. #define HPP__PRINT_FNS(_name, _fn, _idx) \
  346. { \
  347. .name = _name, \
  348. .header = hpp__header_fn, \
  349. .width = hpp__width_fn, \
  350. .entry = hpp__entry_ ## _fn, \
  351. .cmp = hpp__nop_cmp, \
  352. .collapse = hpp__nop_cmp, \
  353. .sort = hpp__sort_ ## _fn, \
  354. .idx = PERF_HPP__ ## _idx, \
  355. .equal = hpp__equal, \
  356. }
  357. struct perf_hpp_fmt perf_hpp__format[] = {
  358. HPP__COLOR_PRINT_FNS("Overhead", overhead, OVERHEAD),
  359. HPP__COLOR_PRINT_FNS("sys", overhead_sys, OVERHEAD_SYS),
  360. HPP__COLOR_PRINT_FNS("usr", overhead_us, OVERHEAD_US),
  361. HPP__COLOR_PRINT_FNS("guest sys", overhead_guest_sys, OVERHEAD_GUEST_SYS),
  362. HPP__COLOR_PRINT_FNS("guest usr", overhead_guest_us, OVERHEAD_GUEST_US),
  363. HPP__COLOR_ACC_PRINT_FNS("Children", overhead_acc, OVERHEAD_ACC),
  364. HPP__PRINT_FNS("Samples", samples, SAMPLES),
  365. HPP__PRINT_FNS("Period", period, PERIOD)
  366. };
  367. struct perf_hpp_list perf_hpp_list = {
  368. .fields = LIST_HEAD_INIT(perf_hpp_list.fields),
  369. .sorts = LIST_HEAD_INIT(perf_hpp_list.sorts),
  370. };
  371. #undef HPP__COLOR_PRINT_FNS
  372. #undef HPP__COLOR_ACC_PRINT_FNS
  373. #undef HPP__PRINT_FNS
  374. #undef HPP_PERCENT_FNS
  375. #undef HPP_PERCENT_ACC_FNS
  376. #undef HPP_RAW_FNS
  377. #undef __HPP_HEADER_FN
  378. #undef __HPP_WIDTH_FN
  379. #undef __HPP_COLOR_PERCENT_FN
  380. #undef __HPP_ENTRY_PERCENT_FN
  381. #undef __HPP_COLOR_ACC_PERCENT_FN
  382. #undef __HPP_ENTRY_ACC_PERCENT_FN
  383. #undef __HPP_ENTRY_RAW_FN
  384. #undef __HPP_SORT_FN
  385. #undef __HPP_SORT_ACC_FN
  386. #undef __HPP_SORT_RAW_FN
  387. void perf_hpp__init(void)
  388. {
  389. int i;
  390. for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
  391. struct perf_hpp_fmt *fmt = &perf_hpp__format[i];
  392. INIT_LIST_HEAD(&fmt->list);
  393. /* sort_list may be linked by setup_sorting() */
  394. if (fmt->sort_list.next == NULL)
  395. INIT_LIST_HEAD(&fmt->sort_list);
  396. }
  397. /*
  398. * If user specified field order, no need to setup default fields.
  399. */
  400. if (is_strict_order(field_order))
  401. return;
  402. if (symbol_conf.cumulate_callchain) {
  403. hpp_dimension__add_output(PERF_HPP__OVERHEAD_ACC);
  404. perf_hpp__format[PERF_HPP__OVERHEAD].name = "Self";
  405. }
  406. hpp_dimension__add_output(PERF_HPP__OVERHEAD);
  407. if (symbol_conf.show_cpu_utilization) {
  408. hpp_dimension__add_output(PERF_HPP__OVERHEAD_SYS);
  409. hpp_dimension__add_output(PERF_HPP__OVERHEAD_US);
  410. if (perf_guest) {
  411. hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_SYS);
  412. hpp_dimension__add_output(PERF_HPP__OVERHEAD_GUEST_US);
  413. }
  414. }
  415. if (symbol_conf.show_nr_samples)
  416. hpp_dimension__add_output(PERF_HPP__SAMPLES);
  417. if (symbol_conf.show_total_period)
  418. hpp_dimension__add_output(PERF_HPP__PERIOD);
  419. }
  420. void perf_hpp_list__column_register(struct perf_hpp_list *list,
  421. struct perf_hpp_fmt *format)
  422. {
  423. list_add_tail(&format->list, &list->fields);
  424. }
  425. void perf_hpp_list__register_sort_field(struct perf_hpp_list *list,
  426. struct perf_hpp_fmt *format)
  427. {
  428. list_add_tail(&format->sort_list, &list->sorts);
  429. }
  430. void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
  431. {
  432. list_del(&format->list);
  433. }
  434. void perf_hpp__cancel_cumulate(void)
  435. {
  436. struct perf_hpp_fmt *fmt, *acc, *ovh, *tmp;
  437. if (is_strict_order(field_order))
  438. return;
  439. ovh = &perf_hpp__format[PERF_HPP__OVERHEAD];
  440. acc = &perf_hpp__format[PERF_HPP__OVERHEAD_ACC];
  441. perf_hpp_list__for_each_format_safe(&perf_hpp_list, fmt, tmp) {
  442. if (acc->equal(acc, fmt)) {
  443. perf_hpp__column_unregister(fmt);
  444. continue;
  445. }
  446. if (ovh->equal(ovh, fmt))
  447. fmt->name = "Overhead";
  448. }
  449. }
  450. static bool fmt_equal(struct perf_hpp_fmt *a, struct perf_hpp_fmt *b)
  451. {
  452. return a->equal && a->equal(a, b);
  453. }
  454. void perf_hpp__setup_output_field(struct perf_hpp_list *list)
  455. {
  456. struct perf_hpp_fmt *fmt;
  457. /* append sort keys to output field */
  458. perf_hpp_list__for_each_sort_list(list, fmt) {
  459. struct perf_hpp_fmt *pos;
  460. perf_hpp_list__for_each_format(list, pos) {
  461. if (fmt_equal(fmt, pos))
  462. goto next;
  463. }
  464. perf_hpp__column_register(fmt);
  465. next:
  466. continue;
  467. }
  468. }
  469. void perf_hpp__append_sort_keys(struct perf_hpp_list *list)
  470. {
  471. struct perf_hpp_fmt *fmt;
  472. /* append output fields to sort keys */
  473. perf_hpp_list__for_each_format(list, fmt) {
  474. struct perf_hpp_fmt *pos;
  475. perf_hpp_list__for_each_sort_list(list, pos) {
  476. if (fmt_equal(fmt, pos))
  477. goto next;
  478. }
  479. perf_hpp__register_sort_field(fmt);
  480. next:
  481. continue;
  482. }
  483. }
  484. static void fmt_free(struct perf_hpp_fmt *fmt)
  485. {
  486. if (fmt->free)
  487. fmt->free(fmt);
  488. }
  489. void perf_hpp__reset_output_field(struct perf_hpp_list *list)
  490. {
  491. struct perf_hpp_fmt *fmt, *tmp;
  492. /* reset output fields */
  493. perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
  494. list_del_init(&fmt->list);
  495. list_del_init(&fmt->sort_list);
  496. fmt_free(fmt);
  497. }
  498. /* reset sort keys */
  499. perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
  500. list_del_init(&fmt->list);
  501. list_del_init(&fmt->sort_list);
  502. fmt_free(fmt);
  503. }
  504. }
  505. /*
  506. * See hists__fprintf to match the column widths
  507. */
  508. unsigned int hists__sort_list_width(struct hists *hists)
  509. {
  510. struct perf_hpp_fmt *fmt;
  511. int ret = 0;
  512. bool first = true;
  513. struct perf_hpp dummy_hpp;
  514. hists__for_each_format(hists, fmt) {
  515. if (perf_hpp__should_skip(fmt, hists))
  516. continue;
  517. if (first)
  518. first = false;
  519. else
  520. ret += 2;
  521. ret += fmt->width(fmt, &dummy_hpp, hists);
  522. }
  523. if (verbose && hists__has(hists, sym)) /* Addr + origin */
  524. ret += 3 + BITS_PER_LONG / 4;
  525. return ret;
  526. }
  527. unsigned int hists__overhead_width(struct hists *hists)
  528. {
  529. struct perf_hpp_fmt *fmt;
  530. int ret = 0;
  531. bool first = true;
  532. struct perf_hpp dummy_hpp;
  533. hists__for_each_format(hists, fmt) {
  534. if (perf_hpp__is_sort_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
  535. break;
  536. if (first)
  537. first = false;
  538. else
  539. ret += 2;
  540. ret += fmt->width(fmt, &dummy_hpp, hists);
  541. }
  542. return ret;
  543. }
  544. void perf_hpp__reset_width(struct perf_hpp_fmt *fmt, struct hists *hists)
  545. {
  546. if (perf_hpp__is_sort_entry(fmt))
  547. return perf_hpp__reset_sort_width(fmt, hists);
  548. if (perf_hpp__is_dynamic_entry(fmt))
  549. return;
  550. BUG_ON(fmt->idx >= PERF_HPP__MAX_INDEX);
  551. switch (fmt->idx) {
  552. case PERF_HPP__OVERHEAD:
  553. case PERF_HPP__OVERHEAD_SYS:
  554. case PERF_HPP__OVERHEAD_US:
  555. case PERF_HPP__OVERHEAD_ACC:
  556. fmt->len = 8;
  557. break;
  558. case PERF_HPP__OVERHEAD_GUEST_SYS:
  559. case PERF_HPP__OVERHEAD_GUEST_US:
  560. fmt->len = 9;
  561. break;
  562. case PERF_HPP__SAMPLES:
  563. case PERF_HPP__PERIOD:
  564. fmt->len = 12;
  565. break;
  566. default:
  567. break;
  568. }
  569. }
  570. void perf_hpp__set_user_width(const char *width_list_str)
  571. {
  572. struct perf_hpp_fmt *fmt;
  573. const char *ptr = width_list_str;
  574. perf_hpp_list__for_each_format(&perf_hpp_list, fmt) {
  575. char *p;
  576. int len = strtol(ptr, &p, 10);
  577. fmt->user_len = len;
  578. if (*p == ',')
  579. ptr = p + 1;
  580. else
  581. break;
  582. }
  583. }
  584. static int add_hierarchy_fmt(struct hists *hists, struct perf_hpp_fmt *fmt)
  585. {
  586. struct perf_hpp_list_node *node = NULL;
  587. struct perf_hpp_fmt *fmt_copy;
  588. bool found = false;
  589. bool skip = perf_hpp__should_skip(fmt, hists);
  590. list_for_each_entry(node, &hists->hpp_formats, list) {
  591. if (node->level == fmt->level) {
  592. found = true;
  593. break;
  594. }
  595. }
  596. if (!found) {
  597. node = malloc(sizeof(*node));
  598. if (node == NULL)
  599. return -1;
  600. node->skip = skip;
  601. node->level = fmt->level;
  602. perf_hpp_list__init(&node->hpp);
  603. hists->nr_hpp_node++;
  604. list_add_tail(&node->list, &hists->hpp_formats);
  605. }
  606. fmt_copy = perf_hpp_fmt__dup(fmt);
  607. if (fmt_copy == NULL)
  608. return -1;
  609. if (!skip)
  610. node->skip = false;
  611. list_add_tail(&fmt_copy->list, &node->hpp.fields);
  612. list_add_tail(&fmt_copy->sort_list, &node->hpp.sorts);
  613. return 0;
  614. }
  615. int perf_hpp__setup_hists_formats(struct perf_hpp_list *list,
  616. struct perf_evlist *evlist)
  617. {
  618. struct perf_evsel *evsel;
  619. struct perf_hpp_fmt *fmt;
  620. struct hists *hists;
  621. int ret;
  622. if (!symbol_conf.report_hierarchy)
  623. return 0;
  624. evlist__for_each_entry(evlist, evsel) {
  625. hists = evsel__hists(evsel);
  626. perf_hpp_list__for_each_sort_list(list, fmt) {
  627. if (perf_hpp__is_dynamic_entry(fmt) &&
  628. !perf_hpp__defined_dynamic_entry(fmt, hists))
  629. continue;
  630. ret = add_hierarchy_fmt(hists, fmt);
  631. if (ret < 0)
  632. return ret;
  633. }
  634. }
  635. return 0;
  636. }