2
# usage ./runlibcgrouptest.sh
3
# Copyright IBM Corporation. 2008
5
# Author: Sudhir Kumar <skumar@linux.vnet.ibm.com>
7
# This program is free software; you can redistribute it and/or modify it
8
# under the terms of version 2.1 of the GNU Lesser General Public License
9
# as published by the Free Software Foundation.
11
# This program is distributed in the hope that it would be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
# Description: This script runs the the basic tests for testing libcgroup apis.
18
DEBUG=false; # for debug messages
19
FS_MOUNTED=0; # 0 for not mounted, 1 for mounted, 2 for multimounted
20
MOUNTPOINT=/dev/cgroup_controllers; # Just to initialize
21
TARGET=/dev/cgroup_controllers;
22
CONTROLLERS=cpu,memory;
23
NUM_MOUNT=1; # Number of places to be mounted on
24
MULTIMOUNT=false; # mounted at one point only
25
NUM_CTLRS=0; # num of controllers supported
33
declare -a allcontrollers;
38
# Function parameter is the string to print out
48
CGROUP=`cat /proc/mounts|grep -w cgroup|tr -s [:space:]|cut -d" " -f3`;
50
# get first word in case of multiple mounts
51
CGROUP=`echo $CGROUP|cut -d" " -f1`;
53
debug "check_mount_fs(): CGROUP is $CGROUP";
54
if [ "$CGROUP" = "cgroup" ]
56
NUM_MOUNT=`cat /proc/mounts|grep -w cgroup|wc -l`;
57
debug "check_mount_fs(): fs mounted at $NUM_MOUNT places";
59
if [ $NUM_MOUNT -eq 1 ]
63
# Any number of mounts is multi mount
77
PROC_ENTRY=`cat /proc/mounts|grep cgroup|\
78
tr -s [:space:]|cut -d" " -f2`;
79
# Get first mountpoint in case of multiple mounts
80
PROC_ENTRY=`echo $PROC_ENTRY|cut -d" " -f1`;
81
if [ ! -z "$PROC_ENTRY" ]
84
# Need to take care of running tasks in any group ??
85
rmdir $TARGET/* 2> /dev/null ;
88
debug "umounted $TARGET";
92
TARGET=/dev/cgroup_controllers; #??
96
# Put all the supported controllers in an array
97
# We have the priority for cpu and memory controller. So prefer to mount
103
if ! echo $line | grep -q ^#
105
allcontrollers[$NUM_CTLRS]=`echo $line | cut -d" " -f1`;
106
if [ ${allcontrollers[$NUM_CTLRS]} == "cpu" ]; then
108
elif [ ${allcontrollers[$NUM_CTLRS]} == "memory" ]; then
111
debug "controller: ${allcontrollers[$NUM_CTLRS]}";
112
NUM_CTLRS=`expr $NUM_CTLRS + 1`;
114
done < /proc/cgroups;
115
debug "Total controllers $NUM_CTLRS";
118
# Get a second controller other than cpu or memory
119
get_second_controller()
122
while [ $i -lt $NUM_CTLRS ]
124
if [ "${allcontrollers[$i]}" != "cpu" ] &&
125
[ "${allcontrollers[$i]}" != "memory" ]
127
CTLR2=${allcontrollers[$i]};
134
# Check if kernel is not having any of the controllers enabled
137
# prefer if cpu and memory controller are enabled
138
if [ ! -z $CPU ] && [ ! -z $MEMORY ]
140
CONTROLLERS=$CPU,$MEMORY ;
143
debug "first controller is $CTLR1";
144
debug "second controller is $CTLR2";
150
get_second_controller;
151
debug "first controller is $CTLR1";
152
debug "second controller is $CTLR2";
154
elif [ ! -z $MEMORY ]
156
CONTROLLERS=$MEMORY ;
158
get_second_controller;
159
debug "first controller is $CTLR1";
160
debug "second controller is $CTLR2";
163
# Kernel has neither cpu nor memory controller enabled. So there is
164
# no point in running the testcases. At least one of them should be
165
# supported.(or should I run testcases with controllers such as
166
# ns, devices etc? Thoughts???)
167
if [ $NUM_CTLRS -lt 2 ]
169
echo "Kernel needs to have 2 controllers enabled";
170
echo "Recompile your kernel with at least 2 controllers"
171
echo "Exiting the tests.....";
181
local NUM_MOUNT=0; # On how many places to mount on
185
# Check if kernel has controllers enabled
188
echo "Kernel has none of cpu/memory controllers enabled";
189
echo "Recompile your kernel with at least one of these enabled"
190
echo "Exiting the tests.....";
194
# At least one Controller is enabled. So proceed further.
197
echo "WARN: No parameter passed to function mount_fs";
198
echo "taking default as 0....So not mounting cgroup fs";
201
debug "mount_fs fs will be mounted on $NUM_MOUNT places";
204
# create so many directories i.e. mountpoints
205
while [ $NUM_MOUNT -ge $CUR_MOUNT ]
207
NEWTARGET="$TARGET-$CUR_MOUNT";
210
echo "WARN: $NEWTARGET already exist..overwriting";
211
check_mount_fs; # Possibly fs might be mounted on it
212
if [ $FS_MOUNTED -gt 0 ]
221
# In case of multimount, mount controllers at diff points
222
if $MULTIMOUNT ; then
223
if [ $CTLR1 ] && [ $CTLR2 ] ; then
224
if [ $CUR_MOUNT -eq 1 ] ; then
230
echo "Only 1 controler enabled in kernel";
231
echo "So not running multiple mount testcases";
236
mount -t cgroup -o $CONTROLLERS cgroup $NEWTARGET;
239
echo "ERROR: in mounting cgroup fs on $NEWTARGET."
244
target[$CUR_MOUNT]=$NEWTARGET;
245
CUR_MOUNT=`expr $CUR_MOUNT + 1`;
246
FS_MOUNTED=`expr $FS_MOUNTED + 1`;
248
# Group created earlier may again be visible if not cleaned.
250
if [ -e $NEWTARGET/group1 ] # first group that is created
252
# Need to handle if tasks are running in them
253
rmdir $NEWTARGET/group*
254
echo "WARN: Earlier groups found and removed...";
257
debug "$CONTROLLERS controllers mounted on $NEWTARGET directory"
260
if [ $FS_MOUNTED -gt 2 ]
271
if [ -z $ctl1 ] || [ -z $ctl2 ]; then
272
echo "Null controller passed to function get_ctl_num"
273
echo "Exiting the testcases....."
276
# Add any new controller developed here
278
# Following list has to be in sync with enums in header
280
ctl_list[1]="memory";
281
ctl_list[2]="cpuset";
284
while [ ! -z ${ctl_list[$i]} ]; do
285
if [ "${ctl_list[$i]}" == "$ctl1" ]; then
289
if [ "${ctl_list[$i]}" == "$ctl2" ]; then
302
./$TEST_EXEC $MOUNT_INFO $ctl1 $ctl2 ${target[1]} ${target[2]};
305
echo Error in running ./$TEST_EXEC
311
echo Sources not compiled. please run make;
315
###############################
317
# Check if kernel has controllers support
318
if [ -e /proc/cgroups ]
322
echo "Your Kernel seems to be too old. Plz recompile your"
323
echo "Kernel with cgroups and appropriate controllers enabled"
324
echo " Exiting the testcases...."
329
if [ $MY_ID -ne 0 ]; then
330
echo "Only root can start this script."
331
echo " Exiting the testcase..."
335
# TestSet01: Run tests without mounting cgroup filesystem
337
echo Running first set of testcases;
338
echo ==============================
340
FILE=libcgrouptest01;
342
# unmount fs if already mounted
343
if [ $FS_MOUNTED -ne 0 ]
347
debug "FS_MOUNTED = $FS_MOUNTED"
348
runtest $FS_MOUNTED $FILE
354
echo Test binary $FILE exited abnormaly with return value $RC;
355
# Do not exit here. Failure in this case does not imply
356
# failure in other cases also
360
# TestSet02: Run tests with mounting cgroup filesystem
362
echo Running second set of testcases;
363
echo ==============================
364
FILE=libcgrouptest01;
366
# mount fs at one point if not already mounted or multimounted
368
if [ $FS_MOUNTED -eq 0 ]
371
elif [ $FS_MOUNTED -gt 1 ]
376
debug "FS_MOUNTED = $FS_MOUNTED"
377
get_ctl_num $CTLR1 $CTLR2;
378
runtest $FS_MOUNTED $FILE
384
echo Test binary $FILE exited abnormaly with return value $RC;
390
# TestSet03: Run tests with mounting cgroup filesystem at multiple points
392
echo Running third set of testcases;
393
echo ==============================
394
FILE=libcgrouptest01;
396
# mount fs at multiple points
399
if [ $FS_MOUNTED -eq 0 ]
402
elif [ $FS_MOUNTED -eq 1 ]
407
debug "FS_MOUNTED = $FS_MOUNTED"
408
get_ctl_num $CTLR1 $CTLR2;
409
runtest $FS_MOUNTED $FILE
415
echo Test binary $FILE exited abnormaly with return value $RC;