1 #!/bin/sh
   2 
   3 #
   4 # Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
   5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6 #
   7 # This code is free software; you can redistribute it and/or modify it
   8 # under the terms of the GNU General Public License version 2 only, as
   9 # published by the Free Software Foundation.
  10 #
  11 # This code is distributed in the hope that it will be useful, but WITHOUT
  12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14 # version 2 for more details (a copy is included in the LICENSE file that
  15 # accompanied this code).
  16 #
  17 # You should have received a copy of the GNU General Public License version
  18 # 2 along with this work; if not, write to the Free Software Foundation,
  19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20 #
  21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22 # or visit www.oracle.com if you need additional information or have any
  23 # questions.
  24 #
  25 
  26 # @test
  27 # @bug 6942989
  28 # @summary Check for WeakReference leak in Logger objects
  29 # @author Daniel D. Daugherty
  30 #
  31 # @library ../../../sun/tools/common
  32 # @build SimpleApplication ShutdownSimpleApplication
  33 # @build LoggerWeakRefLeak
  34 # @run shell/timeout=240 LoggerWeakRefLeak.sh
  35 
  36 # The timeout is: 2 minutes for infrastructure and 2 minutes for the test
  37 #
  38 
  39 . ${TESTSRC}/../../../sun/tools/common/CommonSetup.sh
  40 . ${TESTSRC}/../../../sun/tools/common/ApplicationSetup.sh
  41 
  42 
  43 TEST_NAME="LoggerWeakRefLeak"
  44 TARGET_CLASS="java\.lang\.ref\.WeakReference"
  45 
  46 
  47 # MAIN begins here
  48 #
  49 
  50 seconds=
  51 if [ "$#" -gt 0 ]; then
  52     seconds="$1"
  53 fi
  54 
  55 # see if this version of jmap supports the '-histo:live' option
  56 jmap_option="-histo:live"
  57 set +e
  58 "${JMAP}" 2>&1 | grep ':live' > /dev/null 2>&1
  59 status="$?"
  60 set -e
  61 if [ "$status" != 0 ]; then
  62     echo "INFO: switching jmap option from '$jmap_option'\c"
  63     jmap_option="-histo"
  64     echo " to '$jmap_option'."
  65 fi
  66 
  67 # Start application and use TEST_NAME.port for coordination
  68 startApplication "$TEST_NAME" "$TEST_NAME.port" $seconds
  69 
  70 finished_early=false
  71 
  72 decreasing_cnt=0
  73 increasing_cnt=0
  74 loop_cnt=0
  75 prev_instance_cnt=0
  76 
  77 MAX_JMAP_TRY_CNT=10
  78 jmap_retry_cnt=0
  79 loop_cnt_on_retry=0
  80 
  81 while true; do
  82     # see if the target process has finished its run and bail if it has
  83     set +e
  84     grep "^INFO: final loop count = " "$appOutput" > /dev/null 2>&1
  85     status="$?"
  86     set -e
  87     if [ "$status" = 0 ]; then
  88         break
  89     fi
  90 
  91     # Output format for 'jmap -histo' in JDK1.5.0:
  92     #
  93     #     <#bytes> <#instances> <class_name>
  94     #
  95     # Output format for 'jmap -histo:live':
  96     #
  97     #     <num>: <#instances> <#bytes> <class_name>
  98     #
  99     set +e
 100     "${JMAP}" "$jmap_option" "$appJavaPid" > "$TEST_NAME.jmap" 2>&1
 101     status="$?"
 102     set -e
 103 
 104     if [ "$status" != 0 ]; then
 105         echo "INFO: jmap exited with exit code = $status"
 106 
 107         # There are intermittent jmap failures; see 6498448.
 108         #
 109         # So far the following have been observed in a jmap call
 110         # that was not in a race with target process termination:
 111         #
 112         # (Solaris specific, 2nd sample)
 113         # <pid>: Unable to open door: target process not responding or HotSpot VM not loaded
 114         # The -F option can be used when the target process is not responding
 115         #
 116         # (on Solaris so far)
 117         # java.io.IOException
 118         #
 119         # (on Solaris so far, 1st sample)
 120         # <pid>: Permission denied
 121         #
 122         sed 's/^/INFO: /' "$TEST_NAME.jmap"
 123 
 124         if [ "$loop_cnt" = "$loop_cnt_on_retry" ]; then
 125             # loop count hasn't changed
 126             jmap_retry_cnt=`expr $jmap_retry_cnt + 1`
 127         else
 128             # loop count has changed so remember it
 129             jmap_retry_cnt=1
 130             loop_cnt_on_retry="$loop_cnt"
 131         fi
 132 
 133         # This is '-ge' because we have the original attempt plus
 134         # MAX_JMAP_TRY_CNT - 1 retries.
 135         if [ "$jmap_retry_cnt" -ge "$MAX_JMAP_TRY_CNT" ]; then
 136             echo "INFO: jmap failed $MAX_JMAP_TRY_CNT times in a row" \
 137                 "without making any progress."
 138             echo "FAIL: jmap is unable to take any samples." >&2
 139             killApplication
 140             exit 2
 141         fi
 142 
 143         # short delay and try again
 144         # Note: sleep 1 didn't help with "<pid>: Permission denied"
 145         sleep 2
 146         echo "INFO: retrying jmap (retry=$jmap_retry_cnt, loop=$loop_cnt)."
 147         continue
 148     fi
 149 
 150     set +e
 151     instance_cnt=`grep "${PATTERN_WS}${TARGET_CLASS}${PATTERN_EOL}" \
 152         "$TEST_NAME.jmap" \
 153         | sed '
 154             # strip leading whitespace; does nothing in JDK1.5.0
 155             s/^'"${PATTERN_WS}${PATTERN_WS}"'*//
 156             # strip <#bytes> in JDK1.5.0; does nothing otherwise
 157             s/^[1-9][0-9]*'"${PATTERN_WS}${PATTERN_WS}"'*//
 158             # strip <num>: field; does nothing in JDK1.5.0
 159             s/^[1-9][0-9]*:'"${PATTERN_WS}${PATTERN_WS}"'*//
 160             # strip <class_name> field
 161             s/'"${PATTERN_WS}"'.*//
 162             '`
 163     set -e
 164     if [ -z "$instance_cnt" ]; then
 165         echo "INFO: instance count is unexpectedly empty"
 166         if [ "$loop_cnt" = 0 ]; then
 167             echo "INFO: on the first iteration so no sample was found."
 168             echo "INFO: There is likely a problem with the sed filter."
 169             echo "INFO: start of jmap output:"
 170             cat "$TEST_NAME.jmap"
 171             echo "INFO: end of jmap output."
 172             echo "FAIL: cannot find the instance count value." >&2
 173             killApplication
 174             exit 2
 175         fi
 176     else
 177         echo "INFO: instance_cnt = $instance_cnt"
 178 
 179         if [ "$instance_cnt" -gt "$prev_instance_cnt" ]; then
 180             increasing_cnt=`expr $increasing_cnt + 1`
 181         else
 182             # actually decreasing or the same
 183             decreasing_cnt=`expr $decreasing_cnt + 1`
 184 
 185             # For these particular WeakReference leaks, the count was
 186             # always observed to be increasing so if we get a decreasing
 187             # or the same count, then the leaks are fixed in the bits
 188             # being tested.
 189             echo "INFO: finishing early due to non-increasing instance count."
 190             finished_early=true
 191             killApplication
 192             break
 193         fi
 194         prev_instance_cnt="$instance_cnt"
 195     fi
 196 
 197     # delay between samples
 198     sleep 5
 199 
 200     loop_cnt=`expr $loop_cnt + 1`
 201 done
 202 
 203 if [ $finished_early = false ]; then
 204     stopApplication "$TEST_NAME.port"
 205     waitForApplication
 206 fi
 207 
 208 echo "INFO: $TEST_NAME has finished running."
 209 echo "INFO: increasing_cnt = $increasing_cnt"
 210 echo "INFO: decreasing_cnt = $decreasing_cnt"
 211 if [ "$jmap_retry_cnt" -gt 0 ]; then
 212     echo "INFO: jmap_retry_cnt = $jmap_retry_cnt (in $loop_cnt iterations)"
 213 fi
 214 
 215 if [ "$loop_cnt" = 0 ]; then
 216     echo "FAIL: jmap is unable to take any samples." >&2
 217     exit 2
 218 fi
 219 
 220 echo "INFO: The instance count of" `eval echo $TARGET_CLASS` "objects"
 221 if [ "$decreasing_cnt" = 0 ]; then
 222     echo "INFO: is always increasing."
 223     echo "FAIL: This indicates that there is a memory leak." >&2
 224     exit 2
 225 fi
 226 
 227 echo "INFO: is not always increasing."
 228 echo "PASS: This indicates that there is not a memory leak."
 229 exit 0