3 #include "../util/hist.h"
4 #include "../util/util.h"
5 #include "../util/sort.h"
8 /* hist period print (hpp) functions */
9 static int hpp__header_overhead(struct perf_hpp *hpp)
11 return scnprintf(hpp->buf, hpp->size, "Overhead");
14 static int hpp__width_overhead(struct perf_hpp *hpp __maybe_unused)
19 static int hpp__color_overhead(struct perf_hpp *hpp, struct hist_entry *he)
21 struct hists *hists = he->hists;
22 double percent = 100.0 * he->stat.period / hists->stats.total_period;
24 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%", percent);
27 static int hpp__entry_overhead(struct perf_hpp *hpp, struct hist_entry *he)
29 struct hists *hists = he->hists;
30 double percent = 100.0 * he->stat.period / hists->stats.total_period;
31 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
33 return scnprintf(hpp->buf, hpp->size, fmt, percent);
36 static int hpp__header_overhead_sys(struct perf_hpp *hpp)
38 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
40 return scnprintf(hpp->buf, hpp->size, fmt, "sys");
43 static int hpp__width_overhead_sys(struct perf_hpp *hpp __maybe_unused)
48 static int hpp__color_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
50 struct hists *hists = he->hists;
51 double percent = 100.0 * he->stat.period_sys / hists->stats.total_period;
53 return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
56 static int hpp__entry_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
58 struct hists *hists = he->hists;
59 double percent = 100.0 * he->stat.period_sys / hists->stats.total_period;
60 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
62 return scnprintf(hpp->buf, hpp->size, fmt, percent);
65 static int hpp__header_overhead_us(struct perf_hpp *hpp)
67 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
69 return scnprintf(hpp->buf, hpp->size, fmt, "user");
72 static int hpp__width_overhead_us(struct perf_hpp *hpp __maybe_unused)
77 static int hpp__color_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
79 struct hists *hists = he->hists;
80 double percent = 100.0 * he->stat.period_us / hists->stats.total_period;
82 return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
85 static int hpp__entry_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
87 struct hists *hists = he->hists;
88 double percent = 100.0 * he->stat.period_us / hists->stats.total_period;
89 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
91 return scnprintf(hpp->buf, hpp->size, fmt, percent);
94 static int hpp__header_overhead_guest_sys(struct perf_hpp *hpp)
96 return scnprintf(hpp->buf, hpp->size, "guest sys");
99 static int hpp__width_overhead_guest_sys(struct perf_hpp *hpp __maybe_unused)
104 static int hpp__color_overhead_guest_sys(struct perf_hpp *hpp,
105 struct hist_entry *he)
107 struct hists *hists = he->hists;
108 double percent = 100.0 * he->stat.period_guest_sys / hists->stats.total_period;
110 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
113 static int hpp__entry_overhead_guest_sys(struct perf_hpp *hpp,
114 struct hist_entry *he)
116 struct hists *hists = he->hists;
117 double percent = 100.0 * he->stat.period_guest_sys / hists->stats.total_period;
118 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
120 return scnprintf(hpp->buf, hpp->size, fmt, percent);
123 static int hpp__header_overhead_guest_us(struct perf_hpp *hpp)
125 return scnprintf(hpp->buf, hpp->size, "guest usr");
128 static int hpp__width_overhead_guest_us(struct perf_hpp *hpp __maybe_unused)
133 static int hpp__color_overhead_guest_us(struct perf_hpp *hpp,
134 struct hist_entry *he)
136 struct hists *hists = he->hists;
137 double percent = 100.0 * he->stat.period_guest_us / hists->stats.total_period;
139 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
142 static int hpp__entry_overhead_guest_us(struct perf_hpp *hpp,
143 struct hist_entry *he)
145 struct hists *hists = he->hists;
146 double percent = 100.0 * he->stat.period_guest_us / hists->stats.total_period;
147 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
149 return scnprintf(hpp->buf, hpp->size, fmt, percent);
152 static int hpp__header_baseline(struct perf_hpp *hpp)
154 return scnprintf(hpp->buf, hpp->size, "Baseline");
157 static int hpp__width_baseline(struct perf_hpp *hpp __maybe_unused)
162 static double baseline_percent(struct hist_entry *he)
164 struct hist_entry *pair = he->pair;
165 struct hists *pair_hists = pair ? pair->hists : NULL;
166 double percent = 0.0;
169 u64 total_period = pair_hists->stats.total_period;
170 u64 base_period = pair->stat.period;
172 percent = 100.0 * base_period / total_period;
178 static int hpp__color_baseline(struct perf_hpp *hpp, struct hist_entry *he)
180 double percent = baseline_percent(he);
182 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%", percent);
185 static int hpp__entry_baseline(struct perf_hpp *hpp, struct hist_entry *he)
187 double percent = baseline_percent(he);
188 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
190 return scnprintf(hpp->buf, hpp->size, fmt, percent);
193 static int hpp__header_samples(struct perf_hpp *hpp)
195 const char *fmt = symbol_conf.field_sep ? "%s" : "%11s";
197 return scnprintf(hpp->buf, hpp->size, fmt, "Samples");
200 static int hpp__width_samples(struct perf_hpp *hpp __maybe_unused)
205 static int hpp__entry_samples(struct perf_hpp *hpp, struct hist_entry *he)
207 const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%11" PRIu64;
209 return scnprintf(hpp->buf, hpp->size, fmt, he->stat.nr_events);
212 static int hpp__header_period(struct perf_hpp *hpp)
214 const char *fmt = symbol_conf.field_sep ? "%s" : "%12s";
216 return scnprintf(hpp->buf, hpp->size, fmt, "Period");
219 static int hpp__width_period(struct perf_hpp *hpp __maybe_unused)
224 static int hpp__entry_period(struct perf_hpp *hpp, struct hist_entry *he)
226 const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%12" PRIu64;
228 return scnprintf(hpp->buf, hpp->size, fmt, he->stat.period);
231 static int hpp__header_delta(struct perf_hpp *hpp)
233 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
235 return scnprintf(hpp->buf, hpp->size, fmt, "Delta");
238 static int hpp__width_delta(struct perf_hpp *hpp __maybe_unused)
243 static int hpp__entry_delta(struct perf_hpp *hpp, struct hist_entry *he)
245 struct hist_entry *pair = he->pair;
246 struct hists *pair_hists = pair ? pair->hists : NULL;
247 struct hists *hists = he->hists;
248 u64 old_total, new_total;
249 double old_percent = 0, new_percent = 0;
251 const char *fmt = symbol_conf.field_sep ? "%s" : "%7.7s";
254 old_total = pair_hists ? pair_hists->stats.total_period : 0;
255 if (old_total > 0 && pair)
256 old_percent = 100.0 * pair->stat.period / old_total;
258 new_total = hists->stats.total_period;
260 new_percent = 100.0 * he->stat.period / new_total;
262 diff = new_percent - old_percent;
263 if (fabs(diff) >= 0.01)
264 scnprintf(buf, sizeof(buf), "%+4.2F%%", diff);
266 return scnprintf(hpp->buf, hpp->size, fmt, buf);
269 static int hpp__header_displ(struct perf_hpp *hpp)
271 return scnprintf(hpp->buf, hpp->size, "Displ.");
274 static int hpp__width_displ(struct perf_hpp *hpp __maybe_unused)
279 static int hpp__entry_displ(struct perf_hpp *hpp,
280 struct hist_entry *he)
282 struct hist_entry *pair = he->pair;
283 long displacement = pair ? pair->position - he->position : 0;
284 const char *fmt = symbol_conf.field_sep ? "%s" : "%6.6s";
288 scnprintf(buf, sizeof(buf), "%+4ld", displacement);
290 return scnprintf(hpp->buf, hpp->size, fmt, buf);
293 #define HPP__COLOR_PRINT_FNS(_name) \
294 .header = hpp__header_ ## _name, \
295 .width = hpp__width_ ## _name, \
296 .color = hpp__color_ ## _name, \
297 .entry = hpp__entry_ ## _name
299 #define HPP__PRINT_FNS(_name) \
300 .header = hpp__header_ ## _name, \
301 .width = hpp__width_ ## _name, \
302 .entry = hpp__entry_ ## _name
304 struct perf_hpp_fmt perf_hpp__format[] = {
305 { .cond = false, HPP__COLOR_PRINT_FNS(baseline) },
306 { .cond = true, HPP__COLOR_PRINT_FNS(overhead) },
307 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_sys) },
308 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_us) },
309 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_sys) },
310 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_us) },
311 { .cond = false, HPP__PRINT_FNS(samples) },
312 { .cond = false, HPP__PRINT_FNS(period) },
313 { .cond = false, HPP__PRINT_FNS(delta) },
314 { .cond = false, HPP__PRINT_FNS(displ) }
317 #undef HPP__COLOR_PRINT_FNS
318 #undef HPP__PRINT_FNS
320 void perf_hpp__init(void)
322 if (symbol_conf.show_cpu_utilization) {
323 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].cond = true;
324 perf_hpp__format[PERF_HPP__OVERHEAD_US].cond = true;
327 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].cond = true;
328 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].cond = true;
332 if (symbol_conf.show_nr_samples)
333 perf_hpp__format[PERF_HPP__SAMPLES].cond = true;
335 if (symbol_conf.show_total_period)
336 perf_hpp__format[PERF_HPP__PERIOD].cond = true;
339 void perf_hpp__column_enable(unsigned col, bool enable)
341 BUG_ON(col >= PERF_HPP__MAX_INDEX);
342 perf_hpp__format[col].cond = enable;
345 static inline void advance_hpp(struct perf_hpp *hpp, int inc)
351 int hist_entry__period_snprintf(struct perf_hpp *hpp, struct hist_entry *he,
354 const char *sep = symbol_conf.field_sep;
355 char *start = hpp->buf;
359 if (symbol_conf.exclude_other && !he->parent)
362 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
363 if (!perf_hpp__format[i].cond)
366 if (!sep || !first) {
367 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
368 advance_hpp(hpp, ret);
372 if (color && perf_hpp__format[i].color)
373 ret = perf_hpp__format[i].color(hpp, he);
375 ret = perf_hpp__format[i].entry(hpp, he);
377 advance_hpp(hpp, ret);
380 return hpp->buf - start;
383 int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
386 const char *sep = symbol_conf.field_sep;
387 struct sort_entry *se;
390 list_for_each_entry(se, &hist_entry__sort_list, list) {
394 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
395 ret += se->se_snprintf(he, s + ret, size - ret,
396 hists__col_len(hists, se->se_width_idx));
403 * See hists__fprintf to match the column widths
405 unsigned int hists__sort_list_width(struct hists *hists)
407 struct sort_entry *se;
410 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
411 if (!perf_hpp__format[i].cond)
416 ret += perf_hpp__format[i].width(NULL);
419 list_for_each_entry(se, &hist_entry__sort_list, list)
421 ret += 2 + hists__col_len(hists, se->se_width_idx);
423 if (verbose) /* Addr + origin */
424 ret += 3 + BITS_PER_LONG / 4;