diff --git a/src/blocked.cpp b/src/blocked.cpp index 085090b50..a37dcca58 100644 --- a/src/blocked.cpp +++ b/src/blocked.cpp @@ -525,7 +525,6 @@ void handleClientsBlockedOnKeys(void) { * lookup, invalidating the first one. * See https://github.com/antirez/redis/pull/6554. */ serverTL->fixed_time_expire++; - updateCachedTime(0); /* Serve clients blocked on the key. */ robj *o = lookupKeyWrite(rl->db,rl->key); diff --git a/src/rdb.cpp b/src/rdb.cpp index 3dad6e989..aefc730c0 100644 --- a/src/rdb.cpp +++ b/src/rdb.cpp @@ -2341,10 +2341,6 @@ void rdbLoadProgressCallback(rio *r, const void *buf, size_t len) { (g_pserver->loading_process_events_interval_keys && (r->keys_since_last_callback >= g_pserver->loading_process_events_interval_keys))) { - /* The DB can take some non trivial amount of time to load. Update - * our cached time since it is used to create and update the last - * interaction time with clients and for other important things. */ - updateCachedTime(0); listIter li; listNode *ln; listRewind(g_pserver->masters, &li); diff --git a/src/server.cpp b/src/server.cpp index a006d4da0..f0510c9b6 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2002,7 +2002,7 @@ void databasesCron(bool fMainThread) { * info or not using the 'update_daylight_info' argument. Normally we update * such info only when calling this function from serverCron() but not when * calling it from call(). */ -void updateCachedTime(int update_daylight_info) { +void updateCachedTime() { long long t = ustime(); __atomic_store(&g_pserver->ustime, &t, __ATOMIC_RELAXED); t /= 1000; @@ -2015,12 +2015,10 @@ void updateCachedTime(int update_daylight_info) { * context is safe since we will never fork() while here, in the main * thread. The logging function will call a thread safe version of * localtime that has no locks. */ - if (update_daylight_info) { - struct tm tm; - time_t ut = g_pserver->unixtime; - localtime_r(&ut,&tm); - __atomic_store(&g_pserver->daylight_active, &tm.tm_isdst, __ATOMIC_RELAXED); - } + struct tm tm; + time_t ut = g_pserver->unixtime; + localtime_r(&ut,&tm); + __atomic_store(&g_pserver->daylight_active, &tm.tm_isdst, __ATOMIC_RELAXED); } void checkChildrenDone(void) { @@ -2172,9 +2170,6 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) { * handler if we don't return here fast enough. */ if (g_pserver->watchdog_period) watchdogScheduleSignal(g_pserver->watchdog_period); - /* Update the time cache. */ - updateCachedTime(1); - /* Unpause clients if enough time has elapsed */ unpauseClientsIfNecessary(); @@ -2812,7 +2807,7 @@ void initMasterInfo(redisMaster *master) void initServerConfig(void) { int j; - updateCachedTime(true); + updateCachedTime(); getRandomHexChars(g_pserver->runid,CONFIG_RUN_ID_SIZE); g_pserver->runid[CONFIG_RUN_ID_SIZE] = '\0'; changeReplicationId(); @@ -3915,7 +3910,6 @@ void call(client *c, int flags) { /* Call the command. */ dirty = g_pserver->dirty; - updateCachedTime(0); incrementMvccTstamp(); start = g_pserver->ustime; try { @@ -6072,6 +6066,13 @@ void OnTerminate() serverPanic("std::teminate() called"); } +void *timeThreadMain(void*) { + while (true) { + updateCachedTime(); + usleep(1); + } +} + void *workerThreadMain(void *parg) { int iel = (int)((int64_t)parg); @@ -6419,6 +6420,8 @@ int main(int argc, char **argv) { setOOMScoreAdj(-1); serverAssert(cserver.cthreads > 0 && cserver.cthreads <= MAX_EVENT_LOOPS); + pthread_create(&cserver.time_thread_id, nullptr, timeThreadMain, nullptr); + pthread_attr_t tattr; pthread_attr_init(&tattr); pthread_attr_setstacksize(&tattr, 1 << 23); // 8 MB diff --git a/src/server.h b/src/server.h index 6fa6807c7..1ecf838ea 100644 --- a/src/server.h +++ b/src/server.h @@ -1920,6 +1920,7 @@ struct redisServerConst { pid_t pid; /* Main process pid. */ time_t stat_starttime; /* Server start time */ pthread_t main_thread_id; /* Main thread id */ + pthread_t time_thread_id; char *configfile; /* Absolute config file path, or NULL */ char *executable; /* Absolute executable file path. */ char **exec_argv; /* Executable argv vector (copy). */ @@ -2965,7 +2966,7 @@ void populateCommandTable(void); void resetCommandTableStats(void); void adjustOpenFilesLimit(void); void closeListeningSockets(int unlink_unix_socket); -void updateCachedTime(int update_daylight_info); +void updateCachedTime(); void resetServerStats(void); void activeDefragCycle(void); unsigned int getLRUClock(void);