#! /bin/sh
# set -xv
#=========================================================================
# Copyright (C) GemTalk Systems 1986-2014.  All Rights Reserved..
#
# Name - getlargepages.sh
#
# Purpose - Script to determine the number of large pages for a system
#           using the shrpcmonitor getrequiredsize command.
#           
#
#=========================================================================

usage() {
  cat <<EOF
====================================================================

Usage:
`basename $0` [-h]|<cache-size-kb> <num-procs> <num-shared-counters>

Purpose

  To determine the number of large pages to setup on a system 
  given a desired shared page cache configuration.

  If the arguments are not supplied the user will be prompted for them.

  Make sure \$GEMSTONE is set.

Options

  -h: 

      print this usage message and exit.

  <cache-size-kb> 
  The base size of the desired shared page cache.

  <num-procs> 
   The maximum number of processes allowed to access this shared page cache.
   See info on config parameter SHR_PAGE_CACHE_NUM_PROCS.

  <num-shared-counters> 
  The number of shared counters to be accomodated by this shared page cache.
  See info on the config parameter SHR_PAGE_CACHE_NUM_SHARED_COUNTERS.


====================================================================
EOF
}

# check for -h or no args; print usage and exit
if [ "$1" = "-h" ]; then
  usage
  exit 0
fi 

# check that $GEMSTONE is set
if [ "x$GEMSTONE" = "x" ]; then
  echo "ERROR:  \$GEMSTONE not set" 1>&2
  exit 1
fi

# Check for $GEMSTONE/sys/shrpcmonitor
if [ -f $GEMSTONE/sys/shrpcmonitor ]; then
  shrpcmonExe=$GEMSTONE/sys/shrpcmonitor
else
  echo "ERROR:  \$GEMSTONE/sys/shrpcmonitor not found" 1>&2
  exit 1;
fi

# debug
# echo "Using $shrpcmonExe"

# initialize variables
cacheSizeKb=""
numProcs=""
numSharedCounters=""


cacheSizeKb=$1
numProcs=$2
numSharedCounters=$3

if [ ! $cacheSizeKb ]; then
  usage
  echo
  read -p "Cache Size (KB):           " cacheSizeKb
fi

if [ ! $numProcs ]; then
  read -p "Number of Processes:       " numProcs
fi

if [ ! $numSharedCounters ]; then
  read -p "Number of Shared Counters: " numSharedCounters
fi

# print summary info
echo
echo "Shared Page Cache Monitor"
echo "Large Page Information based on: "
echo 
echo "Base Cache Size (KB):      $cacheSizeKb"
echo "Number of Processes:       $numProcs"
echo "Number of Shared Counters: $numSharedCounters"
echo

if echo $cacheSizeKb | grep "^[0-9]*$" >/dev/null
then
  : # arg OK
else
  echo "ERROR: Cache Size (KB) $cacheSizeKb not a number"
  exit 1
fi

if echo $numProcs | grep "^[0-9]*$" >/dev/null
then
  : # arg OK
else
  echo "ERROR: Number of Processes $numProcs not a number"
  exit 1
fi

if echo $numSharedCounters | grep "^[0-9]*$" >/dev/null
then
  : # arg OK
else
  echo "ERROR: Number of Shared Conters $numSharedCounters not a number"
  exit 1
fi


# now execute..
$shrpcmonExe <<EOF
$cacheSizeKb setcachesizekb
$numProcs setnumprocs
$numSharedCounters setnumsharedctrs
getrequiredsize
EOF
pStatus=$?
if [ $pStatus -ne 0 ]; then
  echo "shrpcmonitor returned $pStatus";
  exit $pStatus
fi
echo
#
# end of script


