MINOR: queue: use atomic-ops to update the queue's index

Doing so allows to retrieve and update the pendconn's queue index outside
of the queue's lock and to save one more percent CPU on a highly-contented
backend.
This commit is contained in:
Willy Tarreau 2021-06-18 10:51:58 +02:00
parent 3eecdb65c5
commit 1335eb9867

View File

@ -133,7 +133,7 @@ unsigned int srv_dynamic_maxconn(const struct server *s)
*/ */
static void __pendconn_unlink_srv(struct pendconn *p) static void __pendconn_unlink_srv(struct pendconn *p)
{ {
p->strm->logs.srv_queue_pos += p->srv->queue.idx - p->queue_idx; p->strm->logs.srv_queue_pos += _HA_ATOMIC_LOAD(&p->srv->queue.idx) - p->queue_idx;
eb32_delete(&p->node); eb32_delete(&p->node);
} }
@ -146,7 +146,7 @@ static void __pendconn_unlink_srv(struct pendconn *p)
*/ */
static void __pendconn_unlink_prx(struct pendconn *p) static void __pendconn_unlink_prx(struct pendconn *p)
{ {
p->strm->logs.prx_queue_pos += p->px->queue.idx - p->queue_idx; p->strm->logs.prx_queue_pos += _HA_ATOMIC_LOAD(&p->px->queue.idx) - p->queue_idx;
eb32_delete(&p->node); eb32_delete(&p->node);
} }
@ -313,16 +313,16 @@ static struct pendconn *pendconn_process_next_strm(struct server *srv, struct pr
use_pp: use_pp:
/* Let's switch from the server pendconn to the proxy pendconn */ /* Let's switch from the server pendconn to the proxy pendconn */
__pendconn_unlink_prx(pp); __pendconn_unlink_prx(pp);
_HA_ATOMIC_INC(&px->queue.idx);
_HA_ATOMIC_DEC(&px->queue.length); _HA_ATOMIC_DEC(&px->queue.length);
_HA_ATOMIC_DEC(&px->totpend); _HA_ATOMIC_DEC(&px->totpend);
px->queue.idx++;
p = pp; p = pp;
goto unlinked; goto unlinked;
use_p: use_p:
__pendconn_unlink_srv(p); __pendconn_unlink_srv(p);
_HA_ATOMIC_INC(&srv->queue.idx);
_HA_ATOMIC_DEC(&srv->queue.length); _HA_ATOMIC_DEC(&srv->queue.length);
_HA_ATOMIC_DEC(&px->totpend); _HA_ATOMIC_DEC(&px->totpend);
srv->queue.idx++;
unlinked: unlinked:
p->strm_flags |= SF_ASSIGNED; p->strm_flags |= SF_ASSIGNED;
p->target = srv; p->target = srv;
@ -422,6 +422,7 @@ struct pendconn *pendconn_add(struct stream *strm)
max_ptr = &px->be_counters.nbpend_max; max_ptr = &px->be_counters.nbpend_max;
} }
p->queue_idx = _HA_ATOMIC_LOAD(&q->idx) - 1; // for logging only
new_max = _HA_ATOMIC_ADD_FETCH(&q->length, 1); new_max = _HA_ATOMIC_ADD_FETCH(&q->length, 1);
old_max = _HA_ATOMIC_LOAD(max_ptr); old_max = _HA_ATOMIC_LOAD(max_ptr);
while (new_max > old_max) { while (new_max > old_max) {
@ -431,7 +432,6 @@ struct pendconn *pendconn_add(struct stream *strm)
__ha_barrier_atomic_store(); __ha_barrier_atomic_store();
HA_SPIN_LOCK(QUEUE_LOCK, &q->lock); HA_SPIN_LOCK(QUEUE_LOCK, &q->lock);
p->queue_idx = q->idx - 1; // for increment
eb32_insert(&q->head, &p->node); eb32_insert(&q->head, &p->node);
HA_SPIN_UNLOCK(QUEUE_LOCK, &q->lock); HA_SPIN_UNLOCK(QUEUE_LOCK, &q->lock);