1 /*
   2  * Copyright (c) 2017, 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 TestAggressiveHeap
  26  * @key gc
  27  * @bug 8179084
  28  * @requires vm.gc.Parallel
  29  * @summary Test argument processing for -XX:+AggressiveHeap.
  30  * @library /test/lib
  31  * @modules java.base java.management
  32  * @run driver TestAggressiveHeap
  33  */
  34 
  35 import java.lang.management.ManagementFactory;
  36 import javax.management.MBeanServer;
  37 import javax.management.ObjectName;
  38 
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 import jdk.test.lib.process.ProcessTools;
  41 
  42 public class TestAggressiveHeap {
  43 
  44     public static void main(String args[]) throws Exception {
  45         if (canUseAggressiveHeapOption()) {
  46             testFlag();
  47         }
  48     }
  49 
  50     // Note: Not a normal boolean flag; -XX:-AggressiveHeap is invalid.
  51     private static final String option = "-XX:+AggressiveHeap";
  52 
  53     // Option requires at least 256M, else error during option processing.
  54     private static final long minMemory = 256 * 1024 * 1024;
  55 
  56     // Setting the heap to half of the physical memory is not suitable for
  57     // a test environment with many tests running concurrently, setting to
  58     // half of the required size instead.
  59     private static final String heapSizeOption = "-Xmx128M";
  60 
  61     // bool UseParallelGC = true {product} {command line}
  62     private static final String parallelGCPattern =
  63         " *bool +UseParallelGC *= *true +\\{product\\} *\\{command line\\}";
  64 
  65     private static void testFlag() throws Exception {
  66         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  67             option, heapSizeOption, "-XX:+PrintFlagsFinal", "-version");
  68 
  69         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  70 
  71         output.shouldHaveExitValue(0);
  72 
  73         String value = output.firstMatch(parallelGCPattern);
  74         if (value == null) {
  75             throw new RuntimeException(
  76                 option + " didn't set UseParallelGC as if from command line");
  77         }
  78     }
  79 
  80     private static boolean haveRequiredMemory() throws Exception {
  81         MBeanServer server = ManagementFactory.getPlatformMBeanServer();
  82         ObjectName os = new ObjectName("java.lang", "type", "OperatingSystem");
  83         Object attr = server.getAttribute(os, "TotalPhysicalMemorySize");
  84         String value = attr.toString();
  85         long memory = Long.parseLong(value);
  86         return memory >= minMemory;
  87     }
  88 
  89     private static boolean canUseAggressiveHeapOption() throws Exception {
  90         if (!haveRequiredMemory()) {
  91             System.out.println(
  92                 "Skipping test of " + option + " : insufficient memory");
  93             return false;
  94         }
  95         return true;
  96     }
  97 }
  98