~ubuntu-branches/ubuntu/maverick/libcgroup/maverick-proposed

« back to all changes in this revision

Viewing changes to tests/runlibcgrouptest.sh

  • Committer: Bazaar Package Importer
  • Author(s): Dustin Kirkland
  • Date: 2009-08-26 11:29:17 UTC
  • Revision ID: james.westby@ubuntu.com-20090826112917-402ews2uj6v350d2
Tags: upstream-0.34
ImportĀ upstreamĀ versionĀ 0.34

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
# usage ./runlibcgrouptest.sh
 
3
# Copyright IBM Corporation. 2008
 
4
#
 
5
# Author:       Sudhir Kumar <skumar@linux.vnet.ibm.com>
 
6
#
 
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.
 
10
#
 
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.
 
14
#
 
15
# Description: This script runs the the basic tests for testing libcgroup apis.
 
16
#
 
17
 
 
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
 
26
CTLR1="";
 
27
CTLR2="";
 
28
CPU="";
 
29
MEMORY="";
 
30
SKIP_TEST=77
 
31
RET=0
 
32
 
 
33
declare -a allcontrollers;
 
34
declare -a targets;
 
35
 
 
36
debug()
 
37
{
 
38
        # Function parameter is the string to print out
 
39
        if $DEBUG
 
40
        then
 
41
                echo SH:DBG: $1;
 
42
        fi
 
43
}
 
44
 
 
45
check_mount_fs ()
 
46
{
 
47
        local NUM_MOUNT=0;
 
48
        CGROUP=`cat /proc/mounts|grep -w cgroup|tr -s [:space:]|cut -d" " -f3`;
 
49
 
 
50
        # get first word in case of multiple mounts
 
51
        CGROUP=`echo $CGROUP|cut -d" " -f1`;
 
52
 
 
53
        debug "check_mount_fs(): CGROUP is $CGROUP";
 
54
        if [ "$CGROUP" = "cgroup" ]
 
55
        then
 
56
                NUM_MOUNT=`cat /proc/mounts|grep -w cgroup|wc -l`;
 
57
                debug "check_mount_fs(): fs mounted at $NUM_MOUNT places";
 
58
 
 
59
                if [ $NUM_MOUNT -eq 1 ]
 
60
                then
 
61
                        FS_MOUNTED=1;
 
62
                else
 
63
                        # Any number of mounts is multi mount
 
64
                        FS_MOUNTED=2;
 
65
                fi;
 
66
                return 0;       # True
 
67
        else
 
68
                FS_MOUNTED=0;
 
69
                return 1;       # false
 
70
        fi
 
71
}
 
72
 
 
73
umount_fs ()
 
