From: Willy Tarreau Date: Fri, 23 Apr 2021 13:17:27 +0000 (+0200) Subject: MINOR: time: avoid unneeded updates to now_offset X-Git-Tag: v2.4-dev17~8 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=481795de1348d939d01c3ec12e317ad51d0d0cca;p=thirdparty%2Fhaproxy.git MINOR: time: avoid unneeded updates to now_offset The time adjustment is very rare, even at high pool rates. Tests show that only 0.2% of tv_update_date() calls require a change of offset. Such concurrent writes to a shared variable have an important impact on future loads, so let's only update the variable if it changed. --- diff --git a/src/time.c b/src/time.c index 41e4d6f866..9b13ad90d1 100644 --- a/src/time.c +++ b/src/time.c @@ -184,7 +184,7 @@ void tv_update_date(int max_wait, int interrupted) unsigned int old_now_ms; unsigned long long old_now; unsigned long long new_now; - ullong ofs = HA_ATOMIC_LOAD(&now_offset); + ullong ofs, ofs_new; uint sec_ofs, usec_ofs; gettimeofday(&date, NULL); @@ -203,6 +203,8 @@ void tv_update_date(int max_wait, int interrupted) _tv_ms_add(&min_deadline, &before_poll, max_wait); _tv_ms_add(&max_deadline, &before_poll, max_wait + 100); + ofs = HA_ATOMIC_LOAD(&now_offset); + if (unlikely(__tv_islt(&date, &before_poll) || // big jump backwards (!interrupted && __tv_islt(&date, &min_deadline)) || // small jump backwards __tv_islt(&max_deadline, &date))) { // big jump forwards @@ -265,8 +267,9 @@ void tv_update_date(int max_wait, int interrupted) usec_ofs += 1000000; sec_ofs -= 1; } - ofs = ((ullong)sec_ofs << 32) + usec_ofs; - HA_ATOMIC_STORE(&now_offset, ofs); + ofs_new = ((ullong)sec_ofs << 32) + usec_ofs; + if (ofs_new != ofs) + HA_ATOMIC_STORE(&now_offset, ofs_new); } /* must be called once at boot to initialize some global variables */