1 /*
  2  * Copyright (c) 2013, 2021, 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  * @bug 8024927
 27  * @summary Testing address of compressed class pointer space as best as possible.
 28  * @requires vm.bits == 64 & !vm.graal.enabled
 29  * @requires vm.flagless
 30  * @library /test/lib
 31  * @modules java.base/jdk.internal.misc
 32  *          java.management
 33  * @run driver CompressedClassPointers
 34  */
 35 
 36 import jdk.test.lib.Platform;
 37 import jdk.test.lib.process.ProcessTools;
 38 import jdk.test.lib.process.OutputAnalyzer;
 39 import jtreg.SkippedException;
 40 
 41 public class CompressedClassPointers {
 42 
 43     static final String logging_option = "-Xlog:gc+metaspace=trace,cds=trace";
 44 
 45     // Returns true if we are to test the narrow klass base; we only do this on
 46     // platforms where we can be reasonably shure that we get reproducable placement).
 47     static boolean testNarrowKlassBase() {
 48         if (Platform.isWindows()) {
 49             return false;
 50         }
 51         return true;
 52 
 53     }
 54 
 55     // CDS off, small heap, ccs size default (1G)
 56     // A small heap should allow us to place the ccs within the lower 32G and thus allow zero based encoding.
 57     /* Lilliput: cannot work due to drastically reduced narrow klass pointer range (atm 2g and that may get
 58        smaller still). There is an argument for improving CDS/CCS reservation and make it more likely to run
 59        zero-based, but that logic has to be rethought.
 60     public static void smallHeapTest() throws Exception {
 61         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 62             "-XX:+UnlockDiagnosticVMOptions",
 63             "-XX:SharedBaseAddress=8g",
 64             "-Xmx128m",
 65             logging_option,
 66             "-Xshare:off",
 67             "-XX:+VerifyBeforeGC", "-version");
 68         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 69         if (testNarrowKlassBase()) {
 70             output.shouldContain("Narrow klass base: 0x0000000000000000");
 71         }
 72         output.shouldHaveExitValue(0);
 73     }
 74      */
 75 
 76     // CDS off, small heap, ccs size explicitely set to 1G
 77     // A small heap should allow us to place the ccs within the lower 32G and thus allow zero based encoding.
 78     /* Lilliput: See comment above.
 79     public static void smallHeapTestWith1G() throws Exception {
 80         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 81             "-XX:+UnlockDiagnosticVMOptions",
 82             "-XX:CompressedClassSpaceSize=1g",
 83             "-Xmx128m",
 84             logging_option,
 85             "-Xshare:off",
 86             "-XX:+VerifyBeforeGC", "-version");
 87         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 88         if (testNarrowKlassBase()) {
 89             output.shouldContain("Narrow klass base: 0x0000000000000000, Narrow klass shift: 3");
 90         }
 91         output.shouldHaveExitValue(0);
 92     }
 93     */
 94 
 95     // CDS off, a very large heap, ccs size left to 1G default.
 96     // We expect the ccs to be mapped somewhere far beyond the heap, such that it is not possible
 97     // to use zero based encoding.
 98     /* Lilliput: I am not sure what the point of this test CCS reservation is independent from
 99        heap. See below the desparate attempts to predict heap reservation on PPC. Why do we even care?
100     public static void largeHeapTest() throws Exception {
101         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
102             "-XX:+UnlockDiagnosticVMOptions",
103             "-XX:+UnlockExperimentalVMOptions",
104             "-Xmx30g",
105             logging_option,
106             "-Xshare:off",
107             "-XX:+VerifyBeforeGC", "-version");
108         OutputAnalyzer output = new OutputAnalyzer(pb.start());
109         if (testNarrowKlassBase() && !Platform.isPPC() && !Platform.isOSX()) {
110             // PPC: in most cases the heap cannot be placed below 32g so there
111             // is room for ccs and narrow klass base will be 0x0. Exception:
112             // Linux 4.1.42 or earlier (see ELF_ET_DYN_BASE in JDK-8244847).
113             // For simplicity we exclude PPC.
114             // OSX: similar.
115             output.shouldNotContain("Narrow klass base: 0x0000000000000000");
116             output.shouldContain("Narrow klass shift: 0");
117         }
118         output.shouldHaveExitValue(0);
119     }
120      */
121 
122     // Settings as in largeHeapTest() except for max heap size. We make max heap
123     // size even larger such that it cannot fit into lower 32G but not too large
124     // for compressed oops.
125     // We expect a zerobased ccs.
126     /* Lilliput: narrow klass pointer range drastically reduced. See comments under smallHeapTest().
127     public static void largeHeapAbove32GTest() throws Exception {
128         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
129             "-XX:+UnlockDiagnosticVMOptions",
130             "-XX:+UnlockExperimentalVMOptions",
131             "-Xmx31g",
132             logging_option,
133             "-Xshare:off",
134             "-XX:+VerifyBeforeGC", "-version");
135         OutputAnalyzer output = new OutputAnalyzer(pb.start());
136         if (testNarrowKlassBase()) {
137             if (!(Platform.isAArch64() && Platform.isOSX())) { // see JDK-8262895
138                 output.shouldContain("Narrow klass base: 0x0000000000000000");
139                 if (!Platform.isAArch64() && !Platform.isOSX()) {
140                     output.shouldContain("Narrow klass shift: 0");
141                 }
142             }
143         }
144         output.shouldHaveExitValue(0);
145     }
146     */
147 
148     // Using large paged heap, metaspace uses small pages.
149     /* Lilliput: not sure what the point of this test is. The ability to have a class space if heap uses
150        large pages? Why would that be a problem? Kept alive for now since it makes no problems even with
151        smaller class pointers.
152      */
153     public static void largePagesForHeapTest() throws Exception {
154         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
155                 "-XX:+UnlockDiagnosticVMOptions",
156                 "-Xmx128m",
157                 "-XX:+UseLargePages",
158                 logging_option,
159                 "-XX:+VerifyBeforeGC", "-version");
160         OutputAnalyzer output = new OutputAnalyzer(pb.start());
161         if (testNarrowKlassBase()) {
162             output.shouldContain("Narrow klass base:");
163         }
164         output.shouldHaveExitValue(0);
165     }
166 
167     public static void heapBaseMinAddressTest() throws Exception {
168         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
169             "-XX:HeapBaseMinAddress=1m",
170             "-Xlog:gc+heap+coops=debug",
171             "-version");
172         OutputAnalyzer output = new OutputAnalyzer(pb.start());
173         output.shouldContain("HeapBaseMinAddress must be at least");
174         output.shouldHaveExitValue(0);
175     }
176 
177     public static void sharingTest() throws Exception {
178         // Test small heaps
179         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
180             "-XX:+UnlockDiagnosticVMOptions",
181             "-XX:SharedArchiveFile=./CompressedClassPointers.jsa",
182             "-Xmx128m",
183             "-XX:SharedBaseAddress=8g",
184             "-XX:+VerifyBeforeGC",
185             "-Xshare:dump",
186             "-Xlog:cds,gc+heap+coops=debug");
187         OutputAnalyzer output = new OutputAnalyzer(pb.start());
188         if (output.firstMatch("Shared spaces are not supported in this VM") != null) {
189             return;
190         }
191         try {
192           output.shouldContain("Loading classes to share");
193           output.shouldHaveExitValue(0);
194 
195           pb = ProcessTools.createJavaProcessBuilder(
196             "-XX:+UnlockDiagnosticVMOptions",
197             "-XX:SharedArchiveFile=./CompressedClassPointers.jsa",
198             "-Xmx128m",
199             "-XX:SharedBaseAddress=8g",
200             "-Xlog:gc+heap+coops=debug",
201             "-Xshare:on",
202             "-version");
203           output = new OutputAnalyzer(pb.start());
204           output.shouldContain("sharing");
205           output.shouldHaveExitValue(0);
206 
207         } catch (RuntimeException e) {
208           output.shouldContain("Unable to use shared archive");
209           output.shouldHaveExitValue(1);
210         }
211     }
212 
213     /* Lilliput: narrow klass pointer range drastically reduced. See comments under smallHeapTest().
214     public static void smallHeapTestNoCoop() throws Exception {
215         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
216             "-XX:-UseCompressedOops",
217             "-XX:+UseCompressedClassPointers",
218             "-XX:+UnlockDiagnosticVMOptions",
219             "-XX:SharedBaseAddress=8g",
220             "-Xmx128m",
221             "-Xlog:gc+metaspace=trace",
222             "-Xshare:off",
223             "-Xlog:cds=trace",
224             "-XX:+VerifyBeforeGC", "-version");
225         OutputAnalyzer output = new OutputAnalyzer(pb.start());
226         output.shouldContain("Narrow klass base: 0x0000000000000000");
227         output.shouldHaveExitValue(0);
228     }
229     */
230 
231     /* Lilliput: narrow klass pointer range drastically reduced. See comments under smallHeapTest().
232     public static void smallHeapTestWith1GNoCoop() throws Exception {
233         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
234             "-XX:-UseCompressedOops",
235             "-XX:+UseCompressedClassPointers",
236             "-XX:+UnlockDiagnosticVMOptions",
237             "-XX:CompressedClassSpaceSize=1g",
238             "-Xmx128m",
239             "-Xlog:gc+metaspace=trace",
240             "-Xshare:off",
241             "-Xlog:cds=trace",
242             "-XX:+VerifyBeforeGC", "-version");
243         OutputAnalyzer output = new OutputAnalyzer(pb.start());
244         output.shouldContain("Narrow klass base: 0x0000000000000000");
245         if (!Platform.isAArch64() && !Platform.isPPC()) {
246             // Currently relax this test for Aarch64 and ppc.
247             output.shouldContain("Narrow klass shift: 0");
248         }
249         output.shouldHaveExitValue(0);
250     }
251     */
252 
253     /* Lilliput: narrow klass pointer range drastically reduced. See comments under smallHeapTest().
254     public static void largeHeapTestNoCoop() throws Exception {
255         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
256             "-XX:-UseCompressedOops",
257             "-XX:+UseCompressedClassPointers",
258             "-XX:+UnlockDiagnosticVMOptions",
259             "-XX:+UnlockExperimentalVMOptions",
260             "-Xmx30g",
261             "-Xlog:gc+metaspace=trace",
262             "-Xshare:off",
263             "-Xlog:cds=trace",
264             "-XX:+VerifyBeforeGC", "-version");
265         OutputAnalyzer output = new OutputAnalyzer(pb.start());
266         output.shouldContain("Narrow klass base: 0x0000000000000000");
267         if (!Platform.isAArch64() && !Platform.isPPC()) {
268             // Currently relax this test for Aarch64 and ppc.
269             output.shouldContain("Narrow klass shift: 0");
270         }
271         output.shouldHaveExitValue(0);
272     }
273     */
274 
275     public static void largePagesTestNoCoop() throws Exception {
276         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
277             "-XX:-UseCompressedOops",
278             "-XX:+UseCompressedClassPointers",
279             "-XX:+UnlockDiagnosticVMOptions",
280             "-Xmx128m",
281             "-XX:+UseLargePages",
282             "-Xlog:gc+metaspace=trace",
283             "-XX:+VerifyBeforeGC", "-version");
284         OutputAnalyzer output = new OutputAnalyzer(pb.start());
285         output.shouldContain("Narrow klass base:");
286         output.shouldHaveExitValue(0);
287     }
288 
289     public static void heapBaseMinAddressTestNoCoop() throws Exception {
290         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
291             "-XX:-UseCompressedOops",
292             "-XX:+UseCompressedClassPointers",
293             "-XX:HeapBaseMinAddress=1m",
294             "-Xlog:gc+heap+coops=debug",
295             "-version");
296         OutputAnalyzer output = new OutputAnalyzer(pb.start());
297         output.shouldContain("HeapBaseMinAddress must be at least");
298         output.shouldHaveExitValue(0);
299     }
300 
301     public static void sharingTestNoCoop() throws Exception {
302         // Test small heaps
303         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
304             "-XX:-UseCompressedOops",
305             "-XX:+UseCompressedClassPointers",
306             "-XX:+UnlockDiagnosticVMOptions",
307             "-XX:SharedArchiveFile=./CompressedClassPointers.jsa",
308             "-Xmx128m",
309             "-XX:SharedBaseAddress=8g",
310             "-XX:+VerifyBeforeGC",
311             "-Xshare:dump",
312             "-Xlog:cds,gc+heap+coops=debug");
313         OutputAnalyzer output = new OutputAnalyzer(pb.start());
314         if (output.firstMatch("Shared spaces are not supported in this VM") != null) {
315             return;
316         }
317         try {
318           output.shouldContain("Loading classes to share");
319           output.shouldHaveExitValue(0);
320 
321           pb = ProcessTools.createJavaProcessBuilder(
322             "-XX:-UseCompressedOops",
323             "-XX:+UseCompressedClassPointers",
324             "-XX:+UnlockDiagnosticVMOptions",
325             "-XX:SharedArchiveFile=./CompressedClassPointers.jsa",
326             "-Xmx128m",
327             "-XX:SharedBaseAddress=8g",
328             "-Xlog:gc+heap+coops=debug",
329             "-Xshare:on",
330             "-version");
331           output = new OutputAnalyzer(pb.start());
332           output.shouldContain("sharing");
333           output.shouldHaveExitValue(0);
334 
335         } catch (RuntimeException e) {
336           output.shouldContain("Unable to use shared archive");
337           output.shouldHaveExitValue(1);
338         }
339     }
340 
341     public static void main(String[] args) throws Exception {
342         // smallHeapTest();
343         // smallHeapTestWith1G();
344         // largeHeapTest();
345         // largeHeapAbove32GTest();
346         largePagesForHeapTest();
347         heapBaseMinAddressTest();
348         sharingTest();
349 
350         if (!Platform.isOSX()) {
351             // Testing compressed class pointers without compressed oops.
352             // This is only possible if the platform supports it. Notably,
353             // on macOS, when compressed oops is disabled and the heap is
354             // given an arbitrary address, that address occasionally collides
355             // with where we would ideally have placed the compressed class
356             // space. Therefore, macOS is omitted for now.
357             // smallHeapTestNoCoop();
358             // smallHeapTestWith1GNoCoop();
359             // largeHeapTestNoCoop();
360             largePagesTestNoCoop();
361             heapBaseMinAddressTestNoCoop();
362             sharingTestNoCoop();
363         }
364     }
365 }
--- EOF ---