|
Message-Id: <20210506153147.11859-1-vincent.donnefort@arm.com> Date: Thu, 6 May 2021 15:31:47 +0000 From: Vincent Donnefort <vincent.donnefort@....com> To: musl@...ts.openwall.com Cc: Vincent Donnefort <vincent.donnefort@....com> Subject: [PATCH v2] 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 file /sys/devices/system/cpu/present. diff --git a/src/conf/sysconf.c b/src/conf/sysconf.c index 3baaed32..a70d38c0 100644 --- a/src/conf/sysconf.c +++ b/src/conf/sysconf.c @@ -1,8 +1,10 @@ +#include <ctype.h> #include <unistd.h> #include <limits.h> #include <errno.h> #include <sys/resource.h> #include <signal.h> +#include <stdbool.h> #include <sys/sysinfo.h> #include "syscall.h" #include "libc.h" @@ -22,6 +24,73 @@ #define RLIM(x) (-32768|(RLIMIT_ ## x)) +#define PRESENT_MAX_READ 128 +#define char_to_int(prev, num) (10 * prev + (num - '0')) + +static inline int get_nrprocessors_conf(void) +{ + FILE *f; + char buffer[PRESENT_MAX_READ]; + int start_chunk = 0, end_chunk = -1; + unsigned int cnt = 0, i = 0; + size_t ret; + + f = fopen("/sys/devices/system/cpu/present", "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: "<start>-<end>" and are + * separated by ",". A chunk can be composed of a single CPU. + * + * e.g. "0-1,4" -> 3 CPUs + */ + while (i < PRESENT_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<sizeof set; i++) + for (; set[i]; set[i]&=set[i]-1, cnt++); + + return cnt; +} + long sysconf(int name) { static const short values[] = { @@ -193,14 +262,13 @@ long sysconf(int name) return SEM_VALUE_MAX; case JT_DELAYTIMER_MAX & 255: return DELAYTIMER_MAX; - case JT_NPROCESSORS_CONF & 255: + case JT_NPROCESSORS_CONF & 255: ; + int cnt = get_nrprocessors_conf(); + if (cnt > 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<sizeof set; i++) - for (; set[i]; set[i]&=set[i]-1, cnt++); - return cnt; + return get_nrprocessors_onln(); case JT_PHYS_PAGES & 255: case JT_AVPHYS_PAGES & 255: ; unsigned long long mem; -- 2.27.0
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.