* Re: [PATCH 0/2] bpf: Add a new API libbpf_num_possible_cpus()
[not found] <20190605231506.2983988-1-hechaol@fb.com>
@ 2019-06-06 5:39 ` Song Liu
[not found] ` <20190605231506.2983988-2-hechaol@fb.com>
[not found] ` <20190605231506.2983988-3-hechaol@fb.com>
2 siblings, 0 replies; 5+ messages in thread
From: Song Liu @ 2019-06-06 5:39 UTC (permalink / raw)
To: Hechao Li; +Cc: bpf, ast, daniel, Kernel Team
> On Jun 5, 2019, at 4:15 PM, Hechao Li <hechaol@fb.com> wrote:
>
> Getting number of possible CPUs is commonly used for per-CPU BPF maps
> and perf_event_maps. Add a new API libbpf_num_possible_cpus() that
> helps user with per-CPU related operations and remove duplicate
> implementations in bpftool and selftests.
>
> Hechao Li (2):
> bpf: add a new API libbpf_num_possible_cpus()
> bpf: use libbpf_num_possible_cpus in bpftool and selftests
>
> tools/bpf/bpftool/common.c | 53 +++-----------------------
> tools/lib/bpf/libbpf.c | 49 ++++++++++++++++++++++++
> tools/lib/bpf/libbpf.h | 16 ++++++++
> tools/lib/bpf/libbpf.map | 1 +
> tools/testing/selftests/bpf/bpf_util.h | 37 +++---------------
> 5 files changed, 76 insertions(+), 80 deletions(-)
>
> --
> 2.17.1
>
Please specify which tree the patch is based on (bpf-next for
this one), and version of patch (v2) in the subject with
git format-patch --subject-prefix="PATCH v2 bpf-next"
Thanks,
Song
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] bpf: add a new API libbpf_num_possible_cpus()
[not found] ` <20190605231506.2983988-2-hechaol@fb.com>
@ 2019-06-06 5:44 ` Song Liu
2019-06-06 16:23 ` Andrii Nakryiko
2019-06-06 16:34 ` Hechao Li
0 siblings, 2 replies; 5+ messages in thread
From: Song Liu @ 2019-06-06 5:44 UTC (permalink / raw)
To: Hechao Li; +Cc: bpf, ast, daniel, Kernel Team
> On Jun 5, 2019, at 4:15 PM, Hechao Li <hechaol@fb.com> wrote:
>
> Adding a new API libbpf_num_possible_cpus() that helps user with
> per-CPU map operations.
>
> Signed-off-by: Hechao Li <hechaol@fb.com>
> ---
> tools/lib/bpf/libbpf.c | 49 ++++++++++++++++++++++++++++++++++++++++
> tools/lib/bpf/libbpf.h | 16 +++++++++++++
> tools/lib/bpf/libbpf.map | 1 +
> 3 files changed, 66 insertions(+)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index ba89d9727137..580b14307237 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -3827,3 +3827,52 @@ void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
> desc->array_offset, addr);
> }
> }
> +
> +int libbpf_num_possible_cpus(void)
> +{
> + static const char *fcpu = "/sys/devices/system/cpu/possible";
> + static int cpus;
> + char buf[128];
> + int len = 0, n = 0, il = 0, ir = 0;
> + int fd = -1;
> + unsigned int start = 0, end = 0;
> +
> + if (cpus > 0)
> + return cpus;
> +
> + fd = open(fcpu, O_RDONLY);
> + if (fd < 0) {
> + pr_warning("Failed to open file %s\n", fcpu);
> + return -errno;
> + }
> + len = read(fd, buf, sizeof(buf));
> + close(fd);
> + if (len <= 0) {
> + pr_warning("Failed to read number of possible cpus from %s\n",
> + fcpu);
> + return -errno;
> + }
> + if (len == sizeof(buf)) {
> + pr_warning("File: %s size overflow\n", fcpu);
> + return -EOVERFLOW;
> + }
> + buf[len] = '\0';
> +
> + for (ir = 0, cpus = 0; ir <= len; ir++) {
> + /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
> + if (buf[ir] == ',' || buf[ir] == '\0') {
> + buf[ir] = '\0';
> + n = sscanf(&buf[il], "%u-%u", &start, &end);
> + if (n <= 0) {
> + pr_warning("Failed to get # CPUs from %s\n",
> + &buf[il]);
> + return -EINVAL;
> + } else if (n == 1) {
> + end = start;
> + }
> + cpus += end - start + 1;
> + il = ir + 1;
> + }
> + }
> + return cpus;
> +}
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index 1af0d48178c8..f5e82eb2e5d4 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -454,6 +454,22 @@ bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
> LIBBPF_API void
> bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
>
> +/*
> + * A helper function to get the number of possible CPUs before looking up
> + * per-CPU maps. Negative errno is returned on failure.
> + *
> + * Example usage:
> + *
> + * int ncpus = libbpf_num_possible_cpus();
> + * if (ncpus <= 0) {
> + * // error handling
> + * }
> + * long values[ncpus];
> + * bpf_map_lookup_elem(per_cpu_map_fd, key, values);
> + *
> + */
> +LIBBPF_API int libbpf_num_possible_cpus(void);
> +
> #ifdef __cplusplus
> } /* extern "C" */
> #endif
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index 46dcda89df21..7f76d19cb02b 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -172,4 +172,5 @@ LIBBPF_0.0.4 {
> btf_dump__new;
> btf__parse_elf;
> bpf_object__load_xattr;
> + libbpf_num_possible_cpus;
Please indent with tab instead of space.
Thanks,
Song
> } LIBBPF_0.0.3;
> --
> 2.17.1
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 2/2] bpf: use libbpf_num_possible_cpus in bpftool and selftests
[not found] ` <20190605231506.2983988-3-hechaol@fb.com>
@ 2019-06-06 5:51 ` Song Liu
0 siblings, 0 replies; 5+ messages in thread
From: Song Liu @ 2019-06-06 5:51 UTC (permalink / raw)
To: Hechao Li; +Cc: bpf, ast, daniel, Kernel Team
> On Jun 5, 2019, at 4:15 PM, Hechao Li <hechaol@fb.com> wrote:
>
> Use the newly added bpf_num_possible_cpus() in bpftool and selftests
> and remove duplicate implementations.
>
> Signed-off-by: Hechao Li <hechaol@fb.com>
> ---
> tools/bpf/bpftool/common.c | 53 +++-----------------------
> tools/testing/selftests/bpf/bpf_util.h | 37 +++---------------
> 2 files changed, 10 insertions(+), 80 deletions(-)
>
> diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c
> index f7261fad45c1..0b1c56758cd9 100644
> --- a/tools/bpf/bpftool/common.c
> +++ b/tools/bpf/bpftool/common.c
> @@ -21,6 +21,7 @@
> #include <sys/vfs.h>
>
> #include <bpf.h>
> +#include <libbpf.h> /* libbpf_num_possible_cpus */
>
> #include "main.h"
>
> @@ -439,57 +440,13 @@ unsigned int get_page_size(void)
>
> unsigned int get_possible_cpus(void)
> {
> - static unsigned int result;
> - char buf[128];
> - long int n;
> - char *ptr;
> - int fd;
> -
> - if (result)
> - return result;
> -
> - fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
> - if (fd < 0) {
> - p_err("can't open sysfs possible cpus");
> - exit(-1);
> - }
> -
> - n = read(fd, buf, sizeof(buf));
> - if (n < 2) {
> - p_err("can't read sysfs possible cpus");
> - exit(-1);
> - }
> - close(fd);
> + int cpus = libbpf_num_possible_cpus();
>
> - if (n == sizeof(buf)) {
> - p_err("read sysfs possible cpus overflow");
> + if (cpus <= 0) {
> + p_err("can't get # of possible cpus");
> exit(-1);
> }
> -
> - ptr = buf;
> - n = 0;
> - while (*ptr && *ptr != '\n') {
> - unsigned int a, b;
> -
> - if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
> - n += b - a + 1;
> -
> - ptr = strchr(ptr, '-') + 1;
> - } else if (sscanf(ptr, "%u", &a) == 1) {
> - n++;
> - } else {
> - assert(0);
> - }
> -
> - while (isdigit(*ptr))
> - ptr++;
> - if (*ptr == ',')
> - ptr++;
> - }
> -
> - result = n;
> -
> - return result;
> + return cpus;
> }
>
> static char *
> diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h
> index a29206ebbd13..9ad9c7595f93 100644
> --- a/tools/testing/selftests/bpf/bpf_util.h
> +++ b/tools/testing/selftests/bpf/bpf_util.h
> @@ -6,44 +6,17 @@
> #include <stdlib.h>
> #include <string.h>
> #include <errno.h>
> +#include <libbpf.h>
>
> static inline unsigned int bpf_num_possible_cpus(void)
> {
> - static const char *fcpu = "/sys/devices/system/cpu/possible";
> - unsigned int start, end, possible_cpus = 0;
> - char buff[128];
> - FILE *fp;
> - int len, n, i, j = 0;
> + int possible_cpus = libbpf_num_possible_cpus();
>
> - fp = fopen(fcpu, "r");
> - if (!fp) {
> - printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
> + if (possible_cpus <= 0) {
> + printf("Failed to get # of possible cpus: '%s'!\n",
> + strerror(-possible_cpus));
This is not correct. The -possible_cpus is not errno, so we cannot
use it with strerror().
I guess we can just go with
printf("Failed to get # of possible cpus!\n");
Thanks,
Song
> exit(1);
> }
> -
> - if (!fgets(buff, sizeof(buff), fp)) {
> - printf("Failed to read %s!\n", fcpu);
> - exit(1);
> - }
> -
> - len = strlen(buff);
> - for (i = 0; i <= len; i++) {
> - if (buff[i] == ',' || buff[i] == '\0') {
> - buff[i] = '\0';
> - n = sscanf(&buff[j], "%u-%u", &start, &end);
> - if (n <= 0) {
> - printf("Failed to retrieve # possible CPUs!\n");
> - exit(1);
> - } else if (n == 1) {
> - end = start;
> - }
> - possible_cpus += end - start + 1;
> - j = i + 1;
> - }
> - }
> -
> - fclose(fp);
> -
> return possible_cpus;
> }
>
> --
> 2.17.1
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] bpf: add a new API libbpf_num_possible_cpus()
2019-06-06 5:44 ` [PATCH 1/2] bpf: add " Song Liu
@ 2019-06-06 16:23 ` Andrii Nakryiko
2019-06-06 16:34 ` Hechao Li
1 sibling, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2019-06-06 16:23 UTC (permalink / raw)
To: Song Liu; +Cc: Hechao Li, bpf, ast, daniel, Kernel Team
On Wed, Jun 5, 2019 at 10:46 PM Song Liu <songliubraving@fb.com> wrote:
>
>
>
> > On Jun 5, 2019, at 4:15 PM, Hechao Li <hechaol@fb.com> wrote:
> >
> > Adding a new API libbpf_num_possible_cpus() that helps user with
> > per-CPU map operations.
> >
> > Signed-off-by: Hechao Li <hechaol@fb.com>
Your patches didn't get into mailing list. I had this problem before,
for me the issue was fixed by trimming the CC list to a minimum, maybe
try that? You should also probably include netdev@vger.kernel.org,
btw.
> > ---
> > tools/lib/bpf/libbpf.c | 49 ++++++++++++++++++++++++++++++++++++++++
> > tools/lib/bpf/libbpf.h | 16 +++++++++++++
> > tools/lib/bpf/libbpf.map | 1 +
> > 3 files changed, 66 insertions(+)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index ba89d9727137..580b14307237 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -3827,3 +3827,52 @@ void bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear)
> > desc->array_offset, addr);
> > }
> > }
> > +
> > +int libbpf_num_possible_cpus(void)
> > +{
> > + static const char *fcpu = "/sys/devices/system/cpu/possible";
> > + static int cpus;
> > + char buf[128];
> > + int len = 0, n = 0, il = 0, ir = 0;
> > + int fd = -1;
> > + unsigned int start = 0, end = 0;
Please sort from longest line length to shortest (so-called "reverse
Christmas tree").
> > +
> > + if (cpus > 0)
> > + return cpus;
> > +
> > + fd = open(fcpu, O_RDONLY);
> > + if (fd < 0) {
> > + pr_warning("Failed to open file %s\n", fcpu);
> > + return -errno;
pr_warning can call into user-specified callback function, which can
perform some actions that will clobber errno variable. So you should
save errno before pr_warning. Also, it's useful to include errno in
error message itself.
> > + }
> > + len = read(fd, buf, sizeof(buf));
> > + close(fd);
> > + if (len <= 0) {
> > + pr_warning("Failed to read number of possible cpus from %s\n",
> > + fcpu);
> > + return -errno;
Same about errno saving.
> > + }
> > + if (len == sizeof(buf)) {
> > + pr_warning("File: %s size overflow\n", fcpu);
> > + return -EOVERFLOW;
> > + }
> > + buf[len] = '\0';
> > +
> > + for (ir = 0, cpus = 0; ir <= len; ir++) {
> > + /* Each sub string separated by ',' has format \d+-\d+ or \d+ */
> > + if (buf[ir] == ',' || buf[ir] == '\0') {
> > + buf[ir] = '\0';
> > + n = sscanf(&buf[il], "%u-%u", &start, &end);
> > + if (n <= 0) {
> > + pr_warning("Failed to get # CPUs from %s\n",
> > + &buf[il]);
> > + return -EINVAL;
> > + } else if (n == 1) {
> > + end = start;
> > + }
> > + cpus += end - start + 1;
> > + il = ir + 1;
> > + }
> > + }
> > + return cpus;
> > +}
> > diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> > index 1af0d48178c8..f5e82eb2e5d4 100644
> > --- a/tools/lib/bpf/libbpf.h
> > +++ b/tools/lib/bpf/libbpf.h
> > @@ -454,6 +454,22 @@ bpf_program__bpil_addr_to_offs(struct bpf_prog_info_linear *info_linear);
> > LIBBPF_API void
> > bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
> >
> > +/*
> > + * A helper function to get the number of possible CPUs before looking up
> > + * per-CPU maps. Negative errno is returned on failure.
> > + *
> > + * Example usage:
> > + *
> > + * int ncpus = libbpf_num_possible_cpus();
> > + * if (ncpus <= 0) {
> > + * // error handling
> > + * }
> > + * long values[ncpus];
> > + * bpf_map_lookup_elem(per_cpu_map_fd, key, values);
> > + *
> > + */
> > +LIBBPF_API int libbpf_num_possible_cpus(void);
> > +
> > #ifdef __cplusplus
> > } /* extern "C" */
> > #endif
> > diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> > index 46dcda89df21..7f76d19cb02b 100644
> > --- a/tools/lib/bpf/libbpf.map
> > +++ b/tools/lib/bpf/libbpf.map
> > @@ -172,4 +172,5 @@ LIBBPF_0.0.4 {
> > btf_dump__new;
> > btf__parse_elf;
> > bpf_object__load_xattr;
> > + libbpf_num_possible_cpus;
>
> Please indent with tab instead of space.
>
> Thanks,
> Song
>
> > } LIBBPF_0.0.3;
> > --
> > 2.17.1
> >
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/2] bpf: add a new API libbpf_num_possible_cpus()
2019-06-06 5:44 ` [PATCH 1/2] bpf: add " Song Liu
2019-06-06 16:23 ` Andrii Nakryiko
@ 2019-06-06 16:34 ` Hechao Li
1 sibling, 0 replies; 5+ messages in thread
From: Hechao Li @ 2019-06-06 16:34 UTC (permalink / raw)
To: Song Liu; +Cc: bpf, ast, daniel, Kernel Team
Song Liu <songliubraving@fb.com> wrote on Wed [2019-Jun-05 22:51:24 -0700]:
>
>
> > On Jun 5, 2019, at 4:15 PM, Hechao Li <hechaol@fb.com> wrote:
> >
> > Use the newly added bpf_num_possible_cpus() in bpftool and selftests
> > and remove duplicate implementations.
> >
> > Signed-off-by: Hechao Li <hechaol@fb.com>
> > ---
> > tools/bpf/bpftool/common.c | 53 +++-----------------------
> > tools/testing/selftests/bpf/bpf_util.h | 37 +++---------------
> > 2 files changed, 10 insertions(+), 80 deletions(-)
> >
> > diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c
> > index f7261fad45c1..0b1c56758cd9 100644
> > --- a/tools/bpf/bpftool/common.c
> > +++ b/tools/bpf/bpftool/common.c
> > @@ -21,6 +21,7 @@
> > #include <sys/vfs.h>
> >
> > #include <bpf.h>
> > +#include <libbpf.h> /* libbpf_num_possible_cpus */
> >
> > #include "main.h"
> >
> > @@ -439,57 +440,13 @@ unsigned int get_page_size(void)
> >
> > unsigned int get_possible_cpus(void)
> > {
> > - static unsigned int result;
> > - char buf[128];
> > - long int n;
> > - char *ptr;
> > - int fd;
> > -
> > - if (result)
> > - return result;
> > -
> > - fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
> > - if (fd < 0) {
> > - p_err("can't open sysfs possible cpus");
> > - exit(-1);
> > - }
> > -
> > - n = read(fd, buf, sizeof(buf));
> > - if (n < 2) {
> > - p_err("can't read sysfs possible cpus");
> > - exit(-1);
> > - }
> > - close(fd);
> > + int cpus = libbpf_num_possible_cpus();
> >
> > - if (n == sizeof(buf)) {
> > - p_err("read sysfs possible cpus overflow");
> > + if (cpus <= 0) {
> > + p_err("can't get # of possible cpus");
> > exit(-1);
> > }
> > -
> > - ptr = buf;
> > - n = 0;
> > - while (*ptr && *ptr != '\n') {
> > - unsigned int a, b;
> > -
> > - if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
> > - n += b - a + 1;
> > -
> > - ptr = strchr(ptr, '-') + 1;
> > - } else if (sscanf(ptr, "%u", &a) == 1) {
> > - n++;
> > - } else {
> > - assert(0);
> > - }
> > -
> > - while (isdigit(*ptr))
> > - ptr++;
> > - if (*ptr == ',')
> > - ptr++;
> > - }
> > -
> > - result = n;
> > -
> > - return result;
> > + return cpus;
> > }
> >
> > static char *
> > diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h
> > index a29206ebbd13..9ad9c7595f93 100644
> > --- a/tools/testing/selftests/bpf/bpf_util.h
> > +++ b/tools/testing/selftests/bpf/bpf_util.h
> > @@ -6,44 +6,17 @@
> > #include <stdlib.h>
> > #include <string.h>
> > #include <errno.h>
> > +#include <libbpf.h>
> >
> > static inline unsigned int bpf_num_possible_cpus(void)
> > {
> > - static const char *fcpu = "/sys/devices/system/cpu/possible";
> > - unsigned int start, end, possible_cpus = 0;
> > - char buff[128];
> > - FILE *fp;
> > - int len, n, i, j = 0;
> > + int possible_cpus = libbpf_num_possible_cpus();
> >
> > - fp = fopen(fcpu, "r");
> > - if (!fp) {
> > - printf("Failed to open %s: '%s'!\n", fcpu, strerror(errno));
> > + if (possible_cpus <= 0) {
> > + printf("Failed to get # of possible cpus: '%s'!\n",
> > + strerror(-possible_cpus));
>
> This is not correct. The -possible_cpus is not errno, so we cannot
> use it with strerror().
>
> I guess we can just go with
>
> printf("Failed to get # of possible cpus!\n");
>
> Thanks,
> Song
I think it works given that libbpf_num_possible_cpus() returns -errno
on error, doesn't it?
Thanks,
Hechao
>
> > exit(1);
> > }
> > -
> > - if (!fgets(buff, sizeof(buff), fp)) {
> > - printf("Failed to read %s!\n", fcpu);
> > - exit(1);
> > - }
> > -
> > - len = strlen(buff);
> > - for (i = 0; i <= len; i++) {
> > - if (buff[i] == ',' || buff[i] == '\0') {
> > - buff[i] = '\0';
> > - n = sscanf(&buff[j], "%u-%u", &start, &end);
> > - if (n <= 0) {
> > - printf("Failed to retrieve # possible CPUs!\n");
> > - exit(1);
> > - } else if (n == 1) {
> > - end = start;
> > - }
> > - possible_cpus += end - start + 1;
> > - j = i + 1;
> > - }
> > - }
> > -
> > - fclose(fp);
> > -
> > return possible_cpus;
> > }
> >
> > --
> > 2.17.1
> >
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2019-06-06 16:34 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <20190605231506.2983988-1-hechaol@fb.com>
2019-06-06 5:39 ` [PATCH 0/2] bpf: Add a new API libbpf_num_possible_cpus() Song Liu
[not found] ` <20190605231506.2983988-2-hechaol@fb.com>
2019-06-06 5:44 ` [PATCH 1/2] bpf: add " Song Liu
2019-06-06 16:23 ` Andrii Nakryiko
2019-06-06 16:34 ` Hechao Li
[not found] ` <20190605231506.2983988-3-hechaol@fb.com>
2019-06-06 5:51 ` [PATCH 2/2] bpf: use libbpf_num_possible_cpus in bpftool and selftests Song Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).