From: Willy Tarreau Date: Sat, 17 Oct 2020 18:15:49 +0000 (+0200) Subject: MINOR: lb/chash: use a read lock in chash_get_server_hash() X-Git-Tag: v2.3-dev8~47 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4b6e3c284a98cb2e6b4758a418066bfce5b9cd05;p=thirdparty%2Fhaproxy.git MINOR: lb/chash: use a read lock in chash_get_server_hash() When using a low hash-balance-factor value, it's possible to loop many times trying to find the best server. Figures in the order of 100-300 times were observed for 1000 servers with a factor of 101 (which seems a bit excessive for such a large farm). Given that there's nothing in that function that prevents multiple threads from working in parallel, let's switch to a read lock. Tests on 8 threads show roughly a 2% performance increase with this. --- diff --git a/src/lb_chash.c b/src/lb_chash.c index a37a40ff2e..709105c0e7 100644 --- a/src/lb_chash.c +++ b/src/lb_chash.c @@ -324,7 +324,7 @@ struct server *chash_get_server_hash(struct proxy *p, unsigned int hash, const s unsigned int dn, dp; int loop; - HA_RWLOCK_WRLOCK(LBPRM_LOCK, &p->lbprm.lock); + HA_RWLOCK_RDLOCK(LBPRM_LOCK, &p->lbprm.lock); if (p->srv_act) root = &p->lbprm.chash.act; @@ -379,7 +379,7 @@ struct server *chash_get_server_hash(struct proxy *p, unsigned int hash, const s } out: - HA_RWLOCK_WRUNLOCK(LBPRM_LOCK, &p->lbprm.lock); + HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, &p->lbprm.lock); return nsrv; }