| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 | #!/usr/bin/env bash# memusg -- Measure memory usage of processes# Usage: memusg COMMAND [ARGS]...## Author: Jaeho Shin <netj@sparcs.org># Created: 2010-08-16set -um# check input[ $# -gt 0 ] || { sed -n '2,/^#$/ s/^# //p' <"$0"; exit 1; }# TODO support more options: peak, footprint, sampling rate, etc.pgid=`ps -o pgid= $$`command="$@"# make sure we're in a separate process groupif [ $pgid = $(ps -o pgid= $(ps -o ppid= $$)) ]; then    cmd=    set -- "$0" "$@"    for a; do cmd+="'${a//"'"/"'\\''"}' "; done    exec bash -i -c "$cmd"fi# detect operating system and prepare measurementcase `uname` in    Darwin|*BSD) sizes() { /bin/ps -o rss= -g $1; } ;;    Linux) sizes() { /bin/ps -o rss= -$1; } ;;    *) echo "`uname`: unsupported operating system" >&2; exit 2 ;;esacrm -f "histogram.mem.usg"# monitor the memory usage in the background.(peak=0while sizes=`sizes $pgid`do    set -- $sizes    sample=$((${@/#/+}))    echo "$sample" >> "histogram.mem.usg"    let peak="sample > peak ? sample : peak"    sleep 0.1doneecho "memusg \`$command\`: peak=$peak kiloBytes" >> "peak.mem.usg") &monpid=$!echo "running: exec $@"# run the given commandexec "$@"
 |