myfunctions.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/bin/bash
  2. # usage ./functions.sh
  3. #################################################################################
  4. # Copyright (c) International Business Machines Corp., 2008 #
  5. # #
  6. # This program is free software; you can redistribute it and/or modify #
  7. # it under the terms of the GNU General Public License as published by #
  8. # the Free Software Foundation; either version 2 of the License, or #
  9. # (at your option) any later version. #
  10. # #
  11. # This program is distributed in the hope that it will be useful, #
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of #
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See #
  14. # the GNU General Public License for more details. #
  15. # #
  16. # You should have received a copy of the GNU General Public License #
  17. # along with this program; if not, write to the Free Software #
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #
  19. # #
  20. #################################################################################
  21. # Name Of File: myfunctions.sh #
  22. # #
  23. # Description: This file has functions for the setup for testing memory #
  24. # controller. setup includes creating controller device, #
  25. # mounting it with cgroup filesystem with option memory and #
  26. # creating groups in it. #
  27. # #
  28. # Functions: setup(): creaes /dev/memctl, mounts cgroup fs on it, creates #
  29. # groups in that etc. #
  30. # setmemlimits(): Sets up memory limits for different groups #
  31. # usage(): Shows the usage of this file. #
  32. # cleanup(): Does full system cleanup #
  33. # #
  34. # Author: Sudhir Kumar <skumar@linux.vnet.ibm.com> #
  35. # #
  36. # History: #
  37. # #
  38. # DATE NAME EMAIL DESC #
  39. # #
  40. # 15/03/08 Sudhir Kumar <skumar@linux.vnet.ibm.com> Created this test #
  41. # #
  42. #################################################################################
  43. # Write the cleanup function
  44. cleanup ()
  45. {
  46. echo "Cleanup called";
  47. rm -f memctl_task_* 2>/dev/null
  48. rmdir /dev/memctl/group* 2> /dev/null
  49. umount /dev/memctl 2> /dev/null
  50. rmdir /dev/memctl 2> /dev/null
  51. }
  52. # Create /dev/memctl & mount the cgroup file system with memory controller
  53. #clean any group created eralier (if any)
  54. setup ()
  55. {
  56. if [ -e /dev/memctl ]
  57. then
  58. echo "WARN:/dev/memctl already exist..overwriting";
  59. cleanup;
  60. mkdir /dev/memctl;
  61. else
  62. mkdir /dev/memctl
  63. fi
  64. mount -t cgroup -omemory cgroup /dev/memctl 2> /dev/null
  65. if [ $? -ne 0 ]
  66. then
  67. echo "ERROR: Could not mount cgroup filesystem on /dev/memctl..Exiting test";
  68. cleanup;
  69. exit -1;
  70. fi
  71. # Group created earlier may again be visible if not cleaned properly...so clean them
  72. if [ -e /dev/memctl/group_1 ]
  73. then
  74. rmdir /dev/memctl/group*
  75. echo "WARN: Earlier groups found and removed...";
  76. fi
  77. # Create different groups
  78. for i in $(seq 1 $NUM_GROUPS)
  79. do
  80. group=group_$i;
  81. mkdir /dev/memctl/$group;# 2>/dev/null
  82. if [ $? -ne 0 ]
  83. then
  84. echo "ERROR: Can't create $group...Check your permissions..Exiting test";
  85. cleanup;
  86. exit -1;
  87. fi
  88. done
  89. }
  90. # The usage of the script file
  91. usage()
  92. {
  93. echo "Could not start memory controller test";
  94. echo "usage: run_memctl_test.sh test_num";
  95. echo "Skipping the memory controller test...";
  96. }
  97. # Function to set memory limits for different groups
  98. setmemlimits()
  99. {
  100. for i in $(seq 1 $NUM_GROUPS)
  101. do
  102. limit=MEMLIMIT_GROUP_${i};
  103. eval limit=\$$limit;
  104. echo -n $limit >/dev/memctl/group_$i/memory.limit_in_bytes;
  105. if [ $? -ne 0 ]
  106. then
  107. echo "Error in setting the memory limits for group_$i"
  108. cleanup;
  109. exit -1;
  110. fi;
  111. done
  112. }