]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: pollers: Allow one polled_mask per thread group
authorOlivier Houchard <ohouchard@haproxy.com>
Thu, 2 Jul 2026 14:36:23 +0000 (16:36 +0200)
committerOlivier Houchard <cognet@ci0.org>
Thu, 30 Jul 2026 11:35:22 +0000 (13:35 +0200)
Similarly to what's been done for fdtab, create one polled_mask per
thread group if GTUNE_NO_TG_FD_SHARING is set.
As the flag can't be set yet, this commit should have no impact.

include/haproxy/fd.h
include/haproxy/tinfo-t.h
src/fd.c
src/haproxy.c

index 9307cfcf85532d8cb8211338ddd9cabfd2193ca4..f993c6bbc5ea1e20d517a4938d0ccd4bccf30b40 100644 (file)
@@ -43,7 +43,7 @@ extern int totalconn;                   /* total # of terminated sessions */
 extern int actconn;                     /* # of active sessions */
 
 extern volatile struct fdlist update_list[MAX_TGROUPS];
-extern struct polled_mask *polled_mask;
+extern THREAD_LOCAL struct polled_mask *polled_mask; /* Array for the polled_mask of each fd */
 
 extern THREAD_LOCAL int *fd_updt;  // FD updates list
 extern THREAD_LOCAL int fd_nbupdt; // number of updates in the list
index 153f506ebbe161eb94773d21ec73f6691e5409f6..572781732c7b67059e15af3b7f87488726f9598b 100644 (file)
@@ -139,6 +139,7 @@ struct tgroup_ctx {
        uint niced_tasks;                 /* number of niced tasks in this group's run queues */
        uint committed_extra_streams;     /* sum of extra front streams committed by muxes in this group */
        struct fdtab *fdtab;              /* fdtab to be used by that thread group */
+       struct polled_mask *polled_mask;  /* polled_mask to be used by that thread group */
 
        /* pad to cache line (64B) */
        char __pad[0];                    /* unused except to check remaining room */
index 158c2d480ab099b6fe83898c1cc15d4080ffb0a4..9c851fe07a9b285f2f7a76def25624430667fd3f 100644 (file)
--- a/src/fd.c
+++ b/src/fd.c
 
 THREAD_LOCAL struct fdtab *fdtab = NULL;  /* array of all the file descriptors */
 static struct fdtab *_fdtab;
-struct polled_mask *polled_mask __read_mostly = NULL;  /* Array for the polled_mask of each fd */
+THREAD_LOCAL struct polled_mask *polled_mask = NULL;  /* Array for the polled_mask of each fd */
+static struct polled_mask *_polled_mask;
 int totalconn;                  /* total # of terminated sessions */
 int actconn;                    /* # of active sessions */
 
@@ -1217,11 +1218,23 @@ int init_pollers()
        fdtab = ha_tgroup_ctx[0].fdtab;
        vma_set_name(_fdtab, global.maxsock * sizeof(*fdtab) * nbfdtab, "fd", "fdtab");
 
-       if ((polled_mask = calloc(global.maxsock, sizeof(*polled_mask))) == NULL) {
+       if ((_polled_mask = calloc(global.maxsock, nbfdtab * sizeof(*polled_mask))) == NULL) {
                ha_alert("Not enough memory to allocate %d entries for polled_mask!\n", global.maxsock);
                goto fail_polledmask;
        }
-       vma_set_name(polled_mask, global.maxsock * sizeof(*polled_mask), "fd", "polled_mask");
+
+       /*
+        * As for fdtab, get on polled_mask per thread group, if needed.
+        */
+       for (p = 0; p < global.nbtgroups; p++) {
+               if (global.tune.options & GTUNE_NO_TG_FD_SHARING)
+                       ha_tgroup_ctx[p].polled_mask = _polled_mask + p * global.maxsock;
+               else
+                       ha_tgroup_ctx[p].polled_mask = _polled_mask;
+       }
+       /* immediately set it for the boot thread as well */
+       polled_mask = ha_tgroup_ctx[0].polled_mask;
+       vma_set_name(_polled_mask, global.maxsock * sizeof(*polled_mask) * nbfdtab, "fd", "polled_mask");
 
        for (p = 0; p < MAX_TGROUPS; p++)
                update_list[p].first = update_list[p].last = -1;
@@ -1247,9 +1260,11 @@ int init_pollers()
        } while (!bp || bp->pref == 0);
 
  fail_info:
-       free(polled_mask);
+       free(_polled_mask);
+       _polled_mask = polled_mask = NULL;
  fail_polledmask:
-       ha_aligned_free(fdtab);
+       ha_aligned_free(_fdtab);
+       _fdtab = fdtab = NULL;
  fail_tab:
        return 0;
 }
@@ -1269,8 +1284,13 @@ void deinit_pollers() {
                        bp->term(bp);
        }
 
-       ha_aligned_free(fdtab);
-       ha_free(&polled_mask);
+       /* free the backing areas, not the (possibly per-group) thread-local
+        * pointers which may not point to the start of the allocations.
+        */
+       ha_aligned_free(_fdtab);
+       _fdtab = fdtab = NULL;
+       ha_free(&_polled_mask);
+       polled_mask = NULL;
 }
 
 /*
index a50baf98d6ebb90137ad94d0f8620ca248d43318..aecb97a61381a12450e28834fce8b9f15c821469 100644 (file)
@@ -3122,6 +3122,7 @@ void *run_thread_poll_loop(void *data)
 
        ha_set_thread(data);
        fdtab = tg_ctx->fdtab;
+       polled_mask = tg_ctx->polled_mask;
        set_thread_cpu_affinity();
        clock_set_local_source();
        ha_random_seed_thread();