]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: logs: use a single index to store the current range and index
authorWilly Tarreau <w@1wt.eu>
Wed, 20 Sep 2023 18:30:27 +0000 (20:30 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 20 Sep 2023 19:38:33 +0000 (21:38 +0200)
By using a single long long to store both the current range and the
next index, we'll make it possible to perform atomic operations instead
of locking. Let's only regroup them for now under a new "curr_rg_idx".
The upper word is the range, the lower is the index.

include/haproxy/log-t.h
src/log.c

index 24450518442904b06f27864d6dc86c01a2036088..a503ef9afd62da4a69ec783b2ca7987421ed3c1c 100644 (file)
@@ -215,10 +215,7 @@ struct smp_info {
        struct smp_log_range *smp_rgs; /* Array of ranges for log sampling. */
        size_t smp_rgs_sz;             /* The size of <smp_rgs> array. */
        size_t smp_sz;             /* The total number of logs to be sampled. */
-       unsigned int curr_rg;      /* The current range to be sampled. */
-       unsigned int curr_idx;     /* A counter to store the current index of the log
-                                   * already sampled.
-                                   */
+       ullong curr_rg_idx;        /* 63:32 = current range; 31:0 = current index */
 };
 
 struct logsrv {
index 6cc8050c13768ee0d06ab3fded6a93c88a627267..a03172bb73760d3f436f327cda2c9ada557a700a 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -1830,11 +1830,14 @@ void process_send_log(struct list *logsrvs, int level, int facility,
 
                if (logsrv->lb.smp_rgs) {
                        struct smp_log_range *smp_rg;
-                       unsigned int next_idx;
+                       uint next_idx, curr_rg;
+                       ullong curr_rg_idx;
 
                        HA_SPIN_LOCK(LOGSRV_LOCK, &logsrv->lock);
-                       next_idx = logsrv->lb.curr_idx + 1;
-                       smp_rg = &logsrv->lb.smp_rgs[logsrv->lb.curr_rg];
+                       curr_rg_idx = logsrv->lb.curr_rg_idx;
+                       next_idx = (curr_rg_idx & 0xFFFFFFFFU) + 1;
+                       curr_rg  = curr_rg_idx >> 32;
+                       smp_rg = &logsrv->lb.smp_rgs[curr_rg];
 
                        /* check if the index we're going to take is within range  */
                        in_range = smp_rg->low <= next_idx && next_idx <= smp_rg->high;
@@ -1842,10 +1845,13 @@ void process_send_log(struct list *logsrvs, int level, int facility,
                                /* Let's consume this range. */
                                if (next_idx == smp_rg->high) {
                                        /* If consumed, let's select the next range. */
-                                       logsrv->lb.curr_rg = (logsrv->lb.curr_rg + 1) % logsrv->lb.smp_rgs_sz;
+                                       curr_rg = (curr_rg + 1) % logsrv->lb.smp_rgs_sz;
                                }
                        }
-                       logsrv->lb.curr_idx = next_idx % logsrv->lb.smp_sz;
+
+                       next_idx = next_idx % logsrv->lb.smp_sz;
+                       curr_rg_idx = ((ullong)curr_rg << 32) + next_idx;
+                       logsrv->lb.curr_rg_idx = curr_rg_idx;
                        HA_SPIN_UNLOCK(LOGSRV_LOCK, &logsrv->lock);
                }
                if (in_range)