-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathmonitor.sh
More file actions
executable file
·68 lines (52 loc) · 2.21 KB
/
monitor.sh
File metadata and controls
executable file
·68 lines (52 loc) · 2.21 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
58
59
60
61
62
63
64
65
66
67
68
command=$*
echo $command
# launch the command in the background
$command &
# get the PID
PID=$!
memlogfile="mem_evolution_${PID}.txt"
cpulogfile="cpu_evolution_${PID}.txt"
timelogfile="time_evolution_${PID}.txt"
idlogfile="pid_evolution_${PID}.txt"
echo "#command line: ${command}" > ${memlogfile}
echo "#memory evolution of process ${PID}; columns indicate active children" >> ${memlogfile}
echo "#command line: ${command}" > ${cpulogfile}
echo "#cpu evolution of process ${PID}; columns indicate active children" >> ${cpulogfile}
echo "#command line: ${command}" > ${idlogfile}
echo "#child process id evolution of leading o2-sim process with number ${PID}; columns indicate active children" >> ${idlogfile}
echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >> ${memlogfile}
echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >> ${cpulogfile}
echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #" >> ${idlogfile}
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
# get time stamp in mili-seconds
echo $(($(date +%s%N)/1000000)) >> ${timelogfile}
child_pid_list=
childprocs ${PID}
# sum up memory from all child processes
mem=`for pid in $child_pid_list; do cat /proc/$pid/smaps | awk -v pid=$pid '/Pss/{mem+=$2} END {print mem/1024.}'; done | tr '\n' ' '`
echo "${mem}" >> ${memlogfile}
# sum up cpu from all child processes
total_time=`for pid in $child_pid_list; do cat /proc/$pid/stat | awk -v pid=$pid '{total_time=$14+$15+$16+$17} END {print total_time/100.}'; done | tr '\n' ' '`
echo "${total_time}" >> ${cpulogfile}
# record the PID
id=`for pid in $child_pid_list; do echo $pid; done | tr '\n' ' '`
echo "${id}" >> ${idlogfile}
# check if the job is still there
ps -p $PID > /dev/null
[ $? == 1 ] && echo "Job finished; Exiting " && break
sleep 0.005
done