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