[systemd-devel] [PATCH 1/3] Enable systemd to set/show limit of memory+Swap usage

Chen Hanxiao chenhanxiao at cn.fujitsu.com
Mon Sep 16 23:21:26 PDT 2013


From: Chen Hanxiao <chenhanxiao at cn.fujitsu.com>

Enable systemd to set/show limit of memory+Swap usage.

Signed-off-by: Chen Hanxiao <chenhanxiao at cn.fujitsu.com>
---
 src/core/cgroup.c                     | 11 +++++++++++
 src/core/cgroup.h                     |  1 +
 src/core/dbus-cgroup.c                | 18 ++++++++++++++++++
 src/core/load-fragment-gperf.gperf.m4 |  1 +
 4 files changed, 31 insertions(+)

diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index 1f41efc..d8bc318 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -34,6 +34,7 @@ void cgroup_context_init(CGroupContext *c) {
 
         c->cpu_shares = 1024;
         c->memory_limit = c->memory_soft_limit = (uint64_t) -1;
+        c->memsw_limit = (uint64_t) -1;
         c->blockio_weight = 1000;
 }
 
@@ -266,6 +267,15 @@ void cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const cha
                 if (r < 0)
                         log_error("Failed to set memory.limit_in_bytes on %s: %s", path, strerror(-r));
 
+                if (c->memsw_limit != (uint64_t) -1) {
+                        sprintf(buf, "%" PRIu64 "\n", c->memsw_limit);
+                        r = cg_set_attribute("memory", path, "memory.memsw.limit_in_bytes", buf);
+                } else
+                        r = cg_set_attribute("memory", path, "memory.memsw.limit_in_bytes", "-1");
+
+                if (r < 0)
+                        log_error("Failed to set memory.memsw.limit_in_bytes on %s: %s", path, strerror(-r));
+
                 if (c->memory_soft_limit != (uint64_t) -1) {
                         sprintf(buf, "%" PRIu64 "\n", c->memory_soft_limit);
                         r = cg_set_attribute("memory", path, "memory.soft_limit_in_bytes", buf);
@@ -337,6 +347,7 @@ CGroupControllerMask cgroup_context_get_mask(CGroupContext *c) {
 
         if (c->memory_accounting ||
             c->memory_limit != (uint64_t) -1 ||
+            c->memsw_limit != (uint64_t) -1 ||
             c->memory_soft_limit != (uint64_t) -1)
                 mask |= CGROUP_MEMORY;
 
diff --git a/src/core/cgroup.h b/src/core/cgroup.h
index 786bd71..3519061 100644
--- a/src/core/cgroup.h
+++ b/src/core/cgroup.h
@@ -77,6 +77,7 @@ struct CGroupContext {
         LIST_HEAD(CGroupBlockIODeviceBandwidth, blockio_device_bandwidths);
 
         uint64_t memory_limit;
+        uint64_t memsw_limit;
         uint64_t memory_soft_limit;
 
         CGroupDevicePolicy device_policy;
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c
index 1f2a396..8235da0 100644
--- a/src/core/dbus-cgroup.c
+++ b/src/core/dbus-cgroup.c
@@ -133,6 +133,7 @@ const BusProperty bus_cgroup_context_properties[] = {
         { "BlockIOWriteBandwidth",   bus_cgroup_append_device_bandwidths, "a(st)", 0                                           },
         { "MemoryAccounting",        bus_property_append_bool,            "b",     offsetof(CGroupContext, memory_accounting)  },
         { "MemoryLimit",             bus_property_append_uint64,          "t",     offsetof(CGroupContext, memory_limit)       },
+        { "MemorySWLimit",           bus_property_append_uint64,          "t",     offsetof(CGroupContext, memsw_limit)       },
         { "MemorySoftLimit",         bus_property_append_uint64,          "t",     offsetof(CGroupContext, memory_soft_limit)  },
         { "DevicePolicy",            bus_cgroup_append_device_policy,     "s",     offsetof(CGroupContext, device_policy)      },
         { "DeviceAllow",             bus_cgroup_append_device_allow,      "a(ss)", 0                                           },
@@ -438,6 +439,23 @@ int bus_cgroup_set_property(
 
                 return 1;
 
+        } else if (streq(name, "MemorySWLimit")) {
+
+                if (dbus_message_iter_get_arg_type(i) != DBUS_TYPE_UINT64)
+                        return -EINVAL;
+
+                if (mode != UNIT_CHECK) {
+                        uint64_t limit;
+
+                        dbus_message_iter_get_basic(i, &limit);
+
+                        c->memsw_limit = limit;
+
+                        unit_write_drop_in_private_format(u, mode, name, "%s=%" PRIu64, name, limit);
+                }
+
+                return 1;
+
         } else if (streq(name, "DevicePolicy")) {
                 const char *policy;
                 CGroupDevicePolicy p;
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
index 33c6880..0471cae 100644
--- a/src/core/load-fragment-gperf.gperf.m4
+++ b/src/core/load-fragment-gperf.gperf.m4
@@ -89,6 +89,7 @@ $1.CPUAccounting,                config_parse_bool,                  0,
 $1.CPUShares,                    config_parse_cpu_shares,            0,                             offsetof($1, cgroup_context)
 $1.MemoryAccounting,             config_parse_bool,                  0,                             offsetof($1, cgroup_context.memory_accounting)
 $1.MemoryLimit,                  config_parse_memory_limit,          0,                             offsetof($1, cgroup_context)
+$1.MemorySWLimit,                config_parse_memory_limit,          0,                             offsetof($1, cgroup_context)
 $1.MemorySoftLimit,              config_parse_memory_limit,          0,                             offsetof($1, cgroup_context)
 $1.DeviceAllow,                  config_parse_device_allow,          0,                             offsetof($1, cgroup_context)
 $1.DevicePolicy,                 config_parse_device_policy,         0,                             offsetof($1, cgroup_context.device_policy)
-- 
1.8.2.1



More information about the systemd-devel mailing list