From 33168caf638660f90fd5d607cdb4b2267a30c707 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Luk=C3=A1=C5=A1=20Ondr=C3=A1=C4=8Dek?= Date: Thu, 17 Jul 2025 01:35:46 +0200 Subject: [PATCH] lib/cache: decrease LMDB size by KRU size --- lib/cache/api.c | 11 ++++++++++- lib/cache/top.c | 33 +++++++++++++++++++++++++-------- lib/cache/top.h | 3 +++ 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/lib/cache/api.c b/lib/cache/api.c index 737d38857..1677240be 100644 --- a/lib/cache/api.c +++ b/lib/cache/api.c @@ -116,6 +116,15 @@ int kr_cache_open(struct kr_cache *cache, const struct kr_cdb_api *api, struct k if (!api) api = kr_cdb_lmdb(); cache->api = api; + + size_t orig_maxsize = opts->maxsize; + if (opts->maxsize) { + const size_t top_size = kr_cache_top_get_size(opts->maxsize); + if (!kr_fails_assert(top_size < opts->maxsize)) { + opts->maxsize -= top_size; + } + } + int ret = cache->api->open(&cache->db, &cache->stats, opts, mm); if (ret == 0) { ret = assert_right_version(cache); @@ -161,7 +170,7 @@ int kr_cache_open(struct kr_cache *cache, const struct kr_cdb_api *api, struct k ret = kr_error(errno); } if (ret == 0) { - ret = kr_cache_top_init(&cache->top, top_path, maxsize); + ret = kr_cache_top_init(&cache->top, top_path, orig_maxsize); free(top_path); } if (ret != 0) { diff --git a/lib/cache/top.c b/lib/cache/top.c index 37cdd363b..a26e5d5b1 100644 --- a/lib/cache/top.c +++ b/lib/cache/top.c @@ -17,9 +17,10 @@ #define FILE_FORMAT_VERSION 1 // fail if different -#define KRU_CAPACITY(cache_size) (cache_size / 64) // KRU size is approx. (8 * capacity) B - // average entry size seems to be 70-100 B - // -> KRU size is around 12.5 % of cache size, so 112.5 % of the set value is required +#define KRU_CAPACITY(cache_size) (cache_size / 128) // KRU size is approx. (8 * capacity) B + // average entry size seems to be 100-200 B, + // make KRU capacity between cache_size/128 and cache_size/64 (power of two) + // -> KRU size: between cache_size/16 and cache_size/8 (cache data size is the rest) #define TICK_SEC 1 #define NORMAL_SIZE (150 + KR_CACHE_SIZE_OVERHEAD) // B; normal size of cache entry // used as baseline for the following @@ -93,13 +94,29 @@ static inline bool first_access(struct kr_cache_top_context *ctx, kru_hash_t has } -int kr_cache_top_init(struct kr_cache_top *top, char *mmap_file, size_t cache_size) { +static inline void get_size_capacity(size_t cache_size, size_t *top_size, size_t *capacity_log) +{ + *top_size = 0; + *capacity_log = 0; + + const size_t capacity = KRU_CAPACITY(cache_size); + for (size_t c = capacity - 1; c > 0; c >>= 1) (*capacity_log)++; + + *top_size = offsetof(struct top_data, kru) + KRU.get_size(*capacity_log); +} + +int kr_cache_top_get_size(size_t cache_size) +{ + size_t top_size, capacity_log; + get_size_capacity(cache_size, &top_size, &capacity_log); + return top_size; +} + +int kr_cache_top_init(struct kr_cache_top *top, char *mmap_file, size_t cache_size) +{ size_t size = 0, capacity_log = 0; if (cache_size > 0) { - const size_t capacity = KRU_CAPACITY(cache_size); - for (size_t c = capacity - 1; c > 0; c >>= 1) capacity_log++; - - size = offsetof(struct top_data, kru) + KRU.get_size(capacity_log); + get_size_capacity(cache_size, &size, &capacity_log); } // else use existing file settings VERBOSE_LOG("INIT, cache size %d, KRU capacity_log %d\n", cache_size, capacity_log); diff --git a/lib/cache/top.h b/lib/cache/top.h index 8cdc17e91..c6eb920cb 100644 --- a/lib/cache/top.h +++ b/lib/cache/top.h @@ -33,6 +33,9 @@ static inline kru_price_t kr_cache_top_entry_price(struct kr_cache_top *top, siz return top->data->base_price_norm / size; } +KR_EXPORT +int kr_cache_top_get_size(size_t cache_size); + KR_EXPORT int kr_cache_top_init(struct kr_cache_top *top, char *mmap_file, size_t cache_size); -- 2.47.2