Skip to contentSkip to conversation

libbpf: Use sysconf to simplify libbpf_num_possible_cpus

PATCH Superseded Muhammad Falak R Wani 3 participants 3 messages Sep 21, 2021 – Sep 22, 2021

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libbpf: Use sysconf to simplify libbpf_num_possible_cpus

Simplify libbpf_num_possible_cpus by using sysconf(_SC_NPROCESSORS_CONF) instead of parsing a file.
This patch is a part of libbpf-1.0 milestone.

Reference: https://github.com/libbpf/libbpf/issue/383

---
 tools/lib/bpf/libbpf.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
tools/lib/bpf/libbpf.c +3 -13
index da65a1666a5e..1d730b08ee44 100644
@@ -10765,25 +10765,15 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
 
 int libbpf_num_possible_cpus(void)
 {
-	static const char *fcpu = "/sys/devices/system/cpu/possible";
 	static int cpus;
-	int err, n, i, tmp_cpus;
-	bool *mask;
+	int tmp_cpus;
 
 	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;
-	for (i = 0; i < n; i++) {
-		if (mask[i])
-			tmp_cpus++;
-	}
-	free(mask);
+	tmp_cpus = sysconf(_SC_NPROCESSORS_CONF);
+	/* sysconf sets errno; no need to use libbpf_err */
 
 	WRITE_ONCE(cpus, tmp_cpus);
 	return tmp_cpus;
Signature · 1 line
2.17.1
#1 lore raw
Quoted 6 lines · Muhammad Falak R Wani

Simplify libbpf_num_possible_cpus by using sysconf(_SC_NPROCESSORS_CONF) instead of parsing a file.
This patch is a part of libbpf-1.0 milestone.

Reference: https://github.com/libbpf/libbpf/issue/383

I've been asking people to use a reference style like this, so that we don't confuse this with proper Linux tags. It's also useful to use "Closes: " keyword to let Github auto-close the issue when this patch eventually is synced into Github. So in this case I'd phrase everything as:

"This patch is a part ([0]) of libbpf-1.0 milestone.

[0] Closes: https://github.com/libbpf/libbpf/issue/383

Please update in the next revision.

Also, keep in mind that we ask to use "[PATCH bpf-next]" prefix when submitting patches against the bpf-next kernel tree. It makes the intent clear and our BPF CI system knows which tree to test against. Thanks.

Quoted 26 lines
---
 tools/lib/bpf/libbpf.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
tools/lib/bpf/libbpf.c +1 -5
index da65a1666a5e..1d730b08ee44 100644
@@ -10765,25 +10765,15 @@ int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz)
 
 int libbpf_num_possible_cpus(void)
 {
-       static const char *fcpu = "/sys/devices/system/cpu/possible";
        static int cpus;
-       int err, n, i, tmp_cpus;
-       bool *mask;
+       int tmp_cpus;
 
        tmp_cpus = READ_ONCE(cpus);
        if (tmp_cpus > 0)
                return tmp_cpus;
 
-       err = parse_cpu_mask_file(fcpu, &mask, &n);
-       if (err)

tools/lib/bpf/libbpf.c int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz) · line 10776

Quoted 10 lines
tools/lib/bpf/libbpf.c +2 -8
-               return libbpf_err(err);
-
-       tmp_cpus = 0;
-       for (i = 0; i < n; i++) {
-               if (mask[i])
-                       tmp_cpus++;
-       }
-       free(mask);
+       tmp_cpus = sysconf(_SC_NPROCESSORS_CONF);
+       /* sysconf sets errno; no need to use libbpf_err */

I'd say it's still a good idea for explicitness and to show that we didn't forget about it :) Plus, if it actually ever fails, we don't want to WRITE_ONCE() here, so please follow the same error handling logic as it was previously with parse_cpu_mask_file.

Quoted 6 lines
tools/lib/bpf/libbpf.c +0 -1
        WRITE_ONCE(cpus, tmp_cpus);
        return tmp_cpus;
--

2.17.1

#2 lore raw
Quoted 5 lines

"This patch is a part ([0]) of libbpf-1.0 milestone.

[0] Closes: https://github.com/libbpf/libbpf/issue/383

Please update in the next revision.

Sure, will send in a V2 of the patch.

Quoted 7 lines

Also, keep in mind that we ask to use "[PATCH bpf-next]" prefix when submitting patches against the bpf-next kernel tree. It makes the intent clear and our BPF CI system knows which tree to test against. Thanks.

Apologies, duly noted for subsequent patches.

Quoted 8 lines

I'd say it's still a good idea for explicitness and to show that we didn't forget about it :) Plus, if it actually ever fails, we don't want to WRITE_ONCE() here, so please follow the same error handling logic as it was previously with parse_cpu_mask_file.

Quoted 3 lines
        WRITE_ONCE(cpus, tmp_cpus);
        return tmp_cpus;

Sure, will adhere to the coding style.

Thank you for your reivew.

-mfrw