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