[PATCH v2 2/3] selftests: cgroup: refactor proactive reclaim code to reclaim_until()
From: Yosry Ahmed
Date: Wed Nov 23 2022 - 04:22:35 EST
Refactor the code that drives writing to memory.reclaim (retrying, error
handling, etc) from test_memcg_reclaim() to a helper called
reclaim_until(), which proactively reclaims from a memcg until its
usage reaches a certain value.
This will be used in a following patch in another test.
Signed-off-by: Yosry Ahmed <yosryahmed@xxxxxxxxxx>
---
.../selftests/cgroup/test_memcontrol.c | 85 +++++++++++--------
1 file changed, 49 insertions(+), 36 deletions(-)
diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c
index 8833359556f3..d4182e94945e 100644
--- a/tools/testing/selftests/cgroup/test_memcontrol.c
+++ b/tools/testing/selftests/cgroup/test_memcontrol.c
@@ -645,6 +645,53 @@ static int test_memcg_max(const char *root)
return ret;
}
+/* Reclaim from @memcg until usage reaches @goal_usage */
+static bool reclaim_until(const char *memcg, long goal_usage)
+{
+ char buf[64];
+ int retries = 5;
+ int err;
+ long current, to_reclaim;
+
+ /* Nothing to do here */
+ if (cg_read_long(memcg, "memory.current") <= goal_usage)
+ return true;
+
+ while (true) {
+ current = cg_read_long(memcg, "memory.current");
+ to_reclaim = current - goal_usage;
+
+ /*
+ * We only keep looping if we get -EAGAIN, which means we could
+ * not reclaim the full amount. This means we got -EAGAIN when
+ * we actually reclaimed the requested amount, so fail.
+ */
+ if (to_reclaim <= 0)
+ break;
+
+ snprintf(buf, sizeof(buf), "%ld", to_reclaim);
+ err = cg_write(memcg, "memory.reclaim", buf);
+ if (!err) {
+ /*
+ * If writing succeeds, then the written amount should have been
+ * fully reclaimed (and maybe more).
+ */
+ current = cg_read_long(memcg, "memory.current");
+ if (!values_close(current, goal_usage, 3) && current > goal_usage)
+ break;
+ return true;
+ }
+
+ /* The kernel could not reclaim the full amount, try again. */
+ if (err == -EAGAIN && retries--)
+ continue;
+
+ /* We got an unexpected error or ran out of retries. */
+ break;
+ }
+ return false;
+}
+
/*
* This test checks that memory.reclaim reclaims the given
* amount of memory (from both anon and file, if possible).
@@ -653,8 +700,7 @@ static int test_memcg_reclaim(const char *root)
{
int ret = KSFT_FAIL, fd, retries;
char *memcg;
- long current, expected_usage, to_reclaim;
- char buf[64];
+ long current, expected_usage;
memcg = cg_name(root, "memcg_test");
if (!memcg)
@@ -705,41 +751,8 @@ static int test_memcg_reclaim(const char *root)
* Reclaim until current reaches 30M, this makes sure we hit both anon
* and file if swap is enabled.
*/
- retries = 5;
- while (true) {
- int err;
-
- current = cg_read_long(memcg, "memory.current");
- to_reclaim = current - MB(30);
-
- /*
- * We only keep looping if we get EAGAIN, which means we could
- * not reclaim the full amount.
- */
- if (to_reclaim <= 0)
- goto cleanup;
-
-
- snprintf(buf, sizeof(buf), "%ld", to_reclaim);
- err = cg_write(memcg, "memory.reclaim", buf);
- if (!err) {
- /*
- * If writing succeeds, then the written amount should have been
- * fully reclaimed (and maybe more).
- */
- current = cg_read_long(memcg, "memory.current");
- if (!values_close(current, MB(30), 3) && current > MB(30))
- goto cleanup;
- break;
- }
-
- /* The kernel could not reclaim the full amount, try again. */
- if (err == -EAGAIN && retries--)
- continue;
-
- /* We got an unexpected error or ran out of retries. */
+ if (!reclaim_until(memcg, MB(30)))
goto cleanup;
- }
ret = KSFT_PASS;
cleanup:
--
2.38.1.584.g0f3c55d4c2-goog