From 2d90e642078c15f001b34a0a50a67fa6eac9a3b9 Mon Sep 17 00:00:00 2001 From: Alexey Tikhonov Date: Tue, 3 Mar 2020 18:44:11 +0100 Subject: [PATCH 28/35] mem-cache: sizes of free and data tables were made consistent Since size of "free table" didn't account for SSS_AVG_*_PAYLOAD factor only small fraction of "data table" was actually used. SSS_AVG_*_PAYLOAD differentiation for different payload types only affected size of hash table and was removed as unjustified. Resolves: https://github.com/SSSD/sssd/issues/5115 Reviewed-by: Sumit Bose --- src/responder/nss/nsssrv.c | 22 +++++++++++------- src/responder/nss/nsssrv_mmap_cache.c | 33 +++++++-------------------- src/responder/nss/nsssrv_mmap_cache.h | 2 -- src/util/mmap_cache.h | 3 --- 4 files changed, 22 insertions(+), 38 deletions(-) diff --git a/src/responder/nss/nsssrv.c b/src/responder/nss/nsssrv.c index 87300058f..21d93ae77 100644 --- a/src/responder/nss/nsssrv.c +++ b/src/responder/nss/nsssrv.c @@ -83,10 +83,9 @@ nss_clear_memcache(TALLOC_CTX *mem_ctx, return ret; } - /* TODO: read cache sizes from configuration */ DEBUG(SSSDBG_TRACE_FUNC, "Clearing memory caches.\n"); ret = sss_mmap_cache_reinit(nctx, nctx->mc_uid, nctx->mc_gid, - SSS_MC_CACHE_ELEMENTS, + -1, /* keep current size */ (time_t) memcache_timeout, &nctx->pwd_mc_ctx); if (ret != EOK) { @@ -96,7 +95,7 @@ nss_clear_memcache(TALLOC_CTX *mem_ctx, } ret = sss_mmap_cache_reinit(nctx, nctx->mc_uid, nctx->mc_gid, - SSS_MC_CACHE_ELEMENTS, + -1, /* keep current size */ (time_t) memcache_timeout, &nctx->grp_mc_ctx); if (ret != EOK) { @@ -106,7 +105,7 @@ nss_clear_memcache(TALLOC_CTX *mem_ctx, } ret = sss_mmap_cache_reinit(nctx, nctx->mc_uid, nctx->mc_gid, - SSS_MC_CACHE_ELEMENTS, + -1, /* keep current size */ (time_t)memcache_timeout, &nctx->initgr_mc_ctx); if (ret != EOK) { @@ -210,6 +209,11 @@ done: static int setup_memcaches(struct nss_ctx *nctx) { + /* TODO: read cache sizes from configuration */ + static const size_t SSS_MC_CACHE_PASSWD_SLOTS = 200000; /* 8mb */ + static const size_t SSS_MC_CACHE_GROUP_SLOTS = 150000; /* 6mb */ + static const size_t SSS_MC_CACHE_INITGROUP_SLOTS = 250000; /* 10mb */ + int ret; int memcache_timeout; @@ -239,11 +243,11 @@ static int setup_memcaches(struct nss_ctx *nctx) return EOK; } - /* TODO: read cache sizes from configuration */ ret = sss_mmap_cache_init(nctx, "passwd", nctx->mc_uid, nctx->mc_gid, SSS_MC_PASSWD, - SSS_MC_CACHE_ELEMENTS, (time_t)memcache_timeout, + SSS_MC_CACHE_PASSWD_SLOTS, + (time_t)memcache_timeout, &nctx->pwd_mc_ctx); if (ret) { DEBUG(SSSDBG_CRIT_FAILURE, "passwd mmap cache is DISABLED\n"); @@ -252,7 +256,8 @@ static int setup_memcaches(struct nss_ctx *nctx) ret = sss_mmap_cache_init(nctx, "group", nctx->mc_uid, nctx->mc_gid, SSS_MC_GROUP, - SSS_MC_CACHE_ELEMENTS, (time_t)memcache_timeout, + SSS_MC_CACHE_GROUP_SLOTS, + (time_t)memcache_timeout, &nctx->grp_mc_ctx); if (ret) { DEBUG(SSSDBG_CRIT_FAILURE, "group mmap cache is DISABLED\n"); @@ -261,7 +266,8 @@ static int setup_memcaches(struct nss_ctx *nctx) ret = sss_mmap_cache_init(nctx, "initgroups", nctx->mc_uid, nctx->mc_gid, SSS_MC_INITGROUPS, - SSS_MC_CACHE_ELEMENTS, (time_t)memcache_timeout, + SSS_MC_CACHE_INITGROUP_SLOTS, + (time_t)memcache_timeout, &nctx->initgr_mc_ctx); if (ret) { DEBUG(SSSDBG_CRIT_FAILURE, "initgroups mmap cache is DISABLED\n"); diff --git a/src/responder/nss/nsssrv_mmap_cache.c b/src/responder/nss/nsssrv_mmap_cache.c index 69e767690..5e23bbe6f 100644 --- a/src/responder/nss/nsssrv_mmap_cache.c +++ b/src/responder/nss/nsssrv_mmap_cache.c @@ -28,13 +28,6 @@ #include "responder/nss/nss_private.h" #include "responder/nss/nsssrv_mmap_cache.h" -/* arbitrary (avg of my /etc/passwd) */ -#define SSS_AVG_PASSWD_PAYLOAD (MC_SLOT_SIZE * 4) -/* short group name and no gids (private user group */ -#define SSS_AVG_GROUP_PAYLOAD (MC_SLOT_SIZE * 3) -/* average place for 40 supplementary groups + 2 names */ -#define SSS_AVG_INITGROUP_PAYLOAD (MC_SLOT_SIZE * 5) - #define MC_NEXT_BARRIER(val) ((((val) + 1) & 0x00ffffff) | 0xf0000000) #define MC_RAISE_BARRIER(m) do { \ @@ -1251,24 +1244,14 @@ errno_t sss_mmap_cache_init(TALLOC_CTX *mem_ctx, const char *name, enum sss_mc_type type, size_t n_elem, time_t timeout, struct sss_mc_ctx **mcc) { + /* sss_mc_header alone occupies whole slot, + * so each entry takes 2 slots at the very least + */ + static const int PAYLOAD_FACTOR = 2; + struct sss_mc_ctx *mc_ctx = NULL; - int payload; int ret, dret; - switch (type) { - case SSS_MC_PASSWD: - payload = SSS_AVG_PASSWD_PAYLOAD; - break; - case SSS_MC_GROUP: - payload = SSS_AVG_GROUP_PAYLOAD; - break; - case SSS_MC_INITGROUPS: - payload = SSS_AVG_INITGROUP_PAYLOAD; - break; - default: - return EINVAL; - } - mc_ctx = talloc_zero(mem_ctx, struct sss_mc_ctx); if (!mc_ctx) { return ENOMEM; @@ -1303,9 +1286,9 @@ errno_t sss_mmap_cache_init(TALLOC_CTX *mem_ctx, const char *name, /* hash table is double the size because it will store both forward and * reverse keys (name/uid, name/gid, ..) */ - mc_ctx->ht_size = MC_HT_SIZE(n_elem * 2); - mc_ctx->dt_size = MC_DT_SIZE(n_elem, payload); - mc_ctx->ft_size = MC_FT_SIZE(n_elem); + mc_ctx->ht_size = MC_HT_SIZE(2 * n_elem / PAYLOAD_FACTOR); + mc_ctx->dt_size = n_elem * MC_SLOT_SIZE; + mc_ctx->ft_size = n_elem / 8; /* 1 bit per slot */ mc_ctx->mmap_size = MC_HEADER_SIZE + MC_ALIGN64(mc_ctx->dt_size) + MC_ALIGN64(mc_ctx->ft_size) + diff --git a/src/responder/nss/nsssrv_mmap_cache.h b/src/responder/nss/nsssrv_mmap_cache.h index e06257949..c40af2fb4 100644 --- a/src/responder/nss/nsssrv_mmap_cache.h +++ b/src/responder/nss/nsssrv_mmap_cache.h @@ -22,8 +22,6 @@ #ifndef _NSSSRV_MMAP_CACHE_H_ #define _NSSSRV_MMAP_CACHE_H_ -#define SSS_MC_CACHE_ELEMENTS 50000 - struct sss_mc_ctx; enum sss_mc_type { diff --git a/src/util/mmap_cache.h b/src/util/mmap_cache.h index 63e096027..d3d92bc98 100644 --- a/src/util/mmap_cache.h +++ b/src/util/mmap_cache.h @@ -40,9 +40,6 @@ typedef uint32_t rel_ptr_t; #define MC_HT_SIZE(elems) ( (elems) * MC_32 ) #define MC_HT_ELEMS(size) ( (size) / MC_32 ) -#define MC_DT_SIZE(elems, payload) ( (elems) * (payload) ) -#define MC_FT_SIZE(elems) ( (elems) / 8 ) -/* ^^ 8 bits per byte so we need just elems/8 bytes to represent all blocks */ #define MC_PTR_ADD(ptr, bytes) (void *)((uint8_t *)(ptr) + (bytes)) #define MC_PTR_DIFF(ptr, base) ((uint8_t *)(ptr) - (uint8_t *)(base)) -- 2.21.3