mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH v2] sysconf: add _SC_NPROCESSORS_CONF support
@ 2021-05-06 15:31 Vincent Donnefort
  2021-05-18 11:33 ` [musl] " Vincent Donnefort
  0 siblings, 1 reply; 3+ messages in thread
From: Vincent Donnefort @ 2021-05-06 15:31 UTC (permalink / raw)
  To: musl; +Cc: Vincent Donnefort

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-18 17:15 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-06 15:31 [musl] [PATCH v2] sysconf: add _SC_NPROCESSORS_CONF support Vincent Donnefort
2021-05-18 11:33 ` [musl] " Vincent Donnefort
2021-05-18 17:14   ` James Y Knight

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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).