Skip to content

Commit 112cb56

Browse files
liu-song-6acmel
authored andcommitted
perf stat: Introduce config stat.bpf-counter-events
Currently, to use BPF to aggregate perf event counters, the user uses --bpf-counters option. Enable "use bpf by default" events with a config option, stat.bpf-counter-events. Events with name in the option will use BPF. This also enables mixed BPF event and regular event in the same sesssion. For example: perf config stat.bpf-counter-events=instructions perf stat -e instructions,cs The second command will use BPF for "instructions" but not "cs". Signed-off-by: Song Liu <song@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Song Liu <songliubraving@fb.com> Link: https://lore.kernel.org/r/20210425214333.1090950-4-song@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent fe3dd82 commit 112cb56

File tree

7 files changed

+63
-23
lines changed

7 files changed

+63
-23
lines changed

tools/perf/Documentation/perf-stat.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ report::
9797
Use BPF programs to aggregate readings from perf_events. This
9898
allows multiple perf-stat sessions that are counting the same metric (cycles,
9999
instructions, etc.) to share hardware counters.
100+
To use BPF programs on common events by default, use
101+
"perf config stat.bpf-counter-events=<list_of_events>".
100102

101103
--bpf-attr-map::
102104
With option "--bpf-counters", different perf-stat sessions share

tools/perf/builtin-stat.c

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ static const char *smi_cost_attrs = {
161161
};
162162

163163
static struct evlist *evsel_list;
164+
static bool all_counters_use_bpf = true;
164165

