perf parse-events: Support PMUs for legacy cache events
authorIan Rogers <irogers@google.com>
Tue, 2 May 2023 22:38:30 +0000 (15:38 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 15 May 2023 12:12:13 +0000 (09:12 -0300)
Allow a legacy cache event to be both, for example,
"L1-dcache-load-miss" and "cpu/L1-dcache-load-miss/" by introducing a
new legacy cache term type.

The term type is processed in config_term_pmu, setting both the type in
perf_event_attr and the config.

The code to determine the config is factored out of
parse_events_add_cache and shared. If the PMU doesn't support legacy
events, currently just core/hybrid PMUs do, then the term is treated
like a PE_NAME term - as before.

If only terms are being parsed, such as for perf_pmu__new_alias, then
the PE_LEGACY_CACHE token is always parsed as PE_NAME.

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Kan Liang <kan.liang@linux.intel.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ahmad Yasin <ahmad.yasin@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Caleb Biggers <caleb.biggers@intel.com>
Cc: Edward Baker <edward.baker@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.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kang Minchul <tegongkang@gmail.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Perry Taylor <perry.taylor@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Rob Herring <robh@kernel.org>
Cc: Samantha Alt <samantha.alt@intel.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Sumanth Korikkar <sumanthk@linux.ibm.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Cc: Weilin Wang <weilin.wang@intel.com>
Cc: Xing Zhengjun <zhengjun.xing@linux.intel.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Link: https://lore.kernel.org/r/20230502223851.2234828-24-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/tests/parse-events.c
tools/perf/util/parse-events.c
tools/perf/util/parse-events.h
tools/perf/util/parse-events.l
tools/perf/util/parse-events.y
tools/perf/util/pmu.c
tools/perf/util/pmu.h

index 43c0778..c3afd0b 100644 (file)
@@ -1875,6 +1875,24 @@ static const struct evlist_test test__events_pmu[] = {
                .check = test__checkevent_raw_pmu,
                /* 5 */
        },
+       {
+               .name  = "cpu/L1-dcache-load-miss/",
+               .valid = test__pmu_cpu_valid,
+               .check = test__checkevent_genhw,
+               /* 6 */
+       },
+       {
+               .name  = "cpu/L1-dcache-load-miss/kp",
+               .valid = test__pmu_cpu_valid,
+               .check = test__checkevent_genhw_modifier,
+               /* 7 */
+       },
+       {
+               .name  = "cpu/L1-dcache-misses,name=cachepmu/",
+               .valid = test__pmu_cpu_valid,
+               .check = test__checkevent_config_cache,
+               /* 8 */
+       },
 };
 
 struct terms_test {
index b5d95fc..f692dd9 100644 (file)
@@ -404,33 +404,27 @@ static int config_attr(struct perf_event_attr *attr,
                       struct parse_events_error *err,
                       config_term_func_t config_term);
 
-int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
-                          struct parse_events_error *err,
-                          struct list_head *head_config,
-                          struct parse_events_state *parse_state)
+/**
+ * parse_events__decode_legacy_cache - Search name for the legacy cache event
+ *                                     name composed of 1, 2 or 3 hyphen
+ *                                     separated sections. The first section is
+ *                                     the cache type while the others are the
+ *                                     optional op and optional result. To make
+ *                                     life hard the names in the table also
+ *                                     contain hyphens and the longest name
+ *                                     should always be selected.
+ */
+static int parse_events__decode_legacy_cache(const char *name, int pmu_type, __u64 *config)
 {
-       struct perf_event_attr attr;
-       LIST_HEAD(config_terms);
-       const char *config_name, *metric_id;
-       int cache_type = -1, cache_op = -1, cache_result = -1;
-       int ret, len;
+       int len, cache_type = -1, cache_op = -1, cache_result = -1;
        const char *name_end = &name[strlen(name) + 1];
-       bool hybrid;
        const char *str = name;
 
-       /*
-        * Search str for the legacy cache event name composed of 1, 2 or 3
-        * hyphen separated sections. The first section is the cache type while
-        * the others are the optional op and optional result. To make life hard
-        * the names in the table also contain hyphens and the longest name
-        * should always be selected.
-        */
        cache_type = parse_aliases(str, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX, &len);
        if (cache_type == -1)
                return -EINVAL;
        str += len + 1;
 
-       config_name = get_config_name(head_config);
        if (str < name_end) {
                cache_op = parse_aliases(str, evsel__hw_cache_op,
                                        PERF_COUNT_HW_CACHE_OP_MAX, &len);
@@ -471,9 +465,28 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
        if (cache_result == -1)
                cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
 
+       *config = ((__u64)pmu_type << PERF_PMU_TYPE_SHIFT) |
+               cache_type | (cache_op << 8) | (cache_result << 16);
+       return 0;
+}
+
+int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
+                          struct parse_events_error *err,
+                          struct list_head *head_config,
+                          struct parse_events_state *parse_state)
+{
+       struct perf_event_attr attr;
+       LIST_HEAD(config_terms);
+       const char *config_name, *metric_id;
+       int ret;
+       bool hybrid;
+
+
        memset(&attr, 0, sizeof(attr));
-       attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
        attr.type = PERF_TYPE_HW_CACHE;
+       ret = parse_events__decode_legacy_cache(name, /*pmu_type=*/0, &attr.config);
+       if (ret)
+               return ret;
 
        if (head_config) {
                if (config_attr(&attr, head_config, err,
@@ -484,6 +497,7 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
                        return -ENOMEM;
        }
 
+       config_name = get_config_name(head_config);
        metric_id = get_config_metric_id(head_config);
        ret = parse_events__add_cache_hybrid(list, idx, &attr,
                                             config_name ? : name,
@@ -1022,6 +1036,7 @@ static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
        [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE]       = "aux-sample-size",
        [PARSE_EVENTS__TERM_TYPE_METRIC_ID]             = "metric-id",
        [PARSE_EVENTS__TERM_TYPE_RAW]                   = "raw",
+       [PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE]          = "legacy-cache",
 };
 
 static bool config_term_shrinked;
@@ -1199,15 +1214,25 @@ static int config_term_pmu(struct perf_event_attr *attr,
                           struct parse_events_term *term,
                           struct parse_events_error *err)
 {
+       if (term->type_term == PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE) {
+               const struct perf_pmu *pmu = perf_pmu__find_by_type(attr->type);
+
+               if (perf_pmu__supports_legacy_cache(pmu)) {
+                       attr->type = PERF_TYPE_HW_CACHE;
+                       return parse_events__decode_legacy_cache(term->config, pmu->type,
+                                                                &attr->config);
+               } else
+                       term->type_term = PARSE_EVENTS__TERM_TYPE_USER;
+       }
        if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
-           term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
+           term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) {
                /*
                 * Always succeed for sysfs terms, as we dont know
                 * at this point what type they need to have.
                 */
                return 0;
-       else
-               return config_term_common(attr, term, err);
+       }
+       return config_term_common(attr, term, err);
 }
 
 #ifdef HAVE_LIBTRACEEVENT
@@ -2147,6 +2172,7 @@ int __parse_events(struct evlist *evlist, const char *str,
                .evlist   = evlist,
                .stoken   = PE_START_EVENTS,
                .fake_pmu = fake_pmu,
+               .match_legacy_cache_terms = true,
        };
        int ret;
 
index f638542..5acb62c 100644 (file)
@@ -71,6 +71,7 @@ enum {
        PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE,
        PARSE_EVENTS__TERM_TYPE_METRIC_ID,
        PARSE_EVENTS__TERM_TYPE_RAW,
+       PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE,
        __PARSE_EVENTS__TERM_TYPE_NR,
 };
 
@@ -122,6 +123,8 @@ struct parse_events_state {
        int                        stoken;
        struct perf_pmu           *fake_pmu;
        char                      *hybrid_pmu_name;
+       /* Should PE_LEGACY_NAME tokens be generated for config terms? */
+       bool                       match_legacy_cache_terms;
        bool                       wild_card_pmus;
 };
 
index 4b35c09..abe0ce6 100644 (file)
@@ -63,6 +63,11 @@ static int str(yyscan_t scanner, int token)
        return token;
 }
 
+static int lc_str(yyscan_t scanner, const struct parse_events_state *state)
+{
+       return str(scanner, state->match_legacy_cache_terms ? PE_LEGACY_CACHE : PE_NAME);
+}
+
 static bool isbpf_suffix(char *text)
 {
        int len = strlen(text);
@@ -185,7 +190,6 @@ lc_op_result        (load|loads|read|store|stores|write|prefetch|prefetches|speculative
 
 %{
        struct parse_events_state *_parse_state = parse_events_get_extra(yyscanner);
-
        {
                int start_token = _parse_state->stoken;
 
@@ -269,6 +273,9 @@ r{num_raw_hex}              { return str(yyscanner, PE_RAW); }
 r0x{num_raw_hex}       { return str(yyscanner, PE_RAW); }
 ,                      { return ','; }
 "/"                    { BEGIN(INITIAL); return '/'; }
+{lc_type}                      { return lc_str(yyscanner, _parse_state); }
+{lc_type}-{lc_op_result}       { return lc_str(yyscanner, _parse_state); }
+{lc_type}-{lc_op_result}-{lc_op_result}        { return lc_str(yyscanner, _parse_state); }
 {name_minus}           { return str(yyscanner, PE_NAME); }
 \[all\]                        { return PE_ARRAY_ALL; }
 "["                    { BEGIN(array); return '['; }
index e7072b5..f84fa1b 100644 (file)
@@ -723,7 +723,7 @@ event_term
        $$ = head;
 }
 
-name_or_raw: PE_RAW | PE_NAME
+name_or_raw: PE_RAW | PE_NAME | PE_LEGACY_CACHE
 
 event_term:
 PE_RAW
@@ -775,6 +775,18 @@ name_or_raw '=' PE_VALUE_SYM_HW
        $$ = term;
 }
 |
+PE_LEGACY_CACHE
+{
+       struct parse_events_term *term;
+
+       if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE,
+                                       $1, 1, true, &@1, NULL)) {
+               free($1);
+               YYABORT;
+       }
+       $$ = term;
+}
+|
 PE_NAME
 {
        struct parse_events_term *term;
index cb33d86..63071d8 100644 (file)
@@ -1650,6 +1650,11 @@ bool is_pmu_core(const char *name)
        return !strcmp(name, "cpu") || is_arm_pmu_core(name);
 }
 
+bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu)
+{
+       return is_pmu_core(pmu->name) || perf_pmu__is_hybrid(pmu->name);
+}
+
 static bool pmu_alias_is_duplicate(struct sevent *alias_a,
                                   struct sevent *alias_b)
 {
index b9a02de..05702bc 100644 (file)
@@ -220,6 +220,7 @@ void perf_pmu__del_formats(struct list_head *formats);
 struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu);
 
 bool is_pmu_core(const char *name);
+bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu);
 void print_pmu_events(const struct print_callbacks *print_cb, void *print_state);
 bool pmu_have_event(const char *pname, const char *name);