diff mbox series

[v2,bpf-next,2/3] libbpf: auto-set PERF_EVENT_ARRAY size to number of CPUs

Message ID 20190626061235.602633-3-andriin@fb.com
State Changes Requested
Delegated to: BPF Maintainers
Headers show
Series libbpf: add perf buffer abstraction and API | expand

Commit Message

Andrii Nakryiko June 26, 2019, 6:12 a.m. UTC
For BPF_MAP_TYPE_PERF_EVENT_ARRAY typically correct size is number of
possible CPUs. This is impossible to specify at compilation time. This
change adds automatic setting of PERF_EVENT_ARRAY size to number of
system CPUs, unless non-zero size is specified explicitly. This allows
to adjust size for advanced specific cases, while providing convenient
and logical defaults.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/libbpf.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

Comments

Song Liu June 26, 2019, 6:57 p.m. UTC | #1
> On Jun 25, 2019, at 11:12 PM, Andrii Nakryiko <andriin@fb.com> wrote:
> 
> For BPF_MAP_TYPE_PERF_EVENT_ARRAY typically correct size is number of
> possible CPUs. This is impossible to specify at compilation time. This
> change adds automatic setting of PERF_EVENT_ARRAY size to number of
> system CPUs, unless non-zero size is specified explicitly. This allows
> to adjust size for advanced specific cases, while providing convenient
> and logical defaults.
> 
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> ---
> tools/lib/bpf/libbpf.c | 17 ++++++++++++++++-
> 1 file changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index c74cc535902a..8f2b8a081ba7 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -2114,6 +2114,7 @@ static int
> bpf_object__create_maps(struct bpf_object *obj)
> {
> 	struct bpf_create_map_attr create_attr = {};
> +	int nr_cpus = 0;
> 	unsigned int i;
> 	int err;
> 
> @@ -2136,7 +2137,21 @@ bpf_object__create_maps(struct bpf_object *obj)
> 		create_attr.map_flags = def->map_flags;
> 		create_attr.key_size = def->key_size;
> 		create_attr.value_size = def->value_size;
> -		create_attr.max_entries = def->max_entries;
> +		if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
> +		    !def->max_entries) {
> +			if (!nr_cpus)
> +				nr_cpus = libbpf_num_possible_cpus();
> +			if (nr_cpus < 0) {
> +				pr_warning("failed to determine number of system CPUs: %d\n",
> +					   nr_cpus);
> +				return nr_cpus;

I think we need to goto err_out here. 

Thanks,
Song
Andrii Nakryiko June 26, 2019, 10:31 p.m. UTC | #2
On Wed, Jun 26, 2019 at 11:57 AM Song Liu <songliubraving@fb.com> wrote:
>
>
>
> > On Jun 25, 2019, at 11:12 PM, Andrii Nakryiko <andriin@fb.com> wrote:
> >
> > For BPF_MAP_TYPE_PERF_EVENT_ARRAY typically correct size is number of
> > possible CPUs. This is impossible to specify at compilation time. This
> > change adds automatic setting of PERF_EVENT_ARRAY size to number of
> > system CPUs, unless non-zero size is specified explicitly. This allows
> > to adjust size for advanced specific cases, while providing convenient
> > and logical defaults.
> >
> > Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> > ---
> > tools/lib/bpf/libbpf.c | 17 ++++++++++++++++-
> > 1 file changed, 16 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> > index c74cc535902a..8f2b8a081ba7 100644
> > --- a/tools/lib/bpf/libbpf.c
> > +++ b/tools/lib/bpf/libbpf.c
> > @@ -2114,6 +2114,7 @@ static int
> > bpf_object__create_maps(struct bpf_object *obj)
> > {
> >       struct bpf_create_map_attr create_attr = {};
> > +     int nr_cpus = 0;
> >       unsigned int i;
> >       int err;
> >
> > @@ -2136,7 +2137,21 @@ bpf_object__create_maps(struct bpf_object *obj)
> >               create_attr.map_flags = def->map_flags;
> >               create_attr.key_size = def->key_size;
> >               create_attr.value_size = def->value_size;
> > -             create_attr.max_entries = def->max_entries;
> > +             if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
> > +                 !def->max_entries) {
> > +                     if (!nr_cpus)
> > +                             nr_cpus = libbpf_num_possible_cpus();
> > +                     if (nr_cpus < 0) {
> > +                             pr_warning("failed to determine number of system CPUs: %d\n",
> > +                                        nr_cpus);
> > +                             return nr_cpus;
>
> I think we need to goto err_out here.

Absolutely, good catch, thanks!

>
> Thanks,
> Song
>
diff mbox series

Patch

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index c74cc535902a..8f2b8a081ba7 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -2114,6 +2114,7 @@  static int
 bpf_object__create_maps(struct bpf_object *obj)
 {
 	struct bpf_create_map_attr create_attr = {};
+	int nr_cpus = 0;
 	unsigned int i;
 	int err;
 
@@ -2136,7 +2137,21 @@  bpf_object__create_maps(struct bpf_object *obj)
 		create_attr.map_flags = def->map_flags;
 		create_attr.key_size = def->key_size;
 		create_attr.value_size = def->value_size;
-		create_attr.max_entries = def->max_entries;
+		if (def->type == BPF_MAP_TYPE_PERF_EVENT_ARRAY &&
+		    !def->max_entries) {
+			if (!nr_cpus)
+				nr_cpus = libbpf_num_possible_cpus();
+			if (nr_cpus < 0) {
+				pr_warning("failed to determine number of system CPUs: %d\n",
+					   nr_cpus);
+				return nr_cpus;
+			}
+			pr_debug("map '%s': setting size to %d\n",
+				 map->name, nr_cpus);
+			create_attr.max_entries = nr_cpus;
+		} else {
+			create_attr.max_entries = def->max_entries;
+		}
 		create_attr.btf_fd = 0;
 		create_attr.btf_key_type_id = 0;
 		create_attr.btf_value_type_id = 0;