-
Notifications
You must be signed in to change notification settings - Fork 0
/
memusg
executable file
·67 lines (57 loc) · 1.54 KB
/
memusg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
# memusg -- Measure memory usage of processes
# Usage: memusg [-out <outfile>] <command>
#
# TODO update header
# Author: Jaeho Shin <netj@sparcs.org>
# Created: 2010-08-16
set -um
# check input
[ $# -gt 0 ] || { sed -n '2,/^#$/ s/^# //p' <"$0"; exit 1; }
outfile="memusg.csv"
if [ $1 = "-out" ]
then
outfile=$2
shift 2
fi
cmd="$@"
#cmd="${cmd/"\""/"\\\""}"
echo "memusg running with cmd=$cmd" > temp.txt
pgid=`ps -o pgid= $$`
# 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
done=
trap "done=1" SIGTERM SIGINT
while sizes=`sizes $pgid`
do
set -- $sizes
sample=$((${@/#/+}))
let peak="sample > peak ? sample : peak"
sleep 0.1
[[ -n $done ]] && break
done
#echo "memusg: peak=$peak" >&2
peakgb=`expr $peak / 1048576`
#threads=`expr 32 / $peakgb`
if [ ! -s $outfile ]; then
#echo -e "cmd\tkb\tgb\tthreads" >> memusg.txt
echo -e "cmd,kb,gb" >> $outfile
fi
#echo -e "$cmdprint\t$peak\t$peakgb\t$threads" >> memusg.txt
echo -e "$cmd,$peak,$peakgb" >> $outfile #TODO add path context
echo "$peak"
) &
monpid=$!
echo "#!/bin/bash" > memusg_tmp.sh
echo "$@" >> memusg_tmp.sh
echo "kill -s SIGTERM $monpid" >> memusg_tmp.sh
echo "rm memusg_tmp.sh" >> memusg_tmp.sh
chmod u+x memusg_tmp.sh
exec "./memusg_tmp.sh" # this is really ugly--there has to be a better way