74
{
 
75
        while check_mount_fs
 
76
        do
 
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" ]
 
82
                then
 
83
                        TARGET=$PROC_ENTRY;
 
84
                        # Need to take care of running tasks in any group ??
 
85
                        rmdir $TARGET/* 2> /dev/null ;
 
86
                        umount $TARGET;
 
87
                        rmdir  $TARGET;
 
88
                        debug "umounted $TARGET";
 
89
                fi;
 
90
        done
 
91
        FS_MOUNTED=0;
 
92
        TARGET=/dev/cgroup_controllers; #??
 
93
        echo "Cleanup done";
 
94
}
 
95
 
 
96
# Put all the supported controllers in an array
 
97
# We have the priority for cpu and memory controller. So prefer to mount
 
98
# them if they exist
 
99
get_all_controllers()
 
100
{
 
101
        while [ 1 ]; do
 
102
                read line || break;
 
103
                if ! echo $line | grep -q ^#
 
104
                then
 
105
                        allcontrollers[$NUM_CTLRS]=`echo $line | cut -d" " -f1`;
 
106
                        if [ ${allcontrollers[$NUM_CTLRS]} == "cpu" ]; then
 
107
                                CPU="cpu";
 
108
                        elif [ ${allcontrollers[$NUM_CTLRS]} == "memory" ]; then
 
109
                                MEMORY="memory";
 
110
                        fi;
 
111
                        debug "controller: ${allcontrollers[$NUM_CTLRS]}";
 
112
                        NUM_CTLRS=`expr $NUM_CTLRS + 1`;
 
113
                fi
 
114
        done < /proc/cgroups;
 
115
        debug "Total controllers $NUM_CTLRS";
 
116
}
 
117
 
 
118
# Get a second controller other than cpu or memory
 
119
get_second_controller()
 
120
{
 
121
        local i=0;
 
122
        while [ $i -lt $NUM_CTLRS ]
 
123
        do
 
124
                if [ "${allcontrollers[$i]}" != "cpu" ] &&
 
125
                                 [ "${allcontrollers[$i]}" != "memory" ]
 
126
                then
 
127
                        CTLR2=${allcontrollers[$i]};
 
128
                        return 0;
 
129
                fi;
 
130
        i=`expr $i + 1`;
 
131
        done;
 
132
}
 
133
 
 
134
# Check if kernel is not having any of the controllers enabled
 
135
no_controllers()
 
136
{
 
137
        # prefer if cpu and memory controller are enabled
 
138
        if [ ! -z $CPU ] && [ ! -z $MEMORY ]
 
139
        then
 
140
                CONTROLLERS=$CPU,$MEMORY ;
 
141
                CTLR1=$CPU;
 
142
                CTLR2=$MEMORY;
 
143
                debug "first controller is $CTLR1";
 
144
                debug "second controller is $CTLR2";
 
145
                return 1;       # false
 
146
        elif [ ! -z $CPU ]
 
147
        then
 
148
                CONTROLLERS=$CPU ;
 
149
                CTLR1=$CPU;
 
150
                get_second_controller;
 
151
                debug "first controller is $CTLR1";
 
152
                debug "second controller is $CTLR2";
 
153
                return 1;       # false
 
154
        elif [ ! -z $MEMORY ]
 
155
        then
 
156
                CONTROLLERS=$MEMORY ;
 
157
                CTLR1=$MEMORY;
 
158
                get_second_controller;
 
159
                debug "first controller is $CTLR1";
 
160
                debug "second controller is $CTLR2";
 
161
                return 1;       # false
 
162
        fi;
 
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 ]
 
168
        then
 
169
                echo "Kernel needs to have 2 controllers enabled";
 
170
                echo "Recompile your kernel with at least 2 controllers"
 
171
                echo "Exiting the tests.....";
 
172
                exit $SKIP_TEST;
 
173
        fi;
 
174
 
 
175
        return 0;       # true
 
176
}
 
177
 
 
178
 
 
179
mount_fs ()
 
180
{
 
181
        local NUM_MOUNT=0;      # On how many places to mount on
 
182
        local CUR_MOUNT=1;
 
183
        FS_MOUNTED=0;
 
184
 
 
185
        # Check if kernel has controllers enabled
 
186
        if no_controllers
 
187
        then
 
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.....";
 
191
                exit $SKIP_TEST;
 
192
        fi;
 
193
 
 
194
        # At least one Controller is enabled. So proceed further.
 
195
        if [ -z $1 ]
 
196
        then
 
197
                echo "WARN: No parameter passed to function mount_fs";
 
198
                echo "taking default as 0....So not mounting cgroup fs";
 
199
        else
 
200
                NUM_MOUNT=$1;
 
201
                debug "mount_fs fs will be mounted on $NUM_MOUNT places";
 
202
        fi;
 
203
 
 
204
        # create so many directories i.e. mountpoints
 
205
        while [ $NUM_MOUNT -ge $CUR_MOUNT ]
 
206
        do
 
207
                NEWTARGET="$TARGET-$CUR_MOUNT";
 
208
                if [ -e $NEWTARGET ]
 
209
                then
 
210
                        echo "WARN: $NEWTARGET already exist..overwriting";
 
211
                        check_mount_fs; # Possibly fs might be mounted on it
 
212
                        if [ $FS_MOUNTED -gt 0 ]
 
213
                        then
 
214
                                umount_fs;
 
215
                        else
 
216
                                rmdir $NEWTARGET ;
 
217
                        fi;
 
218
                fi;
 
219
                mkdir $NEWTARGET;
 
220
 
 
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
 
225
                                        CONTROLLERS=$CTLR1;
 
226
                                else
 
227
                                        CONTROLLERS=$CTLR2;
 
228
                                fi;
 
229
                        else
 
230
                                echo "Only 1 controler enabled in kernel";
 
231
                                echo "So not running multiple mount testcases";
 
232
                                exit $SKIP_TEST;
 
233
                        fi;
 
234
                fi;
 
235
 
 
236
                mount -t cgroup -o $CONTROLLERS cgroup $NEWTARGET;
 
237
                if [ $? -ne 0 ]
 
238
                then
 
239
                        echo "ERROR: in mounting cgroup fs on $NEWTARGET."
 
240
                        echo "Exiting test";
 
241
                        umount_fs;
 
242
                        exit -1;
 
243
                fi;
 
244
                target[$CUR_MOUNT]=$NEWTARGET;
 
245
                CUR_MOUNT=`expr $CUR_MOUNT + 1`;
 
246
                FS_MOUNTED=`expr $FS_MOUNTED + 1`;
 
247
 
 
248
                # Group created earlier may again be visible if not cleaned.
 
249
                # So clean them all
 
250
                if [ -e $NEWTARGET/group1 ] # first group that is created
 
251
                then
 
252
                        # Need to handle if tasks are running in them
 
253
                        rmdir $NEWTARGET/group*
 
254
                        echo "WARN: Earlier groups found and removed...";
 
255
                fi;
 
256
 
 
257
                debug "$CONTROLLERS controllers mounted on $NEWTARGET  directory"
 
258
        done;
 
259
 
 
260
        if [ $FS_MOUNTED -gt 2 ]
 
261
        then
 
262
                FS_MOUNTED=2;
 
263
        fi;
 
264
 
 
265
}
 
266
 
 
267
get_ctl_num()
 
268
{
 
269
        ctl1=$1;
 
270
        ctl2=$2;
 
271
        if [ -z $ctl1 ] || [ -z $ctl2 ]; then
 
272
                echo "Null controller passed to function get_ctl_num"
 
273
                echo "Exiting the testcases....."
 
274
        fi
 
275
 
 
276
        # Add any new controller developed here
 
277
        declare -a ctl_list;
 
278
        # Following list has to be in sync with enums in header
 
279
        ctl_list[0]="cpu";
 
280
        ctl_list[1]="memory";
 
281
        ctl_list[2]="cpuset";
 
282
 
 
283
        local i=0;
 
284
        while [ ! -z ${ctl_list[$i]} ]; do
 
285
                if [ "${ctl_list[$i]}" == "$ctl1" ]; then
 
286
                        ctl1=$i;
 
287
                fi;
 
288
 
 
289
                if [ "${ctl_list[$i]}" == "$ctl2" ]; then
 
290
                        ctl2=$i;
 
291
                fi;
 
292
        i=`expr $i + 1`;
 
293
        done;
 
294
}
 
295
 
 
296
runtest()
 
297
{
 
298
        MOUNT_INFO=$1;
 
299
        TEST_EXEC=$2;
 
300
        if [ -f $TEST_EXEC ]
 
301
        then
 
302
                ./$TEST_EXEC $MOUNT_INFO $ctl1 $ctl2 ${target[1]} ${target[2]};
 
303
                if [ $? -ne 0 ]
 
304
                then
 
305
                        echo Error in running ./$TEST_EXEC
 
306
                        echo Exiting tests.
 
307
                else
 
308
                        PID=$!;
 
309
                fi;
 
310
        else
 
311
                echo Sources not compiled. please run make;
 
312
        fi
 
313
}
 
314
 
 
315
###############################
 
316
# Main starts here
 
317
        # Check if kernel has controllers support
 
318
        if [ -e /proc/cgroups ]
 
319
        then
 
320
                get_all_controllers;
 
321
        else
 
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...."
 
325
                exit $SKIP_TEST;
 
326
        fi;
 
327
 
 
328
        MY_ID=`id -u`
 
329
        if [ $MY_ID -ne 0 ]; then
 
330
                echo "Only root can start this script."
 
331
                echo " Exiting the testcase..."
 
332
                exit $SKIP_TEST
 
333
        fi
 
334
 
 
335
# TestSet01: Run tests without mounting cgroup filesystem
 
336
        echo;
 
337
        echo Running first set of testcases;
 
338
        echo ==============================
 
339
        FS_MOUNTED=0;
 
340
        FILE=libcgrouptest01;
 
341
        check_mount_fs;
 
342
        # unmount fs if already mounted
 
343
        if [ $FS_MOUNTED -ne 0 ]
 
344
        then
 
345
                umount_fs;
 
346
        fi;
 
347
        debug "FS_MOUNTED = $FS_MOUNTED"
 
348
        runtest $FS_MOUNTED $FILE
 
349
 
 
350
        wait $PID;
 
351
        RC=$?;
 
352
        if [ $RC -ne 0 ]
 
353
        then
 
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
 
357
                RET=$RC
 
358
        fi;
 
359
 
 
360
# TestSet02: Run tests with mounting cgroup filesystem
 
361
        echo;
 
362
        echo Running second set of testcases;
 
363
        echo ==============================
 
364
        FILE=libcgrouptest01;
 
365
        check_mount_fs;
 
366
        # mount fs at one point if not already mounted or multimounted
 
367
        NUM_MOUNT=1;
 
368
        if [ $FS_MOUNTED -eq 0 ]
 
369
        then
 
370
                mount_fs $NUM_MOUNT;
 
371
        elif [ $FS_MOUNTED -gt 1 ]
 
372
        then
 
373
                umount_fs;
 
374
                mount_fs $NUM_MOUNT;
 
375
        fi;
 
376
        debug "FS_MOUNTED = $FS_MOUNTED"
 
377
        get_ctl_num $CTLR1 $CTLR2;
 
378
        runtest $FS_MOUNTED $FILE
 
379
 
 
380
        wait $PID;
 
381
        RC=$?;
 
382
        if [ $RC -ne 0 ]
 
383
        then
 
384
                echo Test binary $FILE exited abnormaly with return value $RC;
 
385
                RET=$RC
 
386
        fi;
 
387
        umount_fs;
 
388
 
 
389
 
 
390
# TestSet03: Run tests with mounting cgroup filesystem at multiple points
 
391
        echo;
 
392
        echo Running third set of testcases;
 
393
        echo ==============================
 
394
        FILE=libcgrouptest01;
 
395
        check_mount_fs;
 
396
        # mount fs at multiple points
 
397
        MULTIMOUNT=true;
 
398
        NUM_MOUNT=2;
 
399
        if [ $FS_MOUNTED -eq 0 ]
 
400
        then
 
401
                mount_fs $NUM_MOUNT;
 
402
        elif [ $FS_MOUNTED -eq 1 ]
 
403
        then
 
404
                umount_fs;
 
405
                mount_fs $NUM_MOUNT;
 
406
        fi;
 
407
        debug "FS_MOUNTED = $FS_MOUNTED"
 
408
        get_ctl_num $CTLR1 $CTLR2;
 
409
        runtest $FS_MOUNTED $FILE
 
410
 
 
411
        wait $PID;
 
412
        RC=$?;
 
413
        if [ $RC -ne 0 ]
 
414
        then
 
415
                echo Test binary $FILE exited abnormaly with return value $RC;
 
416
                RET=$RC
 
417
        fi;
 
418
        umount_fs;
 
419
 
 
420
        exit $RET;