perf tools: Add evlist__add_sched_switch()
authorNamhyung Kim <namhyung@kernel.org>
Mon, 3 Oct 2022 20:46:46 +0000 (13:46 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 6 Oct 2022 11:03:53 +0000 (08:03 -0300)
Add a help to create a system-wide sched_switch event.  One merit is
that it sets the system-wide bit before adding it to evlist so that
the libperf can handle the cpu and thread maps correctly.

Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20221003204647.1481128-5-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/arch/x86/util/intel-pt.c
tools/perf/tests/switch-tracking.c
tools/perf/util/evlist.c
tools/perf/util/evlist.h

index 13933020a79eb182d26916108d1ca4505582348e..793b35f2221aacc24395fabb4a5f4f3fbf6843e1 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/bitops.h>
 #include <linux/log2.h>
 #include <linux/zalloc.h>
+#include <linux/err.h>
 #include <cpuid.h>
 
 #include "../../../util/session.h"
@@ -426,20 +427,14 @@ static int intel_pt_track_switches(struct evlist *evlist)
        if (!evlist__can_select_event(evlist, sched_switch))
                return -EPERM;
 
-       err = parse_event(evlist, sched_switch);
-       if (err) {
-               pr_debug2("%s: failed to parse %s, error %d\n",
+       evsel = evlist__add_sched_switch(evlist, true);
+       if (IS_ERR(evsel)) {
+               err = PTR_ERR(evsel);
+               pr_debug2("%s: failed to create %s, error = %d\n",
                          __func__, sched_switch, err);
                return err;
        }
 
-       evsel = evlist__last(evlist);
-
-       evsel__set_sample_bit(evsel, CPU);
-       evsel__set_sample_bit(evsel, TIME);
-
-       evsel->core.system_wide = true;
-       evsel->no_aux_samples = true;
        evsel->immediate = true;
 
        return 0;
index 2d46af9ef93573495fefb200292ef0b7bf22f6eb..87f565c7f650de70a944c0b1625bb9bf6e090515 100644 (file)
@@ -6,6 +6,7 @@
 #include <time.h>
 #include <stdlib.h>
 #include <linux/zalloc.h>
+#include <linux/err.h>
 #include <perf/cpumap.h>
 #include <perf/evlist.h>
 #include <perf/mmap.h>
@@ -398,19 +399,13 @@ static int test__switch_tracking(struct test_suite *test __maybe_unused, int sub
                goto out;
        }
 
-       err = parse_event(evlist, sched_switch);
-       if (err) {
-               pr_debug("Failed to parse event %s\n", sched_switch);
+       switch_evsel = evlist__add_sched_switch(evlist, true);
+       if (IS_ERR(switch_evsel)) {
+               err = PTR_ERR(switch_evsel);
+               pr_debug("Failed to create event %s\n", sched_switch);
                goto out_err;
        }
 
-       switch_evsel = evlist__last(evlist);
-
-       evsel__set_sample_bit(switch_evsel, CPU);
-       evsel__set_sample_bit(switch_evsel, TIME);
-
-       switch_evsel->core.system_wide = true;
-       switch_evsel->no_aux_samples = true;
        switch_evsel->immediate = true;
 
        /* Test moving an event to the front */
index dcf57b271ff13eb191f71cfe27779c60507a4219..6612b00949e7081454fe8cfd56e45f621df6fd4c 100644 (file)
@@ -288,6 +288,23 @@ struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
        return evsel;
 }
 
+struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
+{
+       struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0);
+
+       if (IS_ERR(evsel))
+               return evsel;
+
+       evsel__set_sample_bit(evsel, CPU);
+       evsel__set_sample_bit(evsel, TIME);
+
+       evsel->core.system_wide = system_wide;
+       evsel->no_aux_samples = true;
+
+       evlist__add(evlist, evsel);
+       return evsel;
+};
+
 int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
 {
        struct evsel *evsel, *n;
index 9d967fe3953a87a5f51b274974fbc83b618f3af8..16734c6756b3cd79eb38002cc322190cfcceb02c 100644 (file)
@@ -127,6 +127,7 @@ static inline struct evsel *evlist__add_dummy_on_all_cpus(struct evlist *evlist)
 {
        return evlist__add_aux_dummy(evlist, true);
 }
+struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide);
 
 int evlist__add_sb_event(struct evlist *evlist, struct perf_event_attr *attr,
                         evsel__sb_cb_t cb, void *data);