xref: /aosp_15_r20/external/ltp/testcases/kernel/controllers/memctl/myfunctions.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker#!/bin/bash
2*49cdfc7eSAndroid Build Coastguard Worker# usage ./functions.sh
3*49cdfc7eSAndroid Build Coastguard Worker
4*49cdfc7eSAndroid Build Coastguard Worker#################################################################################
5*49cdfc7eSAndroid Build Coastguard Worker#  Copyright (c) International Business Machines  Corp., 2008                   #
6*49cdfc7eSAndroid Build Coastguard Worker#                                                                               #
7*49cdfc7eSAndroid Build Coastguard Worker#  This program is free software;  you can redistribute it and/or modify        #
8*49cdfc7eSAndroid Build Coastguard Worker#  it under the terms of the GNU General Public License as published by         #
9*49cdfc7eSAndroid Build Coastguard Worker#  the Free Software Foundation; either version 2 of the License, or            #
10*49cdfc7eSAndroid Build Coastguard Worker#  (at your option) any later version.                                          #
11*49cdfc7eSAndroid Build Coastguard Worker#                                                                               #
12*49cdfc7eSAndroid Build Coastguard Worker#  This program is distributed in the hope that it will be useful,              #
13*49cdfc7eSAndroid Build Coastguard Worker#  but WITHOUT ANY WARRANTY;  without even the implied warranty of              #
14*49cdfc7eSAndroid Build Coastguard Worker#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See                    #
15*49cdfc7eSAndroid Build Coastguard Worker#  the GNU General Public License for more details.                             #
16*49cdfc7eSAndroid Build Coastguard Worker#                                                                               #
17*49cdfc7eSAndroid Build Coastguard Worker#  You should have received a copy of the GNU General Public License            #
18*49cdfc7eSAndroid Build Coastguard Worker#  along with this program;  if not, write to the Free Software                 #
19*49cdfc7eSAndroid Build Coastguard Worker#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA      #
20*49cdfc7eSAndroid Build Coastguard Worker#                                                                               #
21*49cdfc7eSAndroid Build Coastguard Worker#################################################################################
22*49cdfc7eSAndroid Build Coastguard Worker#  Name Of File: myfunctions.sh                                                 #
23*49cdfc7eSAndroid Build Coastguard Worker#                                                                               #
24*49cdfc7eSAndroid Build Coastguard Worker#  Description: This file has functions for the setup for testing memory        #
25*49cdfc7eSAndroid Build Coastguard Worker#               controller. setup includes creating controller device,          #
26*49cdfc7eSAndroid Build Coastguard Worker#               mounting it with cgroup filesystem with option memory and       #
27*49cdfc7eSAndroid Build Coastguard Worker#               creating groups in it.                                          #
28*49cdfc7eSAndroid Build Coastguard Worker#                                                                               #
29*49cdfc7eSAndroid Build Coastguard Worker#  Functions:   setup(): creaes /dev/memctl, mounts cgroup fs on it, creates    #
30*49cdfc7eSAndroid Build Coastguard Worker#               groups in that etc.                                             #
31*49cdfc7eSAndroid Build Coastguard Worker#               setmemlimits(): Sets up memory limits for different groups      #
32*49cdfc7eSAndroid Build Coastguard Worker#               usage(): Shows the usage of this file.                          #
33*49cdfc7eSAndroid Build Coastguard Worker#               cleanup(): Does full system cleanup                             #
34*49cdfc7eSAndroid Build Coastguard Worker#                                                                               #
35*49cdfc7eSAndroid Build Coastguard Worker#  Author:       Sudhir Kumar   <[email protected]>                     #
36*49cdfc7eSAndroid Build Coastguard Worker#                                                                               #
37*49cdfc7eSAndroid Build Coastguard Worker#  History:                                                                     #
38*49cdfc7eSAndroid Build Coastguard Worker#                                                                               #
39*49cdfc7eSAndroid Build Coastguard Worker#  DATE         NAME           EMAIL                         DESC               #
40*49cdfc7eSAndroid Build Coastguard Worker#                                                                               #
41*49cdfc7eSAndroid Build Coastguard Worker#  15/03/08  Sudhir Kumar <[email protected]>   Created this test       #
42*49cdfc7eSAndroid Build Coastguard Worker#                                                                               #
43*49cdfc7eSAndroid Build Coastguard Worker#################################################################################
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker	# Write the cleanup function
46*49cdfc7eSAndroid Build Coastguard Workercleanup ()
47*49cdfc7eSAndroid Build Coastguard Worker{
48*49cdfc7eSAndroid Build Coastguard Worker	echo "Cleanup called";
49*49cdfc7eSAndroid Build Coastguard Worker	rm -f memctl_task_* 2>/dev/null
50*49cdfc7eSAndroid Build Coastguard Worker	rmdir /dev/memctl/group* 2> /dev/null
51*49cdfc7eSAndroid Build Coastguard Worker	umount /dev/memctl 2> /dev/null
52*49cdfc7eSAndroid Build Coastguard Worker	rmdir /dev/memctl 2> /dev/null
53*49cdfc7eSAndroid Build Coastguard Worker}
54*49cdfc7eSAndroid Build Coastguard Worker	# Create /dev/memctl &  mount the cgroup file system with memory controller
55*49cdfc7eSAndroid Build Coastguard Worker	#clean any group created eralier (if any)
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Workersetup ()
58*49cdfc7eSAndroid Build Coastguard Worker{
59*49cdfc7eSAndroid Build Coastguard Worker	if [ -e /dev/memctl ]
60*49cdfc7eSAndroid Build Coastguard Worker	then
61*49cdfc7eSAndroid Build Coastguard Worker		echo "WARN:/dev/memctl already exist..overwriting";
62*49cdfc7eSAndroid Build Coastguard Worker		cleanup;
63*49cdfc7eSAndroid Build Coastguard Worker		mkdir /dev/memctl;
64*49cdfc7eSAndroid Build Coastguard Worker	else
65*49cdfc7eSAndroid Build Coastguard Worker		mkdir /dev/memctl
66*49cdfc7eSAndroid Build Coastguard Worker	fi
67*49cdfc7eSAndroid Build Coastguard Worker	mount -t cgroup -omemory cgroup /dev/memctl 2> /dev/null
68*49cdfc7eSAndroid Build Coastguard Worker	if [ $? -ne 0 ]
69*49cdfc7eSAndroid Build Coastguard Worker	then
70*49cdfc7eSAndroid Build Coastguard Worker		echo "ERROR: Could not mount cgroup filesystem on /dev/memctl..Exiting test";
71*49cdfc7eSAndroid Build Coastguard Worker		cleanup;
72*49cdfc7eSAndroid Build Coastguard Worker		exit -1;
73*49cdfc7eSAndroid Build Coastguard Worker	fi
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker	# Group created earlier may again be visible if not cleaned properly...so clean them
76*49cdfc7eSAndroid Build Coastguard Worker	if [ -e /dev/memctl/group_1 ]
77*49cdfc7eSAndroid Build Coastguard Worker	then
78*49cdfc7eSAndroid Build Coastguard Worker		rmdir /dev/memctl/group*
79*49cdfc7eSAndroid Build Coastguard Worker		echo "WARN: Earlier groups found and removed...";
80*49cdfc7eSAndroid Build Coastguard Worker	fi
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker	# Create different groups
83*49cdfc7eSAndroid Build Coastguard Worker	for i in $(seq 1 $NUM_GROUPS)
84*49cdfc7eSAndroid Build Coastguard Worker	do
85*49cdfc7eSAndroid Build Coastguard Worker		group=group_$i;
86*49cdfc7eSAndroid Build Coastguard Worker		mkdir /dev/memctl/$group;# 2>/dev/null
87*49cdfc7eSAndroid Build Coastguard Worker		if [ $? -ne 0 ]
88*49cdfc7eSAndroid Build Coastguard Worker		then
89*49cdfc7eSAndroid Build Coastguard Worker			echo "ERROR: Can't create $group...Check your permissions..Exiting test";
90*49cdfc7eSAndroid Build Coastguard Worker			cleanup;
91*49cdfc7eSAndroid Build Coastguard Worker			exit -1;
92*49cdfc7eSAndroid Build Coastguard Worker		fi
93*49cdfc7eSAndroid Build Coastguard Worker	done
94*49cdfc7eSAndroid Build Coastguard Worker}
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker# The usage of the script file
97*49cdfc7eSAndroid Build Coastguard Workerusage()
98*49cdfc7eSAndroid Build Coastguard Worker{
99*49cdfc7eSAndroid Build Coastguard Worker	echo "Could not start memory controller test";
100*49cdfc7eSAndroid Build Coastguard Worker	echo "usage: run_memctl_test.sh test_num";
101*49cdfc7eSAndroid Build Coastguard Worker	echo "Skipping the memory controller test...";
102*49cdfc7eSAndroid Build Coastguard Worker}
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker# Function to set memory limits for different groups
105*49cdfc7eSAndroid Build Coastguard Workersetmemlimits()
106*49cdfc7eSAndroid Build Coastguard Worker{
107*49cdfc7eSAndroid Build Coastguard Worker	for i in $(seq 1 $NUM_GROUPS)
108*49cdfc7eSAndroid Build Coastguard Worker	do
109*49cdfc7eSAndroid Build Coastguard Worker		limit=MEMLIMIT_GROUP_${i};
110*49cdfc7eSAndroid Build Coastguard Worker		eval limit=\$$limit;
111*49cdfc7eSAndroid Build Coastguard Worker		echo -n $limit >/dev/memctl/group_$i/memory.limit_in_bytes;
112*49cdfc7eSAndroid Build Coastguard Worker		if [ $? -ne 0 ]
113*49cdfc7eSAndroid Build Coastguard Worker		then
114*49cdfc7eSAndroid Build Coastguard Worker			echo "Error in setting the memory limits for group_$i"
115*49cdfc7eSAndroid Build Coastguard Worker			cleanup;
116*49cdfc7eSAndroid Build Coastguard Worker			exit -1;
117*49cdfc7eSAndroid Build Coastguard Worker		fi;
118*49cdfc7eSAndroid Build Coastguard Worker	done
119*49cdfc7eSAndroid Build Coastguard Worker}
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker
122