#!/bin/sh

# Copyright  2004 Sun Microsystems, Inc. All rights reserved. 
# 
# Sun Microsystems, Inc. has intellectual property rights relating to
# technology embodied in the product that is described in this document.
# In particular, and without limitation, these intellectual property rights
# may include one or more of the U.S. patents listed at
# http://www.sun.com/patents and one or more additional patents or pending
# patent applications in the U.S. and in other countries.
# 
# U.S. Government Rights - Commercial software. Government users are subject
# to the Sun Microsystems, Inc. standard license agreement and applicable
# provisions of the FAR and its supplements.
# 
# Use is subject to license terms. 
# 
# This distribution may include materials developed by third parties. Sun,
# Sun Microsystems, the Sun logo, Java and Sun[tm] ONE are trademarks or
# registered trademarks of Sun Microsystems, Inc. in the U.S. and other
# countries. 
# 
# Copyright  2004 Sun Microsystems, Inc. Tous droits rservs. Sun
# Microsystems, Inc. dtient les droits de proprit intellectuels relatifs
#  la technologie incorpore dans le produit qui est dcrit dans ce document.
# En particulier, et ce sans limitation, ces droits de proprit
# intellectuelle peuvent inclure un ou plus des brevets amricains lists
#  l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires
# ou les applications de brevet en attente aux Etats - Unis et dans les
# autres pays.
# 
# L'utilisation est soumise aux termes du contrat de licence.
# 
# Cette distribution peut comprendre des composants dvelopps par des
# tierces parties.
# 
# Sun, Sun Microsystems, le logo Sun, Java et Sun[tm] ONE sont des marques
# de fabrique ou des marques dposes de Sun Microsystems, Inc. aux
# Etats-Unis et dans d'autres pays.


#############################################################################
tuneInitConf() {
    tune_file="$CONTAINER_INSTANCE_DIR/config/init.conf"

    $ECHO $LINE_SEP
    $ECHO "Tuning $tune_file..."
    $ECHO
    $ECHO "File                 : $tune_file"
    $ECHO "Parameter tuning     :"
    $ECHO
    $ECHO "1.   RqThrottle"
    $ECHO "Current Value        : RqThrottle=`getMagnusEntry $tune_file 'RqThrottle'"
    $ECHO "Recommended Value    : RqThrottle=$numRQThrottle"
    $ECHO
    $ECHO "2.   RqThrottleMin"
    $ECHO "Current Value        : RqThrottleMin=`getMagnusEntry $tune_file 'RqThrottleMin'"
    $ECHO "Recommended Value    : RqThrottleMin=$AMTUNE_NUM_WS_RQTHROTTLE_MIN"
    $ECHO
    $ECHO "3.   ThreadIncrement"
    $ECHO "Current Value        : ThreadIncrement=`getMagnusEntry $tune_file 'ThreadIncrement'"
    $ECHO "Recommended Value    : ThreadIncrement=$AMTUNE_NUM_WS_THREAD_INCREMENT"
    $ECHO
    $ECHO "4.   ConnQueueSize"
    $ECHO "Current Value        : ConnQueueSize=`getMagnusEntry $tune_file 'ConnQueueSize'"
    $ECHO "Recommended Value    : ConnQueueSize=$AMTUNE_NUM_TCP_CONN_SIZE"
    $ECHO
    $ECHO "5.   ListenQ"
    $ECHO "Current Value        : ListenQ=`getMagnusEntry $tune_file 'ListenQ'"
    $ECHO "Recommended Value    : ListenQ=$AMTUNE_NUM_TCP_CONN_SIZE"
    $ECHO
    $ECHO

    if [ "$AMTUNE_MODE" = "REVIEW" ]; then
        return
    fi

    check_file_for_write $tune_file
    if [ $? = 100 ]; then
        return
    fi

    delete_line $tune_file "RqThrottle"
    delete_line $tune_file "RqThrottleMin"
    delete_line $tune_file "ThreadIncrement"
    delete_line $tune_file "ConnQueueSize"
    delete_line $tune_file "ListenQ"
    delete_line $tune_file "# Start: Identity Server Tuning"
    delete_line $tune_file "# End: Identity Server Tuning"

    add_to_end $tune_file "# Start: Identity Server Tuning : `date`"
    add_to_end $tune_file "RqThrottle $numRQThrottle"
    add_to_end $tune_file "RqThrottleMin $AMTUNE_NUM_WS_RQTHROTTLE_MIN"
    add_to_end $tune_file "ThreadIncrement $AMTUNE_NUM_WS_THREAD_INCREMENT"
    add_to_end $tune_file "ConnQueueSize $AMTUNE_NUM_TCP_CONN_SIZE"
    add_to_end $tune_file "ListenQ $AMTUNE_NUM_TCP_CONN_SIZE"
    add_to_end $tune_file "# End: Identity Server Tuning"
}

