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
35import java.lang.management.ManagementFactory;
36import javax.management.MBeanServer;
37import javax.management.ObjectName;
38
39import jdk.test.lib.process.OutputAnalyzer;
40import jdk.test.lib.process.ProcessTools;
41
42public 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    // bool UseParallelGC = true {product} {command line}
57    private static final String parallelGCPattern =
58        " *bool +UseParallelGC *= *true +\\{product\\} *\\{command line\\}";
59
60    private static void testFlag() throws Exception {
61        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
62            option, "-XX:+PrintFlagsFinal", "-version");
63
64        OutputAnalyzer output = new OutputAnalyzer(pb.start());
65
66        output.shouldHaveExitValue(0);
67
68        String value = output.firstMatch(parallelGCPattern);
69        if (value == null) {
70            throw new RuntimeException(
71                option + " didn't set UseParallelGC as if from command line");
72        }
73    }
74
75    private static boolean haveRequiredMemory() throws Exception {
76        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
77        ObjectName os = new ObjectName("java.lang", "type", "OperatingSystem");
78        Object attr = server.getAttribute(os, "TotalPhysicalMemorySize");
79        String value = attr.toString();
80        long memory = Long.parseLong(value);
81        return memory >= minMemory;
82    }
83
84    private static boolean canUseAggressiveHeapOption() throws Exception {
85        if (!haveRequiredMemory()) {
86            System.out.println(
87                "Skipping test of " + option + " : insufficient memory");
88            return false;
89        }
90        return true;
91    }
92}
93
94