[PATCH v5 14/16] perf kvm: Add dimensions for percentages

Leo Yan
2023-03-15T14:54:00Z

Add dimensions for count and time percentages, it would be useful for
user to review percentage statistics.

Signed-off-by: Leo Yan <leo.yan@linaro.org>
Reviewed-by: James Clark <james.clark@arm.com>
---
tools/perf/builtin-kvm.c | 98 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 97 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
index 0d857df826cf..ab1cd6b5e528 100644
--- a/tools/perf/builtin-kvm.c
+++ b/tools/perf/builtin-kvm.c
@@ -222,10 +222,105 @@ static struct kvm_dimension dim_mean_time = {
.width = 14,
};

+#define PERC_STR(__s, __v) \
+({ \
+ scnprintf(__s, sizeof(__s), "%.2F%%", __v); \
+ __s; \
+})
+
+static double percent(u64 st, u64 tot)
+{
+ return tot ? 100. * (double) st / (double) tot : 0;
+}
+
+#define EV_METRIC_PERCENT(metric) \
+static int ev_percent_##metric(struct hist_entry *he) \
+{ \
+ struct kvm_event *event; \
+ struct perf_kvm_stat *perf_kvm; \
+ \
+ event = container_of(he, struct kvm_event, he); \
+ perf_kvm = event->perf_kvm; \
+ \
+ return percent(get_event_##metric(event, perf_kvm->trace_vcpu), \
+ perf_kvm->total_##metric); \
+}
+
+EV_METRIC_PERCENT(time)
+EV_METRIC_PERCENT(count)
+
+static int ev_entry_time_precent(struct perf_hpp_fmt *fmt,
+ struct perf_hpp *hpp,
+ struct hist_entry *he)
+{
+ int width = fmt_width(fmt, hpp, he->hists);
+ double per;
+ char buf[10];
+
+ per = ev_percent_time(he);
+ return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
+}
+
+static int64_t
+ev_cmp_time_precent(struct perf_hpp_fmt *fmt __maybe_unused,
+ struct hist_entry *left, struct hist_entry *right)
+{
+ double per_left;
+ double per_right;
+
+ per_left = ev_percent_time(left);
+ per_right = ev_percent_time(right);
+
+ return per_left - per_right;
+}
+
+static struct kvm_dimension dim_time_percent = {
+ .header = "Time%",
+ .name = "percent_time",
+ .cmp = ev_cmp_time_precent,
+ .entry = ev_entry_time_precent,
+ .width = 12,
+};
+
+static int ev_entry_count_precent(struct perf_hpp_fmt *fmt,
+ struct perf_hpp *hpp,
+ struct hist_entry *he)
+{
+ int width = fmt_width(fmt, hpp, he->hists);
+ double per;
+ char buf[10];
+
+ per = ev_percent_count(he);
+ return scnprintf(hpp->buf, hpp->size, "%*s", width, PERC_STR(buf, per));
+}
+
+static int64_t
+ev_cmp_count_precent(struct perf_hpp_fmt *fmt __maybe_unused,
+ struct hist_entry *left, struct hist_entry *right)
+{
+ double per_left;
+ double per_right;
+
+ per_left = ev_percent_count(left);
+ per_right = ev_percent_count(right);
+
+ return per_left - per_right;
+}
+
+static struct kvm_dimension dim_count_percent = {
+ .header = "Sample%",
+ .name = "percent_sample",
+ .cmp = ev_cmp_count_precent,
+ .entry = ev_entry_count_precent,
+ .width = 12,
+};
+
static struct kvm_dimension *dimensions[] = {
&dim_event,
&dim_time,
+ &dim_time_percent,
&dim_count,
+ &dim_count_percent,
&dim_max_time,
&dim_min_time,
&dim_mean_time,
@@ -875,7 +970,8 @@ static int filter_cb(struct hist_entry *he, void *arg __maybe_unused)

static void sort_result(struct perf_kvm_stat *kvm)
{
- const char *output_columns = "ev_name,sample,time,max_t,min_t,mean_t";
+ const char *output_columns = "ev_name,sample,percent_sample,"
+ "time,percent_time,max_t,min_t,mean_t";

kvm_hists__reinit(output_columns, kvm->sort_key);
hists__collapse_resort(&kvm_hists.hists, NULL);
--
2.34.1