]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cpuset: centralize a reliable bound cpu detection
authorWilly Tarreau <w@1wt.eu>
Tue, 11 Jul 2023 14:51:22 +0000 (16:51 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 4 Sep 2023 17:39:17 +0000 (19:39 +0200)
Till now the CPUs that were bound were only retrieved in
thread_cpus_enabled() in order to count the number of CPUs allowed,
and it relied on arch-specific code.

Let's slightly arrange this into ha_cpuset_detect_bound() that
reuses the ha_cpuset struct and the accompanying code. This makes
the code much clearer without having to carry along some arch-specific
stuff out of this area.

Note that the macos-specific code used in thread.c to only count
online CPUs but not retrieve a mask, so for now we can't infer
anything from it and can't implement it.

In addition and more importantly, this function is reliable in that
it will only return a value when the detection is accurate, and will
not return incomplete sets on operating systems where we don't have
an exact list, such as online CPUs.

include/haproxy/cpuset.h
src/cpuset.c

index 2d1b5d37598e8718bd7bdfd4cfd25694189907fc..78c4df270723e2f4bdf43a723cc078cb0812f580 100644 (file)
@@ -48,6 +48,11 @@ void ha_cpuset_assign(struct hap_cpuset *dst, struct hap_cpuset *src);
  */
 int ha_cpuset_size(void);
 
+/* Detects CPUs that are bound to the current process. Returns the number of
+ * CPUs detected or 0 if the detection failed.
+ */
+int ha_cpuset_detect_bound(struct hap_cpuset *set);
+
 /* Returns true if at least one cpu-map directive was configured, otherwise
  * false.
  */
index 42daca463a4d3607f4fb4422b49cc8e7a49c5776..73b97b975940c0036ef6db6cf8fbc8d0d9caa60e 100644 (file)
@@ -147,6 +147,30 @@ int ha_cpuset_size()
 #endif
 }
 
+/* Detects CPUs that are bound to the current process. Returns the number of
+ * CPUs detected or 0 if the detection failed.
+ */
+int ha_cpuset_detect_bound(struct hap_cpuset *set)
+{
+       ha_cpuset_zero(set);
+
+       /* detect bound CPUs depending on the OS's API */
+       if (0
+#if defined(__linux__)
+           || sched_getaffinity(0, sizeof(set->cpuset), &set->cpuset) != 0
+#elif defined(__FreeBSD__)
+           || cpuset_getaffinity(CPU_LEVEL_CPUSET, CPU_WHICH_PID, -1, sizeof(set->cpuset), &set->cpuset) != 0
+#else
+           || 1 // unhandled platform
+#endif
+           ) {
+               /* detection failed */
+               return 0;
+       }
+
+       return ha_cpuset_count(set);
+}
+
 /* Returns true if at least one cpu-map directive was configured, otherwise
  * false.
  */