teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0353-manager-Only-invoke-a-single-sigchld-per-unit-within.patch

923a60
From 2a0feeb4252cbf1207f45e978fe7eb02bb0182ec Mon Sep 17 00:00:00 2001
923a60
From: Kyle Walker <walker.kyle.t@gmail.com>
923a60
Date: Thu, 30 Jun 2016 15:12:18 -0400
923a60
Subject: [PATCH] manager: Only invoke a single sigchld per unit within a
923a60
 cleanup cycle
923a60
923a60
By default, each iteration of manager_dispatch_sigchld() results in a unit level
923a60
sigchld event being invoked. For scope units, this results in a scope_sigchld_event()
923a60
which can seemingly stall for workloads that have a large number of PIDs within the
923a60
scope. The stall exhibits itself as a SIG_0 being initiated for each u->pids entry
923a60
as a result of pid_is_unwaited().
923a60
923a60
v2:
923a60
This patch resolves this condition by only paying to cost of a sigchld in the underlying
923a60
scope unit once per sigchld iteration. A new "sigchldgen" member resides within the
923a60
Unit struct. The Manager is incremented via the sd event loop, accessed via
923a60
sd_event_get_iteration, and the Unit member is set to the same value as the manager each
923a60
time that a sigchld event is invoked. If the Manager iteration value and Unit member
923a60
match, the sigchld event is not invoked for that iteration.
923a60
923a60
Cherry-picked from: 36f20ae3b2975e44b6ef17e453ae06a289e9a122
923a60
Resolves: #1342173
923a60
---
923a60
 src/core/manager.c | 13 ++++++++++++-
923a60
 src/core/unit.c    |  1 +
923a60
 src/core/unit.h    |  3 +++
923a60
 3 files changed, 16 insertions(+), 1 deletion(-)
923a60
923a60
diff --git a/src/core/manager.c b/src/core/manager.c
923a60
index e5226a8a6f..63693b93a6 100644
923a60
--- a/src/core/manager.c
923a60
+++ b/src/core/manager.c
923a60
@@ -1747,14 +1747,25 @@ static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t
923a60
 }
923a60
 
923a60
 static void invoke_sigchld_event(Manager *m, Unit *u, siginfo_t *si) {
923a60
+        uint64_t iteration;
923a60
+        
923a60
         assert(m);
923a60
         assert(u);
923a60
         assert(si);
923a60
 
923a60
+        sd_event_get_iteration(m->event, &iteration);
923a60
+
923a60
         log_unit_debug(u->id, "Child "PID_FMT" belongs to %s", si->si_pid, u->id);
923a60
 
923a60
         unit_unwatch_pid(u, si->si_pid);
923a60
-        UNIT_VTABLE(u)->sigchld_event(u, si->si_pid, si->si_code, si->si_status);
923a60
+
923a60
+        if (UNIT_VTABLE(u)->sigchld_event) {
923a60
+                if (set_size(u->pids) <= 1 || iteration != u->sigchldgen) {
923a60
+                        UNIT_VTABLE(u)->sigchld_event(u, si->si_pid, si->si_code, si->si_status);
923a60
+                        u->sigchldgen = iteration;
923a60
+                } else
923a60
+                        log_debug("%s already issued a sigchld this iteration %llu, skipping. Pids still being watched %d", u->id, iteration, set_size(u->pids));
923a60
+        }
923a60
 }
923a60
 
923a60
 static int manager_dispatch_sigchld(Manager *m) {
923a60
diff --git a/src/core/unit.c b/src/core/unit.c
923a60
index d6ead7d672..d62135d878 100644
923a60
--- a/src/core/unit.c
923a60
+++ b/src/core/unit.c
923a60
@@ -94,6 +94,7 @@ Unit *unit_new(Manager *m, size_t size) {
923a60
         u->unit_file_state = _UNIT_FILE_STATE_INVALID;
923a60
         u->unit_file_preset = -1;
923a60
         u->on_failure_job_mode = JOB_REPLACE;
923a60
+        u->sigchldgen = 0;
923a60
 
923a60
         return u;
923a60
 }
923a60
diff --git a/src/core/unit.h b/src/core/unit.h
923a60
index 0eebc0b891..d936457776 100644
923a60
--- a/src/core/unit.h
923a60
+++ b/src/core/unit.h
923a60
@@ -167,6 +167,9 @@ struct Unit {
923a60
          * process SIGCHLD for */
923a60
         Set *pids;
923a60
 
923a60
+        /* Used in sigchld event invocation to avoid repeat events being invoked */
923a60
+        uint64_t sigchldgen;
923a60
+
923a60
         /* Used during GC sweeps */
923a60
         unsigned gc_marker;
923a60