dcavalca / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone
1abbee
From 338b8f9bca1cd7bd65123808fc7f7b2773e637db Mon Sep 17 00:00:00 2001
1abbee
From: Lennart Poettering <lennart@poettering.net>
1abbee
Date: Wed, 18 Nov 2015 13:37:30 +0100
1abbee
Subject: [PATCH] core: add new RandomSec= setting for time units
1abbee
1abbee
This allows configuration of a random time on top of the elapse events,
1abbee
in order to spread time events in a network evenly across a range.
1abbee
1abbee
Cherry-picked from: 744c7693751830149ae78fdaf95c6c6f99d59f07
1abbee
Resolves: #1305279
1abbee
---
23b3cf
 man/systemd.timer.xml                 | 43 +++++++++++++++++++++++----
23b3cf
 src/core/dbus-timer.c                 | 17 +++++++++++
1abbee
 src/core/load-fragment-gperf.gperf.m4 |  1 +
23b3cf
 src/core/timer.c                      | 28 +++++++++++++++++
1abbee
 src/core/timer.h                      |  1 +
23b3cf
 src/libsystemd/sd-bus/bus-util.c      | 16 ++++++++++
1abbee
 6 files changed, 100 insertions(+), 6 deletions(-)
1abbee
1abbee
diff --git a/man/systemd.timer.xml b/man/systemd.timer.xml
181b3f
index 20890f227..bdd14d888 100644
1abbee
--- a/man/systemd.timer.xml
1abbee
+++ b/man/systemd.timer.xml
1abbee
@@ -180,13 +180,12 @@
1abbee
         <varname>OnUnitInactiveSec=</varname> and ending the time
1abbee
         configured with <varname>AccuracySec=</varname> later. Within
1abbee
         this time window, the expiry time will be placed at a
1abbee
-        host-specific, randomized but stable position that is
1abbee
+        host-specific, randomized, but stable position that is
1abbee
         synchronized between all local timer units. This is done in
1abbee
-        order to distribute the wake-up time in networked
1abbee
-        installations, as well as optimizing power consumption to
1abbee
-        suppress unnecessary CPU wake-ups. To get best accuracy, set
1abbee
-        this option to 1us. Note that the timer is still subject to
1abbee
-        the timer slack configured via
1abbee
+        order to optimize power consumption to suppress unnecessary
1abbee
+        CPU wake-ups. To get best accuracy, set this option to
1abbee
+        1us. Note that the timer is still subject to the timer slack
1abbee
+        configured via
1abbee
         <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>'s
1abbee
         <varname>TimerSlackNSec=</varname> setting. See
1abbee
         <citerefentry><refentrytitle>prctl</refentrytitle><manvolnum>2</manvolnum></citerefentry>
1abbee
@@ -194,6 +193,38 @@
1abbee
         this value as high as possible and as low as
1abbee
         necessary.</para></listitem>
1abbee
       </varlistentry>
1abbee
+
1abbee
+      <varlistentry>
1abbee
+        <term><varname>RandomSec=</varname></term>
1abbee
+
1abbee
+        <listitem><para>Delay the timer by a randomly selected, evenly
1abbee
+        distributed amount of time between 0 and the specified time
1abbee
+        value. Defaults to 0, indicating that no randomized delay
1abbee
+        shall be applied. Each timer unit will determine this delay
1abbee
+        randomly each time it is started, and the delay will simply be
1abbee
+        added on top of the next determined elapsing time. This is
1abbee
+        useful to stretch dispatching of similarly configured timer
1abbee
+        events over a certain amount time, to avoid that they all fire
1abbee
+        at the same time, possibly resulting in resource
1abbee
+        congestion. Note the relation to
1abbee
+        <varname>AccuracySec=</varname> above: the latter allows the
1abbee
+        service manager to coalesce timer events within a specified
1abbee
+        time range in order to minimize wakeups, the former does the
1abbee
+        opposite: it stretches timer events over a time range, to make
1abbee
+        it unlikely that they fire simultaneously. If
1abbee
+        <varname>RandomSec=</varname> and
1abbee
+        <varname>AccuracySec=</varname> are used in conjunction, first
1abbee
+        the a randomized time is added, and the result is then
1abbee
+        possibly shifted further to coalesce it with other timer
1abbee
+        events possibly happening on the system. As mentioned above
1abbee
+        <varname>AccuracySec=</varname> defaults to 1min and
1abbee
+        <varname>RandomSec=</varname> to 0, thus encouraging
1abbee
+        coalescing of timer events. In order to optimally stretch
1abbee
+        timer events over a certain range of time, make sure to set
1abbee
+        <varname>RandomSec=</varname> to a higher value, and
1abbee
+        <varname>AccuracySec=1us</varname>.</para></listitem>
1abbee
+      </varlistentry>
1abbee
+
1abbee
       <varlistentry>
