Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 101 lines (85 sloc) 1.859 kB
6eed299 @milianw cleanup
authored
1 #!/bin/bash
2
3 #
4 # track memory of given application, identified by PID,
5 # using pmap -x, to show RSS and Dirty memory usage.
6 #
7 # visualization can later on be done with the
8 # show_memory.sh script.
9 #
10
2601ebf @milianw add two neat little scripts that track memory consumption of an app v…
authored
11 pid=$1
8ef88ef @milianw make it possible to pass a command directly to track_memory.sh
authored
12 sleep=1;
c3ea5d1 @milianw make it possible to define sleep period, show command + sleep in graph
authored
13
d4489e1 @milianw Make it possible to run track_memory.sh on busybox
authored
14 if [[ "$(pmap --version 2>&1 | grep BusyBox)" ]]; then
15 summer=$(cat <<'AWK'
16 {
17 if ($1 == "total") {
18 pss = $3
19 dirty = $4
20 } else if ($8 == "anon" || $7 == "[heap]") {
21 heap += $3
22 } else if ($7 == "[stack]") {
23 stack += $3
24 }
25 }
26 END {
27 print pss,dirty,heap,stack
28 }
29 AWK
30 )
31 else
32 summer=$(cat <<'AWK'
33 {
34 if ($1 == "total") {
35 rss = $4
36 dirty = $5
37 } else if ($7 == "anon") {
38 heap += $3
39 } else if ($7 == "stack") {
40 stack += $3
41 }
42 }
43 END {
44 print rss,dirty,heap,stack
45 }
46 AWK
47 )
48 fi
49
50 if [[ "$pid" != "" && ! -d /proc/$pid/ ]]; then
8ef88ef @milianw make it possible to pass a command directly to track_memory.sh
authored
51 # maybe a command was given
52 if [[ "$(which $1)" != "" ]]; then
53 echo "launching: $@"
54 $@ &
55 pid=$!
8fb19df @milianw Trap and kill the launched app.
authored
56 trap "{ kill $pid; exit 255; }" SIGINT SIGTERM
8ef88ef @milianw make it possible to pass a command directly to track_memory.sh
authored
57 fi
58 else
59 if [[ "$2" != "" ]]; then
60 sleep=$2
61 fi
c3ea5d1 @milianw make it possible to define sleep period, show command + sleep in graph
authored
62 fi
2601ebf @milianw add two neat little scripts that track memory consumption of an app v…
authored
63
d4489e1 @milianw Make it possible to run track_memory.sh on busybox
authored
64 if [[ "$pid" == "" || ! -d /proc/$pid/ ]]; then
2601ebf @milianw add two neat little scripts that track memory consumption of an app v…
authored
65 echo "cannot find program with pid $pid"
6eed299 @milianw cleanup
authored
66 echo "track_memory.sh PID [SLEEP_TIMEOUT]"
67 echo
68 echo "example: track_memory.sh \$(pidof someapp) 0.1"
2601ebf @milianw add two neat little scripts that track memory consumption of an app v…
authored
69 exit
70 fi
71
1a91ebf @milianw Optionally save output file in env-var defined path
authored
72
73
2601ebf @milianw add two neat little scripts that track memory consumption of an app v…
authored
74 logfile=mem.log.$pid
75
1a91ebf @milianw Optionally save output file in env-var defined path
authored
76 if [[ "${TRACK_MEMORY_OUTPUTDIR}" != "" ]]; then
77 logfile=${TRACK_MEMORY_OUTPUTDIR}/$logfile
78 fi
79
d4489e1 @milianw Make it possible to run track_memory.sh on busybox
authored
80 echo "# $(cat /proc/$pid/cmdline)" > $logfile
c3ea5d1 @milianw make it possible to define sleep period, show command + sleep in graph
authored
81 echo "# $sleep" >> $logfile
82
6eed299 @milianw cleanup
authored
83 cat $logfile
84
d4489e1 @milianw Make it possible to run track_memory.sh on busybox
authored
85 while [ -d /proc/$pid/ ]; do
026947a @milianw echo snapshot data on cli
authored
86 echo -n "snapshot $pid: "
87 pmap -x $pid | awk "$summer" | tee -a $logfile
5db8ff5 @milianw improve {show,track_memory}.sh
authored
88 sleep $sleep
2601ebf @milianw add two neat little scripts that track memory consumption of an app v…
authored
89 done
90
d4489e1 @milianw Make it possible to run track_memory.sh on busybox
authored
91 visualizer=$(dirname $0)/show_memory.sh
92
93 if [ -f "$visualizer" ]; then
94 echo "done tracking, visualizing "$(readlink -f "$logfile")
95 "$visualizer" "$logfile"
96 else
97 echo "done, generated "$(readlink -f "$logfile")
98 fi
8ef88ef @milianw make it possible to pass a command directly to track_memory.sh
authored
99
100 # kate: replace-tabs on;
Something went wrong with that request. Please try again.