From 6fa696bb988c826f5078c8296fb3bcb5ae25c681 Mon Sep 17 00:00:00 2001 From: zliang Date: Thu, 17 Aug 2023 19:31:52 -0600 Subject: [PATCH 1/2] emit system free metrics with '/proc/meminfo' --- src/modules/keydb_modstatsd/modmain.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/modules/keydb_modstatsd/modmain.cpp b/src/modules/keydb_modstatsd/modmain.cpp index ab5d12b05..b40fc3c76 100644 --- a/src/modules/keydb_modstatsd/modmain.cpp +++ b/src/modules/keydb_modstatsd/modmain.cpp @@ -12,6 +12,8 @@ #include #include #include +#include +#include using namespace Statsd; @@ -529,6 +531,18 @@ void handle_client_list_response(struct RedisModuleCtx *ctx, const char *szReply g_stats->gauge("total_replica_client_output_buffer", totalReplicaClientOutputBuffer); } +void emit_system_free_memory() { + std::ifstream meminfo("/proc/meminfo"); + std::string line; + while (std::getline(meminfo, line)) { + if (line.find("MemFree:") != std::string::npos) { + unsigned long memFreeInKB; + std::sscanf(line.c_str(), "MemFree: %lu kB", &memFreeInKB); + g_stats->gauge("systemFreeMemory_MB", memFreeInKB / 1024); + return; + } + } +} void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64_t subevent, void *data) { static time_t lastTime = 0; @@ -607,6 +621,8 @@ void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64 g_stats->gauge("keys", keys); RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, "Emitting metric \"keys\": %llu", keys); g_stats->timing("metrics_time_taken_us", ustime() - startTime); + + emit_system_free_memory(); lastTime = curTime; } From eb14f4298cc69f27a5b093549e8eeb4cabec3d2c Mon Sep 17 00:00:00 2001 From: zliang Date: Fri, 18 Aug 2023 10:20:00 -0600 Subject: [PATCH 2/2] have emit system free memory within metrics_time_taken_us and also add metric time taken for it --- src/modules/keydb_modstatsd/modmain.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/modules/keydb_modstatsd/modmain.cpp b/src/modules/keydb_modstatsd/modmain.cpp index b40fc3c76..011d6108e 100644 --- a/src/modules/keydb_modstatsd/modmain.cpp +++ b/src/modules/keydb_modstatsd/modmain.cpp @@ -614,6 +614,10 @@ void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64 // g_stats->timing("handle_client_info_time_taken_us", ustime() - commandStartTime); // RedisModule_FreeCallReply(reply); + commandStartTime = ustime(); + emit_system_free_memory(); + g_stats->timing("emit_free_system_memory_time_taken_us", ustime() - commandStartTime); + /* Log Keys */ reply = RedisModule_Call(ctx, "dbsize", ""); long long keys = RedisModule_CallReplyInteger(reply); @@ -621,8 +625,6 @@ void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64 g_stats->gauge("keys", keys); RedisModule_Log(ctx, REDISMODULE_LOGLEVEL_DEBUG, "Emitting metric \"keys\": %llu", keys); g_stats->timing("metrics_time_taken_us", ustime() - startTime); - - emit_system_free_memory(); lastTime = curTime; }