perf pmu-events: Reduce processed events by passing PMU
authorIan Rogers <irogers@google.com>
Thu, 24 Aug 2023 04:13:19 +0000 (21:13 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 24 Aug 2023 14:00:09 +0000 (11:00 -0300)
Pass the PMU to pmu_events_table__for_each_event so that entries that
don't match don't need to be processed by callback.

If a NULL PMU is passed then all PMUs are processed.

'perf bench internals pmu-scan's "Average PMU scanning" performance is
reduced by about 5% on an Intel tigerlake.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Gaosheng Cui <cuigaosheng1@huawei.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jing Zhang <renyu.zj@linux.alibaba.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Rob Herring <robh@kernel.org>
Link: https://lore.kernel.org/r/20230824041330.266337-8-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/pmu-events/empty-pmu-events.c
tools/perf/pmu-events/jevents.py
tools/perf/pmu-events/pmu-events.h
tools/perf/tests/pmu-events.c
tools/perf/util/pmu.c
tools/perf/util/pmu.h

index 807f2e5..2d6f748 100644 (file)
@@ -266,12 +266,16 @@ static const struct pmu_sys_events pmu_sys_event_tables[] = {
        },
 };
 
-int pmu_events_table__for_each_event(const struct pmu_events_table *table, pmu_event_iter_fn fn,
-                                   void *data)
+int pmu_events_table__for_each_event(const struct pmu_events_table *table, struct perf_pmu *pmu,
+                                    pmu_event_iter_fn fn, void *data)
 {
        for (const struct pmu_event *pe = &table->entries[0]; pe->name; pe++) {
-               int ret = fn(pe, table, data);
+               int ret;
 
+                if (pmu && !pmu__name_match(pmu, pe->pmu))
+                        continue;
+
+               ret = fn(pe, table, data);
                if (ret)
                        return ret;
        }
@@ -371,7 +375,8 @@ const struct pmu_metrics_table *find_core_metrics_table(const char *arch, const
 int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data)
 {
        for (const struct pmu_events_map *tables = &pmu_events_map[0]; tables->arch; tables++) {
-               int ret = pmu_events_table__for_each_event(&tables->event_table, fn, data);
+               int ret = pmu_events_table__for_each_event(&tables->event_table,
+                                                          /*pmu=*/ NULL, fn, data);
 
                if (ret)
                        return ret;
@@ -408,7 +413,7 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *data)
        for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
             tables->name;
             tables++) {
-               int ret = pmu_events_table__for_each_event(&tables->table, fn, data);
+               int ret = pmu_events_table__for_each_event(&tables->table, /*pmu=*/ NULL, fn, data);
 
                if (ret)
                        return ret;
index 1ad2014..396af53 100755 (executable)
@@ -826,14 +826,20 @@ static int pmu_events_table__for_each_event_pmu(const struct pmu_events_table *t
  }
 
 int pmu_events_table__for_each_event(const struct pmu_events_table *table,
+                                    struct perf_pmu *pmu,
                                     pmu_event_iter_fn fn,
                                     void *data)
 {
         for (size_t i = 0; i < table->num_pmus; i++) {
-                int ret = pmu_events_table__for_each_event_pmu(table, &table->pmus[i],
-                                                               fn, data);
+                const struct pmu_table_entry *table_pmu = &table->pmus[i];
+                const char *pmu_name = &big_c_string[table_pmu->pmu_name.offset];
+                int ret;
 
-                if (ret)
+                if (pmu && !pmu__name_match(pmu, pmu_name))
+                        continue;
+
+                ret = pmu_events_table__for_each_event_pmu(table, table_pmu, fn, data);
+                if (pmu || ret)
                         return ret;
         }
         return 0;
@@ -955,7 +961,8 @@ int pmu_for_each_core_event(pmu_event_iter_fn fn, void *data)
         for (const struct pmu_events_map *tables = &pmu_events_map[0];
              tables->arch;
              tables++) {
-                int ret = pmu_events_table__for_each_event(&tables->event_table, fn, data);
+                int ret = pmu_events_table__for_each_event(&tables->event_table,
+                                                           /*pmu=*/ NULL, fn, data);
 
                 if (ret)
                         return ret;
@@ -992,7 +999,8 @@ int pmu_for_each_sys_event(pmu_event_iter_fn fn, void *data)
         for (const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
              tables->name;
              tables++) {
-                int ret = pmu_events_table__for_each_event(&tables->event_table, fn, data);
+                int ret = pmu_events_table__for_each_event(&tables->event_table,
+                                                           /*pmu=*/ NULL, fn, data);
 
                 if (ret)
                         return ret;
index 6557381..c0303ba 100644 (file)
@@ -77,7 +77,9 @@ typedef int (*pmu_metric_iter_fn)(const struct pmu_metric *pm,
                                  const struct pmu_metrics_table *table,
                                  void *data);
 
-int pmu_events_table__for_each_event(const struct pmu_events_table *table, pmu_event_iter_fn fn,
+int pmu_events_table__for_each_event(const struct pmu_events_table *table,
+                                   struct perf_pmu *pmu,
+                                   pmu_event_iter_fn fn,
                                    void *data);
 int pmu_metrics_table__for_each_metric(const struct pmu_metrics_table *table, pmu_metric_iter_fn fn,
                                     void *data);
index 0b6efab..92d1f6f 100644 (file)
@@ -483,12 +483,14 @@ static int test__pmu_event_table(struct test_suite *test __maybe_unused,
        if (!table || !sys_event_table)
                return -1;
 
-       err = pmu_events_table__for_each_event(table, test__pmu_event_table_core_callback,
+       err = pmu_events_table__for_each_event(table, /*pmu=*/ NULL,
+                                             test__pmu_event_table_core_callback,
                                              &map_events);
        if (err)
                return err;
 
-       err = pmu_events_table__for_each_event(sys_event_table, test__pmu_event_table_sys_callback,
+       err = pmu_events_table__for_each_event(sys_event_table, /*pmu=*/ NULL,
+                                             test__pmu_event_table_sys_callback,
                                              &map_events);
        if (err)
                return err;
index 0c82bbe..9ba9ac3 100644 (file)
@@ -859,28 +859,14 @@ out:
        return res;
 }
 
-struct pmu_add_cpu_aliases_map_data {
-       /* List being added to. */
-       struct list_head *head;
-       /* If a pmu_event lacks a given PMU the default used. */
-       char *default_pmu_name;
-       /* The PMU that we're searching for events for. */
-       struct perf_pmu *pmu;
-};
-
 static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
                                        const struct pmu_events_table *table __maybe_unused,
                                        void *vdata)
 {
-       struct pmu_add_cpu_aliases_map_data *data = vdata;
-       const char *pname = pe->pmu ?: data->default_pmu_name;
+       struct list_head *head = vdata;
 
-       if (!strcmp(pname, data->pmu->name) ||
-           (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->pmu->name))) {
-               /* need type casts to override 'const' */
-               __perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc,
-                                     (char *)pe->event, pe);
-       }
+       /* need type casts to override 'const' */
+       __perf_pmu__new_alias(head, -1, (char *)pe->name, (char *)pe->desc, (char *)pe->event, pe);
        return 0;
 }
 
@@ -890,14 +876,7 @@ static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe,
  */
 void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table)
 {
-       struct pmu_add_cpu_aliases_map_data data = {
-               .head = &pmu->aliases,
-               .default_pmu_name = perf_pmus__default_pmu_name(),
-               .pmu = pmu,
-       };
-
-       pmu_events_table__for_each_event(table, pmu_add_cpu_aliases_map_callback, &data);
-       free(data.default_pmu_name);
+       pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, &pmu->aliases);
 }
 
 static void pmu_add_cpu_aliases(struct perf_pmu *pmu)
@@ -1713,6 +1692,12 @@ int perf_pmu__for_each_event(const struct perf_pmu *pmu, void *state, pmu_event_
        return ret;
 }
 
+bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name)
+{
+       return !strcmp(pmu->name, pmu_name) ||
+               (pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name));
+}
+
 bool perf_pmu__is_software(const struct perf_pmu *pmu)
 {
        if (pmu->is_core || pmu->is_uncore || pmu->auxtrace)
index 03211de..2b17301 100644 (file)
@@ -198,6 +198,7 @@ bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu);
 bool perf_pmu__have_event(const struct perf_pmu *pmu, const char *name);
 size_t perf_pmu__num_events(const struct perf_pmu *pmu);
 int perf_pmu__for_each_event(const struct perf_pmu *pmu, void *state, pmu_event_callback cb);
+bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name);
 
 /**
  * perf_pmu_is_software - is the PMU a software PMU as in it uses the