Skip to content

Commit b0d56ad

Browse files
authored
Add metrics of min and max memory consumption of indexes - [MOD-7584] (#5143)
* implement min and max memory usage metrics * added to info module test
1 parent 8cffbb6 commit b0d56ad

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

src/info_command.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ extern "C" {
1414
#endif
1515

1616
typedef struct TotalSpecsInfo {
17-
size_t total_mem; // Total memory used by the index
17+
size_t total_mem; // Total memory used by the indexes
18+
size_t min_mem; // Memory used by the smallest (local) index
19+
size_t max_mem; // Memory used by the largest (local) index
1820
size_t indexing_time; // Time spent on indexing
1921
InfoGCStats gc_stats; // Garbage collection statistics
2022
} TotalSpecsInfo;
@@ -23,4 +25,4 @@ int IndexInfoCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc);
2325
#ifdef __cplusplus
2426
}
2527
#endif
26-
#endif
28+
#endif

src/module-init/module-init.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ static int initAsLibrary(RedisModuleCtx *ctx) {
8686
return REDISMODULE_OK;
8787
}
8888

89+
#define MEMORY_HUMAN(x) ((x) / (double)(1024 * 1024))
90+
8991
void RS_moduleInfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
9092
// Module version
9193
RedisModule_InfoAddSection(ctx, "version");
@@ -113,7 +115,11 @@ void RS_moduleInfoFunc(RedisModuleInfoCtx *ctx, int for_crash_report) {
113115
RedisModule_InfoAddSection(ctx, "memory");
114116
TotalSpecsInfo total_info = RediSearch_TotalInfo();
115117
RedisModule_InfoAddFieldDouble(ctx, "used_memory_indexes", total_info.total_mem);
116-
RedisModule_InfoAddFieldDouble(ctx, "used_memory_indexes_human", total_info.total_mem / (float)0x100000);
118+
RedisModule_InfoAddFieldDouble(ctx, "used_memory_indexes_human", MEMORY_HUMAN(total_info.total_mem));
119+
RedisModule_InfoAddFieldDouble(ctx, "min_memory_index", total_info.min_mem);
120+
RedisModule_InfoAddFieldDouble(ctx, "min_memory_index_human", MEMORY_HUMAN(total_info.min_mem));
121+
RedisModule_InfoAddFieldDouble(ctx, "max_memory_index", total_info.max_mem);
122+
RedisModule_InfoAddFieldDouble(ctx, "max_memory_index_human", MEMORY_HUMAN(total_info.max_mem));
117123
RedisModule_InfoAddFieldDouble(ctx, "total_indexing_time", total_info.indexing_time / (float)CLOCKS_PER_MILLISEC);
118124

119125
// Cursors

src/redisearch_api.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,7 @@ size_t RediSearch_MemUsage(RSIndex* rm) {
928928
// existing indexes
929929
TotalSpecsInfo RediSearch_TotalInfo(void) {
930930
TotalSpecsInfo info = {0};
931+
info.min_mem = -1; // Initialize to max value
931932
// Traverse `specDict_g`, and aggregate the mem-usage and indexing time of each index
932933
dictIterator *iter = dictGetIterator(specDict_g);
933934
dictEntry *entry;
@@ -939,7 +940,10 @@ TotalSpecsInfo RediSearch_TotalInfo(void) {
939940
}
940941
// Lock for read
941942
pthread_rwlock_rdlock(&sp->rwlock);
942-
info.total_mem += RediSearch_MemUsage((RSIndex *)ref.rm);
943+
size_t cur_mem = RediSearch_MemUsage((RSIndex *)ref.rm);
944+
info.total_mem += cur_mem;
945+
if (info.min_mem > cur_mem) info.min_mem = cur_mem;
946+
if (info.max_mem < cur_mem) info.max_mem = cur_mem;
943947
info.indexing_time += sp->stats.totalIndexTime;
944948

945949
if (sp->gc) {
@@ -951,6 +955,7 @@ TotalSpecsInfo RediSearch_TotalInfo(void) {
951955
pthread_rwlock_unlock(&sp->rwlock);
952956
}
953957
dictReleaseIterator(iter);
958+
if (info.min_mem == -1) info.min_mem = 0; // No index found
954959
return info;
955960
}
956961

tests/pytests/test_info_modules.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ def test_redis_info():
162162
env.assertEqual(res['search_fields_tag']['Sortable'], 1)
163163
env.assertGreater(res['search_used_memory_indexes'], 0)
164164
env.assertGreater(res['search_used_memory_indexes_human'], 0)
165+
env.assertGreater(res['search_min_memory_index'], 0)
166+
env.assertGreater(res['search_min_memory_index_human'], 0)
167+
env.assertGreater(res['search_max_memory_index'], 0)
168+
env.assertGreater(res['search_max_memory_index_human'], 0)
165169
# env.assertGreater(res['search_total_indexing_time'], 0) # Introduces flakiness
166170
env.assertEqual(res['search_global_idle'], 0)
167171
env.assertEqual(res['search_global_total'], 0)

0 commit comments

Comments
 (0)