lkml.org 
[lkml]   [2021]   [Sep]   [21]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH] libbpf: Use sysconf to simplify libbpf_num_possible_cpus
Date
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

Signed-off-by: Muhammad Falak R Wani <falakreyaz@gmail.com>
---
tools/lib/bpf/libbpf.c | 16 +++-------------
1 file changed, 3 insertions(+), 13 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index da65a1666a5e..1d730b08ee44 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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;
--
2.17.1
\
 
 \ /
  Last update: 2021-09-21 08:05    [W:0.039 / U:0.100 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site