summaryrefslogtreecommitdiff
path: root/src/core/timer.c
diff options
context:
space:
mode:
authorFilipe Brandenburger <filbranden@gmail.com>2020-07-10 14:24:00 -0700
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2020-07-15 09:23:09 +0200
commit26698337f3842842af51cd007485f1dcd7c43cf2 (patch)
tree1e343fcc6f691b370122a25359f70162826f7382 /src/core/timer.c
parentd3e0662c7d531748dd3626861a21c684857fc52e (diff)
downloadsystemd-26698337f3842842af51cd007485f1dcd7c43cf2.tar.gz
timer: Adjust calendar timers based on monotonic timer instead of realtime
When the RTC time at boot is off in the future by a few days, OnCalendar= timers will be scheduled based on the time at boot. But if the time has been adjusted since boot, the timers will end up scheduled way in the future, which may cause them not to fire as shortly or often as expected. Update the logic so that the time will be adjusted based on monotonic time. We do that by calculating the adjusted manager startup realtime from the monotonic time stored at that time, by comparing that time with the realtime and monotonic time of the current time. Added a test case to validate this works as expected. The test case creates a QEMU virtual machine with the clock 3 days in the future. Then we adjust the clock back 3 days, and test creating a timer with an OnCalendar= for every 15 minutes. We also check the manager startup timestamp from both `systemd-analyze dump` and from D-Bus. Test output without the corresponding code changes that fix the issue: Timer elapse outside of the expected 20 minute window. next_elapsed=1594686119 now=1594426921 time_delta=259198 With the code changes in, the test passes as expected.
Diffstat (limited to 'src/core/timer.c')
-rw-r--r--src/core/timer.c9
1 files changed, 5 insertions, 4 deletions
diff --git a/src/core/timer.c b/src/core/timer.c
index 7f779fb936..75f1dc1f8b 100644
--- a/src/core/timer.c
+++ b/src/core/timer.c
@@ -346,6 +346,7 @@ static void timer_enter_waiting(Timer *t, bool time_change) {
bool found_monotonic = false, found_realtime = false;
bool leave_around = false;
triple_timestamp ts;
+ dual_timestamp dts;
TimerValue *v;
Unit *trigger;
int r;
@@ -389,10 +390,10 @@ static void timer_enter_waiting(Timer *t, bool time_change) {
/* To make the delay due to RandomizedDelaySec= work even at boot,
* if the scheduled time has already passed, set the time when systemd
- * first started as the scheduled time.
- * Also, we don't have to check t->persistent since the logic implicitly express true. */
- if (v->next_elapse < UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].realtime)
- v->next_elapse = UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].realtime;
+ * first started as the scheduled time. */
+ dual_timestamp_from_monotonic(&dts, UNIT(t)->manager->timestamps[MANAGER_TIMESTAMP_USERSPACE].monotonic);
+ if (v->next_elapse < dts.realtime)
+ v->next_elapse = dts.realtime;
if (!found_realtime)
t->next_elapse_realtime = v->next_elapse;