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@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
38mounted=1
39max=$1
40nbprocess=$2
41
42export TCID="cpuacct_$1_$2"
43export TST_TOTAL=2
44
45. test.sh
46
47setup()
48{
49	tst_require_root
50
51	grep -q -w cpuacct /proc/cgroups
52	if [ $? -ne 0 ]; then
53		tst_brkm TCONF "cpuacct not supported on this system"
54	fi
55
56	mount_point=`grep -w cpuacct /proc/mounts | cut -f 2 | cut -d " " -f2`
57	tst_resm TINFO "cpuacct: $mount_point"
58	if [ "$mount_point" = "" ]; then
59		mounted=0
60		mount_point=/dev/cgroup
61	fi
62
63	TST_CLEANUP=cleanup
64
65	testpath=$mount_point/ltp_$TCID
66
67	if [ "$mounted" -eq "0" ]; then
68		ROD mkdir -p $mount_point
69		ROD mount -t cgroup -o cpuacct none $mount_point
70	fi
71	ROD mkdir $testpath
72}
73
74cleanup()
75{
76	tst_resm TINFO "removing created directories"
77	rmdir $testpath/subgroup_*
78	rmdir $testpath
79	if [ "$mounted" -ne 1 ]; then
80		tst_resm TINFO "Umounting cpuacct"
81		umount $mount_point
82		rmdir $mount_point
83	fi
84}
85
86setup;
87
88# create subgroups
89for i in `seq 1 $max`; do
90	ROD mkdir -p $testpath/subgroup_$i
91done
92
93# create and attach process to subgroups
94for i in `seq 1 $max`; do
95	for j in `seq 1 $nbprocess`; do
96		cpuacct_task $testpath/subgroup_$i/tasks &
97	done
98done
99
100for job in `jobs -p`; do
101	wait $job
102done
103
104acc=0
105fails=0
106for i in `seq 1 $max`; do
107	tmp=`cat $testpath/subgroup_$i/cpuacct.usage`
108	if [ "$tmp" -eq "0" ]; then
109		fails=$((fails + 1))
110	fi
111	acc=$((acc + tmp))
112done
113
114## check that cpuacct.usage != 0 for every subgroup
115if [ "$fails" -eq "1" ]; then
116	tst_resm TFAIL "cpuacct.usage is not equal to 0 for $fails subgroups"
117else
118	tst_resm TPASS "cpuacct.usage is not equal to 0 for every subgroup"
119fi
120
121## check that ltp_subgroup/cpuacct.usage == sum ltp_subgroup/subgroup*/cpuacct.usage
122ref=`cat $testpath/cpuacct.usage`
123if [ "$ref" != "$acc" ]; then
124	tst_resm TFAIL "ltp_test/cpuacct.usage not equal to ltp_test/subgroup*/cpuacct.usage"
125else
126	tst_resm TPASS "ltp_test/cpuacct.usage equal to ltp_test/subgroup*/cpuacct.usage"
127fi
128
129tst_exit
130