]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: time: avoid unneeded updates to now_offset
authorWilly Tarreau <w@1wt.eu>
Fri, 23 Apr 2021 13:17:27 +0000 (15:17 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 23 Apr 2021 16:03:06 +0000 (18:03 +0200)
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.

src/time.c

index 41e4d6f8666cca809f7dedf02c3e8e9bf5cd7287..9b13ad90d166b98660561eaa6084aa80d9c0872e 100644 (file)
@@ -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 */