Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Add evlist__add_sched_switch()

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>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
182bb594 60ea006f

+28 -20
+5 -10
tools/perf/arch/x86/util/intel-pt.c
··· 11 11 #include <linux/bitops.h> 12 12 #include <linux/log2.h> 13 13 #include <linux/zalloc.h> 14 + #include <linux/err.h> 14 15 #include <cpuid.h> 15 16 16 17 #include "../../../util/session.h" ··· 427 426 if (!evlist__can_select_event(evlist, sched_switch)) 428 427 return -EPERM; 429 428 430 - err = parse_event(evlist, sched_switch); 431 - if (err) { 432 - pr_debug2("%s: failed to parse %s, error %d\n", 429 + evsel = evlist__add_sched_switch(evlist, true); 430 + if (IS_ERR(evsel)) { 431 + err = PTR_ERR(evsel); 432 + pr_debug2("%s: failed to create %s, error = %d\n", 433 433 __func__, sched_switch, err); 434 434 return err; 435 435 } 436 436 437 - evsel = evlist__last(evlist); 438 - 439 - evsel__set_sample_bit(evsel, CPU); 440 - evsel__set_sample_bit(evsel, TIME); 441 - 442 - evsel->core.system_wide = true; 443 - evsel->no_aux_samples = true; 444 437 evsel->immediate = true; 445 438 446 439 return 0;
+5 -10
tools/perf/tests/switch-tracking.c
··· 6 6 #include <time.h> 7 7 #include <stdlib.h> 8 8 #include <linux/zalloc.h> 9 + #include <linux/err.h> 9 10 #include <perf/cpumap.h> 10 11 #include <perf/evlist.h> 11 12 #include <perf/mmap.h> ··· 399 398 goto out; 400 399 } 401 400 402 - err = parse_event(evlist, sched_switch); 403 - if (err) { 404 - pr_debug("Failed to parse event %s\n", sched_switch); 401 + switch_evsel = evlist__add_sched_switch(evlist, true); 402 + if (IS_ERR(switch_evsel)) { 403 + err = PTR_ERR(switch_evsel); 404 + pr_debug("Failed to create event %s\n", sched_switch); 405 405 goto out_err; 406 406 } 407 407 408 - switch_evsel = evlist__last(evlist); 409 - 410 - evsel__set_sample_bit(switch_evsel, CPU); 411 - evsel__set_sample_bit(switch_evsel, TIME); 412 - 413 - switch_evsel->core.system_wide = true; 414 - switch_evsel->no_aux_samples = true; 415 408 switch_evsel->immediate = true; 416 409 417 410 /* Test moving an event to the front */
+17
tools/perf/util/evlist.c
··· 288 288 return evsel; 289 289 } 290 290 291 + struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide) 292 + { 293 + struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0); 294 + 295 + if (IS_ERR(evsel)) 296 + return evsel; 297 + 298 + evsel__set_sample_bit(evsel, CPU); 299 + evsel__set_sample_bit(evsel, TIME); 300 + 301 + evsel->core.system_wide = system_wide; 302 + evsel->no_aux_samples = true; 303 + 304 + evlist__add(evlist, evsel); 305 + return evsel; 306 + }; 307 + 291 308 int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs) 292 309 { 293 310 struct evsel *evsel, *n;
+1
tools/perf/util/evlist.h
··· 127 127 { 128 128 return evlist__add_aux_dummy(evlist, true); 129 129 } 130 + struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide); 130 131 131 132 int evlist__add_sb_event(struct evlist *evlist, struct perf_event_attr *attr, 132 133 evsel__sb_cb_t cb, void *data);