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 const char *fmt = hpp->ptr ? "Baseline" : "Overhead";
13 return scnprintf(hpp->buf, hpp->size, fmt);
16 static int hpp__width_overhead(struct perf_hpp *hpp __maybe_unused)
21 static int hpp__color_overhead(struct perf_hpp *hpp, struct hist_entry *he)
23 double percent = 100.0 * he->period / hpp->total_period;
26 struct hists *old_hists = hpp->ptr;
27 u64 total_period = old_hists->stats.total_period;
28 u64 base_period = he->pair ? he->pair->period : 0;
31 percent = 100.0 * base_period / total_period;
36 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%%", percent);
39 static int hpp__entry_overhead(struct perf_hpp *hpp, struct hist_entry *he)
41 double percent = 100.0 * he->period / hpp->total_period;
42 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%%";
45 struct hists *old_hists = hpp->ptr;
46 u64 total_period = old_hists->stats.total_period;
47 u64 base_period = he->pair ? he->pair->period : 0;
50 percent = 100.0 * base_period / total_period;
55 return scnprintf(hpp->buf, hpp->size, fmt, percent);
58 static int hpp__header_overhead_sys(struct perf_hpp *hpp)
60 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
62 return scnprintf(hpp->buf, hpp->size, fmt, "sys");
65 static int hpp__width_overhead_sys(struct perf_hpp *hpp __maybe_unused)
70 static int hpp__color_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
72 double percent = 100.0 * he->period_sys / hpp->total_period;
73 return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
76 static int hpp__entry_overhead_sys(struct perf_hpp *hpp, struct hist_entry *he)
78 double percent = 100.0 * he->period_sys / hpp->total_period;
79 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
81 return scnprintf(hpp->buf, hpp->size, fmt, percent);
84 static int hpp__header_overhead_us(struct perf_hpp *hpp)
86 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
88 return scnprintf(hpp->buf, hpp->size, fmt, "user");
91 static int hpp__width_overhead_us(struct perf_hpp *hpp __maybe_unused)
96 static int hpp__color_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
98 double percent = 100.0 * he->period_us / hpp->total_period;
99 return percent_color_snprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
102 static int hpp__entry_overhead_us(struct perf_hpp *hpp, struct hist_entry *he)
104 double percent = 100.0 * he->period_us / hpp->total_period;
105 const char *fmt = symbol_conf.field_sep ? "%.2f" : "%6.2f%%";
107 return scnprintf(hpp->buf, hpp->size, fmt, percent);
110 static int hpp__header_overhead_guest_sys(struct perf_hpp *hpp)
112 return scnprintf(hpp->buf, hpp->size, "guest sys");
115 static int hpp__width_overhead_guest_sys(struct perf_hpp *hpp __maybe_unused)
120 static int hpp__color_overhead_guest_sys(struct perf_hpp *hpp,
121 struct hist_entry *he)
123 double percent = 100.0 * he->period_guest_sys / hpp->total_period;
124 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
127 static int hpp__entry_overhead_guest_sys(struct perf_hpp *hpp,
128 struct hist_entry *he)
130 double percent = 100.0 * he->period_guest_sys / hpp->total_period;
131 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
133 return scnprintf(hpp->buf, hpp->size, fmt, percent);
136 static int hpp__header_overhead_guest_us(struct perf_hpp *hpp)
138 return scnprintf(hpp->buf, hpp->size, "guest usr");
141 static int hpp__width_overhead_guest_us(struct perf_hpp *hpp __maybe_unused)
146 static int hpp__color_overhead_guest_us(struct perf_hpp *hpp,
147 struct hist_entry *he)
149 double percent = 100.0 * he->period_guest_us / hpp->total_period;
150 return percent_color_snprintf(hpp->buf, hpp->size, " %6.2f%% ", percent);
153 static int hpp__entry_overhead_guest_us(struct perf_hpp *hpp,
154 struct hist_entry *he)
156 double percent = 100.0 * he->period_guest_us / hpp->total_period;
157 const char *fmt = symbol_conf.field_sep ? "%.2f" : " %6.2f%% ";
159 return scnprintf(hpp->buf, hpp->size, fmt, percent);
162 static int hpp__header_samples(struct perf_hpp *hpp)
164 const char *fmt = symbol_conf.field_sep ? "%s" : "%11s";
166 return scnprintf(hpp->buf, hpp->size, fmt, "Samples");
169 static int hpp__width_samples(struct perf_hpp *hpp __maybe_unused)
174 static int hpp__entry_samples(struct perf_hpp *hpp, struct hist_entry *he)
176 const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%11" PRIu64;
178 return scnprintf(hpp->buf, hpp->size, fmt, he->nr_events);
181 static int hpp__header_period(struct perf_hpp *hpp)
183 const char *fmt = symbol_conf.field_sep ? "%s" : "%12s";
185 return scnprintf(hpp->buf, hpp->size, fmt, "Period");
188 static int hpp__width_period(struct perf_hpp *hpp __maybe_unused)
193 static int hpp__entry_period(struct perf_hpp *hpp, struct hist_entry *he)
195 const char *fmt = symbol_conf.field_sep ? "%" PRIu64 : "%12" PRIu64;
197 return scnprintf(hpp->buf, hpp->size, fmt, he->period);
200 static int hpp__header_delta(struct perf_hpp *hpp)
202 const char *fmt = symbol_conf.field_sep ? "%s" : "%7s";
204 return scnprintf(hpp->buf, hpp->size, fmt, "Delta");
207 static int hpp__width_delta(struct perf_hpp *hpp __maybe_unused)
212 static int hpp__entry_delta(struct perf_hpp *hpp, struct hist_entry *he)
214 struct hists *pair_hists = hpp->ptr;
215 u64 old_total, new_total;
216 double old_percent = 0, new_percent = 0;
218 const char *fmt = symbol_conf.field_sep ? "%s" : "%7.7s";
221 old_total = pair_hists->stats.total_period;
222 if (old_total > 0 && he->pair)
223 old_percent = 100.0 * he->pair->period / old_total;
225 new_total = hpp->total_period;
227 new_percent = 100.0 * he->period / new_total;
229 diff = new_percent - old_percent;
230 if (fabs(diff) >= 0.01)
231 scnprintf(buf, sizeof(buf), "%+4.2F%%", diff);
233 return scnprintf(hpp->buf, hpp->size, fmt, buf);
236 static int hpp__header_displ(struct perf_hpp *hpp)
238 return scnprintf(hpp->buf, hpp->size, "Displ.");
241 static int hpp__width_displ(struct perf_hpp *hpp __maybe_unused)
246 static int hpp__entry_displ(struct perf_hpp *hpp,
247 struct hist_entry *he __maybe_unused)
249 const char *fmt = symbol_conf.field_sep ? "%s" : "%6.6s";
252 if (hpp->displacement)
253 scnprintf(buf, sizeof(buf), "%+4ld", hpp->displacement);
255 return scnprintf(hpp->buf, hpp->size, fmt, buf);
258 #define HPP__COLOR_PRINT_FNS(_name) \
259 .header = hpp__header_ ## _name, \
260 .width = hpp__width_ ## _name, \
261 .color = hpp__color_ ## _name, \
262 .entry = hpp__entry_ ## _name
264 #define HPP__PRINT_FNS(_name) \
265 .header = hpp__header_ ## _name, \
266 .width = hpp__width_ ## _name, \
267 .entry = hpp__entry_ ## _name
269 struct perf_hpp_fmt perf_hpp__format[] = {
270 { .cond = true, HPP__COLOR_PRINT_FNS(overhead) },
271 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_sys) },
272 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_us) },
273 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_sys) },
274 { .cond = false, HPP__COLOR_PRINT_FNS(overhead_guest_us) },
275 { .cond = false, HPP__PRINT_FNS(samples) },
276 { .cond = false, HPP__PRINT_FNS(period) },
277 { .cond = false, HPP__PRINT_FNS(delta) },
278 { .cond = false, HPP__PRINT_FNS(displ) }
281 #undef HPP__COLOR_PRINT_FNS
282 #undef HPP__PRINT_FNS
284 void perf_hpp__init(bool need_pair, bool show_displacement)
286 if (symbol_conf.show_cpu_utilization) {
287 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].cond = true;
288 perf_hpp__format[PERF_HPP__OVERHEAD_US].cond = true;
291 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].cond = true;
292 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].cond = true;
296 if (symbol_conf.show_nr_samples)
297 perf_hpp__format[PERF_HPP__SAMPLES].cond = true;
299 if (symbol_conf.show_total_period)
300 perf_hpp__format[PERF_HPP__PERIOD].cond = true;
303 perf_hpp__format[PERF_HPP__DELTA].cond = true;
305 if (show_displacement)
306 perf_hpp__format[PERF_HPP__DISPL].cond = true;
310 static inline void advance_hpp(struct perf_hpp *hpp, int inc)
316 int hist_entry__period_snprintf(struct perf_hpp *hpp, struct hist_entry *he,
319 const char *sep = symbol_conf.field_sep;
320 char *start = hpp->buf;
323 if (symbol_conf.exclude_other && !he->parent)
326 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
327 if (!perf_hpp__format[i].cond)
331 ret = scnprintf(hpp->buf, hpp->size, "%s", sep ?: " ");
332 advance_hpp(hpp, ret);
335 if (color && perf_hpp__format[i].color)
336 ret = perf_hpp__format[i].color(hpp, he);
338 ret = perf_hpp__format[i].entry(hpp, he);
340 advance_hpp(hpp, ret);
343 return hpp->buf - start;
346 int hist_entry__sort_snprintf(struct hist_entry *he, char *s, size_t size,
349 const char *sep = symbol_conf.field_sep;
350 struct sort_entry *se;
353 list_for_each_entry(se, &hist_entry__sort_list, list) {
357 ret += scnprintf(s + ret, size - ret, "%s", sep ?: " ");
358 ret += se->se_snprintf(he, s + ret, size - ret,
359 hists__col_len(hists, se->se_width_idx));
366 * See hists__fprintf to match the column widths
368 unsigned int hists__sort_list_width(struct hists *hists)
370 struct sort_entry *se;
373 for (i = 0; i < PERF_HPP__MAX_INDEX; i++) {
374 if (!perf_hpp__format[i].cond)
379 ret += perf_hpp__format[i].width(NULL);
382 list_for_each_entry(se, &hist_entry__sort_list, list)
384 ret += 2 + hists__col_len(hists, se->se_width_idx);
386 if (verbose) /* Addr + origin */
387 ret += 3 + BITS_PER_LONG / 4;