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