Skip to content

Commit

Permalink
libbpf: Use sysconf to simplify libbpf_num_possible_cpus
Browse files Browse the repository at this point in the history
Simplify libbpf_num_possible_cpus by using sysconf(_SC_NPROCESSORS_CONF)
instead of parsing a file.
This patch is a part ([0]) of libbpf-1.0 milestone.

[0] Closes: libbpf/libbpf#383

Signed-off-by: Muhammad Falak R Wani <falakreyaz@gmail.com>
  • Loading branch information
mfrw authored and Nobody committed Sep 22, 2021
1 parent 34c74aa commit 1eca012
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions tools/lib/bpf/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10898,25 +10898,16 @@ 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);
if (tmp_cpus < 1)
return libbpf_err(-EINVAL);

WRITE_ONCE(cpus, tmp_cpus);
return tmp_cpus;
Expand Down

0 comments on commit 1eca012

Please sign in to comment.