1#! /bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2012 FUJITSU LIMITED
4# Copyright (c) 2014-2016 Linux Test Project
5# Copyright (c) 2021 Joerg Vehlow <[email protected]>
6#
7# Author: Peng Haitao <[email protected]>
8
9MEMCG_TESTFUNC=test
10TST_CNT=4
11
12# Run test cases which checks memory.[memsw.]max_usage_in_bytes after make
13# some memory allocation
14test_max_usage_in_bytes()
15{
16	local item="memory.max_usage_in_bytes"
17	[ $1 -eq 1 ] && item="memory.memsw.max_usage_in_bytes"
18	local check_after_reset=$2
19	local exp_stat_size_low=$MEM_TO_ALLOC
20	local exp_stat_size_up=$MEM_EXPECTED_UPPER
21	local kmem_stat_name="${item##*.}"
22
23	start_memcg_process --mmap-anon -s $MEM_TO_ALLOC
24
25	warmup
26	if [ $? -ne 0 ]; then
27		return
28	fi
29
30	ROD echo $MEMCG_PROCESS_PID \> tasks
31	signal_memcg_process $MEM_TO_ALLOC
32	signal_memcg_process $MEM_TO_ALLOC
33
34	if [ "$kmem_stat_name" = "max_usage_in_bytes" ] ||
35	   [ "$kmem_stat_name" = "usage_in_bytes" ]; then
36		local kmem=$(cat "memory.kmem.${kmem_stat_name}")
37		if [ $? -eq 0 ]; then
38			exp_stat_size_low=$((exp_stat_size_low + kmem))
39			exp_stat_size_up=$((exp_stat_size_up + kmem))
40		fi
41	fi
42
43	check_mem_stat $item $exp_stat_size_low $exp_stat_size_up
44
45	if [ $check_after_reset -eq 1 ]; then
46		echo 0 > $item
47		check_mem_stat $item 0 $PAGESIZES
48	fi
49
50	stop_memcg_process
51}
52
53test1()
54{
55	tst_res TINFO "Test memory.max_usage_in_bytes"
56	test_max_usage_in_bytes 0 0
57}
58
59test2()
60{
61	tst_res TINFO "Test memory.memsw.max_usage_in_bytes"
62	memcg_require_memsw
63
64	EXPECT_PASS echo $MEM_LIMIT \> memory.limit_in_bytes
65	EXPECT_PASS echo $MEM_LIMIT \> memory.memsw.limit_in_bytes
66	test_max_usage_in_bytes 1 0
67}
68
69test3()
70{
71	tst_res TINFO "Test reset memory.max_usage_in_bytes"
72	test_max_usage_in_bytes 0 1
73}
74
75test4()
76{
77	tst_res TINFO "Test reset memory.memsw.max_usage_in_bytes"
78	memcg_require_memsw
79
80	EXPECT_PASS echo $MEM_LIMIT \> memory.limit_in_bytes
81	EXPECT_PASS echo $MEM_LIMIT \> memory.memsw.limit_in_bytes
82	test_max_usage_in_bytes 1 1
83}
84
85. memcg_lib.sh
86
87MEM_TO_ALLOC=$((PAGESIZE * 1024))
88MEM_EXPECTED_UPPER=$((MEM_TO_ALLOC + MEM_USAGE_RANGE))
89MEM_LIMIT=$((MEM_TO_ALLOC * 2))
90
91tst_run
92