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

perf evsel: Add tool event helpers

Convert to and from a string. Fix evsel__tool_name() as array is
off-by-1. Support more than just duration_time as a metric-id.

Fixes: 75eafc970bd9d36d ("perf list: Print all available tool events")
Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Florian Fischer <florian.fischer@muhq.space>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.garry@huawei.com>
Cc: Kim Phillips <kim.phillips@amd.com>
Cc: Madhavan Srinivasan <maddy@linux.ibm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Riccardo Mancini <rickyman7@gmail.com>
Cc: Shunsuke Nakamura <nakamura.shun@fujitsu.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Xing Zhengjun <zhengjun.xing@linux.intel.com>
Link: https://lore.kernel.org/r/20220507053410.3798748-4-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
79932d16 545a96c9

+42 -10
+31 -10
tools/perf/util/evsel.c
··· 59 59 60 60 static clockid_t clockid; 61 61 62 + static const char *const perf_tool_event__tool_names[PERF_TOOL_MAX] = { 63 + NULL, 64 + "duration_time", 65 + "user_time", 66 + "system_time", 67 + }; 68 + 69 + const char *perf_tool_event__to_str(enum perf_tool_event ev) 70 + { 71 + if (ev > PERF_TOOL_NONE && ev < PERF_TOOL_MAX) 72 + return perf_tool_event__tool_names[ev]; 73 + 74 + return NULL; 75 + } 76 + 77 + enum perf_tool_event perf_tool_event__from_str(const char *str) 78 + { 79 + int i; 80 + 81 + perf_tool_event__for_each_event(i) { 82 + if (!strcmp(str, perf_tool_event__tool_names[i])) 83 + return i; 84 + } 85 + return PERF_TOOL_NONE; 86 + } 87 + 88 + 62 89 static int evsel__no_extra_init(struct evsel *evsel __maybe_unused) 63 90 { 64 91 return 0; ··· 624 597 return r + evsel__add_modifiers(evsel, bf + r, size - r); 625 598 } 626 599 627 - static const char *const evsel__tool_names[PERF_TOOL_MAX] = { 628 - "duration_time", 629 - "user_time", 630 - "system_time", 631 - }; 632 - 633 600 static int evsel__tool_name(enum perf_tool_event ev, char *bf, size_t size) 634 601 { 635 - return scnprintf(bf, size, "%s", evsel__tool_names[ev]); 602 + return scnprintf(bf, size, "%s", perf_tool_event__to_str(ev)); 636 603 } 637 604 638 605 static int __evsel__bp_name(char *bf, size_t size, u64 addr, u64 type) ··· 779 758 break; 780 759 781 760 case PERF_TYPE_SOFTWARE: 782 - if (evsel->tool_event) 761 + if (evsel__is_tool(evsel)) 783 762 evsel__tool_name(evsel->tool_event, bf, sizeof(bf)); 784 763 else 785 764 evsel__sw_name(evsel, bf, sizeof(bf)); ··· 812 791 if (evsel->metric_id) 813 792 return evsel->metric_id; 814 793 815 - if (evsel->core.attr.type == PERF_TYPE_SOFTWARE && evsel->tool_event) 816 - return "duration_time"; 794 + if (evsel__is_tool(evsel)) 795 + return perf_tool_event__to_str(evsel->tool_event); 817 796 818 797 return "unknown"; 819 798 }
+11
tools/perf/util/evsel.h
··· 36 36 PERF_TOOL_MAX, 37 37 }; 38 38 39 + const char *perf_tool_event__to_str(enum perf_tool_event ev); 40 + enum perf_tool_event perf_tool_event__from_str(const char *str); 41 + 42 + #define perf_tool_event__for_each_event(ev) \ 43 + for ((ev) = PERF_TOOL_DURATION_TIME; (ev) < PERF_TOOL_MAX; ev++) 44 + 39 45 /** struct evsel - event selector 40 46 * 41 47 * @evlist - evlist this evsel is in, if it is in one. ··· 274 268 int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size); 275 269 const char *evsel__name(struct evsel *evsel); 276 270 const char *evsel__metric_id(const struct evsel *evsel); 271 + 272 + static inline bool evsel__is_tool(const struct evsel *evsel) 273 + { 274 + return evsel->tool_event != PERF_TOOL_NONE; 275 + } 277 276 278 277 const char *evsel__group_name(struct evsel *evsel); 279 278 int evsel__group_desc(struct evsel *evsel, char *buf, size_t size);