1 /* 2 * Copyright (c) 2013, 2024, 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 Testing that, faced with a given (possibly odd) mapping address of class space, the encoding 27 * scheme fits the address 28 * @requires vm.bits == 64 & !vm.graal.enabled & vm.debug == true 29 * @requires vm.flagless 30 * @library /test/lib 31 * @modules java.base/jdk.internal.misc 32 * java.management 33 * @run driver CompressedClassPointersEncodingScheme 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 CompressedClassPointersEncodingScheme { 44 45 private static void test(long forceAddress, boolean COH, long classSpaceSize, long expectedEncodingBase, int expectedEncodingShift) throws IOException { 46 String forceAddressString = String.format("0x%016X", forceAddress).toLowerCase(); 47 String expectedEncodingBaseString = String.format("0x%016X", expectedEncodingBase).toLowerCase(); 48 ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder( 49 "-Xshare:off", // to make CompressedClassSpaceBaseAddress work 50 "-XX:+UnlockDiagnosticVMOptions", 51 "-XX:-UseCompressedOops", // keep VM from optimizing heap location 52 "-XX:+UnlockExperimentalVMOptions", 53 "-XX:" + (COH ? "+" : "-") + "UseCompactObjectHeaders", 54 "-XX:" + (COH ? "+" : "-") + "UseObjectMonitorTable", 55 "-XX:CompressedClassSpaceBaseAddress=" + forceAddress, 56 "-XX:CompressedClassSpaceSize=" + classSpaceSize, 57 "-Xmx128m", 58 "-Xlog:metaspace*", 59 "-version"); 60 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 61 62 output.reportDiagnosticSummary(); 63 64 // We ignore cases where we were not able to map at the force address 65 if (output.contains("reserving class space failed")) { 66 System.out.println("Skipping because we cannot force ccs to " + forceAddressString); 67 return; 68 } 69 70 output.shouldHaveExitValue(0); 71 output.shouldContain("Narrow klass base: " + expectedEncodingBaseString + ", Narrow klass shift: " + expectedEncodingShift); 72 } 73 74 final static long K = 1024; 75 final static long M = K * 1024; 76 final static long G = M * 1024; 77 public static void main(String[] args) throws Exception { 78 // Test ccs nestling right at the end of the 4G range 79 // Expecting base=0, shift=0 80 test(4 * G - 128 * M, false, 128 * M, 0, 0); 81 82 // Test ccs nestling right at the end of the 32G range 83 // Expecting: 84 // - non-aarch64: base=0, shift=3 85 // - aarch64: base to start of class range, shift 0 86 if (Platform.isAArch64()) { 87 // The best we can do on aarch64 is to be *near* the end of the 32g range, since a valid encoding base 88 // on aarch64 must be 4G aligned, and the max. class space size is 3G. 89 long forceAddress = 0x7_0000_0000L; // 28g, and also a valid EOR immediate 90 test(forceAddress, false, 3 * G, forceAddress, 0); 91 } else { 92 test(32 * G - 128 * M, false, 128 * M, 0, 3); 93 } 94 95 // Test ccs starting *below* 4G, but extending upwards beyond 4G. All platforms except aarch64 should pick 96 // zero based encoding. 97 if (Platform.isAArch64()) { 98 long forceAddress = 0xC000_0000L; // make sure we have a valid EOR immediate 99 test(forceAddress, false, G + (128 * M), forceAddress, 0); 100 } else { 101 test(4 * G - 128 * M, false, 2 * 128 * M, 0, 3); 102 } 103 // add more... 104 105 // Compact Object Header Mode with tiny classpointers 106 // On all platforms we expect the VM to chose the smallest possible shift value needed to cover the encoding range 107 long forceAddress = 30 * G; 108 109 long ccsSize = 128 * M; 110 int expectedShift = 6; 111 test(forceAddress, true, ccsSize, forceAddress, expectedShift); 112 113 ccsSize = 512 * M; 114 expectedShift = 8; 115 test(forceAddress, true, ccsSize, forceAddress, expectedShift); 116 117 ccsSize = G; 118 expectedShift = 9; 119 test(forceAddress, true, ccsSize, forceAddress, expectedShift); 120 121 ccsSize = 3 * G; 122 expectedShift = 10; 123 test(forceAddress, true, ccsSize, forceAddress, expectedShift); 124 } 125 }