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