Path: blob/master/test/jdk/sun/jvmstat/testlibrary/utils.sh
41149 views
#1# Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.2# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3#4# This code is free software; you can redistribute it and/or modify it5# under the terms of the GNU General Public License version 2 only, as6# published by the Free Software Foundation.7#8# This code is distributed in the hope that it will be useful, but WITHOUT9# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11# version 2 for more details (a copy is included in the LICENSE file that12# accompanied this code).13#14# You should have received a copy of the GNU General Public License version15# 2 along with this work; if not, write to the Free Software Foundation,16# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17#18# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19# or visit www.oracle.com if you need additional information or have any20# questions.21#2223#2425setup() {26# Verify directory context variables are set27if [ "${TESTJAVA}" = "" ] ; then28echo "TESTJAVA not set. Test cannot execute. Failed."29exit 130fi3132if [ "${TESTCLASSES}" = "" ] ; then33TESTCLASSES="."34fi3536if [ "${TESTSRC}" = "" ] ; then37TESTSRC="."38fi3940OS=`uname -s`41case ${OS} in42Windows_* | CYGWIN*)43PS=";"44FS="\\"45;;46*)47PS=":"48FS="/"49;;50esac51}5253verify_os() {54OS=`uname -s`55case ${OS} in56Windows_95 | Windows_98 | Windows_ME | CYGWIN* )57echo "Test bypassed: jvmstat feature not supported on ${OS}"58exit 059;;60Windows_*)61# verify that the tmp directory supports persistent ACLs, which62# are required for jvmstat to enable its shared memory feature.63# NOTE: FAT type files systems do not support ACLs, but NTFS files64# systems do.65#66echo "temp directory is in: `windir -t`"67TMPDRIVE=`windir -t | cut -d: -f1`68if [ "${TMPDRIVE}" = "" ] ; then69echo "Could not get temp directory drive letter"70exit 171fi7273echo "temp file system characteristics:"74sysinf drives -a | grep "^${TMPDRIVE}"75sysinf drives -a | grep "^${TMPDRIVE}" | grep PERSISTENT_ACLS > /dev/null76case $? in770)78;;791)80echo "Test bypassed: jvmstat feature disabled because the temp"81echo "directory doesn't support persistent ACLs"82exit 083;;842)85echo "Could not get temp directory file system features"86exit 187;;88*)89echo "Unexpected return code from grep - $?"90exit 191;;92esac93;;94esac95}9697# function to kill the process with the given process id98kill_proc() {99kill_pid=$1100101if [ "${kill_pid}" = "" ]102then103echo "kill_proc(): null pid: ignored"104return105fi106107if [ ${kill_pid} -le 0 ]108then109echo "kill_proc(): invalid pid: ${kill_pid}: ignored"110return111fi112113OS=`uname -s`114case $OS in115Windows_*)116case ${SHELL_VERSION} in117[1234567].* | 8.[12345].*)118# when starting processes in the background, mks creates an119# intervening between the parent process and the background120# process. The pid acquired for background process as acquired121# by the $! shell variable is actually the pid of the invervening122# shell and not that of the background process. Sending a specific123# signal to the intervening shell will only effects the intervening124# shell and not the background process, thus leaving the background125# process running. The following code assumes that the pid passed126# into this function as ${kill_pid}, was acquired by $!. Therefore,127# in order to kill the background process, we must first find the128# children of ${kill_pid} (should be only one child) and kill them.129130ps -o pid,ppid | grep "${kill_pid}$"131children=`mks_children ${kill_pid}`132echo "killing children of ${kill_pid}: ${children}"133for child in ${children} ; do134kill_proc_common ${child}135done136;;137*)138# in mks 8.6 and later, the pid returned in $! is now the pid139# of the background process and not that of the intervening shell.140kill_proc_common ${kill_pid}141;;142esac143;;144*)145kill_proc_common ${kill_pid}146;;147esac148}149150mks_children() {151ppid=$1152ps -o pid,ppid | grep "${ppid}$" | awk '153BEGIN { pids="" }154{ pids = pids $1 " " }155END { print pids }'156}157158kill_proc_common() {159kpid=$1160161# verify that the process exists and we can signal it162kill -0 ${kpid} 2>/dev/null163if [ $? -ne 0 ]164then165echo "Could not signal >${kpid}<"166echo "user id = `id`"167echo "process information :"168ps -l -p ${kpid}169return170fi171172kill -TERM ${kpid} # hit it easy first173if [ $? -eq 0 ]174then175sleep 2176kill -0 ${kpid} 2>/dev/null177# check if it's still hanging around178if [ $? -eq 0 ]179then180# it's still lingering, now it it hard181kill -KILL ${kpid} 2>/dev/null182if [ $? -ne 0 ]183then184echo "Could not kill ${kpid}!"185fi186fi187else188echo "Error sending term signal to ${kpid}!"189fi190}191192# check to see if a port is free193checkPort() # port194{195inuse=`netstat -a | egrep "\.$1"`196if [ "${inuse}" = "" ] ; then197echo "free"198else199echo "inuse"200fi201}202203# Get a free port, where port+1 is also free, return 0 when giving up204freePort()205{206start=3000207while [ ${start} -lt 3030 ] ; do208port1=`expr ${start} '+' $$ '%' 1000`209port2=`expr ${port1} '+' 1`210if [ "`checkPort ${port1}`" = "inuse" \211-o "`checkPort ${port2}`" = "inuse" ] ; then212start=`expr ${start} '+' 1`213else214break215fi216done217if [ "`checkPort ${port1}`" = "inuse" \218-o "`checkPort ${port2}`" = "inuse" ] ; then219port1="0"220fi221echo "${port1}"222}223224# Flags used by all jstat calls in jdk/sun/tools/jstat/*.sh225#226# The awk scripts parsing jstat output expect it to be in en-us locale.227# Especially, we must force '.' instead of ',' in numbers.228COMMON_JSTAT_FLAGS="-J-XX:+UsePerfData -J-Duser.language=en -J-Duser.country=en"229230231232