cpuacct.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. TST_SETUP=setup
  15. TST_CLEANUP=cleanup
  16. TST_TESTFUNC=do_test
  17. TST_POS_ARGS=2
  18. TST_USAGE=usage
  19. TST_NEEDS_ROOT=1
  20. TST_NEEDS_TMPDIR=1
  21. . tst_test.sh
  22. mounted=1
  23. max=$1
  24. nbprocess=$2
  25. usage()
  26. {
  27. cat << EOF
  28. usage: $0 nsubgroup nprocess
  29. nsubgroup - number of subgroups to create
  30. nprocess - number of processes to attach to each subgroup
  31. OPTIONS
  32. EOF
  33. }
  34. setup()
  35. {
  36. if ! grep -q -w cpuacct /proc/cgroups; then
  37. tst_brk TCONF "cpuacct not supported on this system"
  38. fi
  39. mount_point=`grep -w cpuacct /proc/mounts | cut -f 2 | cut -d " " -f2`
  40. tst_res TINFO "cpuacct: $mount_point"
  41. if [ "$mount_point" = "" ]; then
  42. mounted=0
  43. mount_point=/dev/cgroup
  44. fi
  45. testpath=$mount_point/ltp_$TST_ID
  46. if [ "$mounted" -eq "0" ]; then
  47. ROD mkdir -p $mount_point
  48. ROD mount -t cgroup -o cpuacct none $mount_point
  49. fi
  50. ROD mkdir $testpath
  51. # create subgroups
  52. for i in `seq 1 $max`; do
  53. ROD mkdir $testpath/subgroup_$i
  54. done
  55. }
  56. cleanup()
  57. {
  58. tst_res TINFO "removing created directories"
  59. if [ -d "$testpath/subgroup_1" ]; then
  60. rmdir $testpath/subgroup_*
  61. fi
  62. rmdir $testpath
  63. if [ "$mounted" -ne 1 ]; then
  64. tst_res TINFO "Umounting cpuacct"
  65. umount $mount_point
  66. rmdir $mount_point
  67. fi
  68. }
  69. do_test()
  70. {
  71. tst_res TINFO "Creating $max subgroups each with $nbprocess processes"
  72. # create and attach process to subgroups
  73. for i in `seq 1 $max`; do
  74. for j in `seq 1 $nbprocess`; do
  75. cpuacct_task $testpath/subgroup_$i/tasks &
  76. echo $! >> task_pids
  77. done
  78. done
  79. for pid in $(cat task_pids); do wait $pid; done
  80. rm -f task_pids
  81. acc=0
  82. fails=0
  83. for i in `seq 1 $max`; do
  84. tmp=`cat $testpath/subgroup_$i/cpuacct.usage`
  85. if [ "$tmp" -eq "0" ]; then
  86. fails=$((fails + 1))
  87. fi
  88. acc=$((acc + tmp))
  89. done
  90. ## check that cpuacct.usage != 0 for every subgroup
  91. if [ "$fails" -gt "0" ]; then
  92. tst_res TFAIL "cpuacct.usage is not equal to 0 for $fails subgroups"
  93. else
  94. tst_res TPASS "cpuacct.usage is not equal to 0 for every subgroup"
  95. fi
  96. ## check that ltp_subgroup/cpuacct.usage == sum ltp_subgroup/subgroup*/cpuacct.usage
  97. ref=`cat $testpath/cpuacct.usage`
  98. if [ "$ref" -ne "$acc" ]; then
  99. tst_res TFAIL "cpuacct.usage $ref not equal to subgroup*/cpuacct.usage $acc"
  100. else
  101. tst_res TPASS "cpuacct.usage equal to subgroup*/cpuacct.usage"
  102. fi
  103. }
  104. tst_run