From fa19d20ac4fbfc04441143128108f5d8807bf983 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 19 Apr 2021 08:50:45 +0200 Subject: [PATCH] MEDIUM: pools: make pool_put_to_cache() always call pool_put_to_local_cache() Till now it used to call it only if there were not too many objects into the local cache otherwise would send the latest one directly into the shared cache. Now it always sends to the local cache and it's up to the local cache to free its oldest objects. From a cache freshness perspective it's better this way since we always evict cold objects instead of hot ones. From an API perspective it's better because it will help make the shared cache invisible to the public API. --- include/haproxy/pool.h | 16 ++++++---------- src/pool.c | 1 - 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/include/haproxy/pool.h b/include/haproxy/pool.h index dc01e1a80..d7ae5f068 100644 --- a/include/haproxy/pool.h +++ b/include/haproxy/pool.h @@ -72,6 +72,7 @@ void pool_destroy_all(); extern THREAD_LOCAL size_t pool_cache_bytes; /* total cache size */ extern THREAD_LOCAL size_t pool_cache_count; /* #cache objects */ +void pool_evict_from_local_cache(struct pool_head *pool); void pool_evict_from_local_caches(); /* returns true if the pool is considered to have too many free objects */ @@ -120,6 +121,10 @@ static inline void pool_put_to_local_cache(struct pool_head *pool, void *ptr) pool_cache_count++; pool_cache_bytes += pool->size; + if (unlikely(pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4 && + ph->count >= 16 + pool_cache_count / 8)) + pool_evict_from_local_cache(pool); + if (unlikely(pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE)) pool_evict_from_local_caches(); } @@ -286,16 +291,7 @@ static inline void *pool_get_from_cache(struct pool_head *pool) */ static inline void pool_put_to_cache(struct pool_head *pool, void *ptr) { - /* put the object back into the cache only if there are not too - * many objects yet in this pool (no more than half of the cached - * is used or this pool uses no more than 1/8 of the cache size). - */ - if ((pool_cache_bytes <= CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4 || - pool->cache[tid].count < 16 + pool_cache_count / 8)) { - pool_put_to_local_cache(pool, ptr); - return; - } - pool_put_to_shared_cache(pool, ptr); + pool_put_to_local_cache(pool, ptr); } diff --git a/src/pool.c b/src/pool.c index 8ccb0c0b8..bc568d225 100644 --- a/src/pool.c +++ b/src/pool.c @@ -196,7 +196,6 @@ void pool_evict_from_local_cache(struct pool_head *pool) { struct pool_cache_head *ph = &pool->cache[tid]; struct pool_cache_item *item; - struct pool_head *pool; while (ph->count >= 16 + pool_cache_count / 8 && pool_cache_bytes > CONFIG_HAP_POOL_CACHE_SIZE * 3 / 4) {