On Thu, May 28, 2020 at 04:25:08PM -0700, Roman Gushchin wrote: > Add a simple test to check the percpu memory accounting. > The test creates a cgroup tree with 1000 child cgroups > and checks values of memory.current and memory.stat::percpu. > > Signed-off-by: Roman Gushchin <guro@xxxxxx> > --- > tools/testing/selftests/cgroup/test_kmem.c | 59 ++++++++++++++++++++++ > 1 file changed, 59 insertions(+) > > diff --git a/tools/testing/selftests/cgroup/test_kmem.c b/tools/testing/selftests/cgroup/test_kmem.c > index 5224dae216e5..a0d4f1a3137d 100644 > --- a/tools/testing/selftests/cgroup/test_kmem.c > +++ b/tools/testing/selftests/cgroup/test_kmem.c > @@ -331,6 +331,64 @@ static int test_kmem_dead_cgroups(const char *root) > return ret; > } > > +/* > + * This test creates a sub-tree with 1000 memory cgroups. > + * Then it checks that the memory.current on the parent level > + * is greater than 0 and approximates matches the percpu value > + * from memory.stat. > + */ > +static int test_percpu_basic(const char *root) > +{ > + int ret = KSFT_FAIL; > + char *parent, *child; > + long current, percpu; > + int i; > + > + parent = cg_name(root, "percpu_basic_test"); > + if (!parent) > + goto cleanup; > + > + if (cg_create(parent)) > + goto cleanup; > + > + if (cg_write(parent, "cgroup.subtree_control", "+memory")) > + goto cleanup; > + > + for (i = 0; i < 1000; i++) { > + child = cg_name_indexed(parent, "child", i); > + if (!child) > + return -1; > + > + if (cg_create(child)) > + goto cleanup_children; > + > + free(child); > + } > + > + current = cg_read_long(parent, "memory.current"); > + percpu = cg_read_key_long(parent, "memory.stat", "percpu "); > + > + if (current > 0 && percpu > 0 && abs(current - percpu) < > + 4096 * 32 * get_nprocs()) So this is checking that we've allocated less than 32 pages per cpu over 1000 child cgroups that's not percpu memory? Is there a more definitive measurement or at least a comment we can leave saying why this limit was chosen. > + ret = KSFT_PASS; > + else > + printf("memory.current %ld\npercpu %ld\n", > + current, percpu); > + > +cleanup_children: > + for (i = 0; i < 1000; i++) { > + child = cg_name_indexed(parent, "child", i); > + cg_destroy(child); > + free(child); > + } > + > +cleanup: > + cg_destroy(parent); > + free(parent); > + > + return ret; > +} > + > #define T(x) { x, #x } > struct kmem_test { > int (*fn)(const char *root); > @@ -341,6 +399,7 @@ struct kmem_test { > T(test_kmem_proc_kpagecgroup), > T(test_kmem_kernel_stacks), > T(test_kmem_dead_cgroups), > + T(test_percpu_basic), > }; > #undef T > > -- > 2.25.4 > >