]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: init: set process' affinity even in foreground
authorWilly Tarreau <w@1wt.eu>
Wed, 19 Jul 2023 16:39:32 +0000 (18:39 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 20 Jul 2023 09:01:09 +0000 (11:01 +0200)
The per-process CPU affinity settings are only applied during forking,
which means that cpu-map are ignored when running in foreground (e.g.
haproxy started with -db). This is historic due to the original semantics
of a process array, but isn't documented and causes surprises when trying
to debug affinity settings.

Let's make sure the setting is applied to the workers themselves even
in foreground. This may be backported to 2.6 though it is really not
important. If backported, it also depends on previous commit:

  BUG/MINOR: cpuset: remove the bogus "proc" from the cpu_map struct

src/haproxy.c

index 993fa3939822f230a082948a88b4b43062f4f350..6b8fdf1583aee361782026ad02bf2b42e960be5a 100644 (file)
@@ -3623,18 +3623,6 @@ int main(int argc, char **argv)
                        in_parent = 1;
                }
 
-#if !defined(USE_THREAD) && defined(USE_CPU_AFFINITY)
-               if (!in_parent && ha_cpuset_count(&cpu_map[0].thread[0])) {   /* only do this if the process has a CPU map */
-
-#if defined(CPUSET_USE_CPUSET) || defined(__DragonFly__)
-                       struct hap_cpuset *set = &cpu_map[0].thread[0];
-                       sched_setaffinity(0, sizeof(set->cpuset), &set->cpuset);
-#elif defined(__FreeBSD__)
-                       struct hap_cpuset *set = &cpu_map[0].thread[0];
-                       ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(set->cpuset), &set->cpuset);
-#endif
-               }
-#endif
                /* close the pidfile both in children and father */
                if (pidfd >= 0) {
                        //lseek(pidfd, 0, SEEK_SET);  /* debug: emulate eglibc bug */
@@ -3775,6 +3763,19 @@ int main(int argc, char **argv)
                fork_poller();
        }
 
+       /* Note that here we can't be in the parent/master anymore */
+#if !defined(USE_THREAD) && defined(USE_CPU_AFFINITY)
+       if (ha_cpuset_count(&cpu_map[0].thread[0])) {   /* only do this if the process has a CPU map */
+
+#if defined(CPUSET_USE_CPUSET) || defined(__DragonFly__)
+               struct hap_cpuset *set = &cpu_map[0].thread[0];
+               sched_setaffinity(0, sizeof(set->cpuset), &set->cpuset);
+#elif defined(__FreeBSD__)
+               struct hap_cpuset *set = &cpu_map[0].thread[0];
+               ret = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, sizeof(set->cpuset), &set->cpuset);
+#endif
+       }
+#endif
        /* try our best to re-enable core dumps depending on system capabilities.
         * What is addressed here :
         *   - remove file size limits