chiark / gitweb /
sd-event: initialization perturbation value right before we use it
authorLennart Poettering <lennart@poettering.net>
Mon, 24 Mar 2014 22:54:21 +0000 (23:54 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 24 Mar 2014 22:54:21 +0000 (23:54 +0100)
That way, we don't forget to initialize it when the watchdog is
initialized before all event sources.

src/libsystemd/sd-event/sd-event.c

index a4b67431ef66ffb97cb17a6afb065d6aa898ebf1..d6a3d1c3f405d2d4c3534635d08ddf875c697a40 100644 (file)
@@ -733,12 +733,30 @@ _public_ int sd_event_add_io(
         return 0;
 }
 
+static void initialize_perturb(sd_event *e) {
+        sd_id128_t bootid = {};
+
+        /* When we sleep for longer, we try to realign the wakeup to
+           the same time wihtin each minute/second/250ms, so that
+           events all across the system can be coalesced into a single
+           CPU wakeup. However, let's take some system-specific
+           randomness for this value, so that in a network of systems
+           with synced clocks timer events are distributed a
+           bit. Here, we calculate a perturbation usec offset from the
+           boot ID. */
+
+        if (_likely_(e->perturb != (usec_t) -1))
+                return;
+
+        if (sd_id128_get_boot(&bootid) >= 0)
+                e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
+}
+
 static int event_setup_timer_fd(
                 sd_event *e,
                 struct clock_data *d,
                 clockid_t clock) {
 
-        sd_id128_t bootid = {};
         struct epoll_event ev = {};
         int r, fd;
 
@@ -762,20 +780,6 @@ static int event_setup_timer_fd(
         }
 
         d->fd = fd;
-
-        /* When we sleep for longer, we try to realign the wakeup to
-           the same time wihtin each minute/second/250ms, so that
-           events all across the system can be coalesced into a single
-           CPU wakeup. However, let's take some system-specific
-           randomness for this value, so that in a network of systems
-           with synced clocks timer events are distributed a
-           bit. Here, we calculate a perturbation usec offset from the
-           boot ID. */
-
-        if (e->perturb == (usec_t) -1)
-                if (sd_id128_get_boot(&bootid) >= 0)
-                        e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
-
         return 0;
 }
 
@@ -1588,6 +1592,8 @@ static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
         if (b <= a + 1)
                 return a;
 
+        initialize_perturb(e);
+
         /*
           Find a good time to wake up again between times a and b. We
           have two goals here: