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