ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
36e8a3
From 8da81d2aba2768ced497790cc05b9f73c6268833 Mon Sep 17 00:00:00 2001
36e8a3
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
36e8a3
Date: Tue, 22 Jan 2019 17:30:48 +0100
36e8a3
Subject: [PATCH] journald: periodically drop cache for all dead PIDs
36e8a3
36e8a3
In normal use, this allow us to drop dead entries from the cache and reduces
36e8a3
the cache size so that we don't evict entries unnecessarily. The time limit is
36e8a3
there mostly to serve as a guard against malicious logging from many different
36e8a3
PIDs.
36e8a3
36e8a3
(cherry-picked from commit 91714a7f427a6c9c5c3be8b3819fee45050028f3)
36e8a3
36e8a3
Related: #1664976
36e8a3
---
36e8a3
 src/journal/journald-context.c | 28 ++++++++++++++++++++++++++--
36e8a3
 src/journal/journald-server.h  |  2 ++
36e8a3
 2 files changed, 28 insertions(+), 2 deletions(-)
36e8a3
36e8a3
diff --git a/src/journal/journald-context.c b/src/journal/journald-context.c
36e8a3
index 0f0dc1de4..51f79fd80 100644
36e8a3
--- a/src/journal/journald-context.c
36e8a3
+++ b/src/journal/journald-context.c
36e8a3
@@ -541,15 +541,39 @@ refresh:
36e8a3
 }
36e8a3
 
36e8a3
 static void client_context_try_shrink_to(Server *s, size_t limit) {
36e8a3
+        ClientContext *c;
36e8a3
+        usec_t t;
36e8a3
+
36e8a3
         assert(s);
36e8a3
 
36e8a3
+        /* Flush any cache entries for PIDs that have already moved on. Don't do this
36e8a3
+         * too often, since it's a slow process. */
36e8a3
+        t = now(CLOCK_MONOTONIC);
36e8a3
+        if (s->last_cache_pid_flush + MAX_USEC < t) {
36e8a3
+                unsigned n = prioq_size(s->client_contexts_lru), idx = 0;
36e8a3
+
36e8a3
+                /* We do a number of iterations based on the initial size of the prioq.  When we remove an
36e8a3
+                 * item, a new item is moved into its places, and items to the right might be reshuffled.
36e8a3
+                 */
36e8a3
+                for (unsigned i = 0; i < n; i++) {
36e8a3
+                        c = prioq_peek_by_index(s->client_contexts_lru, idx);
36e8a3
+
36e8a3
+                        assert(c->n_ref == 0);
36e8a3
+
36e8a3
+                        if (!pid_is_unwaited(c->pid))
36e8a3
+                                client_context_free(s, c);
36e8a3
+                        else
36e8a3
+                                idx ++;
36e8a3
+                }
36e8a3
+
36e8a3
+                s->last_cache_pid_flush = t;
36e8a3
+        }
36e8a3
+
36e8a3
         /* Bring the number of cache entries below the indicated limit, so that we can create a new entry without
36e8a3
          * breaching the limit. Note that we only flush out entries that aren't pinned here. This means the number of
36e8a3
          * cache entries may very well grow beyond the limit, if all entries stored remain pinned. */
36e8a3
 
36e8a3
         while (hashmap_size(s->client_contexts) > limit) {
36e8a3
-                ClientContext *c;
36e8a3
-
36e8a3
                 c = prioq_pop(s->client_contexts_lru);
36e8a3
                 if (!c)
36e8a3
                         break; /* All remaining entries are pinned, give up */
36e8a3
diff --git a/src/journal/journald-server.h b/src/journal/journald-server.h
36e8a3
index 983be8bb8..c6c9b1fb1 100644
36e8a3
--- a/src/journal/journald-server.h
36e8a3
+++ b/src/journal/journald-server.h
36e8a3
@@ -163,6 +163,8 @@ struct Server {
36e8a3
         Hashmap *client_contexts;
36e8a3
         Prioq *client_contexts_lru;
36e8a3
 
36e8a3
+        usec_t last_cache_pid_flush;
36e8a3
+
36e8a3
         ClientContext *my_context; /* the context of journald itself */
36e8a3
         ClientContext *pid1_context; /* the context of PID 1 */
36e8a3
 };