valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone

Blame SOURCES/0305-core-add-new-RandomSec-setting-for-time-units.patch

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