Commit cc2621ce authored by Namhyung Kim's avatar Namhyung Kim

perf hist: Factor out __hpp__fmt_print()

Split the logic to print the histogram values according to the format
string.  This was used in 3 different places so it's better to move out
the logic into a function.
Tested-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240607202918.2357459-2-namhyung@kernel.org
parent 231295a1
...@@ -23,35 +23,42 @@ ...@@ -23,35 +23,42 @@
__ret; \ __ret; \
}) })
static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, static int __hpp__fmt_print(struct perf_hpp *hpp, struct hists *hists, u64 val,
hpp_field_fn get_field, const char *fmt, int len, int nr_samples, const char *fmt, int len,
hpp_snprint_fn print_fn, enum perf_hpp_fmt_type fmtype) hpp_snprint_fn print_fn, enum perf_hpp_fmt_type fmtype)
{ {
int ret;
struct hists *hists = he->hists;
struct evsel *evsel = hists_to_evsel(hists);
char *buf = hpp->buf;
size_t size = hpp->size;
if (fmtype == PERF_HPP_FMT_TYPE__PERCENT) { if (fmtype == PERF_HPP_FMT_TYPE__PERCENT) {
double percent = 0.0; double percent = 0.0;
u64 total = hists__total_period(hists); u64 total = hists__total_period(hists);
if (total) if (total)
percent = 100.0 * get_field(he) / total; percent = 100.0 * val / total;
ret = hpp__call_print_fn(hpp, print_fn, fmt, len, percent); return hpp__call_print_fn(hpp, print_fn, fmt, len, percent);
} else if (fmtype == PERF_HPP_FMT_TYPE__AVERAGE) { }
double average = 0;
if (he->stat.nr_events) if (fmtype == PERF_HPP_FMT_TYPE__AVERAGE) {
average = 1.0 * get_field(he) / he->stat.nr_events; double avg = nr_samples ? (1.0 * val / nr_samples) : 0;
ret = hpp__call_print_fn(hpp, print_fn, fmt, len, average); return hpp__call_print_fn(hpp, print_fn, fmt, len, avg);
} else {
ret = hpp__call_print_fn(hpp, print_fn, fmt, len, get_field(he));
} }
return hpp__call_print_fn(hpp, print_fn, fmt, len, val);
}
static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
hpp_field_fn get_field, const char *fmt, int len,
hpp_snprint_fn print_fn, enum perf_hpp_fmt_type fmtype)
{
int ret;
struct hists *hists = he->hists;
struct evsel *evsel = hists_to_evsel(hists);
char *buf = hpp->buf;
size_t size = hpp->size;
ret = __hpp__fmt_print(hpp, hists, get_field(he), he->stat.nr_events,
fmt, len, print_fn, fmtype);
if (evsel__is_group_event(evsel)) { if (evsel__is_group_event(evsel)) {
int prev_idx, idx_delta; int prev_idx, idx_delta;
struct hist_entry *pair; struct hist_entry *pair;
...@@ -72,30 +79,16 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, ...@@ -72,30 +79,16 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
while (idx_delta--) { while (idx_delta--) {
/* /*
* zero-fill group members in the middle which * zero-fill group members in the middle which have
* have no sample * no samples, pair->hists is not correct but it's
* fine since the value is 0.
*/ */
if (fmtype != PERF_HPP_FMT_TYPE__RAW) { ret += __hpp__fmt_print(hpp, pair->hists, 0, 0,
ret += hpp__call_print_fn(hpp, print_fn, fmt, len, print_fn, fmtype);
fmt, len, 0.0);
} else {
ret += hpp__call_print_fn(hpp, print_fn,
fmt, len, 0ULL);
}
} }
if (fmtype == PERF_HPP_FMT_TYPE__PERCENT) { ret += __hpp__fmt_print(hpp, pair->hists, period, nr_samples,
ret += hpp__call_print_fn(hpp, print_fn, fmt, len, fmt, len, print_fn, fmtype);
100.0 * period / total);
} else if (fmtype == PERF_HPP_FMT_TYPE__AVERAGE) {
double avg = nr_samples ? (period / nr_samples) : 0;
ret += hpp__call_print_fn(hpp, print_fn, fmt,
len, avg);
} else {
ret += hpp__call_print_fn(hpp, print_fn, fmt,
len, period);
}
prev_idx = evsel__group_idx(evsel); prev_idx = evsel__group_idx(evsel);
} }
...@@ -104,15 +97,11 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he, ...@@ -104,15 +97,11 @@ static int __hpp__fmt(struct perf_hpp *hpp, struct hist_entry *he,
while (idx_delta--) { while (idx_delta--) {
/* /*
* zero-fill group members at last which have no sample * zero-fill group members at last which have no sample.
* the hists is not correct but it's fine like above.
*/ */
if (fmtype != PERF_HPP_FMT_TYPE__RAW) { ret += __hpp__fmt_print(hpp, evsel__hists(evsel), 0, 0,
ret += hpp__call_print_fn(hpp, print_fn, fmt, len, print_fn, fmtype);
fmt, len, 0.0);
} else {
ret += hpp__call_print_fn(hpp, print_fn,
fmt, len, 0ULL);
}
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment