daandemeyer / rpms / systemd

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