From 945a2f948eb518090325b0ceab92588a905e0f92 Mon Sep 17 00:00:00 2001 From: antirez Date: Mon, 19 Sep 2016 10:28:05 +0200 Subject: zmalloc: zmalloc_get_smap_bytes_by_field() modified to work for any PID. The goal is to get copy-on-write amount of the child from the parent. --- src/aof.c | 2 +- src/latency.c | 2 +- src/rdb.c | 4 ++-- src/zmalloc.c | 24 ++++++++++++++++++------ src/zmalloc.h | 4 ++-- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/aof.c b/src/aof.c index 5523066b5..fc261db21 100644 --- a/src/aof.c +++ b/src/aof.c @@ -1328,7 +1328,7 @@ int rewriteAppendOnlyFileBackground(void) { redisSetProcTitle("redis-aof-rewrite"); snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid()); if (rewriteAppendOnlyFile(tmpfile) == C_OK) { - size_t private_dirty = zmalloc_get_private_dirty(); + size_t private_dirty = zmalloc_get_private_dirty(-1); if (private_dirty) { serverLog(LL_NOTICE, diff --git a/src/latency.c b/src/latency.c index 6f8b2a59f..53e0ec7be 100644 --- a/src/latency.c +++ b/src/latency.c @@ -79,7 +79,7 @@ int THPIsEnabled(void) { * value of the function is non-zero, the process is being targeted by * THP support, and is likely to have memory usage / latency issues. */ int THPGetAnonHugePagesSize(void) { - return zmalloc_get_smap_bytes_by_field("AnonHugePages:"); + return zmalloc_get_smap_bytes_by_field("AnonHugePages:",-1); } /* ---------------------------- Latency API --------------------------------- */ diff --git a/src/rdb.c b/src/rdb.c index 0cda23c5d..fd80fb3c9 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -1024,7 +1024,7 @@ int rdbSaveBackground(char *filename) { redisSetProcTitle("redis-rdb-bgsave"); retval = rdbSave(filename); if (retval == C_OK) { - size_t private_dirty = zmalloc_get_private_dirty(); + size_t private_dirty = zmalloc_get_private_dirty(-1); if (private_dirty) { serverLog(LL_NOTICE, @@ -1761,7 +1761,7 @@ int rdbSaveToSlavesSockets(void) { retval = C_ERR; if (retval == C_OK) { - size_t private_dirty = zmalloc_get_private_dirty(); + size_t private_dirty = zmalloc_get_private_dirty(-1); if (private_dirty) { serverLog(LL_NOTICE, diff --git a/src/zmalloc.c b/src/zmalloc.c index ab4af99e2..367258746 100644 --- a/src/zmalloc.c +++ b/src/zmalloc.c @@ -304,15 +304,26 @@ float zmalloc_get_fragmentation_ratio(size_t rss) { * /proc/self/smaps. The field must be specified with trailing ":" as it * apperas in the smaps output. * - * Example: zmalloc_get_smap_bytes_by_field("Rss:"); + * If a pid is specified, the information is extracted for such a pid, + * otherwise if pid is -1 the information is reported is about the + * current process. + * + * Example: zmalloc_get_smap_bytes_by_field("Rss:",-1); */ #if defined(HAVE_PROC_SMAPS) -size_t zmalloc_get_smap_bytes_by_field(char *field) { +size_t zmalloc_get_smap_bytes_by_field(char *field, long pid) { char line[1024]; size_t bytes = 0; - FILE *fp = fopen("/proc/self/smaps","r"); int flen = strlen(field); + if (pid == -1) { + FILE *fp = fopen("/proc/self/smaps","r"); + } else { + char filename[128]; + snprintf(filename,sizeof(filename),"/proc/%ld/smaps",pid); + FILE *fp = fopen(filename,"r"); + } + if (!fp) return 0; while(fgets(line,sizeof(line),fp) != NULL) { if (strncmp(line,field,flen) == 0) { @@ -327,14 +338,15 @@ size_t zmalloc_get_smap_bytes_by_field(char *field) { return bytes; } #else -size_t zmalloc_get_smap_bytes_by_field(char *field) { +size_t zmalloc_get_smap_bytes_by_field(char *field, long pid) { ((void) field); + ((void) pid); return 0; } #endif -size_t zmalloc_get_private_dirty(void) { - return zmalloc_get_smap_bytes_by_field("Private_Dirty:"); +size_t zmalloc_get_private_dirty(long pid) { + return zmalloc_get_smap_bytes_by_field("Private_Dirty:",pid); } /* Returns the size of physical memory (RAM) in bytes. diff --git a/src/zmalloc.h b/src/zmalloc.h index a47ea6ccf..9badf8f4c 100644 --- a/src/zmalloc.h +++ b/src/zmalloc.h @@ -75,8 +75,8 @@ void zmalloc_enable_thread_safeness(void); void zmalloc_set_oom_handler(void (*oom_handler)(size_t)); float zmalloc_get_fragmentation_ratio(size_t rss); size_t zmalloc_get_rss(void); -size_t zmalloc_get_private_dirty(void); -size_t zmalloc_get_smap_bytes_by_field(char *field); +size_t zmalloc_get_private_dirty(long pid); +size_t zmalloc_get_smap_bytes_by_field(char *field, long pid); size_t zmalloc_get_memory_size(void); void zlibc_free(void *ptr); -- cgit v1.2.1