Ian Wienand | 72a8be6 | 2015-04-09 13:51:23 +1000 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | set -o errexit |
| 16 | |
| 17 | # time to sleep between checks |
| 18 | SLEEP_TIME=20 |
| 19 | |
| 20 | # MemAvailable is the best estimation and has built-in heuristics |
| 21 | # around reclaimable memory. However, it is not available until 3.14 |
| 22 | # kernel (i.e. Ubuntu LTS Trusty misses it). In that case, we fall |
| 23 | # back to free+buffers+cache as the available memory. |
| 24 | USE_MEM_AVAILBLE=0 |
| 25 | if grep -q '^MemAvailable:' /proc/meminfo; then |
| 26 | USE_MEM_AVAILABLE=1 |
| 27 | fi |
| 28 | |
| 29 | function get_mem_available { |
| 30 | if [[ $USE_MEM_AVAILABLE -eq 1 ]]; then |
| 31 | awk '/^MemAvailable:/ {print $2}' /proc/meminfo |
| 32 | else |
| 33 | awk '/^MemFree:/ {free=$2} |
| 34 | /^Buffers:/ {buffers=$2} |
| 35 | /^Cached:/ {cached=$2} |
| 36 | END { print free+buffers+cached }' /proc/meminfo |
| 37 | fi |
| 38 | } |
| 39 | |
| 40 | # whenever we see less memory available than last time, dump the |
| 41 | # snapshot of current usage; i.e. checking the latest entry in the |
| 42 | # file will give the peak-memory usage |
| 43 | function tracker { |
| 44 | local low_point=$(get_mem_available) |
| 45 | while [ 1 ]; do |
| 46 | |
| 47 | local mem_available=$(get_mem_available) |
| 48 | |
| 49 | if [[ $mem_available -lt $low_point ]]; then |
| 50 | low_point=$mem_available |
| 51 | echo "[[[" |
| 52 | date |
| 53 | echo "---" |
| 54 | # always available greppable output; given difference in |
| 55 | # meminfo output as described above... |
| 56 | echo "peakmem_tracker low_point: $mem_available" |
| 57 | echo "---" |
| 58 | cat /proc/meminfo |
| 59 | echo "---" |
| 60 | # would hierarchial view be more useful (-H)? output is |
| 61 | # not sorted by usage then, however, and the first |
| 62 | # question is "what's using up the memory" |
| 63 | # |
| 64 | # there are a lot of kernel threads, especially on a 8-cpu |
| 65 | # system. do a best-effort removal to improve |
| 66 | # signal/noise ratio of output. |
| 67 | ps --sort=-pmem -eo pid:10,pmem:6,rss:15,ppid:10,cputime:10,nlwp:8,wchan:25,args:100 | |
| 68 | grep -v ']$' |
| 69 | echo "]]]" |
| 70 | fi |
| 71 | |
| 72 | sleep $SLEEP_TIME |
| 73 | done |
| 74 | } |
| 75 | |
| 76 | function usage { |
| 77 | echo "Usage: $0 [-x] [-s N]" 1>&2 |
| 78 | exit 1 |
| 79 | } |
| 80 | |
| 81 | while getopts ":s:x" opt; do |
| 82 | case $opt in |
| 83 | s) |
| 84 | SLEEP_TIME=$OPTARG |
| 85 | ;; |
| 86 | x) |
| 87 | set -o xtrace |
| 88 | ;; |
| 89 | *) |
| 90 | usage |
| 91 | ;; |
| 92 | esac |
| 93 | done |
| 94 | shift $((OPTIND-1)) |
| 95 | |
| 96 | tracker |