• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2009 FUJITSU LIMITED
4# Copyright (c) 2018-2019 ARM Ltd. All Rights Reserved.
5# Copyright (c) 2019-2022 Petr Vorel <pvorel@suse.cz>
6#
7# Author: Li Zefan <lizf@cn.fujitsu.com>
8# Restructure for LTP: Shi Weihua <shiwh@cn.fujitsu.com>
9# Added memcg enable/disable functionality: Rishikesh K Rajak <risrajak@linux.vnet.ibm.com>
10
11TST_TESTFUNC=test
12TST_SETUP=setup
13TST_CLEANUP=cleanup
14TST_CNT=2
15TST_NEEDS_ROOT=1
16TST_NEEDS_CMDS="mount umount cat kill mkdir rmdir grep awk cut"
17
18# Each test case runs for 900 secs when everything fine
19# therefore the default 5 mins timeout is not enough.
20TST_TIMEOUT=2100
21
22setup()
23{
24	cgroup_require "memory"
25	cgroup_version=$(cgroup_get_version "memory")
26	test_path=$(cgroup_get_test_path "memory")
27	task_list=$(cgroup_get_task_list "memory")
28	if [ "$cgroup_version" = "2" ]; then
29		ROD echo "+memory" \> "$test_path/cgroup.subtree_control"
30	fi
31
32	tst_res TINFO "test starts with cgroup version $cgroup_version"
33
34	echo 3 > /proc/sys/vm/drop_caches
35	sleep 2
36	local mem_free=`cat /proc/meminfo | grep MemFree | awk '{ print $2 }'`
37	local swap_free=`cat /proc/meminfo | grep SwapFree | awk '{ print $2 }'`
38	local pgsize=`tst_getconf PAGESIZE`
39
40	MEM=$(( $mem_free + $swap_free / 2 ))
41	MEM=$(( $MEM / 1024 ))
42	RUN_TIME=$(( 15 * 60 ))
43	[ "$pgsize" = "4096" ] && THREAD_SPARE_MB=1 || THREAD_SPARE_MB=8
44
45	tst_res TINFO "Calculated available memory $MEM MB"
46}
47
48cleanup()
49{
50	cgroup_cleanup
51}
52
53# $1 Number of cgroups
54# $2 Allocated MB memory in one process
55# $3 The interval to touch memory in a process
56# $4 Test duration (sec)
57run_stress()
58{
59	local cgroups="$1"
60	local mem_size="$2"
61	local interval="$3"
62	local timeout="$4"
63	local i pid pids
64
65	tst_res TINFO "Testing $cgroups cgroups, using $mem_size MB, interval $interval"
66
67	tst_res TINFO "Starting cgroups"
68	for i in $(seq 0 $(($cgroups-1))); do
69		ROD mkdir "$test_path/$i"
70		memcg_process_stress $mem_size $interval &
71		ROD echo $! \> "$test_path/$i/$task_list"
72		pids="$pids $!"
73	done
74
75	for pid in $pids; do
76		kill -USR1 $pid 2> /dev/null
77	done
78
79	tst_res TINFO "Testing cgroups for ${timeout}s"
80	sleep $timeout
81
82	tst_res TINFO "Killing groups"
83	i=0
84	for pid in $pids; do
85		kill -KILL $pid 2> /dev/null
86		wait $pid 2> /dev/null
87		ROD rmdir "$test_path/$i"
88		i=$((i+1))
89	done
90
91	tst_res TPASS "Test passed"
92}
93
94test1()
95{
96	run_stress 150 $(( ($MEM - 150 * $THREAD_SPARE_MB) / 150 )) 5 $RUN_TIME
97}
98
99test2()
100{
101	run_stress 1 $(( $MEM - $THREAD_SPARE_MB)) 5 $RUN_TIME
102}
103
104. cgroup_lib.sh
105tst_run
106