]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: thread: keep a bitmask of enabled groups in thread_set
authorWilly Tarreau <w@1wt.eu>
Wed, 1 Mar 2023 10:24:29 +0000 (11:24 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 13 Apr 2023 14:57:51 +0000 (16:57 +0200)
We're only checking for 0, 1, or >1 groups enabled there, and we'll soon
need to be more precise and know quickly which groups are non-empty.
Let's just replace the count with a mask of enabled groups. This will
allow to quickly spot the presence of any such group in a set.

include/haproxy/tinfo-t.h
include/haproxy/tinfo.h
src/cfgparse.c
src/thread.c

index 7c201afbb31d6970af7ffa08293be77fb8182beb..ce893cab73953a86255938e6525f828790678620 100644 (file)
@@ -40,7 +40,7 @@ struct thread_set {
                ulong abs[(MAX_THREADS + LONGBITS - 1) / LONGBITS];
                ulong rel[MAX_TGROUPS];
        };
-       uint nbgrp; /* number of non-empty groups in this set, 0 for abs */
+       ulong grps; /* bit field of all non-empty groups, 0 for abs */
 };
 
 /* tasklet classes */
index 3d10560f906982524a7488d6cf59e0162b899c47..ddb26aac7ec961dd3a06769ff8b17c74286268dc 100644 (file)
@@ -77,7 +77,7 @@ static inline int thread_set_nth_group(const struct thread_set *ts, int n)
 {
        int i;
 
-       if (ts->nbgrp) {
+       if (ts->grps) {
                for (i = 0; i < MAX_TGROUPS; i++)
                        if (ts->rel[i] && !n--)
                                return i + 1;
@@ -95,7 +95,7 @@ static inline ulong thread_set_nth_tmask(const struct thread_set *ts, int n)
 {
        int i;
 
-       if (ts->nbgrp) {
+       if (ts->grps) {
                for (i = 0; i < MAX_TGROUPS; i++)
                        if (ts->rel[i] && !n--)
                                return ts->rel[i];
@@ -111,7 +111,7 @@ static inline void thread_set_pin_grp1(struct thread_set *ts, ulong mask)
 {
        int i;
 
-       ts->nbgrp = 1;
+       ts->grps = 1;
        ts->rel[0] = mask;
        for (i = 1; i < MAX_TGROUPS; i++)
                ts->rel[i] = 0;
index 52f40d633de331903ac1217f62950b7d44e3a964..4272c3df185be9f2b9689b65a60f947eabb4c9ad 100644 (file)
@@ -2998,18 +2998,16 @@ init_proxies_list_stage1:
                                                bit = (bind_conf->thread_set.rel[grp] & ~mask) & -(bind_conf->thread_set.rel[grp] & ~mask);
                                                new_ts.rel[grp] |= bit;
                                                mask |= bit;
-
-                                               if (!atleast2(new_ts.rel[grp])) // first time we add a bit: new group
-                                                       new_ts.nbgrp++;
+                                               new_ts.grps |= 1UL << grp;
 
                                                done += shards;
                                        };
 
-                                       BUG_ON(!new_ts.nbgrp); // no more bits left unassigned
+                                       BUG_ON(!new_ts.grps); // no more bits left unassigned
 
-                                       if (new_ts.nbgrp > 1) {
+                                       if (atleast2(new_ts.grps)) {
                                                ha_alert("Proxy '%s': shard number %d spans %d groups in 'bind %s' at [%s:%d]\n",
-                                                        curproxy->id, shard, new_ts.nbgrp, bind_conf->arg, bind_conf->file, bind_conf->line);
+                                                        curproxy->id, shard, my_popcountl(new_ts.grps), bind_conf->arg, bind_conf->file, bind_conf->line);
                                                cfgerr++;
                                                err_code |= ERR_FATAL | ERR_ALERT;
                                                goto out;
@@ -4450,7 +4448,7 @@ init_proxies_list_stage2:
                                                free(err);
                                                cfgerr++;
                                        }
-                                       else if (bind_conf->thread_set.nbgrp > 1) {
+                                       else if (atleast2(bind_conf->thread_set.grps)) {
                                                ha_alert("Peers section '%s': 'thread' spans more than one group in 'bind %s' at [%s:%d].\n",
                                                         curpeers->peers_fe->id, bind_conf->arg, bind_conf->file, bind_conf->line);
                                                cfgerr++;
index d11df8df06e659388cdfdbc9a4094b835110d5a5..84d39d7c3f661f52a5f620388c026c11ee40f62a 100644 (file)
@@ -1240,7 +1240,7 @@ int thread_resolve_group_mask(struct thread_set *ts, int defgrp, char **err)
        ulong mask, imask;
        uint g;
 
-       if (!ts->nbgrp) {
+       if (!ts->grps) {
                /* unspecified group, IDs are global */
                if (thread_set_is_empty(ts)) {
                        /* all threads of all groups, unless defgrp is set and
@@ -1248,7 +1248,8 @@ int thread_resolve_group_mask(struct thread_set *ts, int defgrp, char **err)
                         */
                        for (g = defgrp ? defgrp-1 : 0; g < (defgrp ? defgrp : global.nbtgroups); g++) {
                                new_ts.rel[g] = ha_tgroup_info[g].threads_enabled;
-                               new_ts.nbgrp++;
+                               if (new_ts.rel[g])
+                                       new_ts.grps |= 1UL << g;
                        }
                } else {
                        /* some absolute threads are set, we must remap them to
@@ -1271,9 +1272,9 @@ int thread_resolve_group_mask(struct thread_set *ts, int defgrp, char **err)
                                /* now the mask exactly matches the threads to be enabled
                                 * in this group.
                                 */
-                               if (!new_ts.rel[g] && mask)
-                                       new_ts.nbgrp++;
                                new_ts.rel[g] |= mask;
+                               if (new_ts.rel[g])
+                                       new_ts.grps |= 1UL << g;
                        }
                }
        } else {
@@ -1310,7 +1311,8 @@ int thread_resolve_group_mask(struct thread_set *ts, int defgrp, char **err)
                        }
 
                        new_ts.rel[g] = imask & mask;
-                       new_ts.nbgrp++;
+                       if (new_ts.rel[g])
+                               new_ts.grps |= 1UL << g;
                }
        }
 
@@ -1521,8 +1523,7 @@ int parse_thread_set(const char *arg, struct thread_set *ts, char **err)
                if (ts) {
                        if (is_rel) {
                                /* group-relative thread numbers */
-                               if (!ts->rel[tg - 1])
-                                       ts->nbgrp++;
+                               ts->grps |= 1UL << (tg - 1);
 
                                if (max >= min) {
                                        for (v = min; v <= max; v++)