1 #!/bin/bash
   2 # Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
   3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4 #
   5 # This code is free software; you can redistribute it and/or modify it
   6 # under the terms of the GNU General Public License version 2 only, as
   7 # published by the Free Software Foundation.
   8 #
   9 # This code is distributed in the hope that it will be useful, but WITHOUT
  10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12 # version 2 for more details (a copy is included in the LICENSE file that
  13 # accompanied this code).
  14 #
  15 # You should have received a copy of the GNU General Public License version
  16 # 2 along with this work; if not, write to the Free Software Foundation,
  17 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18 #
  19 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20 # or visit www.oracle.com if you need additional information or have any
  21 # questions.
  22 
  23 # This script contains useful functions for testing heapdump
  24 # feature of VM.
  25 
  26 : ${JAVA:="$TESTJAVA/bin/java"}
  27 : ${JAVA_OPTS:="$TESTJAVAOPTS $TESTVMOPTS -cp $TESTCLASSPATH"}
  28 : ${CP:="$TESTCLASSPATH"}
  29 : ${TEST_CLEANUP:="false"}
  30 : ${JMAP:="$TESTJAVA/bin/jmap"}
  31 : ${JHSDB:="$TESTJAVA/bin/jhsdb"}
  32 
  33 export PATH=$PATH:$TESTNATIVEPATH
  34 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$TESTNATIVEPATH
  35 export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:$TESTNATIVEPATH
  36 
  37 if [ -z "${JAVA}" ]; then
  38         echo JAVA variable is not set.
  39         exit 1
  40 fi
  41 
  42 if [ -n "${testWorkDir}" ]; then
  43         cd ${testWorkDir}
  44 fi
  45 
  46 if [ -z "${DUMPBASE}" ]; then
  47         DUMPBASE=.
  48 fi
  49 if [ -z "$DEBUGGER_JAVA_OPTS" ]; then
  50         DEBUGGER_JAVA_OPTS="$JAVA_OPTS"
  51 fi
  52 
  53 CORE_SUPPORTED=1
  54 
  55 for opt in $DEBUGGER_JAVA_OPTS; do
  56         case $opt in
  57         -D*)
  58                 JMAP="$JMAP -J$opt"
  59                 ;;
  60         esac
  61 done
  62 
  63 export CORE_SUPPORTED
  64 
  65 # Verify heap dump
  66 # This function starts HprofParser and looks for message "Server is ready."
  67 # in output, in which case heap dump is verified.
  68 verify_heapdump() {
  69         filename=$1
  70         shift
  71         echo Verifying ${filename}
  72         echo ${JAVA} -cp $CP jdk.test.lib.hprof.HprofParser ${filename}
  73         ${JAVA} -cp $CP jdk.test.lib.hprof.HprofParser ${filename}
  74 }
  75 
  76 cleanup() {
  77         result="$1"
  78         if [ -n "$DUMPFILE" ]; then
  79                 if [ "$TEST_CLEANUP" != "false" ]; then
  80                         rm -f "$DUMPFILE"
  81                 else
  82                         gzip "$DUMPFILE" || true
  83                 fi
  84         fi
  85 }
  86 
  87 fail() {
  88         message="$1"
  89         res=1
  90         echo "$message"
  91         echo "TEST FAILED"
  92         cleanup $res
  93         exit 1
  94 }
  95 
  96 pass() {
  97         message="$1"
  98         if [ -n "$message" ]; then
  99                 echo "$message"
 100         fi
 101         echo "TEST PASSED"
 102         cleanup 0
 103         exit 0
 104 }
 105 
 106 # Parse VM options that have size argument and return it's value in bytes.
 107 # Function applicable to -Xmn, -Xms, -Xms and all possible -XX: options.
 108 parse_heap_size() {
 109     OPTION=$1
 110     SIZE=0
 111     MULTIPLIER=0
 112 
 113     # On Solaris sed don't support '+' quantificator, so <smth><smth>* is used.
 114     # There is no support for '?' too, so <smth>* is used instead.
 115     # Generally speaking, there sed on Solaris support only basic REs.
 116     case "$OPTION" in
 117         -Xm*)
 118             SIZE=`echo $OPTION | sed -e 's#-Xm[xns]\([0-9][0-9]*\).*#\1#'`
 119             MULTIPLIER=`echo $OPTION | sed -e 's#-Xm[xns][0-9][0-9]*\([kKmMgG]*\)#\1#'`
 120             ;;
 121         -XX*)
 122             SIZE=`echo $OPTION | sed -e 's#[^=][^=]*=\([0-9][0-9]*\).*#\1#'`
 123             MULTIPLIER=`echo $OPTION | sed -e 's#[^=][^=]*=[0-9][0-9]*\([kKmMgG]*\)#\1#'`
 124             ;;
 125     esac
 126 
 127     case "$MULTIPLIER" in
 128         k|K)
 129             SIZE=$(( SIZE * 1024 ))
 130             ;;
 131         m|M)
 132             SIZE=$(( SIZE * 1024 * 1024 ))
 133             ;;
 134         g|G)
 135             SIZE=$(( SIZE * 1024 * 1024 * 1024 ))
 136             ;;
 137     esac
 138 
 139     echo $SIZE
 140 }
 141 
 142 # Derivate max heap size from passed option list.
 143 get_max_heap_size() {
 144     MaxHeapSize=
 145     InitialHeapSize=
 146     MaxNewSize=
 147     NewSize=
 148     OldSize=
 149 
 150     for OPTION in "$@"; do
 151         case "$OPTION" in
 152             -Xmx*|-XX:MaxHeapSize=*)
 153                 MaxHeapSize=`parse_heap_size $OPTION`
 154                 ;;
 155             -Xms*|-XX:InitialHeapSize=*)
 156                 InitialHeapSize=`parse_heap_size $OPTION`
 157                 ;;
 158             -Xmn*|-XX:MaxNewSize=*)
 159                 MaxNewSize=`parse_heap_size $OPTION`
 160                 ;;
 161             -XX:NewSize=*)
 162                 NewSize=`parse_heap_size $OPTION`
 163                 ;;
 164             -XX:OldSize=*)
 165                 OldSize=`parse_heap_size $OPTION`
 166                 ;;
 167         esac
 168     done
 169 
 170     if [ -n "$MaxHeapSize" ]; then
 171         echo "$MaxHeapSize"
 172     elif [ -n "$InitialHeapSize" ]; then
 173         echo "$InitialHeapSize"
 174     elif [ -n "$MaxNewSize" -a -n "$OldSize" ]; then
 175         echo $(( MaxHeapSize + OldSize ))
 176     elif [ -n "$NewSize" -a -n "$OldSize" ]; then
 177         echo $(( 2 * NewSize + OldSize ))
 178     elif [ -n "$OldSize" ]; then
 179         echo $(( 2 * OldSize ))
 180     elif [ -n "$MaxNewSize" ]; then
 181         echo $(( 2 * MaxNewSize ))
 182     elif [ -n "$NewSize" ]; then
 183         echo $(( 3 * NewSize ))
 184     else
 185         echo "128M"
 186     fi
 187 }