• 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 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
22. cgroup_lib.sh
23
24setup()
25{
26	if ! is_cgroup_subsystem_available_and_enabled "memory"; then
27		tst_brk TCONF "Either kernel does not support Memory Resource Controller or feature not enabled"
28	fi
29
30	echo 3 > /proc/sys/vm/drop_caches
31	sleep 2
32	local mem_free=`cat /proc/meminfo | grep MemFree | awk '{ print $2 }'`
33	local swap_free=`cat /proc/meminfo | grep SwapFree | awk '{ print $2 }'`
34	local pgsize=`tst_getconf PAGESIZE`
35
36	MEM=$(( $mem_free + $swap_free / 2 ))
37	MEM=$(( $MEM / 1024 ))
38	RUN_TIME=$(( 15 * 60 ))
39	[ "$pgsize" = "4096" ] && THREAD_SPARE_MB=1 || THREAD_SPARE_MB=8
40
41	tst_res TINFO "Calculated available memory $MEM MB"
42}
43
44cleanup()
45{
46	if [ -e /dev/memcg ]; then
47		umount /dev/memcg 2> /dev/null
48		rmdir /dev/memcg 2> /dev/null
49	fi
50}
51
52do_mount()
53{
54	cleanup
55
56	mkdir /dev/memcg 2> /dev/null
57	mount -t cgroup -omemory memcg /dev/memcg
58}
59
60# $1 Number of cgroups
61# $2 Allocated MB memory in one process
62# $3 The interval to touch memory in a process
63# $4 Test duration (sec)
64run_stress()
65{
66	local cgroups="$1"
67	local mem_size="$2"
68	local interval="$3"
69	local timeout="$4"
70	local i pid pids
71
72	tst_res TINFO "Testing $cgroups cgroups, using $mem_size MB, interval $interval"
73
74	do_mount
75
76	tst_res TINFO "Starting cgroups"
77	for i in $(seq 0 $(($cgroups-1))); do
78		mkdir /dev/memcg/$i 2> /dev/null
79		memcg_process_stress $mem_size $interval &
80		echo $! > /dev/memcg/$i/tasks
81		pids="$! $pids"
82	done
83
84	for pid in $pids; do
85		kill -USR1 $pid 2> /dev/null
86	done
87
88	tst_res TINFO "Testing cgroups for ${timeout}s"
89	sleep $timeout
90
91	tst_res TINFO "Killing groups"
92	i=0
93	for pid in $pids; do
94		kill -KILL $pid 2> /dev/null
95		wait $pid 2> /dev/null
96		rmdir /dev/memcg/$i 2> /dev/null
97		i=$((i+1))
98	done
99
100	tst_res TPASS "Test passed"
101	cleanup
102}
103
104test1()
105{
106	run_stress 150 $(( ($MEM - 150 * $THREAD_SPARE_MB) / 150 )) 5 $RUN_TIME
107}
108
109test2()
110{
111	run_stress 1 $(( $MEM - $THREAD_SPARE_MB)) 5 $RUN_TIME
112}
113
114tst_run
115