check_memory.sh 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. # small program to check the memory consumption in given intervals
  3. # It uses the information extracted from </proc/pid/status> and stores
  4. # the data in simple text files for later usage.
  5. # Start script with the following parameters
  6. # $1: The pid of the job which should be controlled
  7. # $2: The time in seconds between the action to be done
  8. # $3: Name of the file which is used for the output of the simulation
  9. # Used system infrmation
  10. #VmSize: The size of the virtual memory allocated to the process
  11. #VmSize: Virtual memory size
  12. #VmLck: The amount of locked memory
  13. #VmRSS: The amount of memory mapped in RAM ( instead of swapped out )
  14. #VmData: The size of the Data segment
  15. #VmStk: The stack size
  16. #VmExe: The size of the executable segment
  17. #VmLib: The size of the library code
  18. #VmPTE: Size of the Page Table entry
  19. #VmPeak: Peak virtual memory size.
  20. # max memory your program seems to take up (counting loaded shared libraries as part of the program)
  21. # peak of virtual memory
  22. #VmHWM: Peak resident set size ("high water mark").
  23. # how much ram was actually devoted to your program
  24. # peak of physical memory.
  25. pid=$1
  26. sleeptime=$2
  27. filename=$3
  28. check=true
  29. echo -e "Date\t DateInSeconds\t vmPeak\t vmSize\t vmLck\t vmHWM\t vmRSS\t vmData\t vmStk\t vmExe\t vmLib\t vmPTE\t filesize\t CPU\t" > memory_consumption_$pid.txt
  30. while $check; do
  31. datestring=$(date +%H:%M:%S)
  32. dateseconds=$(date +%s)
  33. result=$(cat /proc/$pid/status | grep ^Vm)
  34. vmPeak=$(echo $result | cut -f 2-3 -d" ")
  35. vmSize=$(echo $result | cut -f 5-6 -d" ")
  36. vmLck=$(echo $result | cut -f 8-9 -d" ")
  37. vmHWM=$(echo $result | cut -f 11-12 -d" ")
  38. vmRSS=$(echo $result | cut -f 14-15 -d" ")
  39. vmData=$(echo $result | cut -f 17-18 -d" ")
  40. vmStk=$(echo $result | cut -f 20-21 -d" ")
  41. vmExe=$(echo $result | cut -f 23-24 -d" ")
  42. vmLib=$(echo $result | cut -f 26-27 -d" ")
  43. vmPTE=$(echo $result | cut -f 29-30 -d" ")
  44. if [ -e $filename ]; then
  45. filesize=$(ls -la $filename | cut -f 5 -d" ")
  46. else
  47. filesize=0
  48. fi
  49. cpu=$(ps -p $pid -o %cpu=)
  50. echo "$datestring $dateseconds $vmPeak $vmSize $vmLck $vmHWM $vmRSS $vmData $vmStk $vmExe $vmLib $vmPTE $filesize $cpu" >> memory_consumption_$pid.txt
  51. # if pid does not exist any longer (process ends) leave the loop
  52. if [ ! -e /proc/$pid/status ]; then
  53. echo "There is no entry for pid $pid in /proc any longer."
  54. echo "The program stops now."
  55. check=false;
  56. fi
  57. sleep $sleeptime
  58. done