#! /bin/sh
#set -xv
######################################################################
#
# Copyright (C) VMware, Inc. 1986-2011.  All Rights Reserved.
#
# Script for starting shared cache warming gems using geml command
# options.
#
# Each gem reads a portion of the object table into the shared page
# cache.
#
# See usage() below or run 'startotcachewarmers -h' for detailed 
# command line options
#
# $Id: startcachewarmer2.sh,v 1.2 2008-01-09 22:50:53 stever Exp $
#
######################################################################

# must have $GEMSTONE
if [ "a$GEMSTONE" = "a" ]; then
  echo "ERROR: GemStone scripts require a GEMSTONE environment variable."
  echo "       Please set it to the directory where GemStone resides."
  exit 1
fi

# ensure a minimal path
if [ "x$PATH" = "x" ]; then PATH=:/bin:/usr/bin:/usr/ucb; export PATH; fi 

# error control - do not allow hup
trap '' 1

comid="startotcachewarmers"

usage() {
  cat <<EOF
Usage:
$comid [-h][-n<numGems>][-s<stone>][-w<delayTime>][-W]

-h              display this message and exit 
-n<numGems>     number of gems to start (default: 1)
-s<stone>       name of running stone (default: 'gs64stone')
-w<delayTime>   wait <delayTime> seconds between spawning gems (default: 1)
-W              wait for cache warming gems to exit before exiting this script
                (default: spawn gems in the background and exit immediately)

Description:

Script for starting object table shared cache cache warming gems without invoking
Smalltalk.  geml command options are used instead.

Each geml process reads a portion of the object table into the shared page cache
and exits when finished.
EOF
  exit $status
}

# ---
# defaults
# ---
count=1
numGems=1
stone="gs64stone"
delayTime=1
waitForGems=0

# ---
# process command line
# ---
while getopts "hn:s:w:W" opt; do
  case $opt in
    h) status=0; usage ;;
    n) numGems=$OPTARG ;;
    s) stone=$OPTARG ;;
    w) delayTime=$OPTARG ;;
    W) waitForGems=1 ;;
    \?) status=1; usage ;;
  esac
done

if [ $numGems -lt 1 ]; then
  echo "$comid [ERROR]: numGems is too small."
  status=1
  usage
  exit $status
fi

if [ $delayTime -lt 0 ]; then
  echo "$comid [ERROR]: delayTime cannot be negative."
  status=1
  usage
  exit $status
fi


HERE=`pwd`
touch $HERE/foo > /dev/null 2>&1
status=$?
if [ $status -ne 0 ]; then
  echo "$comid [ERROR]: Current directory must be writable."
  usage
fi

rm $HERE/foo

echo "verifying $stone is running..."
$GEMSTONE/bin/waitstone $stone -1 > /dev/null 2>&1
status=$?
if [ $status -ne 0 ]; then
  error "...no stone named $stoneName found"
  usage
  exit $status
fi
echo "...$stoneName is running."

# ---
# function to start a single gem
# ---
spawnCacheWarmGem() {
  $GEMSTONE/sys/geml << EOF > $thisLogFile 2>&1
  startOtCacheWarmerGem '$stone' $count $numGems
  quit
EOF
}

while [ $count -le $numGems ]; do
  thisLogFile=cacheWarmerGem-$count.log
  if [ -f "$thisLogFile" ]; then
    rm $thisLogFile  >/dev/null 2>&1
  fi

  spawnCacheWarmGem > /dev/null 2>&1 & 
  thisPid=$!
  pids="$thisPid $pids"
  echo "Starting cache warming gem $count of $numGems with process ID $thisPid"

  if [ $count -lt $numGems ]; then
    sleep $delayTime
  else
    echo "Finished spawning $count cache warming gems."
  fi

  count=`expr $count + 1`
done

echo Finished spawning $numGems cache warming gems.

if [ $waitForGems -eq 1 ]; then
  echo waiting for gems with the following process IDs to exit:
  echo $pids
  wait $pids
fi

exit 0