1abbee
         <term><varname>Unit=</varname></term>
1abbee
 
1abbee
diff --git a/src/core/dbus-timer.c b/src/core/dbus-timer.c
181b3f
index 43e785246..cd7bf44ba 100644
1abbee
--- a/src/core/dbus-timer.c
1abbee
+++ b/src/core/dbus-timer.c
1abbee
@@ -181,6 +181,7 @@ const sd_bus_vtable bus_timer_vtable[] = {
1abbee
         BUS_PROPERTY_DUAL_TIMESTAMP("LastTriggerUSec", offsetof(Timer, last_trigger), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1abbee
         SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Timer, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
1abbee
         SD_BUS_PROPERTY("AccuracyUSec", "t", bus_property_get_usec, offsetof(Timer, accuracy_usec), SD_BUS_VTABLE_PROPERTY_CONST),
1abbee
+        SD_BUS_PROPERTY("RandomUSec", "t", bus_property_get_usec, offsetof(Timer, random_usec), SD_BUS_VTABLE_PROPERTY_CONST),
1abbee
         SD_BUS_PROPERTY("Persistent", "b", bus_property_get_bool, offsetof(Timer, persistent), SD_BUS_VTABLE_PROPERTY_CONST),
1abbee
         SD_BUS_PROPERTY("WakeSystem", "b", bus_property_get_bool, offsetof(Timer, wake_system), SD_BUS_VTABLE_PROPERTY_CONST),
1abbee
         SD_BUS_VTABLE_END
1abbee
@@ -284,6 +285,22 @@ static int bus_timer_set_transient_property(
1abbee
 
1abbee
                 return 1;
1abbee
 
1abbee
+        } else if (streq(name, "RandomUSec")) {
1abbee
+                usec_t u = 0;
1abbee
+
1abbee
+                r = sd_bus_message_read(message, "t", &u);
1abbee
+                if (r < 0)
1abbee
+                        return r;
1abbee
+
1abbee
+                if (mode != UNIT_CHECK) {
1abbee
+                        char time[FORMAT_TIMESPAN_MAX];
1abbee
+
1abbee
+                        t->random_usec = u;
1abbee
+                        unit_write_drop_in_private_format(UNIT(t), mode, name, "RandomSec=%s\n", format_timespan(time, sizeof(time), u, USEC_PER_MSEC));
1abbee
+                }
1abbee
+
1abbee
+                return 1;
1abbee
+
1abbee
         } else if (streq(name, "WakeSystem")) {
1abbee
 
1abbee
                 int b;
1abbee
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
181b3f
index 53059845e..5106a98ee 100644
1abbee
--- a/src/core/load-fragment-gperf.gperf.m4
1abbee
+++ b/src/core/load-fragment-gperf.gperf.m4
1abbee
@@ -336,6 +336,7 @@ Timer.OnUnitInactiveSec,         config_parse_timer,                 0,
1abbee
 Timer.Persistent,                config_parse_bool,                  0,                             offsetof(Timer, persistent)
1abbee
 Timer.WakeSystem,                config_parse_bool,                  0,                             offsetof(Timer, wake_system)
1abbee
 Timer.AccuracySec,               config_parse_sec,                   0,                             offsetof(Timer, accuracy_usec)
1abbee
+Timer.RandomSec,                 config_parse_sec,                   0,                             offsetof(Timer, random_usec)
1abbee
 Timer.Unit,                      config_parse_trigger_unit,          0,                             0
1abbee
 m4_dnl
1abbee
 Path.PathExists,                 config_parse_path_spec,             0,                             0
1abbee
diff --git a/src/core/timer.c b/src/core/timer.c
181b3f
index 48cf9c16a..972dd73df 100644
1abbee
--- a/src/core/timer.c
1abbee
+++ b/src/core/timer.c
1abbee
@@ -29,6 +29,7 @@
1abbee
 #include "bus-util.h"
1abbee
 #include "bus-error.h"
1abbee
 #include "mkdir.h"
1abbee
+#include "util.h"
1abbee
 
1abbee
 static const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = {
1abbee
         [TIMER_DEAD] = UNIT_INACTIVE,
1abbee
@@ -315,6 +316,28 @@ static usec_t monotonic_to_boottime(usec_t t) {
1abbee
                 return 0;
1abbee
 }
1abbee
 
1abbee
+static void add_random(Timer *t, usec_t *v) {
1abbee
+        char s[FORMAT_TIMESPAN_MAX];
1abbee
+        usec_t add;
1abbee
+
1abbee
+        assert(t);
1abbee
+        assert(*v);
1abbee
+
1abbee
+        if (t->random_usec == 0)
1abbee
+                return;
1abbee
+        if (*v == USEC_INFINITY)
1abbee
+                return;
1abbee
+
1abbee
+        add = random_u64() % t->random_usec;
1abbee
+
1abbee
+        if (*v + add < *v) /* overflow */
1abbee
+                *v = (usec_t) -2; /* Highest possible value, that is not USEC_INFINITY */
1abbee
+        else
1abbee
+                *v += add;
1abbee
+
1abbee
+        log_unit_info(UNIT(t)->id, "Adding %s random time.", format_timespan(s, sizeof(s), add, 0));
1abbee
+}
1abbee
+
1abbee
 static void timer_enter_waiting(Timer *t, bool initial) {
1abbee
         bool found_monotonic = false, found_realtime = false;
1abbee
         usec_t ts_realtime, ts_monotonic;
1abbee
@@ -431,6 +454,8 @@ static void timer_enter_waiting(Timer *t, bool initial) {
1abbee
         if (found_monotonic) {
1abbee
                 char buf[FORMAT_TIMESPAN_MAX];
1abbee
 
1abbee
+                add_random(t, &t->next_elapse_monotonic_or_boottime);
1abbee
+
1abbee
                 log_unit_debug(UNIT(t)->id, "%s: Monotonic timer elapses in %s.",
1abbee
                                UNIT(t)->id,
1abbee
                                format_timespan(buf, sizeof(buf), t->next_elapse_monotonic_or_boottime > ts_monotonic ? t->next_elapse_monotonic_or_boottime - ts_monotonic : 0, 0));
1abbee
@@ -460,6 +485,9 @@ static void timer_enter_waiting(Timer *t, bool initial) {
1abbee
 
1abbee
         if (found_realtime) {
1abbee
                 char buf[FORMAT_TIMESTAMP_MAX];
1abbee
+
1abbee
+                add_random(t, &t->next_elapse_realtime);
1abbee
+
1abbee
                 log_unit_debug(UNIT(t)->id, "%s: Realtime timer elapses at %s.", UNIT(t)->id, format_timestamp(buf, sizeof(buf), t->next_elapse_realtime));
1abbee
 
1abbee
                 if (t->realtime_event_source) {
1abbee
diff --git a/src/core/timer.h b/src/core/timer.h
181b3f
index de412a043..b97724525 100644
1abbee
--- a/src/core/timer.h
1abbee
+++ b/src/core/timer.h
1abbee
@@ -69,6 +69,7 @@ struct Timer {
1abbee
         Unit meta;
1abbee
 
1abbee
         usec_t accuracy_usec;
1abbee
+        usec_t random_usec;
1abbee
 
1abbee
         LIST_HEAD(TimerValue, values);
1abbee
         usec_t next_elapse_realtime;
1abbee
diff --git a/src/libsystemd/sd-bus/bus-util.c b/src/libsystemd/sd-bus/bus-util.c
181b3f
index 6d5615082..5ecb3bea4 100644
1abbee
--- a/src/libsystemd/sd-bus/bus-util.c
1abbee
+++ b/src/libsystemd/sd-bus/bus-util.c
181b3f
@@ -1360,6 +1360,22 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
181b3f
                         return -EINVAL;
181b3f
                 }
1abbee
 
181b3f
+                if (r < 0)
181b3f
+                        return bus_log_create_error(r);
181b3f
+
181b3f
+                return 0;
1abbee
+        } else if (streq(field, "RandomSec")) {
1abbee
+                usec_t t;
1abbee
+
1abbee
+                r = parse_sec(eq, &t);
1abbee
+                if (r < 0)
1abbee
+                        return log_error_errno(r, "Failed to parse RandomSec= parameter: %s", eq);
1abbee
+
1abbee
+                r = sd_bus_message_append_basic(m, SD_BUS_TYPE_STRING, "RandomUSec");
1abbee
+                if (r < 0)
1abbee
+                        return bus_log_create_error(r);
1abbee
+
1abbee
+                r = sd_bus_message_append(m, "v", "t", t);
181b3f
                 if (r < 0)
181b3f
                         return bus_log_create_error(r);
1abbee