perf evlist: Remove nr_groups
authorIan Rogers <irogers@google.com>
Sun, 12 Mar 2023 02:15:42 +0000 (18:15 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 13 Mar 2023 20:42:27 +0000 (17:42 -0300)
Maintaining the number of groups during event parsing is problematic
and since changing to sort/regroup events can only be computed by a
linear pass over the evlist. As the value is generally only used in
tests, rather than hold it in a variable compute it by passing over
the evlist when necessary.

This change highlights that libpfm's counting of groups with a single
entry disagreed with regular event parsing. The libpfm tests are
updated accordingly.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@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.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Kim Phillips <kim.phillips@amd.com>
Cc: Leo Yan <leo.yan@linaro.org>
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: Sean Christopherson <seanjc@google.com>
Cc: Steinar H. Gunderson <sesse@google.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: Xing Zhengjun <zhengjun.xing@linux.intel.com>
Link: https://lore.kernel.org/r/20230312021543.3060328-2-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
15 files changed:
tools/lib/perf/evlist.c
tools/lib/perf/include/internal/evlist.h
tools/lib/perf/include/perf/evlist.h
tools/perf/builtin-record.c
tools/perf/builtin-report.c
tools/perf/tests/bpf.c
tools/perf/tests/parse-events.c
tools/perf/tests/pfm.c
tools/perf/util/evlist.c
tools/perf/util/evlist.h
tools/perf/util/header.c
tools/perf/util/parse-events.c
tools/perf/util/parse-events.h
tools/perf/util/parse-events.y
tools/perf/util/pfm.c

index 2d6121e..81e8b5f 100644 (file)
@@ -703,7 +703,23 @@ void perf_evlist__set_leader(struct perf_evlist *evlist)
                struct perf_evsel *first = list_entry(evlist->entries.next,
                                                struct perf_evsel, node);
 
-               evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
                __perf_evlist__set_leader(&evlist->entries, first);
        }
 }
+
+int perf_evlist__nr_groups(struct perf_evlist *evlist)
+{
+       struct perf_evsel *evsel;
+       int nr_groups = 0;
+
+       perf_evlist__for_each_evsel(evlist, evsel) {
+               /*
+                * evsels by default have a nr_members of 1, and they are their
+                * own leader. If the nr_members is >1 then this is an
+                * indication of a group.
+                */
+               if (evsel->leader == evsel && evsel->nr_members > 1)
+                       nr_groups++;
+       }
+       return nr_groups;
+}
index 850f070..3339bc2 100644 (file)
@@ -17,7 +17,6 @@ struct perf_mmap_param;
 struct perf_evlist {
        struct list_head         entries;
        int                      nr_entries;
-       int                      nr_groups;
        bool                     has_user_cpus;
        bool                     needs_map_propagation;
        /**
index 9ca399d..e894b77 100644 (file)
@@ -47,4 +47,5 @@ LIBPERF_API struct perf_mmap *perf_evlist__next_mmap(struct perf_evlist *evlist,
             (pos) = perf_evlist__next_mmap((evlist), (pos), overwrite))
 
 LIBPERF_API void perf_evlist__set_leader(struct perf_evlist *evlist);
+LIBPERF_API int perf_evlist__nr_groups(struct perf_evlist *evlist);
 #endif /* __LIBPERF_EVLIST_H */
index fb8aab0..7438816 100644 (file)
@@ -2474,7 +2474,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
                rec->tool.ordered_events = false;
        }
 
-       if (!rec->evlist->core.nr_groups)
+       if (evlist__nr_groups(rec->evlist) == 0)
                perf_header__clear_feat(&session->header, HEADER_GROUP_DESC);
 
        if (data->is_pipe) {
index 2ee2ecc..6400615 100644 (file)
@@ -1481,7 +1481,7 @@ repeat:
 
        setup_forced_leader(&report, session->evlist);
 
-       if (symbol_conf.group_sort_idx && !session->evlist->core.nr_groups) {
+       if (symbol_conf.group_sort_idx && evlist__nr_groups(session->evlist) == 0) {
                parse_options_usage(NULL, options, "group-sort-idx", 0);
                ret = -EINVAL;
                goto error;
index ae9223f..8beb460 100644 (file)
@@ -153,7 +153,6 @@ static int do_test(struct bpf_object *obj, int (*func)(void),
        }
 
        evlist__splice_list_tail(evlist, &parse_state.list);
-       evlist->core.nr_groups = parse_state.nr_groups;
 
        evlist__config(evlist, &opts, NULL);
 
index 71a5cb3..ffa6f0a 100644 (file)
@@ -53,7 +53,7 @@ static int test__checkevent_tracepoint(struct evlist *evlist)
        struct evsel *evsel = evlist__first(evlist);
 
        TEST_ASSERT_VAL("wrong number of entries", 1 == evlist->core.nr_entries);
-       TEST_ASSERT_VAL("wrong number of groups", 0 == evlist->core.nr_groups);
+       TEST_ASSERT_VAL("wrong number of groups", 0 == evlist__nr_groups(evlist));
        TEST_ASSERT_VAL("wrong type", PERF_TYPE_TRACEPOINT == evsel->core.attr.type);
        TEST_ASSERT_VAL("wrong sample_type",
                PERF_TP_SAMPLE_TYPE == evsel->core.attr.sample_type);
@@ -66,7 +66,7 @@ static int test__checkevent_tracepoint_multi(struct evlist *evlist)
        struct evsel *evsel;
 
        TEST_ASSERT_VAL("wrong number of entries", evlist->core.nr_entries > 1);
-       TEST_ASSERT_VAL("wrong number of groups", 0 == evlist->core.nr_groups);
+       TEST_ASSERT_VAL("wrong number of groups", 0 == evlist__nr_groups(evlist));
 
        evlist__for_each_entry(evlist, evsel) {
                TEST_ASSERT_VAL("wrong type",
@@ -677,7 +677,7 @@ static int test__group1(struct evlist *evlist)
        struct evsel *evsel, *leader;
 
        TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
-       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->core.nr_groups);
+       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
 
        /* instructions:k */
        evsel = leader = evlist__first(evlist);
@@ -719,7 +719,7 @@ static int test__group2(struct evlist *evlist)
        struct evsel *evsel, *leader;
 
        TEST_ASSERT_VAL("wrong number of entries", 3 == evlist->core.nr_entries);
-       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->core.nr_groups);
+       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
 
        /* faults + :ku modifier */
        evsel = leader = evlist__first(evlist);
@@ -775,7 +775,7 @@ static int test__group3(struct evlist *evlist __maybe_unused)
        struct evsel *evsel, *leader;
 
        TEST_ASSERT_VAL("wrong number of entries", 5 == evlist->core.nr_entries);
-       TEST_ASSERT_VAL("wrong number of groups", 2 == evlist->core.nr_groups);
+       TEST_ASSERT_VAL("wrong number of groups", 2 == evlist__nr_groups(evlist));
 
        /* group1 syscalls:sys_enter_openat:H */
        evsel = leader = evlist__first(evlist);
@@ -868,7 +868,7 @@ static int test__group4(struct evlist *evlist __maybe_unused)
        struct evsel *evsel, *leader;
 
        TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
-       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->core.nr_groups);
+       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
 
        /* cycles:u + p */
        evsel = leader = evlist__first(evlist);
@@ -912,7 +912,7 @@ static int test__group5(struct evlist *evlist __maybe_unused)
        struct evsel *evsel, *leader;
 
        TEST_ASSERT_VAL("wrong number of entries", 5 == evlist->core.nr_entries);
-       TEST_ASSERT_VAL("wrong number of groups", 2 == evlist->core.nr_groups);
+       TEST_ASSERT_VAL("wrong number of groups", 2 == evlist__nr_groups(evlist));
 
        /* cycles + G */
        evsel = leader = evlist__first(evlist);
@@ -998,7 +998,7 @@ static int test__group_gh1(struct evlist *evlist)
        struct evsel *evsel, *leader;
 
        TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
-       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->core.nr_groups);
+       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
 
        /* cycles + :H group modifier */
        evsel = leader = evlist__first(evlist);
@@ -1038,7 +1038,7 @@ static int test__group_gh2(struct evlist *evlist)
        struct evsel *evsel, *leader;
 
        TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
-       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->core.nr_groups);
+       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
 
        /* cycles + :G group modifier */
        evsel = leader = evlist__first(evlist);
@@ -1078,7 +1078,7 @@ static int test__group_gh3(struct evlist *evlist)
        struct evsel *evsel, *leader;
 
        TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
-       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->core.nr_groups);
+       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
 
        /* cycles:G + :u group modifier */
        evsel = leader = evlist__first(evlist);
@@ -1118,7 +1118,7 @@ static int test__group_gh4(struct evlist *evlist)
        struct evsel *evsel, *leader;
 
        TEST_ASSERT_VAL("wrong number of entries", 2 == evlist->core.nr_entries);
-       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist->core.nr_groups);
+       TEST_ASSERT_VAL("wrong number of groups", 1 == evlist__nr_groups(evlist));
 
        /* cycles:G + :uG group modifier */
        evsel = leader = evlist__first(evlist);
index 71b76de..2e38dfa 100644 (file)
@@ -76,7 +76,7 @@ static int test__pfm_events(struct test_suite *test __maybe_unused,
                                count_pfm_events(&evlist->core),
                                table[i].nr_events);
                TEST_ASSERT_EQUAL(table[i].events,
-                               evlist->core.nr_groups,
+                               evlist__nr_groups(evlist),
                                0);
 
                evlist__delete(evlist);
