Latency monitor: more hooks around the code.

This commit is contained in:
antirez 2014-07-01 17:19:08 +02:00
parent f35abe2ff5
commit de88bc63d5
3 changed files with 40 additions and 5 deletions

View File

@ -230,6 +230,7 @@ int startAppendOnly(void) {
void flushAppendOnlyFile(int force) { void flushAppendOnlyFile(int force) {
ssize_t nwritten; ssize_t nwritten;
int sync_in_progress = 0; int sync_in_progress = 0;
mstime_t latency;
if (sdslen(server.aof_buf) == 0) return; if (sdslen(server.aof_buf) == 0) return;
@ -266,7 +267,11 @@ void flushAppendOnlyFile(int force) {
* While this will save us against the server being killed I don't think * While this will save us against the server being killed I don't think
* there is much to do about the whole server stopping for power problems * there is much to do about the whole server stopping for power problems
* or alike */ * or alike */
latencyStartMonitor(latency);
nwritten = write(server.aof_fd,server.aof_buf,sdslen(server.aof_buf)); nwritten = write(server.aof_fd,server.aof_buf,sdslen(server.aof_buf));
latencyEndMonitor(latency);
latencyAddSampleIfNeeded("aof-write",latency);
if (nwritten != (signed)sdslen(server.aof_buf)) { if (nwritten != (signed)sdslen(server.aof_buf)) {
static time_t last_write_error_log = 0; static time_t last_write_error_log = 0;
int can_log = 0; int can_log = 0;
@ -360,7 +365,10 @@ void flushAppendOnlyFile(int force) {
if (server.aof_fsync == AOF_FSYNC_ALWAYS) { if (server.aof_fsync == AOF_FSYNC_ALWAYS) {
/* aof_fsync is defined as fdatasync() for Linux in order to avoid /* aof_fsync is defined as fdatasync() for Linux in order to avoid
* flushing metadata. */ * flushing metadata. */
latencyStartMonitor(latency);
aof_fsync(server.aof_fd); /* Let's try to get this data on the disk */ aof_fsync(server.aof_fd); /* Let's try to get this data on the disk */
latencyEndMonitor(latency);
latencyAddSampleIfNeeded("aof-fsync-always",latency);
server.aof_last_fsync = server.unixtime; server.aof_last_fsync = server.unixtime;
} else if ((server.aof_fsync == AOF_FSYNC_EVERYSEC && } else if ((server.aof_fsync == AOF_FSYNC_EVERYSEC &&
server.unixtime > server.aof_last_fsync)) { server.unixtime > server.aof_last_fsync)) {
@ -1032,6 +1040,7 @@ int rewriteAppendOnlyFileBackground(void) {
} else { } else {
/* Parent */ /* Parent */
server.stat_fork_time = ustime()-start; server.stat_fork_time = ustime()-start;
latencyAddSampleIfNeeded("fork",server.stat_fork_time/1000);
if (childpid == -1) { if (childpid == -1) {
redisLog(REDIS_WARNING, redisLog(REDIS_WARNING,
"Can't rewrite append only file in background: fork: %s", "Can't rewrite append only file in background: fork: %s",
@ -1081,13 +1090,17 @@ void aofRemoveTempFile(pid_t childpid) {
* to the current length, that is much faster. */ * to the current length, that is much faster. */
void aofUpdateCurrentSize(void) { void aofUpdateCurrentSize(void) {
struct redis_stat sb; struct redis_stat sb;
mstime_t latency;
latencyStartMonitor(latency);
if (redis_fstat(server.aof_fd,&sb) == -1) { if (redis_fstat(server.aof_fd,&sb) == -1) {
redisLog(REDIS_WARNING,"Unable to obtain the AOF file length. stat: %s", redisLog(REDIS_WARNING,"Unable to obtain the AOF file length. stat: %s",
strerror(errno)); strerror(errno));
} else { } else {
server.aof_current_size = sb.st_size; server.aof_current_size = sb.st_size;
} }
latencyEndMonitor(latency);
latencyAddSampleIfNeeded("aof-fstat",latency);
} }
/* A background append only file rewriting (BGREWRITEAOF) terminated its work. /* A background append only file rewriting (BGREWRITEAOF) terminated its work.
@ -1097,12 +1110,14 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
int newfd, oldfd; int newfd, oldfd;
char tmpfile[256]; char tmpfile[256];
long long now = ustime(); long long now = ustime();
mstime_t latency;
redisLog(REDIS_NOTICE, redisLog(REDIS_NOTICE,
"Background AOF rewrite terminated with success"); "Background AOF rewrite terminated with success");
/* Flush the differences accumulated by the parent to the /* Flush the differences accumulated by the parent to the
* rewritten AOF. */ * rewritten AOF. */
latencyStartMonitor(latency);
snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof",
(int)server.aof_child_pid); (int)server.aof_child_pid);
newfd = open(tmpfile,O_WRONLY|O_APPEND); newfd = open(tmpfile,O_WRONLY|O_APPEND);
@ -1118,6 +1133,8 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
close(newfd); close(newfd);
goto cleanup; goto cleanup;
} }
latencyEndMonitor(latency);
latencyAddSampleIfNeeded("aof-rewrite-diff-write",latency);
redisLog(REDIS_NOTICE, redisLog(REDIS_NOTICE,
"Parent diff successfully flushed to the rewritten AOF (%lu bytes)", aofRewriteBufferSize()); "Parent diff successfully flushed to the rewritten AOF (%lu bytes)", aofRewriteBufferSize());
@ -1163,6 +1180,7 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
/* Rename the temporary file. This will not unlink the target file if /* Rename the temporary file. This will not unlink the target file if
* it exists, because we reference it with "oldfd". */ * it exists, because we reference it with "oldfd". */
latencyStartMonitor(latency);
if (rename(tmpfile,server.aof_filename) == -1) { if (rename(tmpfile,server.aof_filename) == -1) {
redisLog(REDIS_WARNING, redisLog(REDIS_WARNING,
"Error trying to rename the temporary AOF file: %s", strerror(errno)); "Error trying to rename the temporary AOF file: %s", strerror(errno));
@ -1170,6 +1188,8 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
if (oldfd != -1) close(oldfd); if (oldfd != -1) close(oldfd);
goto cleanup; goto cleanup;
} }
latencyEndMonitor(latency);
latencyAddSampleIfNeeded("aof-rename",latency);
if (server.aof_fd == -1) { if (server.aof_fd == -1) {
/* AOF disabled, we don't need to set the AOF file descriptor /* AOF disabled, we don't need to set the AOF file descriptor

View File

@ -745,6 +745,7 @@ int rdbSaveBackground(char *filename) {
} else { } else {
/* Parent */ /* Parent */
server.stat_fork_time = ustime()-start; server.stat_fork_time = ustime()-start;
latencyAddSampleIfNeeded("fork",server.stat_fork_time/1000);
if (childpid == -1) { if (childpid == -1) {
server.lastbgsave_status = REDIS_ERR; server.lastbgsave_status = REDIS_ERR;
redisLog(REDIS_WARNING,"Can't save in background: fork: %s", redisLog(REDIS_WARNING,"Can't save in background: fork: %s",
@ -1200,9 +1201,14 @@ void backgroundSaveDoneHandler(int exitcode, int bysignal) {
redisLog(REDIS_WARNING, "Background saving error"); redisLog(REDIS_WARNING, "Background saving error");
server.lastbgsave_status = REDIS_ERR; server.lastbgsave_status = REDIS_ERR;
} else { } else {
mstime_t latency;
redisLog(REDIS_WARNING, redisLog(REDIS_WARNING,
"Background saving terminated by signal %d", bysignal); "Background saving terminated by signal %d", bysignal);
latencyStartMonitor(latency);
rdbRemoveTempFile(server.rdb_child_pid); rdbRemoveTempFile(server.rdb_child_pid);
latencyEndMonitor(latency);
latencyAddSampleIfNeeded("rdb-unlink-temp-file",latency);
/* SIGUSR1 is whitelisted, so we have a way to kill a child without /* SIGUSR1 is whitelisted, so we have a way to kill a child without
* tirggering an error conditon. */ * tirggering an error conditon. */
if (bysignal != SIGUSR1) if (bysignal != SIGUSR1)

View File

@ -858,10 +858,11 @@ void activeExpireCycle(int type) {
* expire. So after a given amount of milliseconds return to the * expire. So after a given amount of milliseconds return to the
* caller waiting for the other active expire cycle. */ * caller waiting for the other active expire cycle. */
iteration++; iteration++;
if ((iteration & 0xf) == 0 && /* check once every 16 iterations. */ if ((iteration & 0xf) == 0) { /* check once every 16 iterations. */
(ustime()-start) > timelimit) long long elapsed = ustime()-start;
{
timelimit_exit = 1; latencyAddSampleIfNeeded("expire-cycle",elapsed/1000);
if (elapsed > timelimit) timelimit_exit = 1;
} }
if (timelimit_exit) return; if (timelimit_exit) return;
/* We don't repeat the cycle if there are less than 25% of keys /* We don't repeat the cycle if there are less than 25% of keys
@ -3120,6 +3121,7 @@ void evictionPoolPopulate(dict *sampledict, dict *keydict, struct evictionPoolEn
int freeMemoryIfNeeded(void) { int freeMemoryIfNeeded(void) {
size_t mem_used, mem_tofree, mem_freed; size_t mem_used, mem_tofree, mem_freed;
int slaves = listLength(server.slaves); int slaves = listLength(server.slaves);
mstime_t latency;
/* Remove the size of slaves output buffers and AOF buffer from the /* Remove the size of slaves output buffers and AOF buffer from the
* count of used memory. */ * count of used memory. */
@ -3152,6 +3154,7 @@ int freeMemoryIfNeeded(void) {
/* Compute how much memory we need to free. */ /* Compute how much memory we need to free. */
mem_tofree = mem_used - server.maxmemory; mem_tofree = mem_used - server.maxmemory;
mem_freed = 0; mem_freed = 0;
latencyStartMonitor(latency);
while (mem_freed < mem_tofree) { while (mem_freed < mem_tofree) {
int j, k, keys_freed = 0; int j, k, keys_freed = 0;
@ -3265,8 +3268,14 @@ int freeMemoryIfNeeded(void) {
if (slaves) flushSlavesOutputBuffers(); if (slaves) flushSlavesOutputBuffers();
} }
} }
if (!keys_freed) return REDIS_ERR; /* nothing to free... */ if (!keys_freed) {
latencyEndMonitor(latency);
latencyAddSampleIfNeeded("eviction-cycle",latency);
return REDIS_ERR; /* nothing to free... */
}
} }
latencyEndMonitor(latency);
latencyAddSampleIfNeeded("eviction-cycle",latency);
return REDIS_OK; return REDIS_OK;
} }