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