1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test TestArrayAllocatorMallocLimit
  26  * @summary Sanity check that the ArrayAllocatorMallocLimit flag can be set.
  27  * The test helps verifying that size_t flags can be set/read.
  28  * @bug 8054823
  29  * @key gc
  30  * @library /testlibrary
  31  * @build com.oracle.java.testlibrary.*
  32  * @run driver TestArrayAllocatorMallocLimit
  33  */
  34 
  35 import com.oracle.java.testlibrary.Asserts;
  36 import com.oracle.java.testlibrary.OutputAnalyzer;
  37 import com.oracle.java.testlibrary.ProcessTools;
  38 import java.math.BigInteger;
  39 
  40 public class TestArrayAllocatorMallocLimit {
  41   public static void main(String [] args) throws Exception {
  42     testDefaultValue();
  43     testSetValue();
  44   }
  45 
  46   private static final String flagName = "ArrayAllocatorMallocLimit";
  47 
  48   //     size_t ArrayAllocatorMallocLimit                 = 18446744073709551615{experimental}
  49   private static final String printFlagsFinalPattern = " *size_t *" + flagName + " *:?= *(\\d+) *\\{experimental\\} *";
  50 
  51   public static void testDefaultValue()  throws Exception {
  52     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  53       "-XX:+UnlockExperimentalVMOptions", "-XX:+PrintFlagsFinal", "-version");
  54 
  55     OutputAnalyzer output = new OutputAnalyzer(pb.start());
  56     String value = output.firstMatch(printFlagsFinalPattern, 1);
  57 
  58     try {
  59       Asserts.assertNotNull(value, "Couldn't find size_t flag " + flagName);
  60 
  61       // A size_t is not always parseable with Long.parseValue,
  62       // use BigInteger instead.
  63       BigInteger biValue = new BigInteger(value);
  64 
  65       // Sanity check that we got a non-zero value.
  66       Asserts.assertNotEquals(biValue, "0");
  67 
  68       output.shouldHaveExitValue(0);
  69     } catch (Exception e) {
  70       System.err.println(output.getOutput());
  71       throw e;
  72     }
  73   }
  74 
  75   public static void testSetValue() throws Exception {
  76     long flagValue = 2048;
  77 
  78     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  79       "-XX:+UnlockExperimentalVMOptions", "-XX:" + flagName + "=" + flagValue, "-XX:+PrintFlagsFinal", "-version");
  80 
  81     OutputAnalyzer output = new OutputAnalyzer(pb.start());
  82     String value = output.firstMatch(printFlagsFinalPattern, 1);
  83 
  84     try {
  85       Asserts.assertNotNull("Couldn't find size_t flag " + flagName);
  86 
  87       long longValue = Long.parseLong(value);
  88 
  89       Asserts.assertEquals(longValue, flagValue);
  90 
  91       output.shouldHaveExitValue(0);
  92     } catch (Exception e) {
  93       System.err.println(output.getOutput());
  94       throw e;
  95     }
  96   }
  97 
  98 }