Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 83 lines (70 sloc) 2.137 kb
fccc685 Initial open-source release
MLstate authored
1 #!/bin/zsh -e
2
3 interval=2
4
5 case $1 in
6 --help)
7 echo "plotmem: plots the memory usage of a command in real-time using gnuplot"
8 echo "Usage: $0 [options] command [command-options]"
9 echo "Options:"
10 echo " -i <int> specify the refresh interval, in seconds (default 2)"
11 exit 0;;
12 -i)
13 shift
14 interval=$1
15 shift;;
16 esac
17
18 "$@" &
19 pid=$!
20 title=$(basename $1)
21
22 data=$(mktemp -t plotmem-XXXXX)
23
24 trap "rm -f $data" EXIT
25
26 procdir=/proc/$pid
27 echo "# timestamp virtual resident fds" >> $data
28 first=1
29 t0=$(date +%s.%N)
30 fds0=0
31 echo "$t0 0 0 0" >> $data
32
33 plot_loop() {
34 while [ -d "$procdir" ]; do
35 vmsize=$(awk '/^VmSize:/ { print $2; }' $procdir/status 2>/dev/null || break)
36 vmrss=$(awk '/^VmRSS:/ { print $2; }' $procdir/status 2>/dev/null || break)
37 fds=$(ls $procdir/fdinfo 2>/dev/null | wc -l)
38 if [ "$fds" -eq "$fds0" ]; then fds=""; else fds0=$fds; fi
39 date +"%s.%N $vmsize $vmrss $fds" >> $data
40 if [ $first -eq 0 ]; then
41 echo "replot";
42 else
43 cat <<EOF
44 plot "$data" using (\$1-$t0):(\$2/1000) with lines title "Virtual", \
45 "" using (\$1-$t0):(0):(\$3/1000) with filledcurves lc rgb "#7777aa" notitle, \
46 "" using (\$1-$t0):(\$3/1000) with lines title "Resident", \
47 "" using (\$1-$t0):4 with points pt 6 title "Open fds" axes x1y2
48 EOF
49 first=0
50 fi
51 sleep $interval
52 done
53 }
54
55 trap "" 2
56
57 catch_stop() {
58 trap 2
59 plot_loop # should stop once program is dead
60 echo "Program killed. Log in $data. Press enter to close and cleanup." >&2
61 read
62 }
63
64 {
65 echo "set term wxt noraise title \"$title\""
66 echo "set title \"$title\""
67 echo 'set yrange [0:]'
68 echo 'set xlabel "Time (seconds)"'
69 echo 'set ylabel "Memory usage (MB)"'
70 echo 'set ytics nomirror'
71 echo 'set y2range [0:]'
72 echo 'set y2tics scale 1'
73 echo 'set y2label "Fds"'
74 echo 'set key left box'
75
76 trap catch_stop 2
77 plot_loop
78
79 trap 2
80 echo "Program ended. Log in $data. Press enter to close and cleanup." >&2
81 read
82 } | gnuplot >/dev/null
Something went wrong with that request. Please try again.