1/*
2 * Copyright (c) 2014, 2016, 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 CheckSegmentedCodeCache
26 * @bug 8015774
27 * @summary Checks VM options related to the segmented code cache
28 * @library /test/lib
29 * @modules java.base/jdk.internal.misc
30 *          java.management
31 *
32 * @build sun.hotspot.WhiteBox
33 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
34 *                                sun.hotspot.WhiteBox$WhiteBoxPermission
35 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
36 *                   -XX:+WhiteBoxAPI
37 *                   compiler.codecache.CheckSegmentedCodeCache
38 */
39
40package compiler.codecache;
41
42import jdk.test.lib.process.OutputAnalyzer;
43import jdk.test.lib.Platform;
44import jdk.test.lib.process.ProcessTools;
45import sun.hotspot.WhiteBox;
46
47public class CheckSegmentedCodeCache {
48    private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
49    // Code heap names
50    private static final String NON_METHOD = "CodeHeap 'non-nmethods'";
51    private static final String PROFILED = "CodeHeap 'profiled nmethods'";
52    private static final String NON_PROFILED = "CodeHeap 'non-profiled nmethods'";
53
54    private static void verifySegmentedCodeCache(ProcessBuilder pb, boolean enabled) throws Exception {
55        OutputAnalyzer out = new OutputAnalyzer(pb.start());
56        out.shouldHaveExitValue(0);
57        if (enabled) {
58            try {
59                // Non-nmethod code heap should be always available with the segmented code cache
60                out.shouldContain(NON_METHOD);
61            } catch (RuntimeException e) {
62                // Check if TieredCompilation is disabled (in a client VM)
63                if(!out.getOutput().contains("-XX:+TieredCompilation not supported in this VM")) {
64                    // Code cache is not segmented
65                    throw new RuntimeException("No code cache segmentation.");
66                    }
67            }
68        } else {
69            out.shouldNotContain(NON_METHOD);
70        }
71    }
72
73    private static void verifyCodeHeapNotExists(ProcessBuilder pb, String... heapNames) throws Exception {
74        OutputAnalyzer out = new OutputAnalyzer(pb.start());
75        out.shouldHaveExitValue(0);
76        for (String name : heapNames) {
77            out.shouldNotContain(name);
78        }
79    }
80
81    private static void failsWith(ProcessBuilder pb, String message) throws Exception {
82        OutputAnalyzer out = new OutputAnalyzer(pb.start());
83        out.shouldContain(message);
84        out.shouldHaveExitValue(1);
85    }
86
87    /**
88    * Check the result of segmented code cache related VM options.
89    */
90    public static void main(String[] args) throws Exception {
91        ProcessBuilder pb;
92
93        // Disabled with ReservedCodeCacheSize < 240MB
94        pb = ProcessTools.createJavaProcessBuilder("-XX:ReservedCodeCacheSize=239m",
95                                                   "-XX:+PrintCodeCache", "-version");
96        verifySegmentedCodeCache(pb, false);
97
98        // Disabled without TieredCompilation
99        pb = ProcessTools.createJavaProcessBuilder("-XX:-TieredCompilation",
100                                                   "-XX:+PrintCodeCache", "-version");
101        verifySegmentedCodeCache(pb, false);
102
103        // Enabled with TieredCompilation and ReservedCodeCacheSize >= 240MB
104        pb = ProcessTools.createJavaProcessBuilder("-XX:+TieredCompilation",
105                                                   "-XX:ReservedCodeCacheSize=240m",
106                                                   "-XX:+PrintCodeCache", "-version");
107        verifySegmentedCodeCache(pb, true);
108        pb = ProcessTools.createJavaProcessBuilder("-XX:+TieredCompilation",
109                                                   "-XX:ReservedCodeCacheSize=400m",
110                                                   "-XX:+PrintCodeCache", "-version");
111        verifySegmentedCodeCache(pb, true);
112
113        // Always enabled if SegmentedCodeCache is set
114        pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
115                                                   "-XX:-TieredCompilation",
116                                                   "-XX:ReservedCodeCacheSize=239m",
117                                                   "-XX:+PrintCodeCache", "-version");
118        verifySegmentedCodeCache(pb, true);
119
120        // The profiled and non-profiled code heaps should not be available in
121        // interpreter-only mode
122        pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
123                                                   "-Xint",
124                                                   "-XX:+PrintCodeCache", "-version");
125        verifyCodeHeapNotExists(pb, PROFILED, NON_PROFILED);
126
127        // If we stop compilation at CompLevel_none or CompLevel_simple we
128        // don't need a profiled code heap.
129        pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
130                                                   "-XX:TieredStopAtLevel=0",
131                                                   "-XX:+PrintCodeCache", "-version");
132        verifyCodeHeapNotExists(pb, PROFILED);
133        pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
134                                                   "-XX:TieredStopAtLevel=1",
135                                                   "-XX:+PrintCodeCache", "-version");
136        verifyCodeHeapNotExists(pb, PROFILED);
137
138        // Fails with too small non-nmethod code heap size
139        pb = ProcessTools.createJavaProcessBuilder("-XX:NonNMethodCodeHeapSize=100K");
140        failsWith(pb, "Invalid NonNMethodCodeHeapSize");
141
142        // Fails if code heap sizes do not add up
143        pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
144                                                   "-XX:ReservedCodeCacheSize=10M",
145                                                   "-XX:NonNMethodCodeHeapSize=5M",
146                                                   "-XX:ProfiledCodeHeapSize=5M",
147                                                   "-XX:NonProfiledCodeHeapSize=5M");
148        failsWith(pb, "Invalid code heap sizes");
149
150        // Fails if not enough space for VM internal code
151        long minUseSpace = WHITE_BOX.getUintxVMFlag("CodeCacheMinimumUseSpace");
152        // minimum size: CodeCacheMinimumUseSpace DEBUG_ONLY(* 3)
153        long minSize = (Platform.isDebugBuild() ? 3 : 1) * minUseSpace;
154        pb = ProcessTools.createJavaProcessBuilder("-XX:+SegmentedCodeCache",
155                                                   "-XX:ReservedCodeCacheSize=" + minSize,
156                                                   "-XX:InitialCodeCacheSize=100K");
157        failsWith(pb, "Not enough space in non-nmethod code heap to run VM");
158    }
159}
160