1 #!/bin/sh
   2 
   3 #
   4 # Copyright (c) 2002, 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 4627316 6743526
  28 # @summary Test option to limit direct memory allocation
  29 #
  30 # @requires (os.arch == "x86_64") | (os.arch == "amd64") | (os.arch == "sparcv9")
  31 # @build LimitDirectMemory
  32 # @run shell LimitDirectMemory.sh
  33 
  34 TMP1=tmp_$$
  35 
  36 runTest() {
  37   echo "Testing: $*"
  38   ${TESTJAVA}/bin/java ${TESTVMOPTS} $*
  39   if [ $? -eq 0 ]
  40   then echo "--- passed as expected"
  41   else
  42     echo "--- failed"
  43     exit 1
  44   fi
  45 }
  46 
  47 
  48 launchFail() {
  49   echo "Testing: -XX:MaxDirectMemorySize=$* -cp ${TESTCLASSES} \
  50      LimitDirectMemory true DEFAULT DEFAULT+1M"
  51   ${TESTJAVA}/bin/java ${TESTVMOPTS} -XX:MaxDirectMemorySize=$* -cp ${TESTCLASSES} \
  52      LimitDirectMemory true DEFAULT DEFAULT+1M > ${TMP1} 2>&1
  53   cat ${TMP1}
  54   cat ${TMP1} | grep -s "Unrecognized VM option: \'MaxDirectMemorySize="
  55   if [ $? -ne 0 ]
  56     then echo "--- failed as expected"
  57   else
  58     echo "--- failed"
  59     exit 1
  60   fi
  61 }
  62 
  63 # $java LimitDirectMemory throwp fill_direct_memory size_per_buffer
  64 
  65 # Memory is properly limited using multiple buffers.
  66 runTest -XX:MaxDirectMemorySize=10 -cp ${TESTCLASSES} LimitDirectMemory true 10 1
  67 runTest -XX:MaxDirectMemorySize=1k -cp ${TESTCLASSES} LimitDirectMemory true 1k 100
  68 runTest -XX:MaxDirectMemorySize=10m -cp ${TESTCLASSES} LimitDirectMemory true 10m 10m
  69 
  70 # We can increase the amount of available memory.
  71 runTest -XX:MaxDirectMemorySize=65M -cp ${TESTCLASSES} \
  72   LimitDirectMemory false 64M 65M
  73 
  74 # Exactly the default amount of memory is available.
  75 runTest -cp ${TESTCLASSES} LimitDirectMemory false 10 1
  76 runTest -Xmx64m -cp ${TESTCLASSES} LimitDirectMemory false 0 DEFAULT
  77 runTest -Xmx64m -cp ${TESTCLASSES} LimitDirectMemory true 0 DEFAULT+1
  78 
  79 # We should be able to eliminate direct memory allocation entirely.
  80 runTest -XX:MaxDirectMemorySize=0 -cp ${TESTCLASSES} LimitDirectMemory true 0 1
  81 
  82 # Setting the system property should not work so we should be able to allocate
  83 # the default amount.
  84 runTest -Dsun.nio.MaxDirectMemorySize=1K -Xmx64m -cp ${TESTCLASSES} \
  85   LimitDirectMemory false DEFAULT-1 DEFAULT/2
  86 
  87 # Various bad values fail to launch the VM.
  88 launchFail foo
  89 launchFail 10kmt
  90 launchFail -1
  91 
  92 # Clean-up
  93 rm ${TMP1}