1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2015 SUSE
4# Author: Cedric Hnyda <chnyda@suse.com>
5#
6# Usage
7# ./cpuacct.sh nbsubgroup nbprocess
8# nbsubgroup: number of subgroup to create
9# nbprocess: number of process to attach to each subgroup
10#
11# Description
12# 1) Find if cpuacct is mounted, if not mounted, cpuacct will be mounted
13# 2) Check that sum ltp_test/subgroup*/cpuacct.usage = ltp_test/cpuacct.usage
14
15TST_SETUP=setup
16TST_CLEANUP=cleanup
17TST_TESTFUNC=do_test
18TST_POS_ARGS=2
19TST_USAGE=usage
20TST_NEEDS_ROOT=1
21TST_NEEDS_TMPDIR=1
22
23. tst_test.sh
24
25mounted=1
26max=$1
27nbprocess=$2
28
29usage()
30{
31	cat << EOF
32usage: $0 nsubgroup nprocess
33
34nsubgroup - number of subgroups to create
35nprocess  - number of processes to attach to each subgroup
36
37OPTIONS
38EOF
39}
40
41setup()
42{
43	if ! grep -q -w cpuacct /proc/cgroups; then
44		tst_brk TCONF "cpuacct not supported on this system"
45	fi
46
47	mount_point=`grep -w cpuacct /proc/mounts | cut -f 2 | cut -d " " -f2`
48	tst_res TINFO "cpuacct: $mount_point"
49	if [ "$mount_point" = "" ]; then
50		mounted=0
51		mount_point=/dev/cgroup
52	fi
53
54	testpath=$mount_point/ltp_$TST_ID
55
56	if [ "$mounted" -eq "0" ]; then
57		ROD mkdir -p $mount_point
58		ROD mount -t cgroup -o cpuacct none $mount_point
59	fi
60
61	ROD mkdir $testpath
62
63	# create subgroups
64	for i in `seq 1 $max`; do
65		ROD mkdir $testpath/subgroup_$i
66	done
67
68}
69
70cleanup()
71{
72	tst_res TINFO "removing created directories"
73
74	if [ -d "$testpath/subgroup_1" ]; then
75		rmdir $testpath/subgroup_*
76	fi
77
78	rmdir $testpath
79
80	if [ "$mounted" -ne 1 ]; then
81		tst_res TINFO "Umounting cpuacct"
82		umount $mount_point
83		rmdir $mount_point
84	fi
85}
86
87do_test()
88{
89	tst_res TINFO "Creating $max subgroups each with $nbprocess processes"
90
91	# create and attach process to subgroups
92	for i in `seq 1 $max`; do
93		for j in `seq 1 $nbprocess`; do
94			cpuacct_task $testpath/subgroup_$i/tasks &
95			echo $! >> task_pids
96		done
97	done
98
99	for pid in $(cat task_pids); do wait $pid; done
100	rm -f task_pids
101
102	acc=0
103	fails=0
104	for i in `seq 1 $max`; do
105		tmp=`cat $testpath/subgroup_$i/cpuacct.usage`
106		if [ "$tmp" -eq "0" ]; then
107			fails=$((fails + 1))
108		fi
109		acc=$((acc + tmp))
110	done
111
112	## check that cpuacct.usage != 0 for every subgroup
113	if [ "$fails" -gt "0" ]; then
114		tst_res TFAIL "cpuacct.usage is not equal to 0 for $fails subgroups"
115	else
116		tst_res TPASS "cpuacct.usage is not equal to 0 for every subgroup"
117	fi
118
119	## check that ltp_subgroup/cpuacct.usage == sum ltp_subgroup/subgroup*/cpuacct.usage
120	ref=`cat $testpath/cpuacct.usage`
121	if [ "$ref" -ne "$acc" ]; then
122		tst_res TFAIL "cpuacct.usage $ref not equal to subgroup*/cpuacct.usage $acc"
123	else
124		tst_res TPASS "cpuacct.usage equal to subgroup*/cpuacct.usage"
125	fi
126}
127
128tst_run
129