]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: cpu-topo: move bound cpu detection from cpuset to cpu-topo
authorWilly Tarreau <w@1wt.eu>
Wed, 22 Jan 2025 16:17:59 +0000 (17:17 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 14 Mar 2025 17:30:30 +0000 (18:30 +0100)
The cpuset files are normally used only for cpu manipulations. It happens
that the initial CPU binding detection was initially placed there since
there was no better place, but in practice, being OS-specific, it should
really be in cpu-topo. This simplifies cpuset which doesn't need to know
about the OS anymore.

include/haproxy/cpu_topo.h
include/haproxy/cpuset.h
src/cfgparse.c
src/cpu_topo.c
src/cpuset.c
src/thread.c

index 0aa74f99e04b83e60676052fb4f45a9b111896e5..bf7d98f71d9f5b90de9911da8e2ffcb0d3569e8a 100644 (file)
@@ -2,7 +2,7 @@
 #define _HAPROXY_CPU_TOPO_H
 
 #include <haproxy/api.h>
-#include <haproxy/cpuset.h>
+#include <haproxy/cpuset-t.h>
 #include <haproxy/cpu_topo-t.h>
 
 extern int cpu_topo_maxcpus;
@@ -16,6 +16,16 @@ extern struct ha_cpu_topo *ha_cpu_topo;
  */
 int cpu_detect_usable(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.
+ */
+int cpu_map_configured(void);
+
 /* Dump the CPU topology <topo> for up to cpu_topo_maxcpus CPUs for
  * debugging purposes. Offline CPUs are skipped.
  */
index 87c4ece8281aec3aab63416d8f02e0db6f005be0..842f09f0d988302ec014a5ff7a6f8bcb8745888c 100644 (file)
@@ -48,11 +48,6 @@ 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);
-
 /* Parse cpu sets. Each CPU set is either a unique number between 0 and
  * ha_cpuset_size() - 1 or a range with two such numbers delimited by a dash
  * ('-'). Each CPU set can be a list of unique numbers or ranges separated by
@@ -68,9 +63,4 @@ int parse_cpu_set(const char **args, struct hap_cpuset *cpu_set, char **err);
  */
 void parse_cpumap(char *cpumap_str, struct hap_cpuset *cpu_set);
 
-/* Returns true if at least one cpu-map directive was configured, otherwise
- * false.
- */
-int cpu_map_configured(void);
-
 #endif /* _HAPROXY_CPUSET_H */
index a498f69cd457acd05855ea0caa36d071fa890cc8..9f93f6c34b8d666dff0c1dc9532457f5cbf677af 100644 (file)
@@ -51,6 +51,7 @@
 #include <haproxy/clock.h>
 #ifdef USE_CPU_AFFINITY
 #include <haproxy/cpuset.h>
+#include <haproxy/cpu_topo.h>
 #endif
 #include <haproxy/connection.h>
 #include <haproxy/errors.h>
index dd545d50f8a7486f97ddddda4e029e1455b20880..1f3495683ae225b2b2d935524b5609ec2564af24 100644 (file)
@@ -1,13 +1,18 @@
 #define _GNU_SOURCE
 
+#include <sched.h>
+#include <string.h>
 #include <unistd.h>
+
 #include <haproxy/api.h>
+#include <haproxy/cpuset.h>
 #include <haproxy/cpu_topo.h>
 
 /* CPU topology information, ha_cpuset_size() entries, allocated at boot */
 int cpu_topo_maxcpus  = -1;  // max number of CPUs supported by OS/haproxy
 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 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
@@ -34,6 +39,45 @@ int cpu_detect_usable(void)
        return 0;
 }
 
