From a7d6a1396ec9608e6b9d48093bc7996cafbd61b9 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 11 Oct 2022 15:42:54 +0200 Subject: [PATCH] MEDIUM: stick-table: switch to rdlock in stktable_lookup() and lookup_key() These functions do not modify anything in the the table except the refcount on success. Let's just lock the table for shared accesses and make use of atomic ops to update the refcount. This brings a nice gain from 425k to 455k under 48 threads (7%), but some contention remains on the exclusive locks in other parts. Note that the refcount continues to be updated under the lock because it's not yet certain whether there are races between it and some of the exclusive lock on the table. The difference is marginal and we prefer to stay on the safe side for now. --- src/stick_table.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/stick_table.c b/src/stick_table.c index 95913b125..44a115fd1 100644 --- a/src/stick_table.c +++ b/src/stick_table.c @@ -335,11 +335,11 @@ struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key { struct stksess *ts; - HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock); + HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock); ts = __stktable_lookup_key(t, key); if (ts) - ts->ref_cnt++; - HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock); + HA_ATOMIC_INC(&ts->ref_cnt); + HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock); return ts; } @@ -373,11 +373,11 @@ struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts) { struct stksess *lts; - HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock); + HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock); lts = __stktable_lookup(t, ts); if (lts) - lts->ref_cnt++; - HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock); + HA_ATOMIC_INC(<s->ref_cnt); + HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock); return lts; }