#!/bin/bash

set -o nounset

print_usage() {
    echo "Usage:"
    echo "./reproduce.sh -jdk <java_home> -vmopts <vm_options> -location <spb | sca>"
    echo ""
}

if [ $# -eq 0 ]; then
    print_usage
    exit 0
fi

while [ $# -gt 0 ]; do
    ARG=$1
    shift
    case $ARG in
        -jdk)
            JAVA_HOME="$1"
            shift
            ;;
        -vmopts)
            JAVA_OPTS="$1"
            shift
            ;;
        -location)
            LOCATION="$1"
            shift
            ;;
        *)
            print_usage
            exit 1
            ;;
    esac
done

case $LOCATION in
    sca)
        TESTBASE="/net/sqenfs-1.us.oracle.com/export1/comp/vm/testbase/sqe/vm/8/build/execution/vm"
        ;;
    spb)
        TESTBASE="/net/vmsqe.ru.oracle.com/export/testbase/sqe/vm/8/build/execution/vm"
        ;;
    *)
        echo "Unknown location $LOCATION"
        ;;
esac


JAVA_OPTS="${JAVA_OPTS} -XX:+PrintGCDetails"
JAVA_OPTS="${JAVA_OPTS} -agentlib:redefineClasses -cp ${TESTBASE}/bin/classes:${TESTBASE}/bin/classes/runtime/ParallelClassLoading/shared/hierarchies/static-load/static-init/anonymous/simple"


EXECUTE_CLASS="runtime.ParallelClassLoading.shared.MultiHierarchiesLoadingController"

testWorkDir=`pwd`
TEST_ARGS="-useXML -baseClassDir ${TESTBASE} -configDir ${testWorkDir}"


ARCH="linux-i586"
COMMON_LIBS_LOCATION="${TESTBASE}/bin"
LD_LIBRARY_PATH="${JAVA_HOME}/jre/lib/i386:${JAVA_HOME}/jre/lib/i386/server"
LD_LIBRARY_PATH="${COMMON_LIBS_LOCATION}/lib/${ARCH}/runtime/ParallelClassLoading/shared:${LD_LIBRARY_PATH}"
export LD_LIBRARY_PATH


cat <<EOF > "$testWorkDir/config.xml"
<?xml version="1.0" ?>
<!DOCTYPE testParameters [
<!ELEMENT testParameters (properties*)>
<!ELEMENT properties (property*)>
<!ATTLIST properties className CDATA #REQUIRED>
<!ELEMENT property EMPTY>
<!ATTLIST property name CDATA #REQUIRED>
<!ATTLIST property value CDATA #REQUIRED>
]>

<testParameters>

  <properties className="runtime.ParallelClassLoading.shared.MultiHierarchiesLoadingController">
    <property name="DEBUG" value="false" />
    <property name="ITERATIONS" value="100" />
    <property name="THREADS_COUNT" value="10" />

	<property name="hierarchiesBaseDir" value="bin/classes/runtime/ParallelClassLoading/shared/hierarchies/static-load/" />
    <property name="loadData" value="static-init/anonymous/simple:custom.C% static-init/anonymous/simple:custom.C%" />
    <property name="threadByHierarchy" value="5" />
    <property name="useSystemLoader" value="true" />
    <property name="redefineTask" value="static-init/anonymous/simple:custom.C%" />
  </properties>

  <properties className="runtime.ParallelClassLoading.shared.ProvokeType">
    <property name="loadingType" value="forName" />
    <property name="instantiation" value="newInstance" />
  </properties>

  <properties className="runtime.ParallelClassLoading.shared.CustomLoader">
  </properties>

</testParameters>
EOF

JAVA="$JAVA_HOME/bin/java"

${JAVA} ${JAVA_OPTS} ${EXECUTE_CLASS} ${TEST_ARGS} &

pid=$!

trap "kill -TERM $pid; exit" INT TERM EXIT

while ps -p $pid &> /dev/null; do
    MEM=$(pmap $pid | awk '{if ($1 == "total") print $0}')
    echo "=== Process size: $MEM ==="
    sleep 5
done

wait $pid

trap - INT TERM EXIT
