Message ID | 20220421234426.3494842-5-yosryahmed@google.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | memcg: introduce per-memcg proactive reclaim | expand |
On Thu, Apr 21, 2022 at 11:44:26PM +0000, Yosry Ahmed wrote: > Add a new test for memory.reclaim that verifies that the interface > correctly reclaims memory as intended, from both anon and file pages. > > Signed-off-by: Yosry Ahmed <yosryahmed@google.com> > Acked-by: Roman Gushchin <roman.gushchin@linux.dev> > --- > .../selftests/cgroup/test_memcontrol.c | 86 +++++++++++++++++++ > 1 file changed, 86 insertions(+) > > diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c > index f2ffb3a30194..5f7c20de2426 100644 > --- a/tools/testing/selftests/cgroup/test_memcontrol.c > +++ b/tools/testing/selftests/cgroup/test_memcontrol.c > @@ -760,6 +760,91 @@ static int test_memcg_max(const char *root) > return ret; > } > > +/* > + * This test checks that memory.reclaim reclaims the given > + * amount of memory (from both anon and file). > + */ > +static int test_memcg_reclaim(const char *root) > +{ > + int ret = KSFT_FAIL, fd, retries; > + char *memcg; > + long current, to_reclaim; > + char buf[64]; > + > + memcg = cg_name(root, "memcg_test"); > + if (!memcg) > + goto cleanup; > + > + if (cg_create(memcg)) > + goto cleanup; > + > + current = cg_read_long(memcg, "memory.current"); > + if (current != 0) > + goto cleanup; > + > + cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(50)); Don't you need is_swap_enabled() check before deciding to do the anon allocations? > + sleep(1); > + > + fd = get_temp_fd(); > + if (fd < 0) > + goto cleanup; > + > + cg_run_nowait(memcg, alloc_pagecache_50M_noexit, (void *)(long)fd); > + sleep(1); These sleep(1)s do not seem robust. Since kernel keeps the page cache around, you can convert anon to use tmpfs and use simple cg_run to trigger the allocations of anon (tmpfs) and file which will remain in memory even after return from cg_run.
On Sat, Apr 23, 2022 at 7:28 AM Shakeel Butt <shakeelb@google.com> wrote: > > On Thu, Apr 21, 2022 at 11:44:26PM +0000, Yosry Ahmed wrote: > > Add a new test for memory.reclaim that verifies that the interface > > correctly reclaims memory as intended, from both anon and file pages. > > > > Signed-off-by: Yosry Ahmed <yosryahmed@google.com> > > Acked-by: Roman Gushchin <roman.gushchin@linux.dev> > > --- > > .../selftests/cgroup/test_memcontrol.c | 86 +++++++++++++++++++ > > 1 file changed, 86 insertions(+) > > > > diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c > > index f2ffb3a30194..5f7c20de2426 100644 > > --- a/tools/testing/selftests/cgroup/test_memcontrol.c > > +++ b/tools/testing/selftests/cgroup/test_memcontrol.c > > @@ -760,6 +760,91 @@ static int test_memcg_max(const char *root) > > return ret; > > } > > > > +/* > > + * This test checks that memory.reclaim reclaims the given > > + * amount of memory (from both anon and file). > > + */ > > +static int test_memcg_reclaim(const char *root) > > +{ > > + int ret = KSFT_FAIL, fd, retries; > > + char *memcg; > > + long current, to_reclaim; > > + char buf[64]; > > + > > + memcg = cg_name(root, "memcg_test"); > > + if (!memcg) > > + goto cleanup; > > + > > + if (cg_create(memcg)) > > + goto cleanup; > > + > > + current = cg_read_long(memcg, "memory.current"); > > + if (current != 0) > > + goto cleanup; > > + > > + cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(50)); > > Don't you need is_swap_enabled() check before deciding to do the anon > allocations? Yes you are right. In the next version will check whether or not swap is enabled and modify the test accordingly. > > + sleep(1); > > + > > + fd = get_temp_fd(); > > + if (fd < 0) > > + goto cleanup; > > + > > + cg_run_nowait(memcg, alloc_pagecache_50M_noexit, (void *)(long)fd); > > + sleep(1); > > These sleep(1)s do not seem robust. Since kernel keeps the page cache > around, you can convert anon to use tmpfs and use simple cg_run to > trigger the allocations of anon (tmpfs) and file which will remain in > memory even after return from cg_run. Other tests in the file are also using sleep approach (see test_memcg_min, although it retries for multiple times until memory.current reaches an expected amount). In my experience it hasn't been flaky running for multiple times on different machines, but I agree it can be flaky (false negative). I am not sure about the allocating file pages with cg_run, is it guaranteed that the page cache will remain in memory until the test ends? If it doesn't, it can also flake, but it would produce false positives (the test could pass because the kernel drained page cache for some other reason although the interface is not working correctly). In my personal opinion, false negative flakes are better than false positives. At least currently the test explicitly and clearly fails if the allocations are not successful. If we rely on the page cache remaining until the test finishes then it could silently pass if the interface is not working correctly. There are a few ways we can go forward with this: 1) Keep everything as-is, but print a message if the test fails due to memory.current not reaching 100MB to make it clear that it didn't fail due to a problem with the interface. 2) Add a sleep/retry loop similar to test_memcg_min instead of sleeping once. 3) Send a signal from forked children when they are done with the allocation, and wait to receive this signal in the test to make sure the allocation is completed. In my opinion we should do (1) (and maybe (2)) for now as (3) could be an overkill if the test is normal passing. Maybe add a comment about (3) being an option in the future if the test flakes. Let me know what you think?
On Sat, Apr 23, 2022 at 02:43:13PM -0700, Yosry Ahmed wrote: [...] > > > + cg_run_nowait(memcg, alloc_pagecache_50M_noexit, (void *)(long)fd); > > > + sleep(1); > > > > These sleep(1)s do not seem robust. Since kernel keeps the page cache > > around, you can convert anon to use tmpfs and use simple cg_run to > > trigger the allocations of anon (tmpfs) and file which will remain in > > memory even after return from cg_run. > > Other tests in the file are also using sleep approach (see > test_memcg_min, although it retries for multiple times until > memory.current reaches an expected amount). In my experience it hasn't > been flaky running for multiple times on different machines, but I > agree it can be flaky (false negative). > If other tests are doing the same then ignore this comment for now. There should be a separate effort to move towards more deterministic approach for the tests instead of sleep(). > I am not sure about the allocating file pages with cg_run, is it > guaranteed that the page cache will remain in memory until the test > ends? If it doesn't, it can also flake, but it would produce false > positives (the test could pass because the kernel drained page cache > for some other reason although the interface is not working > correctly). > > In my personal opinion, false negative flakes are better than false > positives. At least currently the test explicitly and clearly fails if > the allocations are not successful. If we rely on the page cache > remaining until the test finishes then it could silently pass if the > interface is not working correctly. > > There are a few ways we can go forward with this: > 1) Keep everything as-is, but print a message if the test fails due to > memory.current not reaching 100MB to make it clear that it didn't fail > due to a problem with the interface. > 2) Add a sleep/retry loop similar to test_memcg_min instead of sleeping once. > 3) Send a signal from forked children when they are done with the > allocation, and wait to receive this signal in the test to make sure > the allocation is completed. > > In my opinion we should do (1) (and maybe (2)) for now as (3) could be > an overkill if the test is normal passing. Maybe add a comment about > (3) being an option in the future if the test flakes. Let me know what > you think? I am ok with (1).
diff --git a/tools/testing/selftests/cgroup/test_memcontrol.c b/tools/testing/selftests/cgroup/test_memcontrol.c index f2ffb3a30194..5f7c20de2426 100644 --- a/tools/testing/selftests/cgroup/test_memcontrol.c +++ b/tools/testing/selftests/cgroup/test_memcontrol.c @@ -760,6 +760,91 @@ static int test_memcg_max(const char *root) return ret; } +/* + * This test checks that memory.reclaim reclaims the given + * amount of memory (from both anon and file). + */ +static int test_memcg_reclaim(const char *root) +{ + int ret = KSFT_FAIL, fd, retries; + char *memcg; + long current, to_reclaim; + char buf[64]; + + memcg = cg_name(root, "memcg_test"); + if (!memcg) + goto cleanup; + + if (cg_create(memcg)) + goto cleanup; + + current = cg_read_long(memcg, "memory.current"); + if (current != 0) + goto cleanup; + + cg_run_nowait(memcg, alloc_anon_noexit, (void *) MB(50)); + sleep(1); + + fd = get_temp_fd(); + if (fd < 0) + goto cleanup; + + cg_run_nowait(memcg, alloc_pagecache_50M_noexit, (void *)(long)fd); + sleep(1); + + current = cg_read_long(memcg, "memory.current"); + if (!values_close(current, MB(100), 10)) + goto cleanup; + + /* + * Reclaim until current reaches 30M, make sure to reclaim over 50M to + * hit both anon and file. + */ + 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. */ + goto cleanup; + } + + ret = KSFT_PASS; +cleanup: + cg_destroy(memcg); + free(memcg); + close(fd); + + return ret; +} + static int alloc_anon_50M_check_swap(const char *cgroup, void *arg) { long mem_max = (long)arg; @@ -1263,6 +1348,7 @@ struct memcg_test { T(test_memcg_high), T(test_memcg_high_sync), T(test_memcg_max), + T(test_memcg_reclaim), T(test_memcg_oom_events), T(test_memcg_swap_max), T(test_memcg_sock),