Sample and cache RSS in serverCron().

Obtaining the RSS (Resident Set Size) info is slow in Linux and OSX.
This slowed down the generation of the INFO 'memory' section.

Since the RSS does not require to be a real-time measurement, we
now sample it with server.hz frequency (10 times per second by default)
and use this value both to show the INFO rss field and to compute the
fragmentation ratio.

Practically this does not make any difference for memory profiling of
Redis but speeds up the INFO call significantly.
This commit is contained in:
antirez
2014-03-24 12:00:20 +01:00
parent 72257f4b87
commit 4ebc7e3738
4 changed files with 10 additions and 5 deletions

View File

@ -1016,6 +1016,9 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
if (zmalloc_used_memory() > server.stat_peak_memory)
server.stat_peak_memory = zmalloc_used_memory();
/* Sample the RSS here since this is a relatively slow call. */
server.resident_set_size = zmalloc_get_rss();
/* We received a SIGTERM, shutting down here in a safe way, as it is
* not ok doing so inside the signal handler. */
if (server.shutdown_asap) {
@ -1613,6 +1616,7 @@ void initServer() {
/* A few stats we don't want to reset: server startup time, and peak mem. */
server.stat_starttime = time(NULL);
server.stat_peak_memory = 0;
server.resident_set_size = 0;
server.lastbgsave_status = REDIS_OK;
server.aof_last_write_status = REDIS_OK;
server.aof_last_write_errno = 0;
@ -2335,11 +2339,11 @@ sds genRedisInfoString(char *section) {
"mem_allocator:%s\r\n",
zmalloc_used,
hmem,
zmalloc_get_rss(),
server.resident_set_size,
server.stat_peak_memory,
peak_hmem,
((long long)lua_gc(server.lua,LUA_GCCOUNT,0))*1024LL,
zmalloc_get_fragmentation_ratio(),
zmalloc_get_fragmentation_ratio(server.resident_set_size),
ZMALLOC_LIB
);
}