libbpf: introduce new API libbpf_num_online_cpus
PATCH 1/2 libbpf: introduce new API libbpf_num_online_cpus
From: Tonghao Zhang <[email protected]>
Adding a new API libbpf_num_online_cpus() that helps user with fetching online CPUs number.
It's useful in system which number of online CPUs is different with possible CPUs.
--- tools/lib/bpf/libbpf.c | 47 ++++++++++++++++++++++++++++++---------- tools/lib/bpf/libbpf.h | 7 ++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 43 insertions(+), 12 deletions(-)
tools/lib/bpf/libbpf.c +35 -12
@@ -12192,30 +12192,53 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)return parse_cpu_mask_str (buf, mask, mask_sz); } -int libbpf_num_possible_cpus (void ) +static int num_cpus (const char *fcpu) { -static const char *fcpu ="/sys/devices/system/cpu/possible" ; -static int cpus; -int err, n, i, tmp_cpus; +int err, n, i, cpus;bool *mask; - tmp_cpus =READ_ONCE (cpus); -if (tmp_cpus >0 ) -return tmp_cpus; - err =parse_cpu_mask_file (fcpu, &mask, &n);if (err)return libbpf_err (err); - tmp_cpus =0 ; + cpus =0 ;for (i =0 ; i < n; i++) {if (mask[i]) - tmp_cpus++; + cpus++; }free (mask); -WRITE_ONCE (cpus, tmp_cpus); -return tmp_cpus; +return cpus; +} + +int libbpf_num_online_cpus (void ) +{ +static int online_cpus; +int cpus; + + cpus =READ_ONCE (online_cpus); +if (cpus >0 ) +return cpus; + + cpus =num_cpus ("/sys/devices/system/cpu/online" ); + +WRITE_ONCE (online_cpus, cpus); +return cpus; +} + +int libbpf_num_possible_cpus (void ) +{ +static int possible_cpus; +int cpus; + + cpus =READ_ONCE (possible_cpus); +if (cpus >0 ) +return cpus; + + cpus =num_cpus ("/sys/devices/system/cpu/possible" ); + +WRITE_ONCE (possible_cpus, cpus); +return cpus; } static int populate_skeleton_maps(const struct bpf_object *obj,
tools/lib/bpf/libbpf.h +7 -0
@@ -1332,6 +1332,13 @@ LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,*/ LIBBPF_API int libbpf_num_possible_cpus(void); +/** +* @brief **libbpf_num_online_cpus()** is a helper function to get the +* number of online CPUs that the host kernel supports and expects. +* @return number of online CPUs; or error code on failure +*/ +LIBBPF_API int libbpf_num_online_cpus(void); +struct bpf_map_skeleton {const char *name;struct bpf_map **map;
tools/lib/bpf/libbpf.map +1 -0
@@ -381,6 +381,7 @@ LIBBPF_1.1.0 { user_ring_buffer__reserve; user_ring_buffer__reserve_blocking; user_ring_buffer__submit; + libbpf_num_online_cpus; } LIBBPF_1.0.0; LIBBPF_1.2.0 {
Signature · 1 line
2.27.0
PATCH 2/2 bpftool: profile online CPUs instead of possible
From: Tonghao Zhang <[email protected]>
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.
$ 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.
--- tools/bpf/bpftool/prog.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/bpf/bpftool/prog.c +3 -2
@@ -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;
Signature · 1 line
2.27.0
Re: [bpf-next v1 2/2] bpftool: profile online CPUs instead of possible
2023-01-17 12:49 UTC+0800 ~ [email protected]
Quoted 5 lines
From: Tonghao Zhang <[email protected]>
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/ ?
Quoted 50 lines
$ dmidecode -s system-product-name PowerEdge R620 $ cat /sys/devices/system/cpu/online 0-31 $ cat /sys/devices/system/cpu/possible 0-47To fix this issue, use online cpu instead of possible, to create perf event and other resource.
Signed-off-by Tonghao Zhang <[email protected]>Cc Quentin Monnet <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Martin KaFai Lau <[email protected]>Cc Song Liu <[email protected]>Cc Yonghong Song <[email protected]>Cc John Fastabend <[email protected]>Cc KP Singh <[email protected]>Cc Stanislav Fomichev <[email protected]>Cc Hao Luo <[email protected]>Cc Jiri Olsa <[email protected]>--- tools/bpf/bpftool/prog.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)tools/bpf/bpftool/prog.c +2
-1@@ -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 ;
tools/bpf/bpftool/prog.c static int do_profile(int argc, char **argv) · line 2328
Quoted 8 lines
tools/bpf/bpftool/prog.c +1
-1@@ -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.
Re: [bpf-next v1 2/2] bpftool: profile online CPUs instead of possible
Quoted 76 lines · Quentin Monnet
2023-01-17 12:49 UTC+0800 ~ [email protected]
Quoted 5 lines
From: Tonghao Zhang <[email protected]>
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/ ?
Quoted 50 lines
$ dmidecode -s system-product-name PowerEdge R620 $ cat /sys/devices/system/cpu/online 0-31 $ cat /sys/devices/system/cpu/possible 0-47To fix this issue, use online cpu instead of possible, to create perf event and other resource.
Signed-off-by Tonghao Zhang <[email protected]>Cc Quentin Monnet <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Martin KaFai Lau <[email protected]>Cc Song Liu <[email protected]>Cc Yonghong Song <[email protected]>Cc John Fastabend <[email protected]>Cc KP Singh <[email protected]>Cc Stanislav Fomichev <[email protected]>Cc Hao Luo <[email protected]>Cc Jiri Olsa <[email protected]>---
tools/bpf/bpftool/prog.c| 5 +++--1 file changed, 3 insertions(+), 2 deletions(-)tools/bpf/bpftool/prog.c +2
-1@@ -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 ;tools/bpf/bpftool/prog.c static int do_profile(int argc, char **argv) · line 2328
Quoted 8 lines
tools/bpf/bpftool/prog.c +1
-1@@ -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):
Quoted 7 lines
# 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 2It 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
Hi
Thanks for your review and comment.
I don’t test the case that one cpu is offline which is not last CPU.
Quoted 4 lines
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.
Yes, In other way, to fix it, we can use the errno return by open_perf_event. If errno is ENODEV, we can skip this cpu to profile? The patch fix this issue.
tools/bpf/bpftool/common.c +18 -0
@@ -55,6 +55,24 @@ void p_err(const char *fmt, ...)va_end (ap); } +void p_warn (const char *fmt, ...) +{ +va_list ap; + +va_start (ap, fmt); +if (json_output) { +jsonw_start_object (json_wtr); +jsonw_name (json_wtr,"warning" ); +jsonw_vprintf_enquote (json_wtr, fmt, ap); +jsonw_end_object (json_wtr); + }else { +fprintf (stderr,"Warn: " ); +vfprintf (stderr, fmt, ap); +fprintf (stderr,"\n" ); + } +va_end (ap); +} +void p_info (const char *fmt, ...) {va_list ap;
tools/bpf/bpftool/main.h +1 -0
@@ -86,6 +86,7 @@ extern struct btf *base_btf;extern struct hashmap *refs_table; void __printf(1, 2) p_err(const char *fmt, ...); +void __printf(1, 2) p_warn(const char *fmt, ...); void __printf(1, 2) p_info(const char *fmt, ...); bool is_prefix(const char *pfx, const char *str);
tools/bpf/bpftool/prog.c +28 -8
@@ -2233,10 +2233,36 @@ static void profile_close_perf_events(struct profiler_bpf *obj) profile_perf_event_cnt =0 ; } +static int profile_open_perf_event (int mid,int cpu,int map_fd) +{ +int pmu_fd; + + pmu_fd =syscall (__NR_perf_event_open, &metrics[mid].attr, +-1 /*pid*/ , cpu,-1 /*group_fd*/ ,0 ); +if (pmu_fd <0 ) { +if (errno ==ENODEV ) { +p_warn ("cpu %d may be offline, skip %s metric profiling." , + cpu, metrics[mid].name); + profile_perf_event_cnt++; +return 0 ; + } +return -1 ; + } + +if (bpf_map_update_elem (map_fd, + &profile_perf_event_cnt, + &pmu_fd,BPF_ANY ) || +ioctl (pmu_fd,PERF_EVENT_IOC_ENABLE ,0 )) +return -1 ; + + profile_perf_events[profile_perf_event_cnt++] = pmu_fd; +return 0 ; +} +static int profile_open_perf_events (struct profiler_bpf *obj) {unsigned int cpu, m; -int map_fd, pmu_fd; +int map_fd; profile_perf_events =calloc (sizeof (int ), obj->rodata->num_cpu * obj->rodata->num_metric);
@@ -2255,17 +2281,11 @@ static int profile_open_perf_events(struct profiler_bpf *obj)if (!metrics[m].selected)continue ;for (cpu =0 ; cpu < obj->rodata->num_cpu; cpu++) { - pmu_fd =syscall (__NR_perf_event_open, &metrics[m].attr, --1 /*pid*/ , cpu,-1 /*group_fd*/ ,0 ); -if (pmu_fd <0 || -bpf_map_update_elem (map_fd, &profile_perf_event_cnt, - &pmu_fd,BPF_ANY ) || -ioctl (pmu_fd,PERF_EVENT_IOC_ENABLE ,0 )) { +if (profile_open_perf_event (m, cpu, map_fd)) {p_err ("failed to create event %s on cpu %d" , metrics[m].name, cpu);return -1 ; } - profile_perf_events[profile_perf_event_cnt++] = pmu_fd; } }return 0 ;
----
Best Regards, Tonghao <[email protected]>
Re: [bpf-next v1 2/2] bpftool: profile online CPUs instead of possible
2023-01-19 16:22 UTC+0800 ~ Tonghao Zhang <[email protected]>
Quoted 140 lines
Quoted 74 lines · Quentin Monnet
2023-01-17 12:49 UTC+0800 ~ [email protected]
Quoted 5 lines
From: Tonghao Zhang <[email protected]>
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/ ?
Quoted 50 lines
$ dmidecode -s system-product-name PowerEdge R620 $ cat /sys/devices/system/cpu/online 0-31 $ cat /sys/devices/system/cpu/possible 0-47To fix this issue, use online cpu instead of possible, to create perf event and other resource.
Signed-off-by Tonghao Zhang <[email protected]>Cc Quentin Monnet <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Martin KaFai Lau <[email protected]>Cc Song Liu <[email protected]>Cc Yonghong Song <[email protected]>Cc John Fastabend <[email protected]>Cc KP Singh <[email protected]>Cc Stanislav Fomichev <[email protected]>Cc Hao Luo <[email protected]>Cc Jiri Olsa <[email protected]>---
tools/bpf/bpftool/prog.c| 5 +++--1 file changed, 3 insertions(+), 2 deletions(-)tools/bpf/bpftool/prog.c +2
-1@@ -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 ;tools/bpf/bpftool/prog.c static int do_profile(int argc, char **argv) · line 2328
Quoted 8 lines
tools/bpf/bpftool/prog.c +1
-1@@ -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):Quoted 7 lines
# 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 2It 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 byHi
Thanks for your review and comment.
I don’t test the case that one cpu is offline which is not last CPU.Quoted 4 lines
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.
Yes, In other way, to fix it, we can use the errno return by
open_perf_event. If errno is ENODEV, we can skip this cpu to profile? The patch fix this issue.tools/bpf/bpftool/common.c +18
-0@@ -55,6 +55,24 @@ void p_err(const char *fmt, ...)va_end (ap); } +void p_warn (const char *fmt, ...) +{ +va_list ap; + +va_start (ap, fmt); +if (json_output) { +jsonw_start_object (json_wtr); +jsonw_name (json_wtr,"warning" ); +jsonw_vprintf_enquote (json_wtr, fmt, ap); +jsonw_end_object (json_wtr); + }else { +fprintf (stderr,"Warn: " ); +vfprintf (stderr, fmt, ap); +fprintf (stderr,"\n" ); + } +va_end (ap); +} +void p_info (const char *fmt, ...) {va_list ap;tools/bpf/bpftool/main.h +1
-0@@ -86,6 +86,7 @@ extern struct btf *base_btf;extern struct hashmap *refs_table; void __printf(1, 2) p_err(const char *fmt, ...); +void __printf(1, 2) p_warn(const char *fmt, ...); void __printf(1, 2) p_info(const char *fmt, ...); bool is_prefix(const char *pfx, const char *str);tools/bpf/bpftool/prog.c +0
-0@@ -2233,10 +2233,36 @@ static void profile_close_perf_events(struct profiler_bpf *obj) profile_perf_event_cnt =0 ; }
tools/bpf/bpftool/prog.c static void profile_close_perf_events(struct profiler_bpf *obj) · line 2245
Quoted 10 lines
tools/bpf/bpftool/prog.c +10
-0+static int profile_open_perf_event (int mid,int cpu,int map_fd) +{ +int pmu_fd; + + pmu_fd =syscall (__NR_perf_event_open, &metrics[mid].attr, +-1 /*pid*/ , cpu,-1 /*group_fd*/ ,0 ); +if (pmu_fd <0 ) { +if (errno ==ENODEV ) { +p_warn ("cpu %d may be offline, skip %s metric profiling." , + cpu, metrics[mid].name);
Nit: I think it's fine to keep this at the info level (p_info()).
Quoted 46 lines
tools/bpf/bpftool/prog.c +18
-9+ profile_perf_event_cnt++; +return 0 ; + } +return -1 ; + } + +if (bpf_map_update_elem (map_fd, + &profile_perf_event_cnt, + &pmu_fd,BPF_ANY ) || +ioctl (pmu_fd,PERF_EVENT_IOC_ENABLE ,0 )) +return -1 ; + + profile_perf_events[profile_perf_event_cnt++] = pmu_fd; +return 0 ; +} +static int profile_open_perf_events (struct profiler_bpf *obj) {unsigned int cpu, m; -int map_fd, pmu_fd; +int map_fd; profile_perf_events =calloc (sizeof (int ), obj->rodata->num_cpu * obj->rodata->num_metric);@@ -2255,17 +2281,11 @@ static int profile_open_perf_events(struct profiler_bpf *obj)if (!metrics[m].selected)continue ;for (cpu =0 ; cpu < obj->rodata->num_cpu; cpu++) { - pmu_fd =syscall (__NR_perf_event_open, &metrics[m].attr, --1 /*pid*/ , cpu,-1 /*group_fd*/ ,0 ); -if (pmu_fd <0 || -bpf_map_update_elem (map_fd, &profile_perf_event_cnt, - &pmu_fd,BPF_ANY ) || -ioctl (pmu_fd,PERF_EVENT_IOC_ENABLE ,0 )) { +if (profile_open_perf_event (m, cpu, map_fd)) {p_err ("failed to create event %s on cpu %d" , metrics[m].name, cpu);return -1 ; } - profile_perf_events[profile_perf_event_cnt++] = pmu_fd; } }return 0 ; ----
Quoted 2 lines
Best Regards, Tonghao <[email protected]>
I haven't tested this patch, but yes, looks like it should address the issue. Could you submit a proper v2, please?
Quentin
Re: [bpf-next v1 2/2] bpftool: profile online CPUs instead of possible
Quoted 145 lines · Quentin Monnet
2023-01-19 16:22 UTC+0800 ~ Tonghao Zhang <[email protected]>
Quoted 140 lines
Quoted 74 lines · Quentin Monnet
2023-01-17 12:49 UTC+0800 ~ [email protected]
Quoted 5 lines
From: Tonghao Zhang <[email protected]>
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/ ?
Quoted 50 lines
$ dmidecode -s system-product-name PowerEdge R620 $ cat /sys/devices/system/cpu/online 0-31 $ cat /sys/devices/system/cpu/possible 0-47To fix this issue, use online cpu instead of possible, to create perf event and other resource.
Signed-off-by Tonghao Zhang <[email protected]>Cc Quentin Monnet <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Martin KaFai Lau <[email protected]>Cc Song Liu <[email protected]>Cc Yonghong Song <[email protected]>Cc John Fastabend <[email protected]>Cc KP Singh <[email protected]>Cc Stanislav Fomichev <[email protected]>Cc Hao Luo <[email protected]>Cc Jiri Olsa <[email protected]>---
tools/bpf/bpftool/prog.c| 5 +++--1 file changed, 3 insertions(+), 2 deletions(-)tools/bpf/bpftool/prog.c +2
-1@@ -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 ;tools/bpf/bpftool/prog.c static int do_profile(int argc, char **argv) · line 2328
Quoted 8 lines
tools/bpf/bpftool/prog.c +1
-1@@ -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):Quoted 7 lines
# 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 2It 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 byHi
Thanks for your review and comment.
I don’t test the case that one cpu is offline which is not last CPU.Quoted 4 lines
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.
Yes, In other way, to fix it, we can use the errno return by
open_perf_event. If errno is ENODEV, we can skip this cpu to profile? The patch fix this issue.tools/bpf/bpftool/common.c +18
-0@@ -55,6 +55,24 @@ void p_err(const char *fmt, ...)va_end (ap); } +void p_warn (const char *fmt, ...) +{ +va_list ap; + +va_start (ap, fmt); +if (json_output) { +jsonw_start_object (json_wtr); +jsonw_name (json_wtr,"warning" ); +jsonw_vprintf_enquote (json_wtr, fmt, ap); +jsonw_end_object (json_wtr); + }else { +fprintf (stderr,"Warn: " ); +vfprintf (stderr, fmt, ap); +fprintf (stderr,"\n" ); + } +va_end (ap); +} +void p_info (const char *fmt, ...) {va_list ap;tools/bpf/bpftool/main.h +1
-0@@ -86,6 +86,7 @@ extern struct btf *base_btf;extern struct hashmap *refs_table; void __printf(1, 2) p_err(const char *fmt, ...); +void __printf(1, 2) p_warn(const char *fmt, ...); void __printf(1, 2) p_info(const char *fmt, ...); bool is_prefix(const char *pfx, const char *str);tools/bpf/bpftool/prog.c +0
-0@@ -2233,10 +2233,36 @@ static void profile_close_perf_events(struct profiler_bpf *obj) profile_perf_event_cnt =0 ; }
Quoted 12 lines
tools/bpf/bpftool/prog.c static void profile_close_perf_events(struct profiler_bpf *obj) · line 2245
Quoted 10 lines
tools/bpf/bpftool/prog.c +10
-0+static int profile_open_perf_event (int mid,int cpu,int map_fd) +{ +int pmu_fd; + + pmu_fd =syscall (__NR_perf_event_open, &metrics[mid].attr, +-1 /*pid*/ , cpu,-1 /*group_fd*/ ,0 ); +if (pmu_fd <0 ) { +if (errno ==ENODEV ) { +p_warn ("cpu %d may be offline, skip %s metric profiling." , + cpu, metrics[mid].name);Nit: I think it's fine to keep this at the info level (
p_info()).
Ok
Quoted 45 lines
Quoted 45 lines
tools/bpf/bpftool/prog.c +18
-8+ profile_perf_event_cnt++; +return 0 ; + } +return -1 ; + } + +if (bpf_map_update_elem (map_fd, + &profile_perf_event_cnt, + &pmu_fd,BPF_ANY ) || +ioctl (pmu_fd,PERF_EVENT_IOC_ENABLE ,0 )) +return -1 ; + + profile_perf_events[profile_perf_event_cnt++] = pmu_fd; +return 0 ; +} +static int profile_open_perf_events (struct profiler_bpf *obj) {unsigned int cpu, m; -int map_fd, pmu_fd; +int map_fd; profile_perf_events =calloc (sizeof (int ), obj->rodata->num_cpu * obj->rodata->num_metric);@@ -2255,17 +2281,11 @@ static int profile_open_perf_events(struct profiler_bpf *obj)if (!metrics[m].selected)continue ;for (cpu =0 ; cpu < obj->rodata->num_cpu; cpu++) { - pmu_fd =syscall (__NR_perf_event_open, &metrics[m].attr, --1 /*pid*/ , cpu,-1 /*group_fd*/ ,0 ); -if (pmu_fd <0 || -bpf_map_update_elem (map_fd, &profile_perf_event_cnt, - &pmu_fd,BPF_ANY ) || -ioctl (pmu_fd,PERF_EVENT_IOC_ENABLE ,0 )) { +if (profile_open_perf_event (m, cpu, map_fd)) {p_err ("failed to create event %s on cpu %d" , metrics[m].name, cpu);return -1 ; } - profile_perf_events[profile_perf_event_cnt++] = pmu_fd; } }return 0 ;
Quoted 6 lines
Quoted 3 lines
----
Best Regards, Tonghao <[email protected]>I haven't tested this patch, but yes, looks like it should address the issue. Could you submit a proper v2, please?
Yes, v2 will be sent soon. I’m on vacation. So sorry reply you late.
Quoted 1 line
Quentin
----
Best Regards, Tonghao <[email protected]>
2023-01-17 12:49 UTC+0800 ~ [email protected]
Quoted 64 lines
From: Tonghao Zhang <[email protected]>
Adding a new API
libbpf_num_online_cpus()that helps user with fetching online CPUs number.It's useful in system which number of online CPUs is different with possible CPUs.
Signed-off-by Tonghao Zhang <[email protected]>Cc Quentin Monnet <[email protected]>Cc Alexei Starovoitov <[email protected]>Cc Daniel Borkmann <[email protected]>Cc Andrii Nakryiko <[email protected]>Cc Martin KaFai Lau <[email protected]>Cc Song Liu <[email protected]>Cc Yonghong Song <[email protected]>Cc John Fastabend <[email protected]>Cc KP Singh <[email protected]>Cc Stanislav Fomichev <[email protected]>Cc Hao Luo <[email protected]>Cc Jiri Olsa <[email protected]>--- tools/lib/bpf/libbpf.c | 47 ++++++++++++++++++++++++++++++---------- tools/lib/bpf/libbpf.h | 7 ++++++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 43 insertions(+), 12 deletions(-)tools/lib/bpf/libbpf.c +5
-12@@ -12192,30 +12192,53 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)return parse_cpu_mask_str (buf, mask, mask_sz); } -int libbpf_num_possible_cpus (void ) +static int num_cpus (const char *fcpu) { -static const char *fcpu ="/sys/devices/system/cpu/possible" ; -static int cpus; -int err, n, i, tmp_cpus; +int err, n, i, cpus;bool *mask; - tmp_cpus =READ_ONCE (cpus); -if (tmp_cpus >0 ) -return tmp_cpus; - err =parse_cpu_mask_file (fcpu, &mask, &n);if (err)return libbpf_err (err); - tmp_cpus =0 ; + cpus =0 ;for (i =0 ; i < n; i++) {if (mask[i]) - tmp_cpus++; + cpus++; }free (mask); -WRITE_ONCE (cpus, tmp_cpus); -return tmp_cpus; +return cpus;
tools/lib/bpf/libbpf.c int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz) · line 12221
Quoted 10 lines
tools/lib/bpf/libbpf.c +10
-0+} + +int libbpf_num_online_cpus (void ) +{ +static int online_cpus; +int cpus; + + cpus =READ_ONCE (online_cpus); +if (cpus >0 ) +return cpus;
The number of online CPUs can change over time, I don't think you can READ_ONCE()/WRITE_ONCE().
Quoted 46 lines
tools/lib/bpf/libbpf.c +20
-0+ + cpus =num_cpus ("/sys/devices/system/cpu/online" ); + +WRITE_ONCE (online_cpus, cpus); +return cpus; +} + +int libbpf_num_possible_cpus (void ) +{ +static int possible_cpus; +int cpus; + + cpus =READ_ONCE (possible_cpus); +if (cpus >0 ) +return cpus; + + cpus =num_cpus ("/sys/devices/system/cpu/possible" ); + +WRITE_ONCE (possible_cpus, cpus); +return cpus; } static int populate_skeleton_maps(const struct bpf_object *obj,tools/lib/bpf/libbpf.h +7
-0@@ -1332,6 +1332,13 @@ LIBBPF_API int libbpf_probe_bpf_helper(enum bpf_prog_type prog_type,*/ LIBBPF_API int libbpf_num_possible_cpus(void); +/** +* @brief **libbpf_num_online_cpus()** is a helper function to get the +* number of online CPUs that the host kernel supports and expects. +* @return number of online CPUs; or error code on failure +*/ +LIBBPF_API int libbpf_num_online_cpus(void); +struct bpf_map_skeleton {const char *name;struct bpf_map **map;tools/lib/bpf/libbpf.map +0
-0
tools/lib/bpf/libbpf.map LIBBPF_1.1.0 { · line 385
Quoted 5 lines
tools/lib/bpf/libbpf.map +1
-0@@ -381,6 +381,7 @@ LIBBPF_1.1.0 { user_ring_buffer__reserve; user_ring_buffer__reserve_blocking; user_ring_buffer__submit; + libbpf_num_online_cpus; } LIBBPF_1.0.0;
Libbpf v1.1.0 has shipped, so this would now go to the 1.2.0 block below.
tools/lib/bpf/libbpf.map LIBBPF_1.1.0 { · line 387
Quoted 2 lines
tools/lib/bpf/libbpf.map +0
-0LIBBPF_1.2.0 {
Thanks, but I'm not sure retrieving the number of online CPUs, without the list of those in use, is enough for fixing bpftool profiling feature. Please see my answer on the second patch.
Quentin