2617N/A#!/bin/sh
2617N/A
2540N/A#
2540N/A# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2540N/A# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2540N/A#
2540N/A# This code is free software; you can redistribute it and/or modify it
2540N/A# under the terms of the GNU General Public License version 2 only, as
2540N/A# published by the Free Software Foundation.
2540N/A#
2540N/A# This code is distributed in the hope that it will be useful, but WITHOUT
2540N/A# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2540N/A# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2540N/A# version 2 for more details (a copy is included in the LICENSE file that
2540N/A# accompanied this code).
2540N/A#
2540N/A# You should have received a copy of the GNU General Public License version
2540N/A# 2 along with this work; if not, write to the Free Software Foundation,
2540N/A# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2540N/A#
2540N/A# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2540N/A# or visit www.oracle.com if you need additional information or have any
2540N/A# questions.
2540N/A#
2540N/A
2540N/A# @test
2540N/A# @bug 6942989
2540N/A# @summary Check for WeakReference leak in Logger objects
2540N/A# @author Daniel D. Daugherty
2540N/A#
2617N/A# @library ../../../sun/tools/common
2617N/A# @build SimpleApplication ShutdownSimpleApplication
2617N/A# @build LoggerWeakRefLeak
2540N/A# @run shell/timeout=240 LoggerWeakRefLeak.sh
2540N/A
2617N/A# The timeout is: 2 minutes for infrastructure and 2 minutes for the test
2540N/A#
2540N/A
2617N/A. ${TESTSRC}/../../../sun/tools/common/CommonSetup.sh
2617N/A. ${TESTSRC}/../../../sun/tools/common/ApplicationSetup.sh
2540N/A
2540N/A
2540N/ATEST_NAME="LoggerWeakRefLeak"
2540N/ATARGET_CLASS="java\.lang\.ref\.WeakReference"
2540N/A
2540N/A
2540N/A# MAIN begins here
2540N/A#
2540N/A
2540N/Aseconds=
2540N/Aif [ "$#" -gt 0 ]; then
2540N/A seconds="$1"
2540N/Afi
2540N/A
2540N/A# see if this version of jmap supports the '-histo:live' option
2540N/Ajmap_option="-histo:live"
2540N/Aset +e
2617N/A"${JMAP}" 2>&1 | grep ':live' > /dev/null 2>&1
2540N/Astatus="$?"
2540N/Aset -e
2617N/Aif [ "$status" != 0 ]; then
2622N/A # usage message doesn't show ':live' option
2622N/A
2622N/A if $isWindows; then
2622N/A # If SA isn't present, then jmap gives a different usage message
2622N/A # that doesn't show the ':live' option. However, that's a bug that
2622N/A # is covered by 6971851 so we try using the option just to be sure.
2622N/A # For some reason, this problem has only been seen on OpenJDK6 on
2622N/A # Windows. Not sure why.
2622N/A set +e
2622N/A # Note: Don't copy this code to try probing process 0 on Linux; it
2622N/A # will kill the process group in strange ways.
2622N/A "${JMAP}" "$jmap_option" 0 2>&1 | grep 'Usage' > /dev/null 2>&1
2622N/A status="$?"
2622N/A set -e
2622N/A if [ "$status" = 0 ]; then
2622N/A # Usage message generated so flag the problem.
2622N/A status=1
2622N/A else
2622N/A # No usage message so clear the flag.
2622N/A status=0
2622N/A fi
2622N/A fi
2622N/A
2622N/A if [ "$status" != 0 ]; then
2622N/A echo "ERROR: 'jmap $jmap_option' is not supported so this test"
2622N/A echo "ERROR: cannot work reliably. Aborting!"
2622N/A exit 2
2622N/A fi
2540N/Afi
2540N/A
2617N/A# Start application and use TEST_NAME.port for coordination
2617N/AstartApplication "$TEST_NAME" "$TEST_NAME.port" $seconds
2540N/A
2617N/Afinished_early=false
2540N/A
2540N/Adecreasing_cnt=0
2540N/Aincreasing_cnt=0
2540N/Aloop_cnt=0
2540N/Aprev_instance_cnt=0
2540N/A
2617N/AMAX_JMAP_TRY_CNT=10
2617N/Ajmap_retry_cnt=0
2617N/Aloop_cnt_on_retry=0
2617N/A
2540N/Awhile true; do
2617N/A # see if the target process has finished its run and bail if it has
2617N/A set +e
2617N/A grep "^INFO: final loop count = " "$appOutput" > /dev/null 2>&1
2617N/A status="$?"
2617N/A set -e
2617N/A if [ "$status" = 0 ]; then
2617N/A break
2617N/A fi
2617N/A
2540N/A # Output format for 'jmap -histo' in JDK1.5.0:
2540N/A #
2540N/A # <#bytes> <#instances> <class_name>
2540N/A #
2540N/A # Output format for 'jmap -histo:live':
2540N/A #
2540N/A # <num>: <#instances> <#bytes> <class_name>
2540N/A #
2540N/A set +e
2617N/A "${JMAP}" "$jmap_option" "$appJavaPid" > "$TEST_NAME.jmap" 2>&1
2540N/A status="$?"
2540N/A set -e
2540N/A
2540N/A if [ "$status" != 0 ]; then
2540N/A echo "INFO: jmap exited with exit code = $status"
2617N/A
2617N/A # There are intermittent jmap failures; see 6498448.
2617N/A #
2617N/A # So far the following have been observed in a jmap call
2617N/A # that was not in a race with target process termination:
2617N/A #
2617N/A # (Solaris specific, 2nd sample)
2617N/A # <pid>: Unable to open door: target process not responding or HotSpot VM not loaded
2617N/A # The -F option can be used when the target process is not responding
2617N/A #
2617N/A # (on Solaris so far)
2617N/A # java.io.IOException
2617N/A #
2617N/A # (on Solaris so far, 1st sample)
2617N/A # <pid>: Permission denied
2617N/A #
2617N/A sed 's/^/INFO: /' "$TEST_NAME.jmap"
2617N/A
2617N/A if [ "$loop_cnt" = "$loop_cnt_on_retry" ]; then
2617N/A # loop count hasn't changed
2617N/A jmap_retry_cnt=`expr $jmap_retry_cnt + 1`
2617N/A else
2617N/A # loop count has changed so remember it
2617N/A jmap_retry_cnt=1
2617N/A loop_cnt_on_retry="$loop_cnt"
2617N/A fi
2617N/A
2617N/A # This is '-ge' because we have the original attempt plus
2617N/A # MAX_JMAP_TRY_CNT - 1 retries.
2617N/A if [ "$jmap_retry_cnt" -ge "$MAX_JMAP_TRY_CNT" ]; then
2617N/A echo "INFO: jmap failed $MAX_JMAP_TRY_CNT times in a row" \
2617N/A "without making any progress."
2540N/A echo "FAIL: jmap is unable to take any samples." >&2
2617N/A killApplication
2540N/A exit 2
2540N/A fi
2617N/A
2617N/A # short delay and try again
2617N/A # Note: sleep 1 didn't help with "<pid>: Permission denied"
2617N/A sleep 2
2617N/A echo "INFO: retrying jmap (retry=$jmap_retry_cnt, loop=$loop_cnt)."
2617N/A continue
2540N/A fi
2540N/A
2617N/A set +e
2617N/A instance_cnt=`grep "${PATTERN_WS}${TARGET_CLASS}${PATTERN_EOL}" \
2617N/A "$TEST_NAME.jmap" \
2540N/A | sed '
2540N/A # strip leading whitespace; does nothing in JDK1.5.0
2617N/A s/^'"${PATTERN_WS}${PATTERN_WS}"'*//
2540N/A # strip <#bytes> in JDK1.5.0; does nothing otherwise
2617N/A s/^[1-9][0-9]*'"${PATTERN_WS}${PATTERN_WS}"'*//
2540N/A # strip <num>: field; does nothing in JDK1.5.0
2617N/A s/^[1-9][0-9]*:'"${PATTERN_WS}${PATTERN_WS}"'*//
2540N/A # strip <class_name> field
2617N/A s/'"${PATTERN_WS}"'.*//
2540N/A '`
2617N/A set -e
2540N/A if [ -z "$instance_cnt" ]; then
2540N/A echo "INFO: instance count is unexpectedly empty"
2540N/A if [ "$loop_cnt" = 0 ]; then
2540N/A echo "INFO: on the first iteration so no sample was found."
2540N/A echo "INFO: There is likely a problem with the sed filter."
2540N/A echo "INFO: start of jmap output:"
2540N/A cat "$TEST_NAME.jmap"
2540N/A echo "INFO: end of jmap output."
2540N/A echo "FAIL: cannot find the instance count value." >&2
2617N/A killApplication
2540N/A exit 2
2540N/A fi
2540N/A else
2540N/A echo "INFO: instance_cnt = $instance_cnt"
2540N/A
2540N/A if [ "$instance_cnt" -gt "$prev_instance_cnt" ]; then
2540N/A increasing_cnt=`expr $increasing_cnt + 1`
2540N/A else
2617N/A # actually decreasing or the same
2540N/A decreasing_cnt=`expr $decreasing_cnt + 1`
2617N/A
2617N/A # For these particular WeakReference leaks, the count was
2617N/A # always observed to be increasing so if we get a decreasing
2617N/A # or the same count, then the leaks are fixed in the bits
2617N/A # being tested.
2617N/A echo "INFO: finishing early due to non-increasing instance count."
2617N/A finished_early=true
2617N/A killApplication
2617N/A break
2540N/A fi
2540N/A prev_instance_cnt="$instance_cnt"
2540N/A fi
2540N/A
2540N/A # delay between samples
2540N/A sleep 5
2540N/A
2540N/A loop_cnt=`expr $loop_cnt + 1`
2540N/Adone
2540N/A
2617N/Aif [ $finished_early = false ]; then
2617N/A stopApplication "$TEST_NAME.port"
2617N/A waitForApplication
2617N/Afi
2617N/A
2617N/Aecho "INFO: $TEST_NAME has finished running."
2540N/Aecho "INFO: increasing_cnt = $increasing_cnt"
2540N/Aecho "INFO: decreasing_cnt = $decreasing_cnt"
2617N/Aif [ "$jmap_retry_cnt" -gt 0 ]; then
2617N/A echo "INFO: jmap_retry_cnt = $jmap_retry_cnt (in $loop_cnt iterations)"
2617N/Afi
2617N/A
2617N/Aif [ "$loop_cnt" = 0 ]; then
2617N/A echo "FAIL: jmap is unable to take any samples." >&2
2617N/A exit 2
2617N/Afi
2540N/A
2540N/Aecho "INFO: The instance count of" `eval echo $TARGET_CLASS` "objects"
2540N/Aif [ "$decreasing_cnt" = 0 ]; then
2540N/A echo "INFO: is always increasing."
2540N/A echo "FAIL: This indicates that there is a memory leak." >&2
2540N/A exit 2
2540N/Afi
2540N/A
2617N/Aecho "INFO: is not always increasing."
2540N/Aecho "PASS: This indicates that there is not a memory leak."
2540N/Aexit 0