]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: pools: evict excess objects using pool_evict_from_local_cache()
authorWilly Tarreau <w@1wt.eu>
Mon, 19 Apr 2021 06:14:03 +0000 (08:14 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 19 Apr 2021 13:24:33 +0000 (15:24 +0200)
Till now we could only evict oldest objects from all local caches using
pool_evict_from_local_caches() until the cache size was satisfying again,
but there was no way to evict excess objects from a single cache, which
is the reason why pool_put_to_cache() used to refrain from putting into
the local cache and would directly write to the shared cache, resulting
in massive writes when caches were full.

Let's add this new function now. It will stop once the number of objects
in the local cache is no higher than 16+total/8 or the cache size is no
more than 75% full, just like before.

For now the function is not used.

src/pool.c

index 487e0da28bc10eebc1bfaa8f34fc3c2b64a995ee..8ccb0c0b85eae5ea01cd78a4217efa73be6e2906 100644 (file)
@@ -186,6 +186,30 @@ void pool_free_nocache(struct pool_head *pool, void *ptr)
 
 #ifdef CONFIG_HAP_POOLS
 
+/* Evicts some of the oldest objects from one local cache, until its number of
+ * objects is no more than 16+1/8 of the total number of locally cached objects
+ * or the total size of the local cache is no more than 75% of its maximum (i.e.
+ * we don't want a single cache to use all the cache for itself). For this, the
+ * list is scanned in reverse.
+ */
+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) {
+               item = LIST_NEXT(&ph->list, typeof(item), by_pool);
+               ph->count--;
+               pool_cache_bytes -= pool->size;
+               pool_cache_count--;
+               LIST_DEL(&item->by_pool);
+               LIST_DEL(&item->by_lru);
+               pool_put_to_shared_cache(pool, item);
+       }
+}
+
 /* Evicts some of the oldest objects from the local cache, pushing them to the
  * global pool.
  */