Home | History | Annotate | Download | only in cpuacct
      1 #!/bin/sh
      2 
      3 ################################################################################
      4 ##                                                                            ##
      5 ## Copyright (c) 2015 SUSE                                                    ##
      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, but        ##
     13 ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
     14 ## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
     15 ## 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   ##
     20 ## USA                                                                        ##
     21 ##                                                                            ##
     22 ## Author: Cedric Hnyda <chnyda (at] suse.com>                                     ##
     23 ##                                                                            ##
     24 ################################################################################
     25 
     26 # Usage
     27 # ./cpuacct.sh nbsubgroup nbprocess
     28 #
     29 # 1) nbsubgroup : number of subgroup to create
     30 # 2) nbprocess : number of process to attach to each subgroup
     31 #
     32 # Description
     33 #
     34 # 1) Find if cpuacct is mounted, if not mounted, cpuacct will be mounted
     35 # 2) Check that sum ltp_test/subgroup*/cpuacct.usage = ltp_test/cpuacct.usage
     36 #
     37 
     38 TST_ID="cpuacct"
     39 TST_SETUP=setup
     40 TST_CLEANUP=cleanup
     41 TST_TESTFUNC=do_test
     42 TST_POS_ARGS=2
     43 TST_USAGE=usage
     44 TST_NEEDS_ROOT=1
     45 
     46 . tst_test.sh
     47 
     48 mounted=1
     49 max=$1
     50 nbprocess=$2
     51 
     52 usage()
     53 {
     54 	cat << EOF
     55 usage: $0 nsubgroup nprocess
     56 
     57 nsubgroup - number of subgroups to create
     58 nprocess  - number of processes to attach to each subgroup
     59 
     60 OPTIONS
     61 EOF
     62 }
     63 
     64 setup()
     65 {
     66 	if ! grep -q -w cpuacct /proc/cgroups; then
     67 		tst_brk TCONF "cpuacct not supported on this system"
     68 	fi
     69 
     70 	mount_point=`grep -w cpuacct /proc/mounts | cut -f 2 | cut -d " " -f2`
     71 	tst_res TINFO "cpuacct: $mount_point"
     72 	if [ "$mount_point" = "" ]; then
     73 		mounted=0
     74 		mount_point=/dev/cgroup
     75 	fi
     76 
     77 	testpath=$mount_point/ltp_$TST_ID
     78 
     79 	if [ "$mounted" -eq "0" ]; then
     80 		ROD mkdir -p $mount_point
     81 		ROD mount -t cgroup -o cpuacct none $mount_point
     82 	fi
     83 
     84 	ROD mkdir $testpath
     85 
     86 	# create subgroups
     87 	for i in `seq 1 $max`; do
     88 		ROD mkdir $testpath/subgroup_$i
     89 	done
     90 
     91 }
     92 
     93 cleanup()
     94 {
     95 	tst_res TINFO "removing created directories"
     96 
     97 	if [ -d "$testpath/subgroup_1" ]; then
     98 		rmdir $testpath/subgroup_*
     99 	fi
    100 
    101 	rmdir $testpath
    102 
    103 	if [ "$mounted" -ne 1 ]; then
    104 		tst_res TINFO "Umounting cpuacct"
    105 		umount $mount_point
    106 		rmdir $mount_point
    107 	fi
    108 }
    109 
    110 do_test()
    111 {
    112 	tst_res TINFO "Creating $max subgroups each with $nbprocess processes"
    113 
    114 	# create and attach process to subgroups
    115 	for i in `seq 1 $max`; do
    116 		for j in `seq 1 $nbprocess`; do
    117 			cpuacct_task $testpath/subgroup_$i/tasks &
    118 		done
    119 	done
    120 
    121 	wait
    122 
    123 	acc=0
    124 	fails=0
    125 	for i in `seq 1 $max`; do
    126 		tmp=`cat $testpath/subgroup_$i/cpuacct.usage`
    127 		if [ "$tmp" -eq "0" ]; then
    128 			fails=$((fails + 1))
    129 		fi
    130 		acc=$((acc + tmp))
    131 	done
    132 
    133 	## check that cpuacct.usage != 0 for every subgroup
    134 	if [ "$fails" -gt "0" ]; then
    135 		tst_res TFAIL "cpuacct.usage is not equal to 0 for $fails subgroups"
    136 	else
    137 		tst_res TPASS "cpuacct.usage is not equal to 0 for every subgroup"
    138 	fi
    139 
    140 	## check that ltp_subgroup/cpuacct.usage == sum ltp_subgroup/subgroup*/cpuacct.usage
    141 	ref=`cat $testpath/cpuacct.usage`
    142 	if [ "$ref" -ne "$acc" ]; then
    143 		tst_res TFAIL "cpuacct.usage $ref not equal to subgroup*/cpuacct.usage $acc"
    144 	else
    145 		tst_res TPASS "cpuacct.usage equal to subgroup*/cpuacct.usage"
    146 	fi
    147 }
    148 
    149 tst_run
    150