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 22TST_NEEDS_CMDS="awk" 23 24. tst_test.sh 25 26mounted=1 27max=$1 28nbprocess=$2 29 30usage() 31{ 32 cat << EOF 33usage: $0 nsubgroup nprocess 34 35nsubgroup - number of subgroups to create 36nprocess - number of processes to attach to each subgroup 37 38OPTIONS 39EOF 40} 41 42check_free_memory() 43{ 44 local memneeded 45 local memfree=`awk '/MemAvailable/ {print $2}' /proc/meminfo` 46 47 if [ $? -ne 0 ]; then 48 local memcached 49 50 memfree=`awk '/MemFree/ {print $2}' /proc/meminfo` 51 test $? -eq 0 || return 0 52 53 memcached=`awk '/MemCached/ {print $2}' /proc/meminfo` 54 test $? -eq 0 || return 0 55 56 memfree=$((memfree + memcached)) 57 fi 58 59 # On x86_64, each 100 of processes were using ~16 MB of memory, 60 # so try to estimate the needed free memory based on this. 61 memneeded=$((max * nbprocess * 16384 / 100)) 62 63 if [ $memfree -lt $memneeded ]; then 64 tst_brk TCONF "not enough of free memory on this system (approximate need $memneeded kB, free $memfree kB)" 65 fi 66 tst_res TINFO "memory requirements fulfilled (approximate need $memneeded kB, free $memfree kB)" 67 68 return 0 69} 70 71check_limits() 72{ 73 local tasksneeded=$((max * nbprocess)) 74 local tasksmax 75 76 tasksmax=$(tst_get_free_pids) 77 [ $? -eq 0 ] || return 0 78 79 if [ $tasksmax -le $tasksneeded ]; then 80 tst_brk TCONF "limit of tasks is too low (approximate need $tasksneeded, limit $tasksmax)" 81 fi 82 tst_res TINFO "task limit fulfilled (approximate need $tasksneeded, limit $tasksmax)" 83 84 return 0 85} 86 87setup() 88{ 89 if ! grep -q -w cpuacct /proc/cgroups; then 90 tst_brk TCONF "cpuacct not supported on this system" 91 fi 92 93 check_limits 94 # Don't bother with memory limit checks on smaller tests 95 if [ $max -ge 100 ] && [ $nbprocess -ge 100 ]; then 96 check_free_memory 97 fi 98 99 mount_point=`grep -w cpuacct /proc/mounts | cut -f 2 | cut -d " " -f2` 100 tst_res TINFO "cpuacct: $mount_point" 101 if [ "$mount_point" = "" ]; then 102 mounted=0 103 mount_point=/dev/cgroup 104 fi 105 106 testpath=$mount_point/ltp_$TST_ID 107 108 if [ "$mounted" -eq "0" ]; then 109 ROD mkdir -p $mount_point 110 ROD mount -t cgroup -o cpuacct none $mount_point 111 fi 112 113 ROD mkdir $testpath 114 115 # create subgroups 116 for i in `seq 1 $max`; do 117 ROD mkdir $testpath/subgroup_$i 118 done 119 120} 121 122cleanup() 123{ 124 if [ "$testpath" ]; then 125 tst_res TINFO "removing created directories" 126 127 if [ -d "$testpath/subgroup_1" ]; then 128 rmdir $testpath/subgroup_* 129 fi 130 rmdir $testpath 131 fi 132 133 if [ "$mounted" -ne 1 ]; then 134 tst_res TINFO "Umounting cpuacct" 135 umount $mount_point 136 rmdir $mount_point 137 fi 138} 139 140do_test() 141{ 142 tst_res TINFO "Creating $max subgroups each with $nbprocess processes" 143 144 # create and attach process to subgroups 145 for i in `seq 1 $max`; do 146 for j in `seq 1 $nbprocess`; do 147 cpuacct_task $testpath/subgroup_$i/tasks & 148 echo $! >> task_pids 149 done 150 done 151 152 for pid in $(cat task_pids); do wait $pid; done 153 rm -f task_pids 154 155 acc=0 156 fails=0 157 for i in `seq 1 $max`; do 158 tmp=`cat $testpath/subgroup_$i/cpuacct.usage` 159 if [ "$tmp" -eq "0" ]; then 160 fails=$((fails + 1)) 161 fi 162 acc=$((acc + tmp)) 163 done 164 165 ## check that cpuacct.usage != 0 for every subgroup 166 if [ "$fails" -gt "0" ]; then 167 tst_res TFAIL "cpuacct.usage is not equal to 0 for $fails subgroups" 168 else 169 tst_res TPASS "cpuacct.usage is not equal to 0 for every subgroup" 170 fi 171 172 ## check that ltp_subgroup/cpuacct.usage == sum ltp_subgroup/subgroup*/cpuacct.usage 173 ref=`cat $testpath/cpuacct.usage` 174 if [ "$ref" -ne "$acc" ]; then 175 tst_res TFAIL "cpuacct.usage $ref not equal to subgroup*/cpuacct.usage $acc" 176 else 177 tst_res TPASS "cpuacct.usage equal to subgroup*/cpuacct.usage" 178 fi 179} 180 181tst_run 182