From 3862838921eb8c3bf822079447bf678cee0dbd06 Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Fri, 23 Oct 2020 15:56:22 +0000 Subject: [PATCH] cache: reduce memory waste in struct namecache The previous scheme for calculating the total size was doing sizeof on the struct and then adding the wanted space for the buffer. nc_name is at offset 58 while sizeof(struct namecache) is 64. With CACHE_PATH_CUTOFF of 39 bytes and 1 byte of padding we were allocating 104 bytes for the entry and never accounting for the 6 byte padding, wasting that space. --- sys/kern/vfs_cache.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/sys/kern/vfs_cache.c b/sys/kern/vfs_cache.c index 3e3d54febef5..4ecdc3818a7e 100644 --- a/sys/kern/vfs_cache.c +++ b/sys/kern/vfs_cache.c @@ -162,6 +162,7 @@ struct namecache_ts { struct timespec nc_time; /* timespec provided by fs */ struct timespec nc_dotdottime; /* dotdot timespec provided by fs */ int nc_ticks; /* ticks value when entry was added */ + int nc_pad; struct namecache nc_nc; }; @@ -172,12 +173,19 @@ struct namecache_ts { * alignment for everyone. Note this is a nop for 64-bit platforms. */ #define CACHE_ZONE_ALIGNMENT UMA_ALIGNOF(time_t) -#define CACHE_PATH_CUTOFF 39 -#define CACHE_ZONE_SMALL_SIZE (sizeof(struct namecache) + CACHE_PATH_CUTOFF + 1) -#define CACHE_ZONE_SMALL_TS_SIZE (sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1) -#define CACHE_ZONE_LARGE_SIZE (sizeof(struct namecache) + NAME_MAX + 1) -#define CACHE_ZONE_LARGE_TS_SIZE (sizeof(struct namecache_ts) + NAME_MAX + 1) +#ifdef __LP64__ +#define CACHE_PATH_CUTOFF 45 +#define CACHE_LARGE_PAD 6 +#else +#define CACHE_PATH_CUTOFF 41 +#define CACHE_LARGE_PAD 2 +#endif + +#define CACHE_ZONE_SMALL_SIZE (offsetof(struct namecache, nc_name) + CACHE_PATH_CUTOFF + 1) +#define CACHE_ZONE_SMALL_TS_SIZE (offsetof(struct namecache_ts, nc_nc) + CACHE_ZONE_SMALL_SIZE) +#define CACHE_ZONE_LARGE_SIZE (offsetof(struct namecache, nc_name) + NAME_MAX + 1 + CACHE_LARGE_PAD) +#define CACHE_ZONE_LARGE_TS_SIZE (offsetof(struct namecache_ts, nc_nc) + CACHE_ZONE_LARGE_SIZE) _Static_assert((CACHE_ZONE_SMALL_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size"); _Static_assert((CACHE_ZONE_SMALL_TS_SIZE % (CACHE_ZONE_ALIGNMENT + 1)) == 0, "bad zone size");