#! /bin/bash
#*******************************************************************************
# ALMA - Atacama Large Millimiter Array
# (c) European Southern Observatory, 2011 
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# 
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
# "@(#) $Id$"
#
# who       when      what
# --------  --------  ----------------------------------------------
# hsommer  2011-01-12  created
#

. acsstartupLogging.sh

#
# Usage info. Be nice and keep this up-to-date!
#
function printUsage {
	echo "Logs the total memory used by a process, with VSZ (virtual memory) and RSS (non-swapped RAM), both in kB units and counting shared libs fully toward this process."
	echo "Usage: `basename $0` pid "
}

if [ ! $# == 1 ]; then
	printUsage
	exit $EC_BADARGS;
fi

PID="$1"

echo "# Will monitor memory for process $PID"
echo "# TIMESTAMP             VSZ (kB)  RSS (kB)"
while :;
do
	ps -p $PID >/dev/null
	status=$?
	if [ $status -ne 0 ] ; then
		echo "# process $PID terminated."
		break
	else
		timestamp=`getTimeStamp`
		memory=`ps -o vsz=VSZ,rss=RSS $PID | grep -v VSZ`
		echo "$timestamp $memory"
	fi
	sleep 5
done


#**********************************************************
# gnuplot example using captured output of this script
#**********************************************************
# set title "Test results for COMP-4929"
# 
# set xdata time
# set timefmt "%Y-%m-%dT%H:%M:%S"
# set format x "%H:%M"
# 
# set xlabel "time"
# set ylabel "JVM native memory (kB)"
# 
# set term svg font "arial,10" size 800, 600
# set output 'Comp4929loadTimes.svg'
# 
# plot "sampleJvmNativeMemData.txt" using 1:2 title "VSZ",\
#      "sampleJvmNativeMemData.txt" using 1:3 title "RSS"

# __oOo__
