]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cpu-topo: add detection of online CPUs on Linux
authorWilly Tarreau <w@1wt.eu>
Tue, 11 Jul 2023 16:24:20 +0000 (18:24 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 14 Mar 2025 17:30:30 +0000 (18:30 +0100)
This adds a generic function ha_cpuset_detect_online() which for now
only supports linux via /sys. It fills a cpuset with the list of online
CPUs that were detected (or returns a failure).

include/haproxy/cpu_topo.h
src/cpu_topo.c

index bf7d98f71d9f5b90de9911da8e2ffcb0d3569e8a..74a7e70d2008f14f00bff7bca4398ccfcf27a7ee 100644 (file)
@@ -9,6 +9,12 @@ extern int cpu_topo_maxcpus;
 extern int cpu_topo_lastcpu;
 extern struct ha_cpu_topo *ha_cpu_topo;
 
+/* Detects CPUs that are online on the system. It may rely on FS access (e.g.
+ * /sys on Linux). Returns the number of CPUs detected or 0 if the detection
+ * failed.
+ */
+int ha_cpuset_detect_online(struct hap_cpuset *set);
+
 /* Detects the CPUs that will be used based on the ones the process is bound to.
  * Returns non-zero on success, zero on failure. Note that it may not be
  * performed in the function above because some calls may rely on other items
index 1f3495683ae225b2b2d935524b5609ec2564af24..ddf45e6fff61ea1be210e111dcc71247baa5e4b1 100644 (file)
@@ -7,6 +7,8 @@
 #include <haproxy/api.h>
 #include <haproxy/cpuset.h>
 #include <haproxy/cpu_topo.h>
+#include <haproxy/global.h>
+#include <haproxy/tools.h>
 
 /* CPU topology information, ha_cpuset_size() entries, allocated at boot */
 int cpu_topo_maxcpus  = -1;  // max number of CPUs supported by OS/haproxy
@@ -14,6 +16,33 @@ int cpu_topo_lastcpu  = -1;  // last supposed online CPU (no need to look beyond
 struct ha_cpu_topo *ha_cpu_topo = NULL;
 struct cpu_map *cpu_map;
 
+
+/* Detects CPUs that are online on the system. It may rely on FS access (e.g.
+ * /sys on Linux). Returns the number of CPUs detected or 0 if the detection
+ * failed.
+ */
+int ha_cpuset_detect_online(struct hap_cpuset *set)
+{
+#if defined(__linux__)
+
+       ha_cpuset_zero(set);
+
+       /* contains a list of CPUs in the format <low>[-<high>][,...] */
+       if (read_line_to_trash("%s/cpu/online", NUMA_DETECT_SYSTEM_SYSFS_PATH) >= 0) {
+               const char *parse_cpu_set_args[2] = { trash.area, "\0" };
+
+               if (parse_cpu_set(parse_cpu_set_args, set, NULL) != 0)
+                       ha_cpuset_zero(set);
+       }
+
+#else // !__linux__
+
+       ha_cpuset_zero(set);
+
+#endif
+       return ha_cpuset_count(set);
+}
+
 /* Detects the CPUs that will be used based on the ones the process is bound to
  * at boot. The principle is the following: all CPUs from the boot cpuset will
  * be used since we don't know upfront how individual threads will be mapped to