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