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