diff options
author | Patrick Simianer <p@simianer.de> | 2014-02-04 13:01:59 +0100 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-02-04 13:01:59 +0100 |
commit | a53f4a89d25766cbedf1d3a935346ef2b31a4f6a (patch) | |
tree | b8e1d2b18fc7dcfdf5902a1d164a345a3420c7d2 | |
parent | 3e23640b2305d738850ce28dbd575ee02b43c76a (diff) |
memusg
-rw-r--r-- | .gitmodules | 2 | ||||
l---------[m---------] | memusg | 1 | ||||
-rwxr-xr-x | memusg_git/memusg | 46 |
3 files changed, 48 insertions, 1 deletions
diff --git a/.gitmodules b/.gitmodules index 230f8ce..adc5e6f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "memusg"] - path = memusg + path = memusg_git url = https://gist.github.com/526585.git diff --git a/memusg b/memusg -Subproject be4a49e3b62104361a412ed29d1a410a6f8c92e diff --git a/memusg_git/memusg b/memusg_git/memusg new file mode 100755 index 0000000..9044a99 --- /dev/null +++ b/memusg_git/memusg @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# memusg -- Measure memory usage of processes +# Usage: memusg COMMAND [ARGS]... +# +# Author: Jaeho Shin <netj@sparcs.org> +# Created: 2010-08-16 +set -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= $$` +# make sure we're in a separate process group +if [ $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 measurement +case `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 ;; +esac + +# monitor the memory usage in the background. +( +peak=0 +while sizes=`sizes $pgid` +do + set -- $sizes + sample=$((${@/#/+})) + let peak="sample > peak ? sample : peak" + sleep 0.1 +done +echo "memusg: peak=$peak" >&2 +) & +monpid=$! + + +# run the given command +exec "$@" |