mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Roman Gushchin <guro@fb.com>
To: Shuah Khan <shuah@kernel.org>
Cc: <kernel-team@fb.com>, <linux-kernel@vger.kernel.org>,
	Roman Gushchin <guro@fb.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@kernel.org>,
	Vladimir Davydov <vdavydov.dev@gmail.com>,
	Greg Thelen <gthelen@google.com>, Tejun Heo <tj@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: [PATCH] selftests: cgroup: add test for memory.low corner cases
Date: Tue, 22 May 2018 14:31:06 +0100	[thread overview]
Message-ID: <20180522133106.24306-1-guro@fb.com> (raw)

Add a more complicated test for memory.low hierarchical behavior.
It creates the following hierarchy:
        A memory.low = 50M
        B memory.low = 50M
        C memory.low = 50M
        D memory.low = 50M memory.(swap.)max = 200M
        E memory.low = 50M
       / \
      F'  F memory.low = 50M
          G memory.low = 50M
          H memory.low = 50M
          I memory.low = 50M
          J memory.low = 50M
          K memory.low = 50M, memory.usage = 50M

First, it creates local memory pressure by charging pagecache to F'
and checks that K's memory.low actually works (usage ~= 50M).
The it sets C's memory.low to 0 and repeats the test to check
that K's memory.low is not working anymore, despite
that memory.low is disabled above the cgroup with memory pressure (D).

Signed-off-by: Roman Gushchin <guro@fb.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Vladimir Davydov <vdavydov.dev@gmail.com>
Cc: Greg Thelen <gthelen@google.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
---
 tools/testing/selftests/cgroup/test_memcontrol.c | 115 +++++++++++++++++++++++
 1 file changed, 115 insertions(+)

diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c
index beae06c9c899..45168a845a5d 100644
--- a/tools/testing/selftests/cgroup/test_memcontrol.c
+++ b/tools/testing/selftests/cgroup/test_memcontrol.c
@@ -512,6 +512,120 @@ static int test_memcg_low(const char *root)
 	return ret;
 }
 
+static int alloc_pagecache_500M(const char *cgroup, void *arg)
+{
+	int fd = (long)arg;
+
+	return alloc_pagecache(fd, MB(500));
+}
+
+/*
+ * The test creates 10 nested memory cgroups with memory.min set to 50M,
+ * with 50M of pagecache charget to the leaf cgroup.
+ * Then it sets memory.max and memory.swap.max to 200M on the 3rd level,
+ * and creates memory pressure on 5th level.
+ * First it checks that memory.low actually works:
+ * expected usage on 9th level is 50M.
+ * Then it set memory.low on 2nd level to 0 and checks
+ * that memory.low stopped working:
+ * expected usage on 9th level is < 20M.
+ */
+static int test_memcg_low_nested(const char *root)
+{
+	int ret = KSFT_FAIL;
+	char *cgroup[10] = {NULL};
+	char *cgroup2;
+	long usage;
+	int i, fd;
+
+	fd = get_temp_fd();
+	if (fd < 0)
+		goto cleanup;
+
+	for (i = 0; i < ARRAY_SIZE(cgroup); i++) {
+		cgroup[i] = cg_name_indexed(i ? cgroup[i - 1] : root, "cg", i);
+		if (!cgroup[i])
+			goto cleanup;
+
+		if (cg_create(cgroup[i]))
+			goto cleanup;
+
+		if (i < ARRAY_SIZE(cgroup) - 1)
+			if (cg_write(cgroup[i], "cgroup.subtree_control",
+				     "+memory"))
+				goto cleanup;
+
+		if (i == 3) {
+			if (cg_write(cgroup[i], "memory.max", "200M"))
+				goto cleanup;
+
+			if (cg_write(cgroup[i], "memory.swap.max", "0"))
+				goto cleanup;
+		}
+
+		if (cg_write(cgroup[i], "memory.low", "50M"))
+			goto cleanup;
+	}
+
+	cgroup2 = cg_name(cgroup[5], "memcg_pressure");
+	if (!cgroup2)
+		goto cleanup;
+
+	if (cg_create(cgroup2))
+		goto cleanup;
+
+	/* Part 1 */
+	if (cg_run(cgroup[ARRAY_SIZE(cgroup) - 1], alloc_pagecache_50M,
+		   (void *)(long)fd))
+		goto cleanup;
+
+	if (cg_run(cgroup2, alloc_pagecache_500M, (void *)(long)fd))
+		goto cleanup;
+
+	if (!values_close(cg_read_long(cgroup[ARRAY_SIZE(cgroup) - 1],
+				       "memory.current"), MB(50), 3))
+		goto cleanup;
+
+	close(fd);
+	fd = get_temp_fd();
+	if (fd < 0)
+		goto cleanup;
+
+	/* Part 2 */
+	if (cg_write(cgroup[2], "memory.low", "0"))
+		goto cleanup;
+
+	if (cg_run(cgroup[ARRAY_SIZE(cgroup) - 1], alloc_pagecache_50M,
+		   (void *)(long)fd))
+		goto cleanup;
+
+	if (cg_run(cgroup2, alloc_pagecache_500M, (void *)(long)fd))
+		goto cleanup;
+
+	usage = cg_read_long(cgroup[ARRAY_SIZE(cgroup) - 1], "memory.current");
+	if (usage > MB(20))
+		goto cleanup;
+
+	ret = KSFT_PASS;
+
+cleanup:
+	if (cgroup2) {
+		cg_destroy(cgroup2);
+		free(cgroup2);
+	}
+
+	for (i = ARRAY_SIZE(cgroup) - 1; i >= 0; i--) {
+		if (!cgroup[i])
+			continue;
+
+		cg_destroy(cgroup[i]);
+		free(cgroup[i]);
+	}
+
+	close(fd);
+	return ret;
+}
+
 static int alloc_pagecache_max_30M(const char *cgroup, void *arg)
 {
 	size_t size = MB(50);
@@ -781,6 +895,7 @@ struct memcg_test {
 	T(test_memcg_current),
 	T(test_memcg_min),
 	T(test_memcg_low),
+	T(test_memcg_low_nested),
 	T(test_memcg_high),
 	T(test_memcg_max),
 	T(test_memcg_oom_events),
-- 
2.14.3

                 reply	other threads:[~2018-05-22 13:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180522133106.24306-1-guro@fb.com \
    --to=guro@fb.com \
    --cc=akpm@linux-foundation.org \
    --cc=gthelen@google.com \
    --cc=hannes@cmpxchg.org \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhocko@kernel.org \
    --cc=shuah@kernel.org \
    --cc=tj@kernel.org \
    --cc=vdavydov.dev@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®