Skip to content

Commit

Permalink
memory fragmentation ratio in INFO output
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Sep 2, 2010
1 parent a047bf5 commit eddb388
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/config.h
Expand Up @@ -21,6 +21,11 @@
#define redis_stat stat
#endif

/* test for proc filesystem */
#ifdef __linux__
#define HAVE_PROCFS 1
#endif

/* test for backtrace() */
#if defined(__APPLE__) || defined(__linux__)
#define HAVE_BACKTRACE 1
Expand Down
2 changes: 2 additions & 0 deletions src/redis.c
Expand Up @@ -1166,6 +1166,7 @@ sds genRedisInfoString(void) {
"blocked_clients:%d\r\n"
"used_memory:%zu\r\n"
"used_memory_human:%s\r\n"
"mem_fragmentation_ratio:%.2f\r\n"
"changes_since_last_save:%lld\r\n"
"bgsave_in_progress:%d\r\n"
"last_save_time:%ld\r\n"
Expand All @@ -1192,6 +1193,7 @@ sds genRedisInfoString(void) {
server.blpop_blocked_clients,
zmalloc_used_memory(),
hmem,
zmalloc_get_fragmentation_ratio(),
server.dirty,
server.bgsavechildpid != -1,
server.lastsave,
Expand Down
42 changes: 42 additions & 0 deletions src/zmalloc.c
Expand Up @@ -32,6 +32,10 @@
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "config.h"

#if defined(__sun)
Expand Down Expand Up @@ -170,3 +174,41 @@ size_t zmalloc_used_memory(void) {
void zmalloc_enable_thread_safeness(void) {
zmalloc_thread_safe = 1;
}

/* Fragmentation = RSS / allocated-bytes */
float zmalloc_get_fragmentation_ratio(void) {
#ifdef HAVE_PROCFS
size_t allocated = zmalloc_used_memory();
int page = sysconf(_SC_PAGESIZE);
size_t rss;
char buf[4096];
char filename[256];
int fd, count;
char *p, *x;

snprintf(filename,256,"/proc/%d/stat",getpid());
if ((fd = open(filename,O_RDONLY)) == -1) return 0;
if (read(fd,buf,4096) <= 0) {
close(fd);
return 0;
}
close(fd);

p = buf;
count = 23; /* RSS is the 24th field in /proc/<pid>/stat */
while(p && count--) {
p = strchr(p,' ');
if (p) p++;
}
if (!p) return 0;
x = strchr(p,' ');
if (!x) return 0;
*x = '\0';

rss = strtoll(p,NULL,10);
rss *= page;
return (float)rss/allocated;
#else
return 0;
#endif
}
1 change: 1 addition & 0 deletions src/zmalloc.h
Expand Up @@ -38,5 +38,6 @@ void zfree(void *ptr);
char *zstrdup(const char *s);
size_t zmalloc_used_memory(void);
void zmalloc_enable_thread_safeness(void);
float zmalloc_get_fragmentation_ratio(void);

#endif /* _ZMALLOC_H */

0 comments on commit eddb388

Please sign in to comment.