Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/tools/common/ApplicationSetup.sh
38839 views
#!/bin/sh12#3# Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.4# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.5#6# This code is free software; you can redistribute it and/or modify it7# under the terms of the GNU General Public License version 2 only, as8# published by the Free Software Foundation.9#10# This code is distributed in the hope that it will be useful, but WITHOUT11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13# version 2 for more details (a copy is included in the LICENSE file that14# accompanied this code).15#16# You should have received a copy of the GNU General Public License version17# 2 along with this work; if not, write to the Free Software Foundation,18# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19#20# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21# or visit www.oracle.com if you need additional information or have any22# questions.23#242526# Support functions to start, stop, wait for or kill a given SimpleApplication2728# Starts a given app as background process, usage:29# startApplication <class> port-file [args...]30#31# The following variables are set:32#33# appJavaPid - application's Java pid34# appOtherPid - pid associated with the app other than appJavaPid35# appPidList - all pids associated with the app36# appOutput - file containing stdout and stderr from the app37#38# Waits for at least one line of output from the app to indicate39# that it is up and running.40#41startApplication()42{43appOutput="${TESTCLASSES}/Application.out"4445${JAVA} -XX:+UsePerfData -classpath "${TESTCLASSPATH:-${TESTCLASSES}}" "$@" > "$appOutput" 2>&1 &46appJavaPid="$!"47appOtherPid=48appPidList="$appJavaPid"4950echo "INFO: waiting for $1 to initialize..."51_cnt=052while true; do53# if the app doesn't start then the JavaTest/JTREG timeout will54# kick in so this isn't really a endless loop55sleep 156out=`tail -1 "$appOutput"`57if [ -n "$out" ]; then58# we got some output from the app so it's running59break60fi61_cnt=`expr $_cnt + 1`62echo "INFO: waited $_cnt second(s) ..."63done64unset _cnt6566if $isWindows; then67# Windows requires special handling68appOtherPid="$appJavaPid"6970if $isCygwin; then71appJavaPid=`ps -p "$appOtherPid" \72| sed -n '73# See if $appOtherPid is in PID column; there are sometimes74# non-blanks in column 1 (I and S observed so far)75/^.'"${PATTERN_WS}${PATTERN_WS}*${appOtherPid}${PATTERN_WS}"'/{76# strip PID column77s/^.'"${PATTERN_WS}${PATTERN_WS}*${appOtherPid}${PATTERN_WS}${PATTERN_WS}"'*//78# strip PPID column79s/^[1-9][0-9]*'"${PATTERN_WS}${PATTERN_WS}"'*//80# strip PGID column81s/^[1-9][0-9]*'"${PATTERN_WS}${PATTERN_WS}"'*//82# strip everything after WINPID column83s/'"${PATTERN_WS}"'.*//84p85q86}87'`88echo "INFO: Cygwin pid=$appOtherPid maps to Windows pid=$appJavaPid"89else90# show PID, PPID and COMM columns only91appJavaPid=`ps -o pid,ppid,comm \92| sed -n '93# see if appOtherPid is in either PID or PPID columns94/'"${PATTERN_WS}${appOtherPid}${PATTERN_WS}"'/{95# see if this is a java command96/java'"${PATTERN_EOL}"'/{97# strip leading white space98s/^'"${PATTERN_WS}${PATTERN_WS}"'*//99# strip everything after the first word100s/'"${PATTERN_WS}"'.*//101# print the pid and we are done102p103q104}105}106'`107echo "INFO: MKS shell pid=$appOtherPid; Java pid=$appJavaPid"108fi109110if [ -z "$appJavaPid" ]; then111echo "ERROR: could not find app's Java pid." >&2112killApplication113exit 2114fi115appPidList="$appOtherPid $appJavaPid"116fi117118echo "INFO: $1 is process $appJavaPid"119echo "INFO: $1 output is in $appOutput"120}121122123# Stops a simple application by invoking ShutdownSimpleApplication124# class with a specific port-file, usage:125# stopApplication port-file126#127# Note: When this function returns, the SimpleApplication (or a subclass)128# may still be running because the application has not yet reached the129# shutdown check.130#131stopApplication()132{133$JAVA -XX:+UsePerfData -classpath "${TESTCLASSPATH:-${TESTCLASSES}}" ShutdownSimpleApplication $1134}135136137# Wait for a simple application to stop running.138#139waitForApplication() {140if [ $isWindows = false ]; then141# non-Windows is easy; just one process142echo "INFO: waiting for $appJavaPid"143set +e144wait "$appJavaPid"145set -e146147elif $isCygwin; then148# Cygwin pid and not the Windows pid149echo "INFO: waiting for $appOtherPid"150set +e151wait "$appOtherPid"152set -e153154else # implied isMKS155# MKS has intermediate shell and Java process156echo "INFO: waiting for $appJavaPid"157158# appJavaPid can be empty if pid search in startApplication() failed159if [ -n "$appJavaPid" ]; then160# only need to wait for the Java process161set +e162wait "$appJavaPid"163set -e164fi165fi166}167168169# Kills a simple application by sending a SIGTERM to the appropriate170# process(es); on Windows SIGQUIT (-9) is used.171#172killApplication()173{174if [ $isWindows = false ]; then175# non-Windows is easy; just one process176echo "INFO: killing $appJavaPid"177set +e178kill -TERM "$appJavaPid" # try a polite SIGTERM first179sleep 2180# send SIGQUIT (-9) just in case SIGTERM didn't do it181# but don't show any complaints182kill -QUIT "$appJavaPid" > /dev/null 2>&1183wait "$appJavaPid"184set -e185186elif $isCygwin; then187# Cygwin pid and not the Windows pid188echo "INFO: killing $appOtherPid"189set +e190kill -9 "$appOtherPid"191wait "$appOtherPid"192set -e193194else # implied isMKS195# MKS has intermediate shell and Java process196echo "INFO: killing $appPidList"197set +e198kill -9 $appPidList199set -e200201# appJavaPid can be empty if pid search in startApplication() failed202if [ -n "$appJavaPid" ]; then203# only need to wait for the Java process204set +e205wait "$appJavaPid"206set -e207fi208fi209}210211212