summaryrefslogtreecommitdiffstats
path: root/tools/perf/util/parse-events.c
diff options
context:
space:
mode:
authorIan Rogers <irogers@google.com>2024-04-16 08:15:29 +0200
committerArnaldo Carvalho de Melo <acme@redhat.com>2024-04-27 03:07:20 +0200
commite30a7912f498c58062d0b75e59c181dd48f1cc56 (patch)
tree17d2b4b6e7226b1cfeaf3c466575bd32abae49c7 /tools/perf/util/parse-events.c
parentperf parse-events: Inline parse_events_evlist_error (diff)
downloadlinux-e30a7912f498c58062d0b75e59c181dd48f1cc56.tar.xz
linux-e30a7912f498c58062d0b75e59c181dd48f1cc56.zip
perf parse-events: Improvements to modifier parsing
Use a struct/bitmap rather than a copied string from lexer. In lexer give improved error message when too many precise flags are given or repeated modifiers. Before: $ perf stat -e 'cycles:kuk' true event syntax error: 'cycles:kuk' \___ Bad modifier ... $ perf stat -e 'cycles:pppp' true event syntax error: 'cycles:pppp' \___ Bad modifier ... $ perf stat -e '{instructions:p,cycles:pp}:pp' -a true event syntax error: '..cycles:pp}:pp' \___ Bad modifier ... After: $ perf stat -e 'cycles:kuk' true event syntax error: 'cycles:kuk' \___ Duplicate modifier 'k' (kernel) ... $ perf stat -e 'cycles:pppp' true event syntax error: 'cycles:pppp' \___ Maximum precise value is 3 ... $ perf stat -e '{instructions:p,cycles:pp}:pp' true event syntax error: '..cycles:pp}:pp' \___ Maximum combined precise value is 3, adding precision to "cycles:pp" ... Signed-off-by: Ian Rogers <irogers@google.com> Reviewed-by: Kan Liang <kan.liang@linux.intel.com> Tested-by: Atish Patra <atishp@rivosinc.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Beeman Strong <beeman@rivosinc.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: James Clark <james.clark@arm.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: https://lore.kernel.org/r/20240416061533.921723-14-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/parse-events.c')
-rw-r--r--tools/perf/util/parse-events.c250
1 files changed, 93 insertions, 157 deletions
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index ebada37ef98a..3ab533d0e653 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -1700,12 +1700,6 @@ int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state
return -EINVAL;
}
-int parse_events__modifier_group(struct list_head *list,
- char *event_mod)
-{
- return parse_events__modifier_event(list, event_mod, true);
-}
-
void parse_events__set_leader(char *name, struct list_head *list)
{
struct evsel *leader;
@@ -1720,183 +1714,125 @@ void parse_events__set_leader(char *name, struct list_head *list)
leader->group_name = name;
}
-struct event_modifier {
- int eu;
- int ek;
- int eh;
- int eH;
- int eG;
- int eI;
- int precise;
- int precise_max;
- int exclude_GH;
- int sample_read;
- int pinned;
- int weak;
- int exclusive;
- int bpf_counter;
-};
+static int parse_events__modifier_list(struct parse_events_state *parse_state,
+ YYLTYPE *loc,
+ struct list_head *list,
+ struct parse_events_modifier mod,
+ bool group)
+{
+ struct evsel *evsel;
-static int get_event_modifier(struct event_modifier *mod, char *str,
- struct evsel *evsel)
-{
- int eu = evsel ? evsel->core.attr.exclude_user : 0;
- int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
- int eh = evsel ? evsel->core.attr.exclude_hv : 0;
- int eH = evsel ? evsel->core.attr.exclude_host : 0;
- int eG = evsel ? evsel->core.attr.exclude_guest : 0;
- int eI = evsel ? evsel->core.attr.exclude_idle : 0;
- int precise = evsel ? evsel->core.attr.precise_ip : 0;
- int precise_max = 0;
- int sample_read = 0;
- int pinned = evsel ? evsel->core.attr.pinned : 0;
- int exclusive = evsel ? evsel->core.attr.exclusive : 0;
-
- int exclude = eu | ek | eh;
- int exclude_GH = evsel ? evsel->exclude_GH : 0;
- int weak = 0;
- int bpf_counter = 0;
-
- memset(mod, 0, sizeof(*mod));
-
- while (*str) {
- if (*str == 'u') {
+ if (!group && mod.weak) {
+ parse_events_error__handle(parse_state->error, loc->first_column,
+ strdup("Weak modifier is for use with groups"), NULL);
+ return -EINVAL;
+ }
+
+ __evlist__for_each_entry(list, evsel) {
+ /* Translate modifiers into the equivalent evsel excludes. */
+ int eu = group ? evsel->core.attr.exclude_user : 0;
+ int ek = group ? evsel->core.attr.exclude_kernel : 0;
+ int eh = group ? evsel->core.attr.exclude_hv : 0;
+ int eH = group ? evsel->core.attr.exclude_host : 0;
+ int eG = group ? evsel->core.attr.exclude_guest : 0;
+ int exclude = eu | ek | eh;
+ int exclude_GH = group ? evsel->exclude_GH : 0;
+
+ if (mod.precise) {
+ /* use of precise requires exclude_guest */
+ eG = 1;
+ }
+ if (mod.user) {
if (!exclude)
exclude = eu = ek = eh = 1;
if (!exclude_GH && !perf_guest)
eG = 1;
eu = 0;
- } else if (*str == 'k') {
+ }
+ if (mod.kernel) {
if (!exclude)
exclude = eu = ek = eh = 1;
ek = 0;
- } else if (*str == 'h') {
+ }
+ if (mod.hypervisor) {
if (!exclude)
exclude = eu = ek = eh = 1;
eh = 0;
- } else if (*str == 'G') {
+ }
+ if (mod.guest) {
if (!exclude_GH)
exclude_GH = eG = eH = 1;
eG = 0;
- } else if (*str == 'H') {
+ }
+ if (mod.host) {
if (!exclude_GH)
exclude_GH = eG = eH = 1;
eH = 0;
- } else if (*str == 'I') {
- eI = 1;
- } else if (*str == 'p') {
- precise++;
- /* use of precise requires exclude_guest */
- if (!exclude_GH)
- eG = 1;
- } else if (*str == 'P') {
- precise_max = 1;
- } else if (*str == 'S') {
- sample_read = 1;
- } else if (*str == 'D') {
- pinned = 1;
- } else if (*str == 'e') {
- exclusive = 1;
- } else if (*str == 'W') {
- weak = 1;
- } else if (*str == 'b') {
- bpf_counter = 1;
- } else
- break;
-
- ++str;
+ }
+ evsel->core.attr.exclude_user = eu;
+ evsel->core.attr.exclude_kernel = ek;
+ evsel->core.attr.exclude_hv = eh;
+ evsel->core.attr.exclude_host = eH;
+ evsel->core.attr.exclude_guest = eG;
+ evsel->exclude_GH = exclude_GH;
+
+ /* Simple modifiers copied to the evsel. */
+ if (mod.precise) {
+ u8 precise = evsel->core.attr.precise_ip + mod.precise;
+ /*
+ * precise ip:
+ *
+ * 0 - SAMPLE_IP can have arbitrary skid
+ * 1 - SAMPLE_IP must have constant skid
+ * 2 - SAMPLE_IP requested to have 0 skid
+ * 3 - SAMPLE_IP must have 0 skid
+ *
+ * See also PERF_RECORD_MISC_EXACT_IP
+ */
+ if (precise > 3) {
+ char *help;
+
+ if (asprintf(&help,
+ "Maximum combined precise value is 3, adding precision to \"%s\"",
+ evsel__name(evsel)) > 0) {
+ parse_events_error__handle(parse_state->error,
+ loc->first_column,
+ help, NULL);
+ }
+ return -EINVAL;
+ }
+ evsel->core.attr.precise_ip = precise;
+ }
+ if (mod.precise_max)
+ evsel->precise_max = 1;
+ if (mod.non_idle)
+ evsel->core.attr.exclude_idle = 1;
+ if (mod.sample_read)
+ evsel->sample_read = 1;
+ if (mod.pinned && evsel__is_group_leader(evsel))
+ evsel->core.attr.pinned = 1;
+ if (mod.exclusive && evsel__is_group_leader(evsel))
+ evsel->core.attr.exclusive = 1;
+ if (mod.weak)
+ evsel->weak_group = true;
+ if (mod.bpf)
+ evsel->bpf_counter = true;
}
-
- /*
- * precise ip:
- *
- * 0 - SAMPLE_IP can have arbitrary skid
- * 1 - SAMPLE_IP must have constant skid
- * 2 - SAMPLE_IP requested to have 0 skid
- * 3 - SAMPLE_IP must have 0 skid
- *
- * See also PERF_RECORD_MISC_EXACT_IP
- */
- if (precise > 3)
- return -EINVAL;
-
- mod->eu = eu;
- mod->ek = ek;
- mod->eh = eh;
- mod->eH = eH;
- mod->eG = eG;
- mod->eI = eI;
- mod->precise = precise;
- mod->precise_max = precise_max;
- mod->exclude_GH = exclude_GH;
- mod->sample_read = sample_read;
- mod->pinned = pinned;
- mod->weak = weak;
- mod->bpf_counter = bpf_counter;
- mod->exclusive = exclusive;
-
return 0;
}
-/*
- * Basic modifier sanity check to validate it contains only one
- * instance of any modifier (apart from 'p') present.
- */
-static int check_modifier(char *str)
+int parse_events__modifier_group(struct parse_events_state *parse_state, void *loc,
+ struct list_head *list,
+ struct parse_events_modifier mod)
{
- char *p = str;
-
- /* The sizeof includes 0 byte as well. */
- if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1))
- return -1;
-
- while (*p) {
- if (*p != 'p' && strchr(p + 1, *p))
- return -1;
- p++;
- }
-
- return 0;
+ return parse_events__modifier_list(parse_state, loc, list, mod, /*group=*/true);
}
-int parse_events__modifier_event(struct list_head *list, char *str, bool add)
+int parse_events__modifier_event(struct parse_events_state *parse_state, void *loc,
+ struct list_head *list,
+ struct parse_events_modifier mod)
{
- struct evsel *evsel;
- struct event_modifier mod;
-
- if (str == NULL)
- return 0;
-
- if (check_modifier(str))
- return -EINVAL;
-
- if (!add && get_event_modifier(&mod, str, NULL))
- return -EINVAL;
-
- __evlist__for_each_entry(list, evsel) {
- if (add && get_event_modifier(&mod, str, evsel))
- return -EINVAL;
-
- evsel->core.attr.exclude_user = mod.eu;
- evsel->core.attr.exclude_kernel = mod.ek;
- evsel->core.attr.exclude_hv = mod.eh;
- evsel->core.attr.precise_ip = mod.precise;
- evsel->core.attr.exclude_host = mod.eH;
- evsel->core.attr.exclude_guest = mod.eG;
- evsel->core.attr.exclude_idle = mod.eI;
- evsel->exclude_GH = mod.exclude_GH;
- evsel->sample_read = mod.sample_read;
- evsel->precise_max = mod.precise_max;
- evsel->weak_group = mod.weak;
- evsel->bpf_counter = mod.bpf_counter;
-
- if (evsel__is_group_leader(evsel)) {
- evsel->core.attr.pinned = mod.pinned;
- evsel->core.attr.exclusive = mod.exclusive;
- }
- }
-
- return 0;
+ return parse_events__modifier_list(parse_state, loc, list, mod, /*group=*/false);
}
int parse_events_name(struct list_head *list, const char *name)