#!/bin/sh
#
# Copyright (C) 2005 Anders Logg
#
# This file is part of DOLFIN.
#
# DOLFIN 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 3 of the License, or
# (at your option) any later version.
#
# DOLFIN 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 DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#
# First added:  2005-12-13
# Last changed: 2005-12-13
#            
# Monitor memory usage for given command. This script
# will probably only work on Linux.
#
# Note that this script runs the given command as a new
# process, so killing the script (ctrl-c) won't kill the
# running process.

exec $* &
pid="$!"

echo "Monitoring memory usage for process with id $pid..."
PIDFILE="/proc/$pid/status"
while [ 1 ]; do
    if [ -e $PIDFILE ]; then
	cat $PIDFILE | grep VmSize | awk '{ print $2/1024" MB"}'
    else
	exit 0
    fi
    sleep 1
done
