xref: /aosp_15_r20/external/ltp/testcases/kernel/controllers/memcg/control/memcg_control_test.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2010 Mohamed Naufal Basheer
4# Author: Mohamed Naufal Basheer
5
6TST_TESTFUNC=test
7TST_SETUP=setup
8TST_CLEANUP=cleanup
9TST_CNT=1
10TST_NEEDS_ROOT=1
11TST_NEEDS_TMPDIR=1
12
13PAGE_SIZE=$(tst_getconf PAGESIZE)
14
15TOT_MEM_LIMIT=$PAGE_SIZE
16ACTIVE_MEM_LIMIT=$PAGE_SIZE
17PROC_MEM=$((PAGE_SIZE * 2))
18
19STATUS_PIPE="status_pipe"
20
21# Check if the test process is killed on crossing boundary
22test_proc_kill()
23{
24	mem_process -m $PROC_MEM &
25	sleep 1
26	ROD echo $! \> "$test_dir/$task_list"
27
28	#Instruct the test process to start acquiring memory
29	echo m > $STATUS_PIPE
30	sleep 5
31
32	#Check if killed
33	ps -p $! > /dev/null 2> /dev/null
34	if [ $? -eq 0 ]; then
35		echo m > $STATUS_PIPE
36		echo x > $STATUS_PIPE
37	else
38		: $((KILLED_CNT += 1))
39	fi
40}
41
42# Validate the memory usage limit imposed by the hierarchically topmost group
43test1()
44{
45	cd $TST_TMPDIR
46
47	tst_res TINFO "Test #1: Checking if the memory usage limit imposed by the topmost group is enforced"
48
49	ROD echo "$ACTIVE_MEM_LIMIT" \> "$test_dir/$memory_limit"
50
51	# If the kernel is built without swap, the $memsw_memory_limit file is missing
52	if [ -e "$test_dir/$memsw_memory_limit" ]; then
53		ROD echo "$TOT_MEM_LIMIT" \> "$test_dir/$memsw_memory_limit"
54	fi
55
56	KILLED_CNT=0
57	test_proc_kill
58
59	if [ $PROC_MEM -gt $TOT_MEM_LIMIT ] && [ $KILLED_CNT -eq 0 ]; then
60		tst_res TFAIL "Test #1: failed"
61	else
62		tst_res TPASS "Test #1: passed"
63	fi
64}
65
66setup()
67{
68	cgroup_require "memory"
69	cgroup_version=$(cgroup_get_version "memory")
70	test_dir=$(cgroup_get_test_path "memory")
71	task_list=$(cgroup_get_task_list "memory")
72
73	if [ "$cgroup_version" = "2" ]; then
74		memory_limit="memory.max"
75		memsw_memory_limit="memory.swap.max"
76	else
77		memory_limit="memory.limit_in_bytes"
78		memsw_memory_limit="memory.memsw.limit_in_bytes"
79	fi
80
81	tst_res TINFO "Test starts with cgroup version $cgroup_version"
82}
83
84cleanup()
85{
86	cgroup_cleanup
87}
88
89. cgroup_lib.sh
90tst_run
91