perf hists: Introduce perf_hpp__list for period related columns
[deliverable/linux.git] / tools / perf / ui / hist.c
CommitLineData
ea251d51
NK
1#include <math.h>
2
3#include "../util/hist.h"
4#include "../util/util.h"
5#include "../util/sort.h"
6
7
8/* hist period print (hpp) functions */
9static int hpp__header_overhead(struct perf_hpp *hpp)
10{
5395a048 11 return scnprintf(hpp->buf, hpp->size, "Overhead");
ea251d51
NK
12}
13
1d037ca1 14static int hpp__width_overhead(struct perf_hpp *hpp __maybe_unused)
ea251d51
NK
15{
16 return 8;
17}
18
19static int hpp__color_overhead(struct perf_hpp *hpp, struct hist_entry *he)
20{
b5ff71c3 21 struct hists *hists = he->hists;
b24c28f7 22 double percent = 100.0 * he->stat.period / hists->stats.total_period;
ea251d51 23
721b3112 24 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%", percent);
ea251d51
NK
25}
26
27static int hpp__entry_overhead(struct perf_hpp *hpp, struct hist_entry *he)
28{
b5ff71c3 29 struct hists *hists = he->hists;
b24c28f7 30 double percent = 100.0 * he->stat.period / hists->stats.total_period;
721b3112 31 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
ea251d51 32
9ffad987 33 return scnprintf(hpp->buf, hpp->size, fmt, percent);
ea251d51
NK
34}
35
36static int hpp__header_overhead_sys(struct perf_hpp *hpp)
37{
721b3112 38 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
9ffad987
NK
39
40 return scnprintf(hpp->buf, hpp->size, fmt, "sys");
ea251d51
NK
41}
42
1d037ca1 43static int hpp__width_overhead_sys(struct perf_hpp *hpp __maybe_unused)
ea251d51 44{
721b3112 45 return 7;
ea251d51
NK
46}
47
48static int hpp__color_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
49{
b5ff71c3 50 struct hists *hists = he->hists;
b24c28f7 51 double percent = 100.0 * he->stat.period_sys / hists->stats.total_period;
b5ff71c3 52
721b3112 53 return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
ea251d51
NK
54}
55
56static int hpp__entry_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
57{
b5ff71c3 58 struct hists *hists = he->hists;
b24c28f7 59 double percent = 100.0 * he->stat.period_sys / hists->stats.total_period;
721b3112 60 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
9ffad987
NK
61
62 return scnprintf(hpp->buf, hpp->size, fmt, percent);
ea251d51
NK
63}
64
65static int hpp__header_overhead_us(struct perf_hpp *hpp)
66{
721b3112 67 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
9ffad987
NK
68
69 return scnprintf(hpp->buf, hpp->size, fmt, "user");
ea251d51
NK
70}
71
1d037ca1 72static int hpp__width_overhead_us(struct perf_hpp *hpp __maybe_unused)
ea251d51 73{
721b3112 74 return 7;
ea251d51
NK
75}
76
77static int hpp__color_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
78{
b5ff71c3 79 struct hists *hists = he->hists;
b24c28f7 80 double percent = 100.0 * he->stat.period_us / hists->stats.total_period;
b5ff71c3 81
721b3112 82 return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
ea251d51
NK
83}
84
85static int hpp__entry_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
86{
b5ff71c3 87 struct hists *hists = he->hists;
b24c28f7 88 double percent = 100.0 * he->stat.period_us / hists->stats.total_period;
721b3112 89 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
9ffad987
NK
90
91 return scnprintf(hpp->buf, hpp->size, fmt, percent);
ea251d51
NK
92}
93
94static int hpp__header_overhead_guest_sys(struct perf_hpp *hpp)
95{
96 return scnprintf(hpp->buf, hpp->size, "guest sys");
97}
98
1d037ca1 99static int hpp__width_overhead_guest_sys(struct perf_hpp *hpp __maybe_unused)
ea251d51
NK
100{
101 return 9;
102}
103
104static int hpp__color_overhead_guest_sys(struct perf_hpp *hpp,
105 struct hist_entry *he)
106{
b5ff71c3 107 struct hists *hists = he->hists;
b24c28f7 108 double percent = 100.0 * he->stat.period_guest_sys / hists->stats.total_period;
b5ff71c3 109
721b3112 110 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
ea251d51
NK
111}
112
113static int hpp__entry_overhead_guest_sys(struct perf_hpp *hpp,
114 struct hist_entry *he)
115{
b5ff71c3 116 struct hists *hists = he->hists;
b24c28f7 117 double percent = 100.0 * he->stat.period_guest_sys / hists->stats.total_period;
721b3112 118 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
9ffad987
NK
119
120 return scnprintf(hpp->buf, hpp->size, fmt, percent);
ea251d51
NK
121}
122
123static int hpp__header_overhead_guest_us(struct perf_hpp *hpp)
124{
125 return scnprintf(hpp->buf, hpp->size, "guest usr");
126}
127
1d037ca1 128static int hpp__width_overhead_guest_us(struct perf_hpp *hpp __maybe_unused)
ea251d51
NK
129{
130 return 9;
131}
132
133static int hpp__color_overhead_guest_us(struct perf_hpp *hpp,
134 struct hist_entry *he)
135{
b5ff71c3 136 struct hists *hists = he->hists;
b24c28f7 137 double percent = 100.0 * he->stat.period_guest_us / hists->stats.total_period;
b5ff71c3 138
721b3112 139 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
ea251d51
NK
140}
141
142static int hpp__entry_overhead_guest_us(struct perf_hpp *hpp,
143 struct hist_entry *he)
144{
b5ff71c3 145 struct hists *hists = he->hists;
b24c28f7 146 double percent = 100.0 * he->stat.period_guest_us / hists->stats.total_period;
721b3112 147 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
9ffad987
NK
148
149 return scnprintf(hpp->buf, hpp->size, fmt, percent);
ea251d51
NK
150}
151
5395a048
JO
152static int hpp__header_baseline(struct perf_hpp *hpp)
153{
154 return scnprintf(hpp->buf, hpp->size, "Baseline");
155}
156
157static int hpp__width_baseline(struct perf_hpp *hpp __maybe_unused)
158{
159 return 8;
160}
161
162static double baseline_percent(struct hist_entry *he)
163{
b821c732 164 struct hist_entry *pair = hist_entry__next_pair(he);
5395a048
JO
165 struct hists *pair_hists = pair ? pair->hists : NULL;
166 double percent = 0.0;
167
168 if (pair) {
169 u64 total_period = pair_hists->stats.total_period;
b24c28f7 170 u64 base_period = pair->stat.period;
5395a048
JO
171
172 percent = 100.0 * base_period / total_period;
173 }
174
175 return percent;
176}
177
178static int hpp__color_baseline(struct perf_hpp *hpp, struct hist_entry *he)
179{
180 double percent = baseline_percent(he);
181
b821c732 182 if (hist_entry__has_pairs(he))
6e92349d
JO
183 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%", percent);
184 else
185 return scnprintf(hpp->buf, hpp->size, " ");
5395a048
JO
186}
187
188static int hpp__entry_baseline(struct perf_hpp *hpp, struct hist_entry *he)
189{
190 double percent = baseline_percent(he);
191 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
192
b821c732 193 if (hist_entry__has_pairs(he) || symbol_conf.field_sep)
6e92349d
JO
194 return scnprintf(hpp->buf, hpp->size, fmt, percent);
195 else
196 return scnprintf(hpp->buf, hpp->size, " ");
5395a048
JO
197}
198
ea251d51
NK
199static int hpp__header_samples(struct perf_hpp *hpp)
200{
9ffad987
NK
201 const char *fmt = symbol_conf.field_sep ? "%s" : "%11s";
202
203 return scnprintf(hpp->buf, hpp->size, fmt, "Samples");
ea251d51
NK
204}
205
1d037ca1 206static int hpp__width_samples(struct perf_hpp *hpp __maybe_unused)
ea251d51
NK
207{
208 return 11;
209}
210
211static int hpp__entry_samples(struct perf_hpp *hpp, struct hist_entry *he)
212{
9ffad987
NK
213 const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%11" PRIu64;
214
b24c28f7 215 return scnprintf(hpp->buf, hpp->size, fmt, he->stat.nr_events);
ea251d51
NK
216}
217
218static int hpp__header_period(struct perf_hpp *hpp)
219{
9ffad987
NK
220 const char *fmt = symbol_conf.field_sep ? "%s" : "%12s";
221
222 return scnprintf(hpp->buf, hpp->size, fmt, "Period");
ea251d51
NK
223}
224
1d037ca1 225static int hpp__width_period(struct perf_hpp *hpp __maybe_unused)
ea251d51
NK
226{
227 return 12;
228}
229
230static int hpp__entry_period(struct perf_hpp *hpp, struct hist_entry *he)
231{
9ffad987
NK
232 const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%12" PRIu64;
233
b24c28f7 234 return scnprintf(hpp->buf, hpp->size, fmt, he->stat.period);
ea251d51
NK
235}
236
61949b21
JO
237static int hpp__header_period_baseline(struct perf_hpp *hpp)
238{
239 const char *fmt = symbol_conf.field_sep ? "%s" : "%12s";
240
241 return scnprintf(hpp->buf, hpp->size, fmt, "Period Base");
242}
243
244static int hpp__width_period_baseline(struct perf_hpp *hpp __maybe_unused)
245{
246 return 12;
247}
248
249static int hpp__entry_period_baseline(struct perf_hpp *hpp, struct hist_entry *he)
250{
b821c732 251 struct hist_entry *pair = hist_entry__next_pair(he);
61949b21
JO
252 u64 period = pair ? pair->stat.period : 0;
253 const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%12" PRIu64;
254
255 return scnprintf(hpp->buf, hpp->size, fmt, period);
256}
ea251d51
NK
257static int hpp__header_delta(struct perf_hpp *hpp)
258{
9ffad987
NK
259 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
260
261 return scnprintf(hpp->buf, hpp->size, fmt, "Delta");
ea251d51
NK
262}
263
1d037ca1 264static int hpp__width_delta(struct perf_hpp *hpp __maybe_unused)
ea251d51
NK
265{
266 return 7;
267}
268
269static int hpp__entry_delta(struct perf_hpp *hpp, struct hist_entry *he)
270{
9ffad987
NK
271 const char *fmt = symbol_conf.field_sep ? "%s" : "%7.7s";
272 char buf[32] = " ";
96c47f19 273 double diff;
ea251d51 274
96c47f19
JO
275 if (he->diff.computed)
276 diff = he->diff.period_ratio_delta;
277 else
278 diff = perf_diff__compute_delta(he);
ea251d51 279
9ffad987
NK
280 if (fabs(diff) >= 0.01)
281 scnprintf(buf, sizeof(buf), "%+4.2F%%", diff);
ea251d51 282
9ffad987 283 return scnprintf(hpp->buf, hpp->size, fmt, buf);
ea251d51
NK
284}
285
7aaf6b35
JO
286static int hpp__header_ratio(struct perf_hpp *hpp)
287{
288 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
289
290 return scnprintf(hpp->buf, hpp->size, fmt, "Ratio");
291}
292
293static int hpp__width_ratio(struct perf_hpp *hpp __maybe_unused)
294{
295 return 14;
296}
297
298static int hpp__entry_ratio(struct perf_hpp *hpp, struct hist_entry *he)
299{
7aaf6b35
JO
300 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
301 char buf[32] = " ";
96c47f19
JO
302 double ratio;
303
304 if (he->diff.computed)
305 ratio = he->diff.period_ratio;
306 else
307 ratio = perf_diff__compute_ratio(he);
7aaf6b35
JO
308
309 if (ratio > 0.0)
310 scnprintf(buf, sizeof(buf), "%+14.6F", ratio);
311
312 return scnprintf(hpp->buf, hpp->size, fmt, buf);
313}
314
81d5f958
JO
315static int hpp__header_wdiff(struct perf_hpp *hpp)
316{
317 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
318
319 return scnprintf(hpp->buf, hpp->size, fmt, "Weighted diff");
320}
321
322static int hpp__width_wdiff(struct perf_hpp *hpp __maybe_unused)
323{
324 return 14;
325}
326
327static int hpp__entry_wdiff(struct perf_hpp *hpp, struct hist_entry *he)
328{
329 const char *fmt = symbol_conf.field_sep ? "%s" : "%14s";
330 char buf[32] = " ";
331 s64 wdiff;
332
333 if (he->diff.computed)
334 wdiff = he->diff.wdiff;
335 else
336 wdiff = perf_diff__compute_wdiff(he);
337
338 if (wdiff != 0)
339 scnprintf(buf, sizeof(buf), "%14ld", wdiff);
340
341 return scnprintf(hpp->buf, hpp->size, fmt, buf);
342}
343
ea251d51
NK
344static int hpp__header_displ(struct perf_hpp *hpp)
345{
346 return scnprintf(hpp->buf, hpp->size, "Displ.");
347}
348
1d037ca1 349static int hpp__width_displ(struct perf_hpp *hpp __maybe_unused)
ea251d51
NK
350{
351 return 6;
352}
353
1d037ca1 354static int hpp__entry_displ(struct perf_hpp *hpp,
dd464345 355 struct hist_entry *he)
ea251d51 356{
b821c732 357 struct hist_entry *pair = hist_entry__next_pair(he);
dd464345 358 long displacement = pair ? pair->position - he->position : 0;
9ffad987
NK
359 const char *fmt = symbol_conf.field_sep ? "%s" : "%6.6s";
360 char buf[32] = " ";
ea251d51 361
dd464345
JO
362 if (displacement)
363 scnprintf(buf, sizeof(buf), "%+4ld", displacement);
ea251d51 364
9ffad987 365 return scnprintf(hpp->buf, hpp->size, fmt, buf);
ea251d51
NK
366}
367
ed279da2
JO
368static int hpp__header_formula(struct perf_hpp *hpp)
369{
370 const char *fmt = symbol_conf.field_sep ? "%s" : "%70s";
371
372 return scnprintf(hpp->buf, hpp->size, fmt, "Formula");
373}
374
375static int hpp__width_formula(struct perf_hpp *hpp __maybe_unused)
376{
377 return 70;
378}
379
380static int hpp__entry_formula(struct perf_hpp *hpp, struct hist_entry *he)
381{
382 const char *fmt = symbol_conf.field_sep ? "%s" : "%-70s";
383 char buf[96] = " ";
384
385 perf_diff__formula(buf, sizeof(buf), he);
386 return scnprintf(hpp->buf, hpp->size, fmt, buf);
387}
388
1240005e
JO
389#define HPP__COLOR_PRINT_FNS(_name) \
390 { \
391 .header = hpp__header_ ## _name, \
392 .width = hpp__width_ ## _name, \
393 .color = hpp__color_ ## _name, \
394 .entry = hpp__entry_ ## _name \
395 }
ea251d51 396
1240005e
JO
397#define HPP__PRINT_FNS(_name) \
398 { \
399 .header = hpp__header_ ## _name, \
400 .width = hpp__width_ ## _name, \
401 .entry = hpp__entry_ ## _name \
402 }
ea251d51
NK
403
404struct perf_hpp_fmt perf_hpp__format[] = {
1240005e
JO
405 HPP__COLOR_PRINT_FNS(baseline),
406 HPP__COLOR_PRINT_FNS(overhead),
407 HPP__COLOR_PRINT_FNS(overhead_sys),
408 HPP__COLOR_PRINT_FNS(overhead_us),
409 HPP__COLOR_PRINT_FNS(overhead_guest_sys),
410 HPP__COLOR_PRINT_FNS(overhead_guest_us),
411 HPP__PRINT_FNS(samples),
412 HPP__PRINT_FNS(period),
413 HPP__PRINT_FNS(period_baseline),
414 HPP__PRINT_FNS(delta),
415 HPP__PRINT_FNS(ratio),
416 HPP__PRINT_FNS(wdiff),
417 HPP__PRINT_FNS(displ),
418 HPP__PRINT_FNS(formula)
ea251d51
NK
419};
420
1240005e
JO
421LIST_HEAD(perf_hpp__list);
422
ea251d51
NK
423#undef HPP__COLOR_PRINT_FNS
424#undef HPP__PRINT_FNS
425
1d77822e 426void perf_hpp__init(void)
ea251d51
NK
427{
428 if (symbol_conf.show_cpu_utilization) {
1240005e
JO
429 perf_hpp__column_enable(PERF_HPP__OVERHEAD_SYS);
430 perf_hpp__column_enable(PERF_HPP__OVERHEAD_US);
ea251d51
NK
431
432 if (perf_guest) {
1240005e
JO
433 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_SYS);
434 perf_hpp__column_enable(PERF_HPP__OVERHEAD_GUEST_US);
ea251d51
NK
435 }
436 }
437
438 if (symbol_conf.show_nr_samples)
1240005e 439 perf_hpp__column_enable(PERF_HPP__SAMPLES);
ea251d51
NK
440
441 if (symbol_conf.show_total_period)
1240005e 442 perf_hpp__column_enable(PERF_HPP__PERIOD);
1d77822e 443}
ea251d51 444
1240005e
JO
445void perf_hpp__column_register(struct perf_hpp_fmt *format)
446{
447 list_add_tail(&format->list, &perf_hpp__list);
448}
449
450void perf_hpp__column_enable(unsigned col)
1d77822e
JO
451{
452 BUG_ON(col >= PERF_HPP__MAX_INDEX);
1240005e 453 perf_hpp__column_register(&perf_hpp__format[col]);
ea251d51
NK
454}
455
456static inline void advance_hpp(struct perf_hpp *hpp, int inc)
457{
458 hpp->buf += inc;
459 hpp->size -= inc;
460}
461
462int hist_entry__period_snprintf(struct perf_hpp *hpp, struct hist_entry *he,
463 bool color)
464{
465 const char *sep = symbol_conf.field_sep;
1240005e 466 struct perf_hpp_fmt *fmt;
ea251d51 467 char *start = hpp->buf;
1240005e 468 int ret;
5395a048 469 bool first = true;
ea251d51
NK
470
471 if (symbol_conf.exclude_other && !he->parent)
472 return 0;
473
1240005e 474 perf_hpp__for_each_format(fmt) {
5395a048 475 if (!sep || !first) {
ea251d51
NK
476 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
477 advance_hpp(hpp, ret);
5395a048 478 first = false;
ea251d51
NK
479 }
480
1240005e
JO
481 if (color && fmt->color)
482 ret = fmt->color(hpp, he);
ea251d51 483 else
1240005e 484 ret = fmt->entry(hpp, he);
ea251d51
NK
485
486 advance_hpp(hpp, ret);
487 }
488
489 return hpp->buf - start;
490}
491
492int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
493 struct hists *hists)
494{
495 const char *sep = symbol_conf.field_sep;
496 struct sort_entry *se;
497 int ret = 0;
498
499 list_for_each_entry(se, &hist_entry__sort_list, list) {
500 if (se->elide)
501 continue;
502
503 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
504 ret += se->se_snprintf(he, s + ret, size - ret,
505 hists__col_len(hists, se->se_width_idx));
506 }
507
508 return ret;
509}
7e62ef44
NK
510
511/*
512 * See hists__fprintf to match the column widths
513 */
514unsigned int hists__sort_list_width(struct hists *hists)
515{
1240005e 516 struct perf_hpp_fmt *fmt;
7e62ef44 517 struct sort_entry *se;
1240005e 518 int i = 0, ret = 0;
7e62ef44 519
1240005e 520 perf_hpp__for_each_format(fmt) {
7e62ef44
NK
521 if (i)
522 ret += 2;
523
1240005e 524 ret += fmt->width(NULL);
7e62ef44
NK
525 }
526
527 list_for_each_entry(se, &hist_entry__sort_list, list)
528 if (!se->elide)
529 ret += 2 + hists__col_len(hists, se->se_width_idx);
530
531 if (verbose) /* Addr + origin */
532 ret += 3 + BITS_PER_LONG / 4;
533
534 return ret;
535}
This page took 0.078222 seconds and 5 git commands to generate.