xref: /aosp_15_r20/external/ltp/testcases/kernel/controllers/memcg/memcontrol02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*\
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  * Conversion of second kself test in cgroup/test_memcontrol.c.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * Original description:
9*49cdfc7eSAndroid Build Coastguard Worker  * "This test creates a memory cgroup, allocates some anonymous memory
10*49cdfc7eSAndroid Build Coastguard Worker  * and some pagecache and check memory.current and some memory.stat
11*49cdfc7eSAndroid Build Coastguard Worker  * values."
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * Note that the V1 rss and cache counters were renamed to anon and
14*49cdfc7eSAndroid Build Coastguard Worker  * file in V2. Besides error reporting, this test differs from the
15*49cdfc7eSAndroid Build Coastguard Worker  * kselftest in the following ways:
16*49cdfc7eSAndroid Build Coastguard Worker  *
17*49cdfc7eSAndroid Build Coastguard Worker  * . It supports V1.
18*49cdfc7eSAndroid Build Coastguard Worker  * . It writes instead of reads to fill the page cache. Because no
19*49cdfc7eSAndroid Build Coastguard Worker  *   pages were allocated on tmpfs.
20*49cdfc7eSAndroid Build Coastguard Worker  * . It runs on most filesystems available
21*49cdfc7eSAndroid Build Coastguard Worker  * . On EXFAT and extN we change the margin of error between all and file
22*49cdfc7eSAndroid Build Coastguard Worker  *   memory to 50%. Because these allocate non-page-cache memory during writes.
23*49cdfc7eSAndroid Build Coastguard Worker  */
24*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #include "memcontrol_common.h"
27*49cdfc7eSAndroid Build Coastguard Worker #include "pgsize_helpers.h"
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker static size_t page_size;
30*49cdfc7eSAndroid Build Coastguard Worker static struct tst_cg_group *cg_child;
31*49cdfc7eSAndroid Build Coastguard Worker static int fd;
32*49cdfc7eSAndroid Build Coastguard Worker static int file_to_all_error = 10;
33*49cdfc7eSAndroid Build Coastguard Worker 
alloc_anon_50M_check(void)34*49cdfc7eSAndroid Build Coastguard Worker static void alloc_anon_50M_check(void)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker 	const ssize_t size = MB(50);
37*49cdfc7eSAndroid Build Coastguard Worker 	char *buf, *ptr;
38*49cdfc7eSAndroid Build Coastguard Worker 	ssize_t anon, current;
39*49cdfc7eSAndroid Build Coastguard Worker 	const char *const anon_key_fmt =
40*49cdfc7eSAndroid Build Coastguard Worker 		TST_CG_VER_IS_V1(tst_cg, "memory") ? "rss %zd" : "anon %zd";
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	buf = SAFE_MALLOC(size);
43*49cdfc7eSAndroid Build Coastguard Worker 	for (ptr = buf; ptr < buf + size; ptr += kernel_page_size())
44*49cdfc7eSAndroid Build Coastguard Worker 		*ptr = 0;
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CG_SCANF(cg_child, "memory.current", "%zd", &current);
47*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(current >= size,
48*49cdfc7eSAndroid Build Coastguard Worker 		     "(memory.current=%zd) >= (size=%zd)", current, size);
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CG_LINES_SCANF(cg_child, "memory.stat", anon_key_fmt, &anon);
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(anon > 0, "(memory.stat.anon=%zd) > 0", anon);
53*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(values_close(size, anon, 3),
54*49cdfc7eSAndroid Build Coastguard Worker 		     "(size=%zd) ~= (memory.stat.anon=%zd)", size, anon);
55*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(values_close(anon, current, 3),
56*49cdfc7eSAndroid Build Coastguard Worker 		     "(memory.current=%zd) ~= (memory.stat.anon=%zd)",
57*49cdfc7eSAndroid Build Coastguard Worker 		     current, anon);
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker 
alloc_pagecache_50M_check(void)60*49cdfc7eSAndroid Build Coastguard Worker static void alloc_pagecache_50M_check(void)
61*49cdfc7eSAndroid Build Coastguard Worker {
62*49cdfc7eSAndroid Build Coastguard Worker 	const size_t size = MB(50);
63*49cdfc7eSAndroid Build Coastguard Worker 	size_t current, file;
64*49cdfc7eSAndroid Build Coastguard Worker 	const char *const file_key_fmt =
65*49cdfc7eSAndroid Build Coastguard Worker 		TST_CG_VER_IS_V1(tst_cg, "memory") ? "cache %zd" : "file %zd";
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(TMPDIR"/tmpfile", O_RDWR | O_CREAT, 0600);
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CG_SCANF(cg_child, "memory.current", "%zu", &current);
70*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TINFO, "Created temp file: memory.current=%zu", current);
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 	alloc_pagecache(fd, size);
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CG_SCANF(cg_child, "memory.current", "%zu", &current);
75*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(current >= size,
76*49cdfc7eSAndroid Build Coastguard Worker 			 "(memory.current=%zu) >= (size=%zu)", current, size);
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CG_LINES_SCANF(cg_child, "memory.stat", file_key_fmt, &file);
79*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(file > 0, "(memory.stat.file=%zd) > 0", file);
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(values_close(file, current, file_to_all_error),
82*49cdfc7eSAndroid Build Coastguard Worker 			 "(memory.current=%zd) ~= (memory.stat.file=%zd)",
83*49cdfc7eSAndroid Build Coastguard Worker 			 current, file);
84*49cdfc7eSAndroid Build Coastguard Worker 
85*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker 
test_memcg_current(unsigned int n)88*49cdfc7eSAndroid Build Coastguard Worker static void test_memcg_current(unsigned int n)
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker 	size_t current;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	cg_child = tst_cg_group_mk(tst_cg, "child");
93*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CG_SCANF(cg_child, "memory.current", "%zu", &current);
94*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EXPR(current == 0, "(current=%zu) == 0", current);
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 	if (!SAFE_FORK()) {
97*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CG_PRINTF(cg_child, "cgroup.procs", "%d", getpid());
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CG_SCANF(cg_child, "memory.current", "%zu", &current);
100*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Added proc to memcg: memory.current=%zu",
101*49cdfc7eSAndroid Build Coastguard Worker 			current);
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 		if (!n)
104*49cdfc7eSAndroid Build Coastguard Worker 			alloc_anon_50M_check();
105*49cdfc7eSAndroid Build Coastguard Worker 		else
106*49cdfc7eSAndroid Build Coastguard Worker 			alloc_pagecache_50M_check();
107*49cdfc7eSAndroid Build Coastguard Worker 	} else {
108*49cdfc7eSAndroid Build Coastguard Worker 		tst_reap_children();
109*49cdfc7eSAndroid Build Coastguard Worker 		cg_child = tst_cg_group_rm(cg_child);
110*49cdfc7eSAndroid Build Coastguard Worker 	}
111*49cdfc7eSAndroid Build Coastguard Worker }
112*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)113*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
114*49cdfc7eSAndroid Build Coastguard Worker {
115*49cdfc7eSAndroid Build Coastguard Worker 	page_size = SAFE_SYSCONF(_SC_PAGESIZE);
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	switch (tst_fs_type(TMPDIR)) {
118*49cdfc7eSAndroid Build Coastguard Worker 	case TST_VFAT_MAGIC:
119*49cdfc7eSAndroid Build Coastguard Worker 	case TST_EXFAT_MAGIC:
120*49cdfc7eSAndroid Build Coastguard Worker 	case TST_EXT234_MAGIC:
121*49cdfc7eSAndroid Build Coastguard Worker 		file_to_all_error = 50;
122*49cdfc7eSAndroid Build Coastguard Worker 		break;
123*49cdfc7eSAndroid Build Coastguard Worker 	}
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)126*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
127*49cdfc7eSAndroid Build Coastguard Worker {
128*49cdfc7eSAndroid Build Coastguard Worker 	if (cg_child)
129*49cdfc7eSAndroid Build Coastguard Worker 		cg_child = tst_cg_group_rm(cg_child);
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker 
132*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
133*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
134*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
135*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = 2,
136*49cdfc7eSAndroid Build Coastguard Worker 	.test = test_memcg_current,
137*49cdfc7eSAndroid Build Coastguard Worker 	.mount_device = 1,
138*49cdfc7eSAndroid Build Coastguard Worker 	.dev_min_size = 300,
139*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = TMPDIR,
140*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
141*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
142*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
143*49cdfc7eSAndroid Build Coastguard Worker 	.needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
144*49cdfc7eSAndroid Build Coastguard Worker };
145