1 /*
  2  * Copyright (c) 2013, 2023, 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  * @summary Test the various CPU-specific reservation schemes
 27  * @requires vm.bits == 64 & !vm.graal.enabled & vm.debug == true
 28  * @requires vm.flagless
 29  * @requires (os.family != "windows") & (os.family != "aix")
 30  * @library /test/lib
 31  * @modules java.base/jdk.internal.misc
 32  *          java.management
 33  * @run driver CompressedCPUSpecificClassSpaceReservation
 34  */
 35 
 36 import jdk.test.lib.Platform;
 37 import jdk.test.lib.process.OutputAnalyzer;
 38 import jdk.test.lib.process.ProcessTools;
 39 import jtreg.SkippedException;
 40 
 41 import java.io.IOException;
 42 
 43 public class CompressedCPUSpecificClassSpaceReservation {
 44     // Note: windows: On windows, we currently have the issue that os::reserve_memory_aligned relies on
 45     // os::attempt_reserve_memory_at because VirtualAlloc cannot be unmapped in parts; this precludes use of
 46     // +SimulateFullAddressSpace (VM won't be able to reserve heap). Therefore we exclude the test for windows
 47     // for now.
 48 
 49     private static void do_test(boolean CDS) throws IOException {
 50         // We start the VM with -XX:+SimulateFullAdressSpace, which means the JVM will go through all motions
 51         // of reserving the cds+class space, but never succeed. That means we see every single allocation attempt.
 52         // We start with -Xlog options enabled. The expected output goes like this:
 53         // [0.017s][debug][os,map] reserve_between (range [0x0000000000000000-0x0000000100000000), size 0x41000000, alignment 0x1000000, randomize: 1)
 54         ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(
 55                 "-Xshare:" + (CDS ? "on" : "off"),
 56                 "-Xmx128m",
 57                 "-XX:CompressedClassSpaceSize=128m",
 58                 "-Xlog:metaspace*", "-Xlog:metaspace+map=trace", "-Xlog:os+map=trace",
 59                 "-XX:+SimulateFullAddressSpace", // So that no resevation attempt will succeed
 60                 "-version");
 61         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 62 
 63         final String tryReserveForUnscaled = "reserve_between (range [0x0000000000000000-0x0000000100000000)";
 64         final String tryReserveForZeroBased = "reserve_between (range [0x0000000100000000-0x0000000800000000)";
 65         final String tryReserveFor16bitMoveIntoQ3 = "reserve_between (range [0x0000000100000000-0x0001000000000000)";
 66         if (Platform.isAArch64()) {
 67             if (CDS) {
 68                 output.shouldNotContain(tryReserveForUnscaled);
 69             } else {
 70                 output.shouldContain(tryReserveForUnscaled);
 71             }
 72             output.shouldContain("Trying to reserve at an EOR-compatible address");
 73             output.shouldNotContain(tryReserveForZeroBased);
 74             output.shouldContain(tryReserveFor16bitMoveIntoQ3);
 75         } else if (Platform.isPPC()) {
 76             if (CDS) {
 77                 output.shouldNotContain(tryReserveForUnscaled);
 78                 output.shouldNotContain(tryReserveForZeroBased);
 79             } else {
 80                 output.shouldContain(tryReserveForUnscaled);
 81                 output.shouldContain(tryReserveForZeroBased);
 82             }
 83             output.shouldContain(tryReserveFor16bitMoveIntoQ3);
 84         } else if (Platform.isRISCV64()) {
 85             output.shouldContain(tryReserveForUnscaled); // unconditionally
 86             if (CDS) {
 87                 output.shouldNotContain(tryReserveForZeroBased);
 88                 // bits 32..44
 89                 output.shouldContain("reserve_between (range [0x0000000100000000-0x0000100000000000)");
 90             } else {
 91                 output.shouldContain(tryReserveForZeroBased);
 92                 // bits 32..44, but not lower than zero-based limit
 93                 output.shouldContain("reserve_between (range [0x0000000800000000-0x0000100000000000)");
 94             }
 95             // bits 44..64
 96             output.shouldContain("reserve_between (range [0x0000100000000000-0xffffffffffffffff)");
 97         } else if (Platform.isS390x()) {
 98             output.shouldContain(tryReserveForUnscaled); // unconditionally
 99             if (CDS) {
100                 output.shouldNotContain(tryReserveForZeroBased);
101             } else {
102                 output.shouldContain(tryReserveForZeroBased);
103             }
104             output.shouldContain(tryReserveFor16bitMoveIntoQ3);
105         } else if (Platform.isX64()) {
106             if (CDS) {
107                 output.shouldNotContain(tryReserveForUnscaled);
108                 output.shouldNotContain(tryReserveForZeroBased);
109             } else {
110                 output.shouldContain(tryReserveForUnscaled);
111                 output.shouldContain(tryReserveForZeroBased);
112             }
113         } else {
114             throw new RuntimeException("Unexpected platform");
115         }
116 
117         // In all cases we should have managed to map successfully eventually
118         if (CDS) {
119             output.shouldContain("CDS archive(s) mapped at:");
120         } else {
121             output.shouldContain("CDS archive(s) not mapped");
122         }
123         output.shouldContain("Compressed class space mapped at:");
124     }
125 
126     public static void main(String[] args) throws Exception {
127         System.out.println("Test with CDS");
128         do_test(true);
129         System.out.println("Test without CDS");
130         do_test(false);
131     }
132 }