Skip to content

Instantly share code, notes, and snippets.

@pulcheri
Created January 31, 2013 13:06
Show Gist options
  • Save pulcheri/4682724 to your computer and use it in GitHub Desktop.
Save pulcheri/4682724 to your computer and use it in GitHub Desktop.
#!/bin/sh
USAGE="Usage: $0 processName"
if [ $# -ne 1 ]; then
echo $USAGE
exit 1
fi
LOG_FILE="memusage.csv"
rm $LOG_FILE
echo "ElapsedTime,VmSize,VmRSS" > $LOG_FILE
ELAPSED_TIME=0
PERIOD=1 # seconds
# Monitor memory usage forever until script is killed
while :
do
SUM_VM_SIZE=0
SUM_RSS_SIZE=0
SUM_THREADS_SIZE=0
SUM_CPU=0
PREV_TOTAL=0
PREV_IDLE=0
# In case the monitored process has not yet started
# keep searching until its PID is found
PROCESS_PIDS=""
while :
do
PROCESS_PIDS=`pidof $1`
if [ -z "$PROCESS_PIDS" ];then
echo "nu such '$1' process "
exit
fi
if [ "$PROCESS_PIDS.X" != ".X" ]; then
break
fi
done
for PID in ${PROCESS_PIDS} ; do
c=`cat /proc/$PID/status`
VM_SIZE=`echo "$c" | awk '/VmSize/ {print $2}' `
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=`echo "$c" | awk '/VmRSS/ {print $2}'`
if [ "$VM_RSS.X" = ".X" ]; then
continue
fi
SUM_RSS_SIZE=`expr $SUM_RSS_SIZE + $VM_RSS`
VM_THRDS=`echo "$c" | awk '/Threads/ {print $2}'`
if [ "$VM_THRDS.X" = ".X" ]; then
continue
fi
SUM_THREADS_SIZE=`expr $SUM_THREADS_SIZE + $VM_THRDS`
done
########################################################################
#CPU
cpus=`top -b -n 1 | grep "[0-9][0-9]\.[0-9][0-9] $1" | awk '{print $9}'`
SUM_CPU=0
CPUS=0
for cpu in $cpus; do
ncpu=$((cpu+0))
SUM_CPU=$((ncpu+SUM_CPU))
CPUS=$((CPUS+1))
#echo "cpu=" "$cpu and $SUM_CPU"
done
#SUM_CPU=`expr $SUM_CPU+$DIFF_USAGE`
TOTALRP=$((CPUS*SUM_THREADS_SIZE))
########################################################################
echo "VM: $SUM_VM_SIZE, RSS: $SUM_RSS_SIZE THRDS: $SUM_THREADS_SIZE PROCS:$CPUS TOTAL: $TOTALRP CPU%: $SUM_CPU"
sleep $PERIOD
VM_SIZE=""
VM_RSS=""
VM_THRDS=""
# Needs to get actual elapsed time instead of doing this
ELAPSED_TIME=`expr $ELAPSED_TIME + $PERIOD`
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment