vmrss 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env bash
  2. if [ $# -eq 0 ]; then
  3. echo "Usage: $0 <pid>"
  4. exit 1
  5. fi
  6. function print_vmrss() {
  7. declare -a arr
  8. arr=("$1" 0)
  9. total=0
  10. while [ ${#arr[@]} -gt 0 ]; do
  11. # remove last element
  12. space=${arr[${#arr[@]}-1]}
  13. unset arr[${#arr[@]}-1]
  14. pid=${arr[${#arr[@]}-1]}
  15. unset arr[${#arr[@]}-1]
  16. [ -d "/proc/$pid" ] || continue
  17. GREP_OPTS=${GREP_OPTS:-"
  18. --color=auto
  19. --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}
  20. "}
  21. mem=$(grep $GREP_OPTS VmRSS /proc/$pid/status \
  22. | grep $GREP_OPTS -o '[0-9]\+' \
  23. | awk '{print $1/1024}')
  24. #Add decimals to total
  25. total=$(echo $mem+$total | bc)
  26. # name of process
  27. name=$(ps -p $pid -o comm=)
  28. printf "%${space}s%s($pid): $mem MB\n" '' "$name"
  29. # get children
  30. children=$(pgrep -P $pid)
  31. # add children to array
  32. for child in $children; do
  33. arr+=("$child" $((space+2)))
  34. done
  35. done
  36. printf "Total: $total MB\n"
  37. }
  38. # check VMRSS_MONITOR = 1
  39. if [ ! -z "$VMRSS_MONITOR" ]; then
  40. while true; do
  41. if ps -p $1 > /dev/null
  42. then
  43. print_vmrss $1
  44. sleep 0.5
  45. else
  46. break
  47. fi
  48. done
  49. print_vmrss $1
  50. else
  51. print_vmrss $1
  52. fi