From 973dbaf63a564ddf0adf74162060acfaadd4555a Mon Sep 17 00:00:00 2001 From: chendexi Date: Sat, 1 Jul 2023 13:46:52 +0800 Subject: [PATCH] Added ability to open and attach performance events --- src/llcstat/llcstat.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/llcstat/llcstat.c b/src/llcstat/llcstat.c index 5b3693c4..28c5ba2c 100644 --- a/src/llcstat/llcstat.c +++ b/src/llcstat/llcstat.c @@ -77,3 +77,37 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state) return 0; } + +static int nr_cpus; + +static int open_and_attach_perf_event(__u64 config, int period, + struct bpf_program *prog, + struct bpf_link *links[]) +{ + struct perf_event_attr attr = { + .type = PERF_TYPE_HARDWARE, + .freq = 0, + .sample_period = period, + .config = config + }; + int i, fd; + + for (i = 0; i < nr_cpus; i++) { + fd = syscall(__NR_perf_event_open, &attr, -1, i, -1, 0); + if (fd < 0) { + /* Ignore CPU that is offline */ + if (errno == ENODEV) + continue; + warning("Failed to init perf sampling: %s\n", + strerror(errno)); + return -1; + } + links[i] = bpf_program__attach_perf_event(prog, fd); + if (!links[i]) { + warning("Failed to attach perf event on CPU: %d\n", i); + close(fd); + return -1; + } + } + return 0; +} -- Gitee