Skip to content

Commit 80dcbad

Browse files
zliang-scGitHub Enterprise
authored andcommitted
Merge pull request #216 from Snapchat/add-free-memory-and-shard-num-metrics-emit-statsd
emit system free memory metrics with '/proc/meminfo'
2 parents abd6e77 + eb14f42 commit 80dcbad

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

src/modules/keydb_modstatsd/modmain.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <sys/time.h>
1313
#include <regex>
1414
#include <sstream>
15+
#include <iostream>
16+
#include <fstream>
1517

1618
using namespace Statsd;
1719

@@ -529,6 +531,18 @@ void handle_client_list_response(struct RedisModuleCtx *ctx, const char *szReply
529531
g_stats->gauge("total_replica_client_output_buffer", totalReplicaClientOutputBuffer);
530532
}
531533

534+
void emit_system_free_memory() {
535+
std::ifstream meminfo("/proc/meminfo");
536+
std::string line;
537+
while (std::getline(meminfo, line)) {
538+
if (line.find("MemFree:") != std::string::npos) {
539+
unsigned long memFreeInKB;
540+
std::sscanf(line.c_str(), "MemFree: %lu kB", &memFreeInKB);
541+
g_stats->gauge("systemFreeMemory_MB", memFreeInKB / 1024);
542+
return;
543+
}
544+
}
545+
}
532546

533547
void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64_t subevent, void *data) {
534548
static time_t lastTime = 0;
@@ -600,6 +614,10 @@ void event_cron_handler(struct RedisModuleCtx *ctx, RedisModuleEvent eid, uint64
600614
// g_stats->timing("handle_client_info_time_taken_us", ustime() - commandStartTime);
601615
// RedisModule_FreeCallReply(reply);
602616

617+
commandStartTime = ustime();
618+
emit_system_free_memory();
619+
g_stats->timing("emit_free_system_memory_time_taken_us", ustime() - commandStartTime);
620+
603621
/* Log Keys */
604622
reply = RedisModule_Call(ctx, "dbsize", "");
605623
long long keys = RedisModule_CallReplyInteger(reply);

0 commit comments

Comments
 (0)