From 9518765b1f7b48f4e4a83818aae1547172ab4945 Mon Sep 17 00:00:00 2001 From: 10077240 <330416470@qq.com> Date: Wed, 8 Aug 2018 07:40:39 +0800 Subject: [PATCH] reset cpu affinity when new process forked. New forked process should not compete same core with redis server. --- app/redis-3.2.8/src/aof.c | 1 + app/redis-3.2.8/src/rdb.c | 2 ++ app/redis-3.2.8/src/scripting.c | 1 + app/redis-3.2.8/src/sentinel.c | 1 + app/redis-3.2.8/src/server.c | 30 ++++++++++++++++++++++++++++++ app/redis-3.2.8/src/server.h | 3 +++ 6 files changed, 38 insertions(+) diff --git a/app/redis-3.2.8/src/aof.c b/app/redis-3.2.8/src/aof.c index 862b5e13d..83bcbbb00 100644 --- a/app/redis-3.2.8/src/aof.c +++ b/app/redis-3.2.8/src/aof.c @@ -1272,6 +1272,7 @@ int rewriteAppendOnlyFileBackground(void) { /* Child */ closeListeningSockets(0); + resetCpuAffinity("aof-rewrite"); redisSetProcTitle("redis-aof-rewrite"); snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid()); if (rewriteAppendOnlyFile(tmpfile) == C_OK) { diff --git a/app/redis-3.2.8/src/rdb.c b/app/redis-3.2.8/src/rdb.c index da660671a..1dfbf864a 100644 --- a/app/redis-3.2.8/src/rdb.c +++ b/app/redis-3.2.8/src/rdb.c @@ -930,6 +930,7 @@ int rdbSaveBackground(char *filename) { /* Child */ closeListeningSockets(0); + resetCpuAffinity("rdb-bgsave"); redisSetProcTitle("redis-rdb-bgsave"); retval = rdbSave(filename); if (retval == C_OK) { @@ -1631,6 +1632,7 @@ int rdbSaveToSlavesSockets(void) { zfree(fds); closeListeningSockets(0); + resetCpuAffinity("rdb2slave"); redisSetProcTitle("redis-rdb-to-slaves"); retval = rdbSaveRioWithEOFMark(&slave_sockets,NULL); diff --git a/app/redis-3.2.8/src/scripting.c b/app/redis-3.2.8/src/scripting.c index 0a2ce1f33..4d3778a2c 100644 --- a/app/redis-3.2.8/src/scripting.c +++ b/app/redis-3.2.8/src/scripting.c @@ -1611,6 +1611,7 @@ int ldbStartSession(client *c) { * to the clients. */ serverLog(LL_WARNING,"Redis forked for debugging eval"); closeListeningSockets(0); + resetCpuAffinity("redis-dbg"); } else { /* Parent */ listAddNodeTail(ldb.children,(void*)(unsigned long)cp); diff --git a/app/redis-3.2.8/src/sentinel.c b/app/redis-3.2.8/src/sentinel.c index 0168aa637..42f0c95f4 100644 --- a/app/redis-3.2.8/src/sentinel.c +++ b/app/redis-3.2.8/src/sentinel.c @@ -763,6 +763,7 @@ void sentinelRunPendingScripts(void) { sj->pid = 0; } else if (pid == 0) { /* Child */ + resetCpuAffinity(NULL); execve(sj->argv[0],sj->argv,environ); /* If we are here an error occurred. */ _exit(2); /* Don't retry execution. */ diff --git a/app/redis-3.2.8/src/server.c b/app/redis-3.2.8/src/server.c index 835b18688..94d9020bc 100644 --- a/app/redis-3.2.8/src/server.c +++ b/app/redis-3.2.8/src/server.c @@ -2558,6 +2558,36 @@ void closeListeningSockets(int unlink_unix_socket) { } } +/* Reset cpu affinity as soon as new process fork(). + * For new process will use same cpu core with redis server. */ +void resetCpuAffinity(const char* name) +{ + int j = 0, s = 0; + cput_set_t cpuset_frm, cpuset_to; + pthread_t thread; +#ifdef __linux__ + thread = pthread_self(); + CPU_ZERO(&cpuset_frm); + CPU_ZERO(&cpuset_to); + + pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset_frm); + for (j = 0; j < CPU_SETSIZE; j++) + { + if ( CPU_ISSET(j, &cpuset_frm) ) + continue; + CPU_SET(j, &cpuset_to); + } + s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset_to); + if (s != 0) + serverLog(LL_WARNING,"set cpu affinity, failed."); + if (name!=NULL) + { + pthread_setname_np(thread, name); + } +#endif + return; +} + int prepareForShutdown(int flags) { int save = flags & SHUTDOWN_SAVE; int nosave = flags & SHUTDOWN_NOSAVE; diff --git a/app/redis-3.2.8/src/server.h b/app/redis-3.2.8/src/server.h index 3fa7c3ac1..fb49e56a3 100644 --- a/app/redis-3.2.8/src/server.h +++ b/app/redis-3.2.8/src/server.h @@ -42,6 +42,7 @@ #include #include #include +#define _GNU_SOURCE #include #include #include @@ -1332,6 +1333,8 @@ void populateCommandTable(void); void resetCommandTableStats(void); void adjustOpenFilesLimit(void); void closeListeningSockets(int unlink_unix_socket); +void resetCpuAffinity(const char* name); + void updateCachedTime(void); void resetServerStats(void); unsigned int getLRUClock(void);