#!/bin/sh
#
# Show memory usage overview graph from usage.csv files.
# This file is part of sp-endurance.
#
# Copyright (C) 2007 by Nokia Corporation
#
# Contact: Eero Tamminen <eero.tamminen@nokia.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License 
# version 2 as published by the Free Software Foundation. 
#
# This program 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
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA

if [ $# -lt 2 ]; then
	name=${0##*/}
	echo
	echo "usage: $name dir1 dir2 ..."
	echo
	echo "Shows a memory usage overview graph from endurance data as ASCII"
	echo "As arguments expects the endurance data directories."
	echo "Note: the shown values are incorrect if swap is used!"
	echo
	echo "Example: $name [0-9][0-9][0-9]"
	echo
	exit 1
fi
echo "Memory usage in given endurance rounds:"
for i in $*; do
	file="$i/usage.csv"
	if [ \! -f "$file" ]; then
		echo "ERROR: '$file' is missing!"
		exit 1
	fi
	echo -n "$i: "
	awk '
BEGIN { FS=" kB,"; max=78-14 }
/^MemTotal,/ { found=1; next }
! found { next }
found { total=$1; free=$2; buff=$3; cached=$4; exit 0 }
END {
  used=total-free-buff-cached;
  usebars=int(max*used/total);
  freebars=max-usebars;
  for (i = 0; i < usebars; i++) printf "0";
  for (i = 0; i < freebars; i++) printf " ";
  printf "|%6dK\n", used
}' $file
done
