Merge pull request #259 from jinhao2/master

reset cpu affinity when new process forked.
dev
johnjiang 2018-08-15 17:00:49 +08:00 committed by GitHub
commit 1911eac7db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 0 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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);

View File

@ -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. */

View File

@ -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 HAVE_FF_KQUEUE
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;

View File

@ -42,6 +42,7 @@
#include <unistd.h>
#include <errno.h>
#include <inttypes.h>
#define _GNU_SOURCE
#include <pthread.h>
#include <syslog.h>
#include <netinet/in.h>
@ -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);