tuneServerXML() {
    tune_file="$CONTAINER_INSTANCE_DIR/config/server.xml"

    $ECHO $LINE_SEP
    $ECHO "Tuning $tune_file..."
    $ECHO
    $ECHO "File                 : $tune_file"
    $ECHO "Parameter tuning     :"
    $ECHO
    $ECHO "1.   Acceptor Threads"
    $ECHO "Current Value        : acceptor-threads=`get_token_in_line $tune_file 'acceptor-threads' 'acceptor-threads'"
    $ECHO "Recommended Value    : acceptor-threads=$acceptorThreads"
    $ECHO
    $ECHO "2.   native-library-path-prefix (if Solaris 8)"
    $ECHO "Current Value        : native-library-path-prefix=`get_token_in_line $tune_file 'native-library-path-prefix' 'native-library-path-prefix'"
    $ECHO "Recommended Value    : native-library-path-prefix=/usr/lib/lwp"
    $ECHO
    $ECHO "3.   Max and Min Heap Size"
    $ECHO "Current Value        : Min Heap: `getServerXMLJVMOptionEntry $tune_file '-Xms' 'jvm-options'` Max Heap: `getServerXMLJVMOptionEntry $tune_file '-Xmx' 'jvm-options'`"
    $ECHO "Recommended Value    : -Xms${maxHeapSize}M -Xmx${maxHeapSize}M"
    $ECHO
    $ECHO "4.   LogGC Output"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-Xloggc' 'jvm-options'`"
    $ECHO "Recommended Value    : -Xloggc:$CONTAINER_INSTANCE_DIR/logs/gc.log"
    $ECHO
    $ECHO "5.   JVM in Server mode"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-server' 'jvm-options'`"
    $ECHO "Recommended Value    : -server"
    $ECHO
    $ECHO "6.   Stack Size"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-Xss' 'jvm-options'`"
    $ECHO "Recommended Value    : -Xss${AMTUNE_PER_THREAD_STACK_SIZE_IN_KB}k"
    $ECHO
    $ECHO "7.   New Size"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:NewSize' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:NewSize=${maxNewSize}M"
    $ECHO
    $ECHO "8.   Max New Size"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:MaxNewSize' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:MaxNewSize=${maxNewSize}M"
    $ECHO
    $ECHO "9.   Perm Size"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:PermSize' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:PermSize=${maxPermSize}M (if the calculated value is >= ${AMTUNE_MIN_PERM_SIZE_AS7}M )"
    $ECHO
    $ECHO "10.  Max Perm Size"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:MaxPermSize' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:MaxPermSize=${maxPermSize}M (if the calculated value is >= ${AMTUNE_MIN_PERM_SIZE_AS7}M )"
    $ECHO
    $ECHO "11.  Disable Explicit GC"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:+DisableExplicitGC' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:+DisableExplicitGC"
    $ECHO
    $ECHO "12.  Use Parallel GC"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:+UseParNewGC' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:+UseParNewGC"
    $ECHO
    $ECHO "13.  CMSPermGenSweepingEnabled"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:+CMSPermGenSweepingEnabled' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:+CMSPermGenSweepingEnabled"
    $ECHO
    $ECHO "14.  CMS Class Unloading"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:+CMSClassUnloadingEnabled' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:+CMSClassUnloadingEnabled"
    $ECHO
    $ECHO "15.  CMS Full GCs before Compact"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:+UseCMSCompactAtFullCollection' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:+UseCMSCompactAtFullCollection"
    $ECHO
    $ECHO "16.  CMS Full GCs before Compact"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:CMSFullGCsBeforeCompaction' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:CMSFullGCsBeforeCompaction=0"
    $ECHO
    $ECHO "17.  Soft Reference Cleanup"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:SoftRefLRUPolicyMSPerMB' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:SoftRefLRUPolicyMSPerMB=0"
    $ECHO
    $ECHO "18.  Print Class Histogram"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:+PrintClassHistogram' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:+PrintClassHistogram"
    $ECHO
    $ECHO "19.  Print GC Details"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:+PrintGCDetails' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:+PrintGCDetails"
    $ECHO
    $ECHO "20.  Print GC Time Stamps"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:+PrintGCTimeStamps' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:+PrintGCTimeStamps"
    $ECHO
    $ECHO "21.  OverrideDefaultLibthread (if Solaris 8)"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:+OverrideDefaultLibthread' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:+OverrideDefaultLibthread"
    $ECHO
    $ECHO "22.  Enable Conc Mark Sweep GC"
    $ECHO "Current Value        : `getServerXMLJVMOptionEntry $tune_file '-XX:+UseConcMarkSweepGC' 'jvm-options'`"
    $ECHO "Recommended Value    : -XX:+UseConcMarkSweepGC"
    $ECHO
    $ECHO
 
    if [ "$AMTUNE_MODE" = "REVIEW" ]; then
        return
    fi

    check_file_for_write $tune_file
    if [ $? = 100 ]; then
        return
    fi

    substitute_token_in_line $tune_file "acceptor-threads" "acceptor-threads" "$acceptorThreads" 0 ":"

    if [ ! "`/bin/uname -r | grep 8 | wc -m`" -eq "0" ] ; then
        $ECHO  "Solaris 8 specific changes"
        substitute_token_in_line $tune_file "native-library-path-prefix" "native-library-path-prefix" "/usr/lib/lwp" 2 ":"
    fi

    delete_line $tune_file "Start: Performance Related JVM Options for IS"
    delete_line $tune_file "<jvm-options>-Xms"
    delete_line $tune_file "<jvm-options>-Xmx"
    delete_line $tune_file "<jvm-options> -Xms"
    delete_line $tune_file "<jvm-options> -Xmx"
    delete_line $tune_file "<jvm-options>-Xloggc"
    delete_line $tune_file "<jvm-options>-Xrs"
    delete_line $tune_file "<jvm-options>-server"
    delete_line $tune_file "<jvm-options>-Xss"
    delete_line $tune_file "<jvm-options>-XX:NewSize"
    delete_line $tune_file "<jvm-options>-XX:MaxNewSize"
    delete_line $tune_file "<jvm-options>-XX:PermSize"
    delete_line $tune_file "<jvm-options>-XX:MaxPermSize"
    delete_line $tune_file "<jvm-options>-XX:CMSFullGCsBeforeCompaction"
    delete_line $tune_file "<jvm-options>-XX:+DisableExplicitGC"
    delete_line $tune_file "<jvm-options>-XX:+UseParNewGC"
    delete_line $tune_file "<jvm-options>-XX:+UseConcMarkSweepGC"
    delete_line $tune_file "<jvm-options>-XX:+CMSPermGenSweepingEnabled"
    delete_line $tune_file "<jvm-options>-XX:+CMSClassUnloadingEnabled"
    delete_line $tune_file "<jvm-options>-XX:+UseCMSCompactAtFullCollection"
    delete_line $tune_file "<jvm-options>-XX:SoftRefLRUPolicyMSPerMB"
    delete_line $tune_file "<jvm-options>-XX:+PrintClassHistogram"
    delete_line $tune_file "<jvm-options>-XX:+PrintGCDetails"
    delete_line $tune_file "<jvm-options>-XX:+PrintGCTimeStamps"
    delete_line $tune_file "<jvm-options>-XX:+OverrideDefaultLibthread"
    delete_line $tune_file "End: Performance Related JVM Options for IS"

    insert_line $tune_file "java.util.logging.manager" "<!-- Start: Performance Related JVM Options for IS -->"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-Xms${maxHeapSize}M -Xmx${maxHeapSize}M</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-Xloggc:$CONTAINER_INSTANCE_DIR/logs/gc.log</jvm-options>"
    #insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-Xrs</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-server</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-Xss${AMTUNE_PER_THREAD_STACK_SIZE_IN_KB}k</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:NewSize=${maxNewSize}M</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:MaxNewSize=${maxNewSize}M</jvm-options>"

    if [ $maxPermSize -ge $AMTUNE_MIN_PERM_SIZE_AS7 ]; then
        insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:PermSize=${maxPermSize}M</jvm-options>"
        insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:MaxPermSize=${maxPermSize}M</jvm-options>"
    fi

    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:+DisableExplicitGC</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:+UseParNewGC</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:+UseConcMarkSweepGC</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:+CMSPermGenSweepingEnabled</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:+CMSClassUnloadingEnabled</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:+UseCMSCompactAtFullCollection</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:CMSFullGCsBeforeCompaction=0</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:SoftRefLRUPolicyMSPerMB=0</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:+PrintClassHistogram</jvm-options>"
    #insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:+PrintGCDetails</jvm-options>"
    insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:+PrintGCTimeStamps</jvm-options>"

    if [ ! "`/bin/uname -r | grep 8 | wc -m`" -eq "0" ] ; then
        $ECHO  "Solaris 8 specific changes"
        insert_line $tune_file "java.util.logging.manager" "    <jvm-options>-XX:+OverrideDefaultLibthread</jvm-options>"
    fi


    insert_line $tune_file "java.util.logging.manager" "<!-- End: Performance Related JVM Options for IS -->"

}

#############################################################################
# Start of main program
#############################################################################
SCRIPT_LOCATION=`/usr/bin/dirname $0`

# import the environment
if [ -f $SCRIPT_LOCATION/amtune-env ]; then
    if [ ! "$INIT_STATUS" = "INIT_COMPLETE" ]; then
        . $SCRIPT_LOCATION/amtune-env
    fi
fi

$ECHO "IS - Application Server Tuning Script"           1>>$DEBUG_FILE 2>&1

tuneInitConf                                            1>>$DEBUG_FILE 2>&1
tuneServerXML                                           1>>$DEBUG_FILE 2>&1

$ECHO $PARA_SEP                                         1>>$DEBUG_FILE 2>&1