165166
static struct target target = {
166167
.uid = UINT_MAX,
@@ -401,6 +402,9 @@ static int read_affinity_counters(struct timespec *rs)
401402
struct affinity affinity;
402403
int i, ncpus, cpu;
403404

405+
if (all_counters_use_bpf)
406+
return 0;
407+
404408
if (affinity__setup(&affinity) < 0)
405409
return -1;
406410

@@ -415,6 +419,8 @@ static int read_affinity_counters(struct timespec *rs)
415419
evlist__for_each_entry(evsel_list, counter) {
416420
if (evsel__cpu_iter_skip(counter, cpu))
417421
continue;
422+
if (evsel__is_bpf(counter))
423+
continue;
418424
if (!counter->err) {
419425
counter->err = read_counter_cpu(counter, rs,
420426
counter->cpu_iter - 1);
@@ -431,6 +437,9 @@ static int read_bpf_map_counters(void)
431437
int err;
432438

433439
evlist__for_each_entry(evsel_list, counter) {
440+
if (!evsel__is_bpf(counter))
441+
continue;
442+
434443
err = bpf_counter__read(counter);
435444
if (err)
436445
return err;
@@ -441,14 +450,10 @@ static int read_bpf_map_counters(void)
441450
static void read_counters(struct timespec *rs)
442451
{
443452
struct evsel *counter;
444-
int err;
445453

446454
if (!stat_config.stop_read_counter) {
447-
if (target__has_bpf(&target))
448-
err = read_bpf_map_counters();
449-
else
450-
err = read_affinity_counters(rs);
451-
if (err < 0)
455+
if (read_bpf_map_counters() ||
456+
read_affinity_counters(rs))
452457
return;
453458
}
454459

@@ -537,12 +542,13 @@ static int enable_counters(void)
537542
struct evsel *evsel;
538543
int err;
539544

540-
if (target__has_bpf(&target)) {
541-
evlist__for_each_entry(evsel_list, evsel) {
542-
err = bpf_counter__enable(evsel);
543-
if (err)
544-
return err;
545-
}
545+
evlist__for_each_entry(evsel_list, evsel) {
546+
if (!evsel__is_bpf(evsel))
547+
continue;
548+
549+
err = bpf_counter__enable(evsel);
550+
if (err)
551+
return err;
546552
}
547553

548554
if (stat_config.initial_delay < 0) {
@@ -786,11 +792,11 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
786792
if (affinity__setup(&affinity) < 0)
787793
return -1;
788794

789-
if (target__has_bpf(&target)) {
790-
evlist__for_each_entry(evsel_list, counter) {
791-
if (bpf_counter__load(counter, &target))
792-
return -1;
793-
}
795+
evlist__for_each_entry(evsel_list, counter) {
796+
if (bpf_counter__load(counter, &target))
797+
return -1;
798+
if (!evsel__is_bpf(counter))
799+
all_counters_use_bpf = false;
794800
}
795801

796802
evlist__for_each_cpu (evsel_list, i, cpu) {
@@ -807,6 +813,8 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
807813
continue;
808814
if (counter->reset_group || counter->errored)
809815
continue;
816+
if (evsel__is_bpf(counter))
817+
continue;
810818
try_again:
811819
if (create_perf_stat_counter(counter, &stat_config, &target,
812820
counter->cpu_iter - 1) < 0) {

tools/perf/util/bpf_counter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ int bpf_counter__load(struct evsel *evsel, struct target *target)
790790
{
791791
if (target->bpf_str)
792792
evsel->bpf_counter_ops = &bpf_program_profiler_ops;
793-
else if (target->use_bpf)
793+
else if (target->use_bpf ||
794+
evsel__match_bpf_counter_events(evsel->name))
794795
evsel->bpf_counter_ops = &bperf_ops;
795796

796797
if (evsel->bpf_counter_ops)

tools/perf/util/config.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "util/hist.h" /* perf_hist_config */
1919
#include "util/llvm-utils.h" /* perf_llvm_config */
2020
#include "util/stat.h" /* perf_stat__set_big_num */
21+
#include "util/evsel.h" /* evsel__hw_names, evsel__use_bpf_counters */
2122
#include "build-id.h"
2223
#include "debug.h"
2324
#include "config.h"
@@ -460,6 +461,9 @@ static int perf_stat_config(const char *var, const char *value)
460461
if (!strcmp(var, "stat.no-csv-summary"))
461462
perf_stat__set_no_csv_summary(perf_config_bool(var, value));
462463

464+
if (!strcmp(var, "stat.bpf-counter-events"))
465+
evsel__bpf_counter_events = strdup(value);
466+
463467
/* Add other config variables here. */
464468
return 0;
465469
}

tools/perf/util/evsel.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,28 @@ const char *evsel__hw_names[PERF_COUNT_HW_MAX] = {
492492
"ref-cycles",
493493
};
494494

495+
char *evsel__bpf_counter_events;
496+
497+
bool evsel__match_bpf_counter_events(const char *name)
498+
{
499+
int name_len;
500+
bool match;
501+
char *ptr;
502+
503+
if (!evsel__bpf_counter_events)
504+
return false;
505+
506+
ptr = strstr(evsel__bpf_counter_events, name);
507+
name_len = strlen(name);
508+
509+
/* check name matches a full token in evsel__bpf_counter_events */
510+
match = (ptr != NULL) &&
511+
((ptr == evsel__bpf_counter_events) || (*(ptr - 1) == ',')) &&
512+
((*(ptr + name_len) == ',') || (*(ptr + name_len) == '\0'));
513+
514+
return match;
515+
}
516+
495517
static const char *__evsel__hw_name(u64 config)
496518
{
497519
if (config < PERF_COUNT_HW_MAX && evsel__hw_names[config])

tools/perf/util/evsel.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,21 @@ void evsel__calc_id_pos(struct evsel *evsel);
239239

240240
bool evsel__is_cache_op_valid(u8 type, u8 op);
241241

242+
static inline bool evsel__is_bpf(struct evsel *evsel)
243+
{
244+
return evsel->bpf_counter_ops != NULL;
245+
}
246+
242247
#define EVSEL__MAX_ALIASES 8
243248

244249
extern const char *evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX][EVSEL__MAX_ALIASES];
245250
extern const char *evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX][EVSEL__MAX_ALIASES];
246251
extern const char *evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX][EVSEL__MAX_ALIASES];
247252
extern const char *evsel__hw_names[PERF_COUNT_HW_MAX];
248253
extern const char *evsel__sw_names[PERF_COUNT_SW_MAX];
254+
extern char *evsel__bpf_counter_events;
255+
bool evsel__match_bpf_counter_events(const char *name);
256+
249257
int __evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result, char *bf, size_t size);
250258
const char *evsel__name(struct evsel *evsel);
251259

tools/perf/util/target.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ static inline bool target__has_cpu(struct target *target)
6666
return target->system_wide || target->cpu_list;
6767
}
6868

69-
static inline bool target__has_bpf(struct target *target)
70-
{
71-
return target->bpf_str || target->use_bpf;
72-
}
73-
7469
static inline bool target__none(struct target *target)
7570
{
7671
return !target__has_task(target) && !target__has_cpu(target);

0 commit comments

Comments
 (0)