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 23129 invoked from network); 6 Jul 2021 09:55:52 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 6 Jul 2021 09:55:52 -0000 Received: (qmail 24058 invoked by uid 550); 6 Jul 2021 09:55:50 -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 24028 invoked from network); 6 Jul 2021 09:55:49 -0000 From: Vincent Donnefort To: musl@lists.openwall.com Cc: jyknight@google.com, Vincent Donnefort Date: Tue, 6 Jul 2021 09:55:24 +0000 Message-Id: <20210706095524.1883-1-vincent.donnefort@arm.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [musl] [PATCH v3] 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 that the system can contain, 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 otal 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 file /sys/devices/system/cpu/possible. This static file will not change during the lifespan of the Linux kernel and contains a CPU mask with all the CPUs that can be brought online. diff --git a/src/conf/sysconf.c b/src/conf/sysconf.c index 3baaed32..b4b842f5 100644 --- a/src/conf/sysconf.c +++ b/src/conf/sysconf.c @@ -1,8 +1,10 @@ +#include #include #include #include #include #include +#include #include #include "syscall.h" #include "libc.h" @@ -22,6 +24,73 @@ #define RLIM(x) (-32768|(RLIMIT_ ## x)) +#define POSSIBLE_MAX_READ 128 +#define char_to_int(prev, num) (10 * prev + (num - '0')) + +static inline int get_nrprocessors_conf(void) +{ + FILE *f; + char buffer[POSSIBLE_MAX_READ]; + int start_chunk = 0, end_chunk = -1; + unsigned int cnt = 0, i = 0; + size_t ret; + + f = fopen("/sys/devices/system/cpu/possible", "r"); + if (!f) + return 0; + + ret = fread(buffer, sizeof(*buffer), + sizeof(buffer) / sizeof(*buffer), f); + if (!feof(f) || ferror(f) || ret < 2) + goto end; + + /* + * Count the number of CPUs in the CPU mask. A CPU Mask is described by + * chunks. Chunks have the following format: "-" and are + * separated by ",". A chunk can be composed of a single CPU. + * + * e.g. "0-1,4" -> 3 CPUs + */ + while (i < POSSIBLE_MAX_READ) { + if (buffer[i] == ',' || buffer[i] == '\0') { + if (end_chunk > -1) + cnt += (end_chunk - start_chunk) + 1; + else + cnt++; + start_chunk = 0; + end_chunk = -1; + if (buffer[i] == '\0') + break; + } else if (buffer[i] == '-') { + end_chunk = 0; + } else if (isdigit(buffer[i])) { + if (end_chunk == -1) + start_chunk = + char_to_int(start_chunk, buffer[i]); + else + end_chunk = char_to_int(end_chunk, buffer[i]); + } + i++; + } + +end: + fclose(f); + + 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