From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 2523 invoked from network); 5 May 2021 13:25:43 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 5 May 2021 13:25:43 -0000 Received: (qmail 9929 invoked by uid 550); 5 May 2021 13:25:39 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 32018 invoked from network); 5 May 2021 13:07:17 -0000 From: Vincent Donnefort To: musl@lists.openwall.com Cc: Vincent Donnefort Date: Wed, 5 May 2021 13:06:15 +0000 Message-Id: <20210505130615.6222-1-vincent.donnefort@arm.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [musl] [PATCH] sysconf: add _SC_NPROCESSORS_CONF support Currently, _SC_NPROCESSORS_CONF is always equal to _SC_NPROCESSORS_ONLN. However, it is expected from the first one to give the total number of CPUs in the system, while the later must return only the number of CPUs which are currently online. This distinction is important for a software such as trace-cmd. Trace-cmd is a front-end for the kernel tracing tool ftrace. When recording traces, trace-cmd needs to get the total number of CPUs available in the system (_SC_NPROCESSORS_CONF) and not only the online ones otherwise if a CPU goes offline some data might be missing. Hence, add a specific method to get _SC_NPROCESSORS_CONF, based on the sysfs CPU entries /sys/devices/system/cpu/cpu[0-9] diff --git a/src/conf/sysconf.c b/src/conf/sysconf.c index 3baaed32..6281cfb6 100644 --- a/src/conf/sysconf.c +++ b/src/conf/sysconf.c @@ -1,12 +1,17 @@ +#include #include #include #include #include #include +#include #include #include "syscall.h" #include "libc.h" +#define _GNU_SOURCE +#include + #define JT(x) (-256|(x)) #define VER JT(1) #define JT_ARG_MAX JT(2) @@ -22,6 +27,42 @@ #define RLIM(x) (-32768|(RLIMIT_ ## x)) +static inline int get_nrprocessors_conf(void) +{ + DIR *d = opendir("/sys/devices/system/cpu"); + struct dirent *de; + unsigned int cnt = 0; + + if (!d) + return -1; + + while ((de = readdir(d))) { + if (de->d_type == DT_DIR && + strlen(de->d_name) > 3 && + de->d_name[0] == 'c' && + de->d_name[1] == 'p' && + de->d_name[2] == 'u' && + isdigit(de->d_name[3])) + cnt++; + } + + closedir(d); + + return cnt; +} + +static inline int get_nrprocessors_onln(void) +{ + unsigned char set[128] = {1}; + int i, cnt; + + __syscall(SYS_sched_getaffinity, 0, sizeof set, set); + for (i=cnt=0; i 0) + return cnt; + return get_nrprocessors_onln(); case JT_NPROCESSORS_ONLN & 255: ; - unsigned char set[128] = {1}; - int i, cnt; - __syscall(SYS_sched_getaffinity, 0, sizeof set, set); - for (i=cnt=0; i