linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Muhammad Falak R Wani <falakreyaz@gmail.com>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>
Cc: Martin KaFai Lau <kafai@fb.com>, Song Liu <songliubraving@fb.com>,
	Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	netdev@vger.kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Muhammad Falak R Wani <falakreyaz@gmail.com>
Subject: [PATCH v2 bpf-next] libbpf: Use sysconf to simplify libbpf_num_possible_cpus
Date: Wed, 22 Sep 2021 12:37:48 +0530	[thread overview]
Message-ID: <20210922070748.21614-1-falakreyaz@gmail.com> (raw)

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: https://github.com/libbpf/libbpf/issues/383

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

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index ef5db34bf913..f1c0abe5b58d 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -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;
-- 
2.17.1


             reply	other threads:[~2021-09-22  7:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-22  7:07 Muhammad Falak R Wani [this message]
2021-09-22 21:22 ` [PATCH v2 bpf-next] libbpf: Use sysconf to simplify libbpf_num_possible_cpus Daniel Borkmann
2021-09-22 21:38   ` Andrii Nakryiko
2021-09-23  2:49     ` Muhammad Falak Wani

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210922070748.21614-1-falakreyaz@gmail.com \
    --to=falakreyaz@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=kafai@fb.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).