TestHumongousCodeCacheRoots.java revision 11833:1cbffa2beba6
1/*
2 * Copyright (c) 2013, 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
26 * @key regression
27 * @key gc
28 * @bug 8027756
29 * @library /test/lib
30 * @modules java.base/jdk.internal.misc
31 *          java.management
32 * @build sun.hotspot.WhiteBox
33 * @run main ClassFileInstaller sun.hotspot.WhiteBox
34 *                              sun.hotspot.WhiteBox$WhiteBoxPermission
35 * @summary Humongous objects may have references from the code cache
36 * @run main TestHumongousCodeCacheRoots
37*/
38
39import jdk.test.lib.process.OutputAnalyzer;
40import jdk.test.lib.process.ProcessTools;
41import sun.hotspot.WhiteBox;
42
43import java.util.ArrayList;
44import java.util.Arrays;
45
46class TestHumongousCodeCacheRootsHelper {
47
48    static final int n = 1000000;
49    static final int[] AA = new int[n];
50    static final int[] BB = new int[n];
51
52    public static void main(String args[]) throws Exception {
53        // do some work so that the compiler compiles this method, inlining the
54        // reference to the integer array (which is a humonguous object) into
55        // the code cache.
56        for(int i = 0; i < n; i++) {
57            AA[i] = 0;
58            BB[i] = 0;
59        }
60        // trigger a GC that checks that the verification code allows humongous
61        // objects with code cache roots; objects should be all live here.
62        System.gc();
63
64        // deoptimize everyhing: this should make all compiled code zombies.
65        WhiteBox wb = WhiteBox.getWhiteBox();
66        wb.deoptimizeAll();
67
68        // trigger a GC that checks that the verification code allows humongous
69        // objects with code cache roots; objects should be all live here.
70        System.gc();
71
72        // wait a little for the code cache sweeper to try to clean up zombie nmethods
73        // and unregister the code roots.
74        try { Thread.sleep(5000); } catch (InterruptedException ex) { }
75
76        // do some work on the arrays to make sure that they need to be live after the GCs
77        for(int i = 0; i < n; i++) {
78            AA[i] = 1;
79            BB[i] = 10;
80        }
81
82        System.out.println();
83    }
84}
85
86public class TestHumongousCodeCacheRoots {
87
88  /**
89   * Executes a class in a new VM process with the given parameters.
90   * @param vmargs Arguments to the VM to run
91   * @param classname Name of the class to run
92   * @param arguments Arguments to the class
93   * @param useTestDotJavaDotOpts Use test.java.opts as part of the VM argument string
94   * @return The OutputAnalyzer with the results for the invocation.
95   */
96  public static OutputAnalyzer runWhiteBoxTest(String[] vmargs, String classname, String[] arguments, boolean useTestDotJavaDotOpts) throws Exception {
97    ArrayList<String> finalargs = new ArrayList<String>();
98
99    String[] whiteboxOpts = new String[] {
100      "-Xbootclasspath/a:.",
101      "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
102      "-cp", System.getProperty("java.class.path"),
103    };
104
105    if (useTestDotJavaDotOpts) {
106      // System.getProperty("test.java.opts") is '' if no options is set,
107      // we need to skip such a result
108      String[] externalVMOpts = new String[0];
109      if (System.getProperty("test.java.opts") != null && System.getProperty("test.java.opts").length() != 0) {
110        externalVMOpts = System.getProperty("test.java.opts").split(" ");
111      }
112      finalargs.addAll(Arrays.asList(externalVMOpts));
113    }
114
115    finalargs.addAll(Arrays.asList(vmargs));
116    finalargs.addAll(Arrays.asList(whiteboxOpts));
117    finalargs.add(classname);
118    finalargs.addAll(Arrays.asList(arguments));
119
120    ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));
121    OutputAnalyzer output = new OutputAnalyzer(pb.start());
122    try {
123        output.shouldHaveExitValue(0);
124    } catch (RuntimeException e) {
125        // It's ok if there is no client vm in the jdk.
126        if (output.firstMatch("Unrecognized option: -client") == null) {
127            throw e;
128        }
129    }
130
131    return output;
132  }
133
134  public static void runTest(String compiler, String[] other) throws Exception {
135    ArrayList<String> joined = new ArrayList<String>();
136    joined.add(compiler);
137    joined.addAll(Arrays.asList(other));
138    runWhiteBoxTest(joined.toArray(new String[0]), TestHumongousCodeCacheRootsHelper.class.getName(),
139      new String[] {}, false);
140  }
141
142  public static void main(String[] args) throws Exception {
143    final String[] baseArguments = new String[] {
144      "-XX:+UseG1GC", "-XX:G1HeapRegionSize=1M", "-Xmx100M", // make sure we get a humongous region
145      "-XX:+UnlockDiagnosticVMOptions",
146      "-XX:InitiatingHeapOccupancyPercent=1", // strong code root marking
147      "-XX:+G1VerifyHeapRegionCodeRoots", "-XX:+VerifyAfterGC", // make sure that verification is run
148    };
149    runTest("-client", baseArguments);
150    runTest("-server", baseArguments);
151  }
152}
153
154