naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
c2dfb7
From a016ef4ab29ed62da547db008866624f75ed6407 Mon Sep 17 00:00:00 2001
c2dfb7
From: Chris Down <chris@chrisdown.name>
c2dfb7
Date: Tue, 16 Apr 2019 18:14:09 +0100
c2dfb7
Subject: [PATCH] cgroup: Create UNIT_DEFINE_ANCESTOR_MEMORY_LOOKUP
c2dfb7
c2dfb7
This is in preparation for creating unit_get_ancestor_memory_min.
c2dfb7
c2dfb7
(cherry picked from commit 6264b85e92aeddb74b8d8808a08c9eae8390a6a5)
c2dfb7
c2dfb7
Related: #1763435
c2dfb7
---
c2dfb7
 src/core/cgroup.c | 55 ++++++++++++++++++++++++++---------------------
c2dfb7
 1 file changed, 30 insertions(+), 25 deletions(-)
c2dfb7
c2dfb7
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
c2dfb7
index f804bf4727..46a89ff5e1 100644
c2dfb7
--- a/src/core/cgroup.c
c2dfb7
+++ b/src/core/cgroup.c
c2dfb7
@@ -372,31 +372,36 @@ int cgroup_add_device_allow(CGroupContext *c, const char *dev, const char *mode)
c2dfb7
         return 0;
c2dfb7
 }
c2dfb7
 
c2dfb7
-uint64_t unit_get_ancestor_memory_low(Unit *u) {
c2dfb7
-        CGroupContext *c;
c2dfb7
-
c2dfb7
-        /* 1. Is MemoryLow set in this unit? If so, use that.
c2dfb7
-         * 2. Is DefaultMemoryLow set in any ancestor? If so, use that.
c2dfb7
-         * 3. Otherwise, return CGROUP_LIMIT_MIN. */
c2dfb7
-
c2dfb7
-        assert(u);
c2dfb7
-
c2dfb7
-        c = unit_get_cgroup_context(u);
c2dfb7
-
c2dfb7
-        if (c->memory_low_set)
c2dfb7
-                return c->memory_low;
c2dfb7
-
c2dfb7
-        while (UNIT_ISSET(u->slice)) {
c2dfb7
-                u = UNIT_DEREF(u->slice);
c2dfb7
-                c = unit_get_cgroup_context(u);
c2dfb7
-
c2dfb7
-                if (c->default_memory_low_set)
c2dfb7
-                        return c->default_memory_low;
c2dfb7
-        }
c2dfb7
-
c2dfb7
-        /* We've reached the root, but nobody had DefaultMemoryLow set, so set it to the kernel default. */
c2dfb7
-        return CGROUP_LIMIT_MIN;
c2dfb7
-}
c2dfb7
+#define UNIT_DEFINE_ANCESTOR_MEMORY_LOOKUP(entry)                       \
c2dfb7
+        uint64_t unit_get_ancestor_##entry(Unit *u) {                   \
c2dfb7
+                CGroupContext *c;                                       \
c2dfb7
+                                                                        \
c2dfb7
+                /* 1. Is entry set in this unit? If so, use that.       \
c2dfb7
+                 * 2. Is the default for this entry set in any          \
c2dfb7
+                 *    ancestor? If so, use that.                        \
c2dfb7
+                 * 3. Otherwise, return CGROUP_LIMIT_MIN. */            \
c2dfb7
+                                                                        \
c2dfb7
+                assert(u);                                              \
c2dfb7
+                                                                        \
c2dfb7
+                c = unit_get_cgroup_context(u);                         \
c2dfb7
+                                                                        \
c2dfb7
+                if (c->entry##_set)                                     \
c2dfb7
+                        return c->entry;                                \
c2dfb7
+                                                                        \
c2dfb7
+                while (UNIT_ISSET(u->slice)) {                          \
c2dfb7
+                        u = UNIT_DEREF(u->slice);                       \
c2dfb7
+                        c = unit_get_cgroup_context(u);                 \
c2dfb7
+                                                                        \
c2dfb7
+                        if (c->default_##entry##_set)                   \
c2dfb7
+                                return c->default_##entry;              \
c2dfb7
+                }                                                       \
c2dfb7
+                                                                        \
c2dfb7
+                /* We've reached the root, but nobody had default for   \
c2dfb7
+                 * this entry set, so set it to the kernel default. */  \
c2dfb7
+                return CGROUP_LIMIT_MIN;                                \
c2dfb7
+}
c2dfb7
+
c2dfb7
+UNIT_DEFINE_ANCESTOR_MEMORY_LOOKUP(memory_low);
c2dfb7
 
c2dfb7
 static int lookup_block_device(const char *p, dev_t *ret) {
c2dfb7
         struct stat st;