File: procmon.sh

package info (click to toggle)
finit 4.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,216 kB
  • sloc: ansic: 17,060; sh: 6,281; makefile: 532
file content (51 lines) | stat: -rwxr-xr-x 1,537 bytes parent folder | download | duplicates (3)
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
#!/bin/sh
USAGE="Usage: $0 processName"
LOG_FILE="/tmp/memusage.csv"
ELAPSED_TIME=0
PERIOD=1        # seconds

if [ $# -ne 1 ]; then
        echo $USAGE
        exit 1
fi

echo "ElapsedTime,VmSize,VmRSS" > $LOG_FILE

# Monitor memory usage forever until script is killed
while :
do
        SUM_VM_SIZE=0
        SUM_RSS_SIZE=0
        # In case the monitored process has not yet started
        # keep searching until its PID is found
        PROCESS_PIDS=""
        while :
        do
                PROCESS_PIDS=`/bin/pidof $1`
                if [ "$PROCESS_PIDS.X" != ".X" ]; then
                        break
                fi
        done

        for PID in ${PROCESS_PIDS} ; do
                VM_SIZE=`awk '/VmSize/ {print $2}' < /proc/$PID/status`
                if [ "$VM_SIZE.X" = ".X" ]; then
                        continue
                fi
                #echo exprVM_ $SUM_VM_SIZE + $VM_SIZE
                SUM_VM_SIZE=`expr $SUM_VM_SIZE + $VM_SIZE`

                VM_RSS=`awk '/VmRSS/ {print $2}' < /proc/$PID/status`
                if [ "$VM_RSS.X" = ".X" ]; then
                        continue
                fi
                SUM_RSS_SIZE=`expr $SUM_RSS_SIZE + $VM_RSS`
        done
        echo "$ELAPSED_TIME sec, $SUM_VM_SIZE KB, $SUM_RSS_SIZE KB"
        echo "$ELAPSED_TIME,$SUM_VM_SIZE,$SUM_RSS_SIZE" >> $LOG_FILE
        sleep $PERIOD
        VM_SIZE=""
        VM_RSS=""
        # Needs to get actual elapsed time instead of doing this
        ELAPSED_TIME=`expr $ELAPSED_TIME + $PERIOD`
done