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 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:CompressedClassSpaceBaseAddress=" + forceAddress,
 55                 "-XX:CompressedClassSpaceSize=" + classSpaceSize,
 56                 "-Xmx128m",
 57                 "-Xlog:metaspace*",
 58                 "-version");
 59         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 60 
 61         output.reportDiagnosticSummary();
 62 
 63         // We ignore cases where we were not able to map at the force address
 64         if (output.contains("reserving class space failed")) {
 65             System.out.println("Skipping because we cannot force ccs to " + forceAddressString);
 66             return;
 67         }
 68 
 69         output.shouldHaveExitValue(0);
 70         output.shouldContain("Narrow klass base: " + expectedEncodingBaseString + ", Narrow klass shift: " + expectedEncodingShift);
 71     }
 72 
 73     final static long K = 1024;
 74     final static long M = K * 1024;
 75     final static long G = M * 1024;
 76     public static void main(String[] args) throws Exception {
 77         // Test ccs nestling right at the end of the 4G range
 78         // Expecting base=0, shift=0
 79         test(4 * G - 128 * M, false, 128 * M, 0, 0);
 80 
 81         // Test ccs nestling right at the end of the 32G range
 82         // Expecting:
 83         // - non-aarch64: base=0, shift=3
 84         // - aarch64: base to start of class range, shift 0
 85         if (Platform.isAArch64()) {
 86             // The best we can do on aarch64 is to be *near* the end of the 32g range, since a valid encoding base
 87             // on aarch64 must be 4G aligned, and the max. class space size is 3G.
 88             long forceAddress = 0x7_0000_0000L; // 28g, and also a valid EOR immediate
 89             test(forceAddress, false, 3 * G, forceAddress, 0);
 90         } else {
 91             test(32 * G - 128 * M, false, 128 * M, 0, 3);
 92         }
 93 
 94         // Test ccs starting *below* 4G, but extending upwards beyond 4G. All platforms except aarch64 should pick
 95         // zero based encoding.
 96         if (Platform.isAArch64()) {
 97             long forceAddress = 0xC000_0000L; // make sure we have a valid EOR immediate
 98             test(forceAddress, false, G + (128 * M), forceAddress, 0);
 99         } else {
100             test(4 * G - 128 * M, false, 2 * 128 * M, 0, 3);
101         }
102         // add more...
103 
104         // Compact Object Header Mode with tiny classpointers
105         // On all platforms we expect the VM to chose the smallest possible shift value needed to cover the encoding range
106         long forceAddress = 30 * G;
107 
108         long ccsSize = 128 * M;
109         int expectedShift = 6;
110         test(forceAddress, true, ccsSize, forceAddress, expectedShift);
111 
112         ccsSize = 512 * M;
113         expectedShift = 8;
114         test(forceAddress, true, ccsSize, forceAddress, expectedShift);
115 
116         ccsSize = G;
117         expectedShift = 9;
118         test(forceAddress, true, ccsSize, forceAddress, expectedShift);
119 
120         ccsSize = 3 * G;
121         expectedShift = 10;
122         test(forceAddress, true, ccsSize, forceAddress, expectedShift);
123     }
124 }