From 3fba3ccd96b066d10401f1f9b65293d59d14082c Mon Sep 17 00:00:00 2001 From: sundb Date: Tue, 7 Mar 2023 15:06:58 +0800 Subject: [PATCH] Skip test for sdsRemoveFreeSpace when mem_allocator is not jemalloc (#11878) Test `trim on SET with big value` (introduced from #11817) fails under mac m1 with libc mem_allocator. The reason is that malloc(33000) will allocate 65536 bytes(>42000). This test still passes under ubuntu with libc mem_allocator. ``` *** [err]: trim on SET with big value in tests/unit/type/string.tcl Expected [r memory usage key] < 42000 (context: type source line 471 file /Users/iospack/data/redis_fork/tests/unit/type/string.tcl cmd {assert {[r memory usage key] < 42000}} proc ::test) ``` simple test under mac m1 with libc mem_allocator: ```c void *p = zmalloc(33000); printf("malloc size: %zu\n", zmalloc_size(p)); # output malloc size: 65536 ``` --- tests/modules/basics.c | 13 ++++++++++++- tests/unit/moduleapi/misc.tcl | 2 ++ tests/unit/type/string.tcl | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/modules/basics.c b/tests/modules/basics.c index 579d8f629..897cb5d87 100644 --- a/tests/modules/basics.c +++ b/tests/modules/basics.c @@ -417,11 +417,22 @@ int TestTrimString(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { size_t string_len = RedisModule_MallocSizeString(s); RedisModule_TrimStringAllocation(s); size_t len_after_trim = RedisModule_MallocSizeString(s); - if (len_after_trim < string_len) { + + /* Determine if using jemalloc memory allocator. */ + RedisModuleServerInfoData *info = RedisModule_GetServerInfo(ctx, "memory"); + const char *field = RedisModule_ServerInfoGetFieldC(info, "mem_allocator"); + int use_jemalloc = !strncmp(field, "jemalloc", 8); + + /* Jemalloc will reallocate `s` from 2k to 1k after RedisModule_TrimStringAllocation(), + * but non-jemalloc memory allocators may keep the old size. */ + if ((use_jemalloc && len_after_trim < string_len) || + (!use_jemalloc && len_after_trim <= string_len)) + { RedisModule_ReplyWithSimpleString(ctx, "OK"); } else { RedisModule_ReplyWithError(ctx, "String was not trimmed as expected."); } + RedisModule_FreeServerInfo(ctx, info); RedisModule_Free(tmp); RedisModule_FreeString(ctx,s); return REDISMODULE_OK; diff --git a/tests/unit/moduleapi/misc.tcl b/tests/unit/moduleapi/misc.tcl index 4bf0d58ea..6bf7b8c2a 100644 --- a/tests/unit/moduleapi/misc.tcl +++ b/tests/unit/moduleapi/misc.tcl @@ -525,6 +525,7 @@ start_server {tags {"modules"}} { assert_equal {0} [r test.get_n_events] } +if {[string match {*jemalloc*} [s mem_allocator]]} { test {test RM_Call with large arg for SET command} { # set a big value to trigger increasing the query buf r set foo [string repeat A 100000] @@ -533,6 +534,7 @@ start_server {tags {"modules"}} { # asset the value was trimmed assert {[r memory usage bar] < 42000}; # 42K to count for Jemalloc's additional memory overhead. } +} ;# if jemalloc test "Unload the module - misc" { assert_equal {OK} [r module unload misc] diff --git a/tests/unit/type/string.tcl b/tests/unit/type/string.tcl index f32f72368..3734d1f50 100644 --- a/tests/unit/type/string.tcl +++ b/tests/unit/type/string.tcl @@ -460,6 +460,7 @@ start_server {tags {"string"}} { } } +if {[string match {*jemalloc*} [s mem_allocator]]} { test {trim on SET with big value} { # set a big value to trigger increasing the query buf r set key [string repeat A 100000] @@ -468,6 +469,7 @@ start_server {tags {"string"}} { # asset the value was trimmed assert {[r memory usage key] < 42000}; # 42K to count for Jemalloc's additional memory overhead. } +} ;# if jemalloc test {Extended SET can detect syntax errors} { set e {}