-
Notifications
You must be signed in to change notification settings - Fork 125
/
plotmem
executable file
·82 lines (70 loc) · 2.09 KB
/
plotmem
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/zsh -e
interval=2
case $1 in
--help)
echo "plotmem: plots the memory usage of a command in real-time using gnuplot"
echo "Usage: $0 [options] command [command-options]"
echo "Options:"
echo " -i <int> specify the refresh interval, in seconds (default 2)"
exit 0;;
-i)
shift
interval=$1
shift;;
esac
"$@" &
pid=$!
title=$(basename $1)
data=$(mktemp -t plotmem-XXXXX)
trap "rm -f $data" EXIT
procdir=/proc/$pid
echo "# timestamp virtual resident fds" >> $data
first=1
t0=$(date +%s.%N)
fds0=0
echo "$t0 0 0 0" >> $data
plot_loop() {
while [ -d "$procdir" ]; do
vmsize=$(awk '/^VmSize:/ { print $2; }' $procdir/status 2>/dev/null || break)
vmrss=$(awk '/^VmRSS:/ { print $2; }' $procdir/status 2>/dev/null || break)
fds=$(ls $procdir/fdinfo 2>/dev/null | wc -l)
if [ "$fds" -eq "$fds0" ]; then fds=""; else fds0=$fds; fi
date +"%s.%N $vmsize $vmrss $fds" >> $data
if [ $first -eq 0 ]; then
echo "replot";
else
cat <<EOF
plot "$data" using (\$1-$t0):(\$2/1000) with lines title "Virtual", \
"" using (\$1-$t0):(0):(\$3/1000) with filledcurves lc rgb "#7777aa" notitle, \
"" using (\$1-$t0):(\$3/1000) with lines title "Resident", \
"" using (\$1-$t0):4 with points pt 6 title "Open fds" axes x1y2
EOF
first=0
fi
sleep $interval
done
}
trap "" 2
catch_stop() {
trap 2
plot_loop # should stop once program is dead
echo "Program killed. Log in $data. Press enter to close and cleanup." >&2
read
}
{
echo "set term wxt noraise title \"$title\""
echo "set title \"$title\""
echo 'set yrange [0:]'
echo 'set xlabel "Time (seconds)"'
echo 'set ylabel "Memory usage (MB)"'
echo 'set ytics nomirror'
echo 'set y2range [0:]'
echo 'set y2tics scale 1'
echo 'set y2label "Fds"'
echo 'set key left box'
trap catch_stop 2
plot_loop
trap 2
echo "Program ended. Log in $data. Press enter to close and cleanup." >&2
read
} | gnuplot >/dev/null