[systemd-devel] [PATCH 3/4] cgroups: support for MemorySoftLimit= setting
Mika Eloranta
mel at ohmu.fi
Wed Oct 9 16:18:36 PDT 2013
Add a MemorySoftLimit setting that behaves the same way
as MemoryLimit, except that it controls the
memory.soft_limit_in_bytes cgroup attribute.
---
man/systemd.resource-control.xml | 7 ++++++-
src/core/cgroup.c | 14 ++++++++++++++
src/core/cgroup.h | 1 +
src/core/dbus-cgroup.c | 1 +
src/core/load-fragment-gperf.gperf.m4 | 1 +
src/core/load-fragment.c | 34 ++++++++++++++++++++++++++++++++++
src/core/load-fragment.h | 1 +
src/systemctl/systemctl.c | 4 ++--
8 files changed, 60 insertions(+), 3 deletions(-)
diff --git a/man/systemd.resource-control.xml b/man/systemd.resource-control.xml
index dca7294..e80c02f 100644
--- a/man/systemd.resource-control.xml
+++ b/man/systemd.resource-control.xml
@@ -138,7 +138,7 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
</varlistentry>
<varlistentry>
- <term><varname>MemoryLimit=, MemoryAndSwapLimit=, KernelMemoryLimit=<replaceable>bytes</replaceable></varname></term>
+ <term><varname>MemoryLimit=, MemoryAndSwapLimit=, KernelMemoryLimit=, MemorySoftLimit=<replaceable>bytes</replaceable></varname></term>
<listitem>
<para>Specify the limit on maximum memory usage of the
@@ -157,6 +157,11 @@ along with systemd; If not, see <http://www.gnu.org/licenses/>.
<literal>KernelMemoryLimit</literal> controls the
<literal>memory.kmem.limit_in_bytes</literal> control group
attribute, which limits the amound of kernel memory used.
+ <literal>MemorySoftLimit</literal> controls the
+ <literal>memory.soft_limit_in_bytes</literal> control group
+ attribute, which is used when the system detects memory
+ contention or low memory, control groups are pushed back
+ to their soft limits.
For details about these control group attributes,
see <ulink
url="https://www.kernel.org/doc/Documentation/cgroups/memory.txt">memory.txt</ulink>.</para>
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index 41a09b0..d7aa49e 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -36,6 +36,7 @@ void cgroup_context_init(CGroupContext *c) {
c->memory_limit = (uint64_t) -1;
c->memory_and_swap_limit = (uint64_t) -1;
c->kernel_memory_limit = (uint64_t) -1;
+ c->memory_soft_limit = (uint64_t) -1;
c->blockio_weight = 1000;
}
@@ -96,6 +97,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
"%sCPUShares=%lu\n"
"%sBlockIOWeight=%lu\n"
"%sMemoryLimit=%" PRIu64 "\n"
+ "%sMemorySoftLimit=%" PRIu64 "\n"
"%sMemoryAndSwapLimit=%" PRIu64 "\n"
"%sKernelMemoryLimit=%" PRIu64 "\n"
"%sDevicePolicy=%s\n",
@@ -105,6 +107,7 @@ void cgroup_context_dump(CGroupContext *c, FILE* f, const char *prefix) {
prefix, c->cpu_shares,
prefix, c->blockio_weight,
prefix, c->memory_limit,
+ prefix, c->memory_soft_limit,
prefix, c->memory_and_swap_limit,
prefix, c->kernel_memory_limit,
prefix, cgroup_device_policy_to_string(c->device_policy));
@@ -293,6 +296,16 @@ void cgroup_context_apply(CGroupContext *c, CGroupControllerMask mask, const cha
if (r < 0)
log_error("Failed to set memory.kmem.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);
+ } else {
+ r = cg_set_attribute("memory", path, "memory.soft_limit_in_bytes", "-1");
+ }
+
+ if (r < 0)
+ log_error("Failed to set memory.soft_limit_in_bytes on %s: %s", path, strerror(-r));
}
if (mask & CGROUP_DEVICE) {
@@ -357,6 +370,7 @@ CGroupControllerMask cgroup_context_get_mask(CGroupContext *c) {
if (c->memory_accounting ||
c->memory_limit != (uint64_t) -1 ||
c->memory_and_swap_limit != (uint64_t) -1 ||
+ c->memory_soft_limit != (uint64_t) -1 ||
c->kernel_memory_limit != (uint64_t) -1)
mask |= CGROUP_MEMORY;
diff --git a/src/core/cgroup.h b/src/core/cgroup.h
index 644eafc..501fb1b 100644
--- a/src/core/cgroup.h
+++ b/src/core/cgroup.h
@@ -79,6 +79,7 @@ struct CGroupContext {
uint64_t memory_limit;
uint64_t memory_and_swap_limit;
uint64_t kernel_memory_limit;
+ uint64_t memory_soft_limit;
CGroupDevicePolicy device_policy;
LIST_HEAD(CGroupDeviceAllow, device_allow);
diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c
index d398e7c..15f2e21 100644
--- a/src/core/dbus-cgroup.c
+++ b/src/core/dbus-cgroup.c
@@ -135,6 +135,7 @@ const BusProperty bus_cgroup_context_properties[] = {
{ "MemoryLimit", bus_property_append_uint64, "t", offsetof(CGroupContext, memory_limit) },
{ "MemoryAndSwapLimit", bus_property_append_uint64, "t", offsetof(CGroupContext, memory_and_swap_limit) },
{ "KernelMemoryLimit", bus_property_append_uint64, "t", offsetof(CGroupContext, kernel_memory_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 },
{}
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
index e60d014..7945922 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.MemorySoftLimit, config_parse_memory_soft_limit, 0, offsetof($1, cgroup_context)
$1.MemoryAndSwapLimit, config_parse_memory_and_swap_limit, 0, offsetof($1, cgroup_context)
$1.KernelMemoryLimit, config_parse_kernel_memory_limit, 0, offsetof($1, cgroup_context)
$1.DeviceAllow, config_parse_device_allow, 0, offsetof($1, cgroup_context)
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
index 91a9971..3852632 100644
--- a/src/core/load-fragment.c
+++ b/src/core/load-fragment.c
@@ -2105,6 +2105,39 @@ int config_parse_memory_and_swap_limit(
return 0;
}
+int config_parse_memory_soft_limit(
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *section,
+ const char *lvalue,
+ int ltype,
+ const char *rvalue,
+ void *data,
+ void *userdata) {
+
+ CGroupContext *c = data;
+ off_t bytes;
+ int r;
+
+ if (isempty(rvalue)) {
+ c->memory_soft_limit = (uint64_t) -1;
+ return 0;
+ }
+
+ assert_cc(sizeof(uint64_t) == sizeof(off_t));
+
+ r = parse_bytes(rvalue, &bytes);
+ if (r < 0) {
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+ "Memory soft limit '%s' invalid. Ignoring.", rvalue);
+ return 0;
+ }
+
+ c->memory_soft_limit = (uint64_t) bytes;
+ return 0;
+}
+
int config_parse_kernel_memory_limit(
const char *unit,
const char *filename,
@@ -2778,6 +2811,7 @@ void unit_dump_config_items(FILE *f) {
{ config_parse_syscall_filter, "SYSCALL" },
{ config_parse_cpu_shares, "SHARES" },
{ config_parse_memory_limit, "LIMIT" },
+ { config_parse_memory_soft_limit, "LIMIT" },
{ config_parse_memory_and_swap_limit, "LIMIT" },
{ config_parse_kernel_memory_limit, "LIMIT" },
{ config_parse_device_allow, "DEVICE" },
diff --git a/src/core/load-fragment.h b/src/core/load-fragment.h
index c79c7f2..f90ce1a 100644
--- a/src/core/load-fragment.h
+++ b/src/core/load-fragment.h
@@ -78,6 +78,7 @@ int config_parse_environ(const char *unit, const char *filename, unsigned line,
int config_parse_unit_slice(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_cpu_shares(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_memory_limit(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_memory_soft_limit(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_memory_and_swap_limit(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_kernel_memory_limit(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
int config_parse_device_policy(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index cf00334..1bea3bd 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -3658,8 +3658,8 @@ static int append_assignment(DBusMessageIter *iter, const char *assignment) {
!dbus_message_iter_append_basic(&sub, DBUS_TYPE_BOOLEAN, &b))
return log_oom();
- } else if (streq(field, "MemoryLimit") || streq(field, "MemoryAndSwapLimit")
- || streq(field, "KernelMemoryLimit")) {
+ } else if (streq(field, "MemoryLimit") || streq(field, "MemoryAndSwapLimit") ||
+ streq(field, "MemorySoftLimit") || streq(field, "KernelMemoryLimit")) {
off_t bytes;
uint64_t u;
--
1.8.3.2
More information about the systemd-devel
mailing list