-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathmonitor-mem.sh
More file actions
executable file
·57 lines (45 loc) · 1.58 KB
/
monitor-mem.sh
File metadata and controls
executable file
·57 lines (45 loc) · 1.58 KB
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
#!/bin/bash
command="$*"
echo ${command}
# launch the command in the background
eval ${command} &
# get the PID
PID=$!
memlogfile="mem_evolution_${PID}.log"
echo "#memory evolution of process ${PID}; columns indicate active children" > ${memlogfile}
echo "#command line: ${command}" >> ${memlogfile}
child_pid_list=
# finds out all the (recursive) child process starting from a parent
# output includes the parent
# output is saves
childprocs() {
local parent=$1
if [ "$parent" ] ; then
child_pid_list="$child_pid_list $parent"
for childpid in $(pgrep -P ${parent}); do
childprocs $childpid
done;
fi
}
# while this PID exists we sample
while [ 1 ] ; do
child_pid_list=
childprocs ${PID}
# sum up memory from all child processes
mem=`for pid in $child_pid_list; do cat /proc/$pid/smaps 2>/dev/null | awk -v pid=$pid '/Pss/{mem+=$2} END {print mem/1024.}'; done | tr '\n' ' '`
echo "${mem}" >> ${memlogfile}
# check if the job is still there
ps -p $PID > /dev/null
[ $? == 1 ] && echo "Job finished; Exiting " && break
# take a measurement at some defined rate (can be customized)
sleep ${JOBUTILS_MONITORMEM_SLEEP:-0.005}
done
# record return code of original command
wait ${PID}
RC=$?
# print summary
MAXMEM=`awk '/^[0-9]/{ for(i=1; i<=NF;i++) j+=$i; print j; j=0 }' ${memlogfile} | awk 'BEGIN {m = 0} //{if($1>m){m=$1}} END {print m}'`
AVGMEM=`awk '/^[0-9]/{ for(i=1; i<=NF;i++) j+=$i; print j; j=0 }' ${memlogfile} | awk 'BEGIN {a = 0;c = 0} //{c=c+1;a=a+$1} END {print a/c}'`
echo "PROCESS MAX MEM = ${MAXMEM}"
echo "PROCESS AVG MEM = ${AVGMEM}"
exit ${RC}