From: antirez Date: Fri, 8 Jan 2010 15:38:48 +0000 (-0500) Subject: used_memory_human added to INFO output. Human readable amount of memory used. X-Git-Url: https://git.saurik.com/redis.git/commitdiff_plain/ec6c7a1d78d4f42f78d5e006d7b1e804388088a1 used_memory_human added to INFO output. Human readable amount of memory used. --- diff --git a/TODO b/TODO index 70e12672..ca8e6ddc 100644 --- a/TODO +++ b/TODO @@ -13,11 +13,9 @@ VERSION 1.4 TODO (Hash type) Virtual Memory sub-TODO: * Check if the page selection algorithm is working well. -* Fix support for large files * Divide swappability of objects by refcount -* While loading DB from snapshot or AOF, swap objects as needed if maxmemory - is reached, calling swapOneObject(). * vm-swap-file . The swap file should go where the user wants, and if it's already there and of the right size we can avoid to create it again. +* it should be possible to give the vm-max-memory option in megabyte, gigabyte, ..., just using 2GB, 100MB, and so forth. VERSION 1.6 TODO (Virtual memory) ================================= diff --git a/redis.c b/redis.c index 7caa8103..35e11fa5 100644 --- a/redis.c +++ b/redis.c @@ -5407,6 +5407,27 @@ static void sortCommand(redisClient *c) { zfree(vector); } +/* Convert an amount of bytes into a human readable string in the form + * of 100B, 2G, 100M, 4K, and so forth. */ +static void bytesToHuman(char *s, unsigned long long n) { + double d; + + if (n < 1024) { + /* Bytes */ + sprintf(s,"%lluB",n); + return; + } else if (n < (1024*1024)) { + d = (double)n/(1024); + sprintf(s,"%.2fK",d); + } else if (n < (1024LL*1024*1024)) { + d = (double)n/(1024*1024); + sprintf(s,"%.2fM",d); + } else if (n < (1024LL*1024*1024*1024)) { + d = (double)n/(1024LL*1024*1024); + sprintf(s,"%.2fM",d); + } +} + /* Create the string returned by the INFO command. This is decoupled * by the INFO command itself as we need to report the same information * on memory corruption problems. */ @@ -5414,7 +5435,9 @@ static sds genRedisInfoString(void) { sds info; time_t uptime = time(NULL)-server.stat_starttime; int j; - + char hmem[64]; + + bytesToHuman(hmem,server.usedmemory); info = sdscatprintf(sdsempty(), "redis_version:%s\r\n" "arch_bits:%s\r\n" @@ -5426,6 +5449,7 @@ static sds genRedisInfoString(void) { "connected_slaves:%d\r\n" "blocked_clients:%d\r\n" "used_memory:%zu\r\n" + "used_memory_human:%s\r\n" "changes_since_last_save:%lld\r\n" "bgsave_in_progress:%d\r\n" "last_save_time:%ld\r\n" @@ -5444,6 +5468,7 @@ static sds genRedisInfoString(void) { listLength(server.slaves), server.blockedclients, server.usedmemory, + hmem, server.dirty, server.bgsavechildpid != -1, server.lastsave,