• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <joerg.vehlow@aox-tech.de>
6#
7# Author: Peng Haitao <penght@cn.fujitsu.com>
8
9MEMCG_TESTFUNC=test
10TST_CNT=4
11
12. memcg_lib.sh
13
14MEM_TO_ALLOC=$((PAGESIZE * 1024))
15MEM_EXPECTED_UPPER=$((MEM_TO_ALLOC + MEM_USAGE_RANGE))
16MEM_LIMIT=$((MEM_TO_ALLOC * 2))
17
18# Run test cases which checks memory.[memsw.]max_usage_in_bytes after make
19# some memory allocation
20test_max_usage_in_bytes()
21{
22	local item="memory.max_usage_in_bytes"
23	[ $1 -eq 1 ] && item="memory.memsw.max_usage_in_bytes"
24	local check_after_reset=$2
25	local exp_stat_size_low=$MEM_TO_ALLOC
26	local exp_stat_size_up=$MEM_EXPECTED_UPPER
27	local kmem_stat_name="${item##*.}"
28
29	start_memcg_process --mmap-anon -s $MEM_TO_ALLOC
30
31	warmup
32	if [ $? -ne 0 ]; then
33		return
34	fi
35
36	ROD echo $MEMCG_PROCESS_PID \> tasks
37	signal_memcg_process $MEM_TO_ALLOC
38	signal_memcg_process $MEM_TO_ALLOC
39
40	if [ "$kmem_stat_name" = "max_usage_in_bytes" ] ||
41	   [ "$kmem_stat_name" = "usage_in_bytes" ]; then
42		local kmem=$(cat "memory.kmem.${kmem_stat_name}")
43		if [ $? -eq 0 ]; then
44			exp_stat_size_low=$((exp_stat_size_low + kmem))
45			exp_stat_size_up=$((exp_stat_size_up + kmem))
46		fi
47	fi
48
49	check_mem_stat $item $exp_stat_size_low $exp_stat_size_up
50
51	if [ $check_after_reset -eq 1 ]; then
52		echo 0 > $item
53		check_mem_stat $item 0 $PAGESIZES
54	fi
55
56	stop_memcg_process
57}
58
59test1()
60{
61	tst_res TINFO "Test memory.max_usage_in_bytes"
62	test_max_usage_in_bytes 0 0
63}
64
65test2()
66{
67	tst_res TINFO "Test memory.memsw.max_usage_in_bytes"
68	memcg_require_memsw
69
70	EXPECT_PASS echo $MEM_LIMIT \> memory.limit_in_bytes
71	EXPECT_PASS echo $MEM_LIMIT \> memory.memsw.limit_in_bytes
72	test_max_usage_in_bytes 1 0
73}
74
75test3()
76{
77	tst_res TINFO "Test reset memory.max_usage_in_bytes"
78	test_max_usage_in_bytes 0 1
79}
80
81test4()
82{
83	tst_res TINFO "Test reset memory.memsw.max_usage_in_bytes"
84	memcg_require_memsw
85
86	EXPECT_PASS echo $MEM_LIMIT \> memory.limit_in_bytes
87	EXPECT_PASS echo $MEM_LIMIT \> memory.memsw.limit_in_bytes
88	test_max_usage_in_bytes 1 1
89}
90
91tst_run
92