#! /bin/sh
#set -xv
######################################################################
#
# Copyright (C) GemTalk Systems 1986-2014.  All Rights Reserved.
#
# Script for starting shared cache warmer gem.
#
# Phase 1:
# gem reads the object table into the shared page cache.
#
# Phase 2: (optional)
# Reads data pages.  Data pages can be read into the shared cache 
# or only into the UNIX file buffer cache.  
# Phase 2 is run if '-d' or '-D; is specified on the command line.
#
# See usage() below or run 'startcachewarmer -h' for detailed 
# command line options
#
#
######################################################################

# 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="startcachewarmer"

usage() {
  cat <<EOF
Usage:
$comid [-d|-D] [-h] [-H host] [-l cacheFullLimit] [-n numSessions] [-s stone] [-W] [-L logPath]

  -d    read data pages into the cache. (default: only object table pages are read)
  -D    read data pages into the UNIX file system buffers. 
NOTE: -d and -D are mutually exclusive. If both -d and -D are passed on
the command line, the last of these flags is used.
  -h    display this message and exit. 
  -H    host name or IP address where the stone is running.  ONLY to be used when warming a
        remote shared page cache.
  -l    stop cache warming if the free frame count falls below <cacheFullLimit>. 
        Use -1 to have system compute a default value. Use 0 to force cache warming 
        to continue even if the shared cache is full.  (default: -1)
  -L    path to a writable log file directory. (default: current directory)
  -n    number of worker sessions to use. (default: numberOfCpus + numExtents)
        There is always one additional "master" session allocated and the warmer 
        will exit with an error if not enough sessions are available.
  -s    name of running stone. (default: 'gs64stone')
  -W    wait for cache warming gems to exit before exiting this script (default: 
        spawn gem in the background and exit immediately)

Description:

Script for starting shared cache cache warming gem.

Phase 1:
Gem reads the dependency map and object table pages into the shared page cache.

Phase 2: (only executed if either the '-d' or '-D' option is specfied)
Data pages are read into the shared page cache ('-d') or only the UNIX file buffer ('-D') 

Note: the most efficient cache warming algorithm needs to be in transaction and 
  subjects the operating system to a heavy load of disk reads.  GemStone system
  performance during the cache warming is not expected to be good.

EOF
  exit $status
}


# ---
# defaults
# ---
readDataPagesOpCode="0"
cacheFullLimit=-1
numSessions=0
stone="gs64stone"
waitForGem=0
logPath=`pwd`

# ---
# process command line
# ---
while getopts "dDhH:l:L:n:s:W" opt; do
  case $opt in
    d) readDataPagesOpCode="1" ;;
    D) readDataPagesOpCode="2" ;;
    h) status=0; usage ;;
    H) stoneHost=$OPTARG ;; # Fix 44298
    l) cacheFullLimit=$OPTARG ;;
    L) logPath=$OPTARG ;;
    n) numSessions=$OPTARG ;;
    s) stone=$OPTARG ;;
    W) waitForGem=1 ;;
# Request 38663: accept a directory for log files
    \?) status=1; usage ;;
  esac
done

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

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

if [ ! -d $logPath ]; then
  echo "$comid [ERROR]: $logPath is not a directory."
  status=1
  usage
fi

if [ ! -w $logPath ]; then
  echo "$comid [ERROR]: $logPath is not writable."
  status=1
  usage
fi

# Fix 44298 - support for warmers on remote caches
if [ "$stoneHost" = "" ]; then # Stone running on our host
  STN_NRS=$stone
  STN_WS_NRS=$stone
  STN_HOST_PRINTED="(localhost)"
else
# warmer gem needs single quotes around the NRS.
# waitstone does not like single quotes.
  STN_NRS="'!@$stoneHost!$stone'"
  STN_WS_NRS="!@$stoneHost!$stone"
  STN_HOST_PRINTED=$stoneHost
fi

# check to make sure $stone is a running stone
$GEMSTONE/bin/waitstone $STN_WS_NRS -1 >/dev/null 2>&1
if [ $? -ne 0 ]; then
  if [ ! -w $logPath ]; then
    echo "$comid [ERROR]: $stone is not a running stone."
  else
    echo "$comid [ERROR]: $stone is not a running stone on host $stoneHost"
  fi
  status=1
  usage
fi

slash="/"
testFile=${logPath}${slash}foo
touch $testFile >/dev/null 2>&1
if [ $? -eq 0 ]; then
  rm -f $testFile >/dev/null 2>&1
else
  slash=""
  testFile=${logPath}${slash}foo
  touch $testFile >/dev/null 2>&1
  if [ $? -ne 0 ]; then
    echo "$comid [ERROR]: cannot create file in directory $logPath"
    status=1
    usage
  fi
  rm -f $testFile >/dev/null 2>&1
fi

cat <<EOF
Cache warming configuration:
        NumSessions: $numSessions
    Read data pages: $readDataPagesOpCode
     cacheFullLimit: $cacheFullLimit
              stone: $stone
          stoneHost: $STN_HOST_PRINTED
         waitForGem: $waitForGem
       logDirectory: $logPath
Spawning cache warming gem...
EOF

logName=${logPath}${slash}cacheWarmer.log
rm -f $logName >/dev/null 2>&1
GEMSTONE_CHILD_LOG=$logName
export GEMSTONE_CHILD_LOG

# The default behaviour is to delete this process's log file if it exits
# normally. If you want to keep this process's log file even on normal exit
# then uncomment the following
# GEMSTONE_KEEP_LOG=1 ; export GEMSTONE_KEEP_LOG

# ---
# start the cache warmer gem
# ---

$GEMSTONE/sys/gem otCacheWarmerGem $STN_NRS $numSessions $cacheFullLimit $readDataPagesOpCode > $logName 2>&1 &

thisPid=$!
echo "Started cache warmer gem with process id: $thisPid."

if [ $waitForGem -eq 1 ]; then
  echo waiting for warmer gem with process ID $thisPid
  wait $thisPid
fi

exit 0
