Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf kvm: Polish sorting key

Since histograms supports sorting, the tool doesn't need to maintain the
mapping between the sorting keys and the corresponding comparison
callbacks, therefore, this patch removes structure kvm_event_key.

But we still need to validate the sorting key, this patch uses an array
for sorting keys and renames function select_key() to is_valid_key()
to validate the sorting key passed by user.

Signed-off-by: Leo Yan <leo.yan@linaro.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: linux-arm-kernel@lists.infradead.org
Link: https://lore.kernel.org/r/20230315145112.186603-2-leo.yan@linaro.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Leo Yan and committed by
Arnaldo Carvalho de Melo
c695d48a f57a6414

+10 -27
+10 -19
tools/perf/builtin-kvm.c
··· 77 77 COMPARE_EVENT_KEY(count, stats.n); 78 78 COMPARE_EVENT_KEY(mean, stats.mean); 79 79 80 - #define DEF_SORT_NAME_KEY(name, compare_key) \ 81 - { #name, cmp_event_ ## compare_key } 82 - 83 - static struct kvm_event_key keys[] = { 84 - DEF_SORT_NAME_KEY(sample, count), 85 - DEF_SORT_NAME_KEY(time, mean), 86 - { NULL, NULL } 87 - }; 88 - 89 80 struct kvm_hists { 90 81 struct hists hists; 91 82 struct perf_hpp_list list; ··· 750 759 return true; 751 760 } 752 761 753 - static bool select_key(struct perf_kvm_stat *kvm) 762 + static bool is_valid_key(struct perf_kvm_stat *kvm) 754 763 { 755 - int i; 764 + static const char *key_array[] = { 765 + "ev_name", "sample", "time", "max_t", "min_t", "mean_t", 766 + }; 767 + unsigned int i; 756 768 757 - for (i = 0; keys[i].name; i++) { 758 - if (!strcmp(keys[i].name, kvm->sort_key)) { 759 - kvm->compare = keys[i].key; 769 + for (i = 0; i < ARRAY_SIZE(key_array); i++) 770 + if (!strcmp(key_array[i], kvm->sort_key)) 760 771 return true; 761 - } 762 - } 763 772 764 - pr_err("Unknown compare key:%s\n", kvm->sort_key); 773 + pr_err("Unsupported sort key: %s\n", kvm->sort_key); 765 774 return false; 766 775 } 767 776 ··· 1191 1200 return ret; 1192 1201 1193 1202 if (!verify_vcpu(kvm->trace_vcpu) || 1194 - !select_key(kvm) || 1203 + !is_valid_key(kvm) || 1195 1204 !register_kvm_events_ops(kvm)) { 1196 1205 goto out; 1197 1206 } ··· 1386 1395 if (!verify_vcpu(vcpu)) 1387 1396 goto exit; 1388 1397 1389 - if (!select_key(kvm)) 1398 + if (!is_valid_key(kvm)) 1390 1399 goto exit; 1391 1400 1392 1401 if (!register_kvm_events_ops(kvm))
-8
tools/perf/util/kvm-stat.h
··· 49 49 struct hist_entry he; 50 50 }; 51 51 52 - typedef int64_t (*key_cmp_fun)(struct kvm_event*, struct kvm_event*, int); 53 - 54 - struct kvm_event_key { 55 - const char *name; 56 - key_cmp_fun key; 57 - }; 58 - 59 52 struct child_event_ops { 60 53 void (*get_key)(struct evsel *evsel, 61 54 struct perf_sample *sample, ··· 91 98 const char *exit_reasons_isa; 92 99 93 100 struct kvm_events_ops *events_ops; 94 - key_cmp_fun compare; 95 101 96 102 u64 total_time; 97 103 u64 total_count;