2023-01-17 12:49 UTC+0800 ~ tong@xxxxxxxxxxxxx > From: Tonghao Zhang <tong@xxxxxxxxxxxxx> > > The number of online cpu may be not equal to possible cpu. > bpftool prog profile, can not create pmu event on possible > but not online cpu. s/not/on/ ? > > $ dmidecode -s system-product-name > PowerEdge R620 > $ cat /sys/devices/system/cpu/online > 0-31 > $ cat /sys/devices/system/cpu/possible > 0-47 > > To fix this issue, use online cpu instead of possible, to > create perf event and other resource. > > Signed-off-by: Tonghao Zhang <tong@xxxxxxxxxxxxx> > Cc: Quentin Monnet <quentin@xxxxxxxxxxxxx> > Cc: Alexei Starovoitov <ast@xxxxxxxxxx> > Cc: Daniel Borkmann <daniel@xxxxxxxxxxxxx> > Cc: Andrii Nakryiko <andrii@xxxxxxxxxx> > Cc: Martin KaFai Lau <martin.lau@xxxxxxxxx> > Cc: Song Liu <song@xxxxxxxxxx> > Cc: Yonghong Song <yhs@xxxxxx> > Cc: John Fastabend <john.fastabend@xxxxxxxxx> > Cc: KP Singh <kpsingh@xxxxxxxxxx> > Cc: Stanislav Fomichev <sdf@xxxxxxxxxx> > Cc: Hao Luo <haoluo@xxxxxxxxxx> > Cc: Jiri Olsa <jolsa@xxxxxxxxxx> > --- > tools/bpf/bpftool/prog.c | 5 +++-- > 1 file changed, 3 insertions(+), 2 deletions(-) > > diff --git a/tools/bpf/bpftool/prog.c b/tools/bpf/bpftool/prog.c > index cfc9fdc1e863..08b352dd799e 100644 > --- a/tools/bpf/bpftool/prog.c > +++ b/tools/bpf/bpftool/prog.c > @@ -2056,6 +2056,7 @@ static int profile_parse_metrics(int argc, char **argv) > > static void profile_read_values(struct profiler_bpf *obj) > { > + __u32 possible_cpus = libbpf_num_possible_cpus(); > __u32 m, cpu, num_cpu = obj->rodata->num_cpu; > int reading_map_fd, count_map_fd; > __u64 counts[num_cpu]; > @@ -2080,7 +2081,7 @@ static void profile_read_values(struct profiler_bpf *obj) > profile_total_count += counts[cpu]; > > for (m = 0; m < ARRAY_SIZE(metrics); m++) { > - struct bpf_perf_event_value values[num_cpu]; > + struct bpf_perf_event_value values[possible_cpus]; > > if (!metrics[m].selected) > continue; > @@ -2321,7 +2322,7 @@ static int do_profile(int argc, char **argv) > if (num_metric <= 0) > goto out; > > - num_cpu = libbpf_num_possible_cpus(); > + num_cpu = libbpf_num_online_cpus(); > if (num_cpu <= 0) { > p_err("failed to identify number of CPUs"); > goto out; Thanks, but it doesn't seem to be enough to solve the issue. How did you test it? With your series applied locally, I'm trying the following (Intel x86_64, CPUs: 0..7): # echo 0 > /sys/devices/system/cpu/cpu2/online # ./bpftool prog profile id 1525 duration 1 cycles instructions Error: failed to create event cycles on cpu 2 It seems that we're still trying to open the perf events on the offline CPU in profile_open_perf_events(), because even though we try to use fewer of the possible CPUs we're still referencing them in order by their index. So it works if I only disabled the last CPUs instead (#7, then #6, ...), but to work with _any_ CPU disabled, we would need to retrieve the list of online CPUs.