+/* 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.
+ */
+int cpu_map_configured(void)
+{
+       int grp, thr;
+
+       for (grp = 0; grp < MAX_TGROUPS; grp++) {
+               for (thr = 0; thr < MAX_THREADS_PER_GROUP; thr++)
+                       if (ha_cpuset_count(&cpu_map[grp].thread[thr]))
+                               return 1;
+       }
+       return 0;
+}
+
 /* Dump the CPU topology <topo> for up to cpu_topo_maxcpus CPUs for
  * debugging purposes. Offline CPUs are skipped.
  */
@@ -109,6 +153,10 @@ static int cpu_topo_alloc(void)
        cpu_topo_maxcpus = cpu_topo_get_maxcpus();
        cpu_topo_lastcpu = cpu_topo_maxcpus - 1;
 
+       cpu_map = calloc(MAX_TGROUPS, sizeof(*cpu_map));
+       if (!cpu_map)
+               return 0;
+
        /* allocate the structures used to store CPU topology info */
        ha_cpu_topo = (struct ha_cpu_topo*)malloc(cpu_topo_maxcpus * sizeof(*ha_cpu_topo));
        if (!ha_cpu_topo)
@@ -129,6 +177,7 @@ static int cpu_topo_alloc(void)
 static void cpu_topo_deinit(void)
 {
        ha_free(&ha_cpu_topo);
+       ha_free(&cpu_map);
 }
 
 INITCALL0(STG_ALLOC, cpu_topo_alloc);
index 6104898ef9bcbf8bcdf034c3048e5a0b6b5b40b7..6b5c6d9cafff02e1543c94e415576cea8fdd7b04 100644 (file)
@@ -1,14 +1,10 @@
 #define _GNU_SOURCE
-#include <sched.h>
-#include <ctype.h>
 
 #include <haproxy/compat.h>
 #include <haproxy/cpuset.h>
 #include <haproxy/intops.h>
 #include <haproxy/tools.h>
 
-struct cpu_map *cpu_map;
-
 void ha_cpuset_zero(struct hap_cpuset *set)
 {
 #if defined(CPUSET_USE_CPUSET) || defined(CPUSET_USE_FREEBSD_CPUSET)
@@ -149,30 +145,6 @@ 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);
-}
-
 /* Parse cpu sets. Each CPU set is either a unique number between 0 and
  * ha_cpuset_size() - 1 or a range with two such numbers delimited by a dash
  * ('-'). Each CPU set can be a list of unique numbers or ranges separated by
@@ -258,39 +230,3 @@ void parse_cpumap(char *cpumap_str, struct hap_cpuset *cpu_set)
                ++i;
        } while (comma);
 }
-
-/* Returns true if at least one cpu-map directive was configured, otherwise
- * false.
- */
-int cpu_map_configured(void)
-{
-       int grp, thr;
-
-       for (grp = 0; grp < MAX_TGROUPS; grp++) {
-               for (thr = 0; thr < MAX_THREADS_PER_GROUP; thr++)
-                       if (ha_cpuset_count(&cpu_map[grp].thread[thr]))
-                               return 1;
-       }
-       return 0;
-}
-
-/* Allocates everything needed to store CPU information at boot.
- * Returns non-zero on success, zero on failure.
- */
-static int cpuset_alloc(void)
-{
-       /* allocate the structures used to store CPU topology info */
-       cpu_map = calloc(MAX_TGROUPS, sizeof(*cpu_map));
-       if (!cpu_map)
-               return 0;
-
-       return 1;
-}
-
-static void cpuset_deinit(void)
-{
-       ha_free(&cpu_map);
-}
-
-INITCALL0(STG_ALLOC, cpuset_alloc);
-REGISTER_POST_DEINIT(cpuset_deinit);
index 7796d5c5e8c55c483c061b09c5c77d063f84ee3e..1606249938ab91b4ae54158e2e5dd195f7dabd05 100644 (file)
@@ -35,6 +35,7 @@
 #    include <mach/thread_policy.h>
 #  endif
 #  include <haproxy/cpuset.h>
+#  include <haproxy/cpu_topo.h>
 #endif
 
 #include <haproxy/cfgparse.h>