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 41e4d6f..9b13ad9 100644
--- a/src/time.c
+++ b/src/time.c
@@ -184,7 +184,7 @@
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 @@
_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 @@
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 */