@@ -103,22 +103,22 @@ static int test__pfm_group(struct test_suite *test __maybe_unused,
                {
                        .events = "{instructions}",
                        .nr_events = 1,
-                       .nr_groups = 1,
+                       .nr_groups = 0,
                },
                {
                        .events = "{instructions},{}",
                        .nr_events = 1,
-                       .nr_groups = 1,
+                       .nr_groups = 0,
                },
                {
                        .events = "{},{instructions}",
                        .nr_events = 1,
-                       .nr_groups = 1,
+                       .nr_groups = 0,
                },
                {
                        .events = "{instructions},{instructions}",
                        .nr_events = 2,
-                       .nr_groups = 2,
+                       .nr_groups = 0,
                },
                {
                        .events = "{instructions,cycles},{instructions,cycles}",
@@ -161,7 +161,7 @@ static int test__pfm_group(struct test_suite *test __maybe_unused,
                                count_pfm_events(&evlist->core),
                                table[i].nr_events);
                TEST_ASSERT_EQUAL(table[i].events,
-                               evlist->core.nr_groups,
+                               evlist__nr_groups(evlist),
                                table[i].nr_groups);
 
                evlist__delete(evlist);
index 9e4b2bb..b74e122 100644 (file)
@@ -1777,7 +1777,7 @@ bool evlist__exclude_kernel(struct evlist *evlist)
  */
 void evlist__force_leader(struct evlist *evlist)
 {
-       if (!evlist->core.nr_groups) {
+       if (evlist__nr_groups(evlist) == 0) {
                struct evsel *leader = evlist__first(evlist);
 
                evlist__set_leader(evlist);
index d89d8f9..46cf402 100644 (file)
@@ -9,6 +9,7 @@
 #include <api/fd/array.h>
 #include <internal/evlist.h>
 #include <internal/evsel.h>
+#include <perf/evlist.h>
 #include "events_stats.h"
 #include "evsel.h"
 #include <pthread.h>
@@ -255,6 +256,11 @@ static inline struct evsel *evlist__last(struct evlist *evlist)
        return container_of(evsel, struct evsel, core);
 }
 
+static inline int evlist__nr_groups(struct evlist *evlist)
+{
+       return perf_evlist__nr_groups(&evlist->core);
+}
+
 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size);
 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size);
 
index 404d816..2768702 100644 (file)
@@ -786,7 +786,7 @@ static int write_pmu_mappings(struct feat_fd *ff,
 static int write_group_desc(struct feat_fd *ff,
                            struct evlist *evlist)
 {
-       u32 nr_groups = evlist->core.nr_groups;
+       u32 nr_groups = evlist__nr_groups(evlist);
        struct evsel *evsel;
        int ret;
 
@@ -2807,7 +2807,6 @@ static int process_group_desc(struct feat_fd *ff, void *data __maybe_unused)
         * Rebuild group relationship based on the group_desc
         */
        session = container_of(ff->ph, struct perf_session, header);
-       session->evlist->core.nr_groups = nr_groups;
 
        i = nr = 0;
        evlist__for_each_entry(session->evlist, evsel) {
index 93a9065..9ec3c1d 100644 (file)
@@ -2260,7 +2260,6 @@ int __parse_events(struct evlist *evlist, const char *str,
        if (!ret) {
                struct evsel *last;
 
-               evlist->core.nr_groups += parse_state.nr_groups;
                last = evlist__last(evlist);
                last->cmdline_group_boundary = true;
 
index fdac44d..767ad17 100644 (file)
@@ -122,7 +122,6 @@ struct parse_events_error {
 struct parse_events_state {
        struct list_head           list;
        int                        idx;
-       int                        nr_groups;
        struct parse_events_error *error;
        struct evlist             *evlist;
        struct list_head          *terms;
index f1b153c..3a04602 100644 (file)
@@ -49,14 +49,6 @@ static void free_list_evsel(struct list_head* list_evsel)
        free(list_evsel);
 }
 
-static void inc_group_count(struct list_head *list,
-                      struct parse_events_state *parse_state)
-{
-       /* Count groups only have more than 1 members */
-       if (!list_is_last(list->next, list))
-               parse_state->nr_groups++;
-}
-
 %}
 
 %token PE_START_EVENTS PE_START_TERMS
@@ -201,7 +193,6 @@ PE_NAME '{' events '}'
 {
        struct list_head *list = $3;
 
-       inc_group_count(list, _parse_state);
        /* Takes ownership of $1. */
        parse_events__set_leader($1, list);
        $$ = list;
@@ -211,7 +202,6 @@ PE_NAME '{' events '}'
 {
        struct list_head *list = $2;
 
-       inc_group_count(list, _parse_state);
        parse_events__set_leader(NULL, list);
        $$ = list;
 }
index b59ba82..6c11914 100644 (file)
@@ -112,7 +112,6 @@ int parse_libpfm_events_option(const struct option *opt, const char *str,
                                   "cannot close a non-existing event group\n");
                                goto error;
                        }
-                       evlist->core.nr_groups++;
                        grp_leader = NULL;
                        grp_evt = -1;
                }