1 /*
   2  * Copyright (c) 2013, 2019, 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.opt.final.UseCompressedOops == true & os.family != "windows"
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  * @run main CompressedClassPointers
  33  */
  34 
  35 import jdk.test.lib.Platform;
  36 import jdk.test.lib.process.ProcessTools;
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 import jtreg.SkippedException;
  39 
  40 public class CompressedClassPointers {
  41 
  42     public static void smallHeapTest() throws Exception {
  43         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  44             "-XX:+UnlockDiagnosticVMOptions",
  45             "-XX:SharedBaseAddress=8g",
  46             "-Xmx128m",
  47             "-Xlog:gc+metaspace=trace",
  48             "-Xshare:off",
  49             "-Xlog:cds=trace",
  50             "-XX:+VerifyBeforeGC", "-version");
  51         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  52         output.shouldContain("Narrow klass base: 0x0000000000000000");
  53         output.shouldHaveExitValue(0);
  54     }
  55 
  56     public static void smallHeapTestWith1G() throws Exception {
  57         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  58             "-XX:+UnlockDiagnosticVMOptions",
  59             "-XX:CompressedClassSpaceSize=1g",
  60             "-Xmx128m",
  61             "-Xlog:gc+metaspace=trace",
  62             "-Xshare:off",
  63             "-Xlog:cds=trace",
  64             "-XX:+VerifyBeforeGC", "-version");
  65         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  66         output.shouldContain("Narrow klass base: 0x0000000000000000, Narrow klass shift: 3");
  67         output.shouldHaveExitValue(0);
  68     }
  69 
  70     public static void largeHeapTest() throws Exception {
  71         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  72             "-XX:+UnlockDiagnosticVMOptions",
  73             "-XX:+UnlockExperimentalVMOptions",
  74             "-Xmx30g",
  75             "-XX:-UseAOT", // AOT explicitly set klass shift to 3.
  76             "-Xlog:gc+metaspace=trace",
  77             "-Xshare:off",
  78             "-Xlog:cds=trace",
  79             "-XX:+VerifyBeforeGC", "-version");
  80         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  81         output.shouldNotContain("Narrow klass base: 0x0000000000000000");
  82         output.shouldContain("Narrow klass shift: 0");
  83         output.shouldHaveExitValue(0);
  84     }
  85 
  86     public static void largePagesTest() throws Exception {
  87         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  88             "-XX:+UnlockDiagnosticVMOptions",
  89             "-Xmx128m",
  90             "-XX:+UseLargePages",
  91             "-Xlog:gc+metaspace=trace",
  92             "-XX:+VerifyBeforeGC", "-version");
  93         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  94         output.shouldContain("Narrow klass base:");
  95         output.shouldHaveExitValue(0);
  96     }
  97 
  98     public static void heapBaseMinAddressTest() throws Exception {
  99         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 100             "-XX:HeapBaseMinAddress=1m",
 101             "-Xlog:gc+heap+coops=debug",
 102             "-version");
 103         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 104         output.shouldContain("HeapBaseMinAddress must be at least");
 105         output.shouldHaveExitValue(0);
 106     }
 107 
 108     public static void sharingTest() throws Exception {
 109         // Test small heaps
 110         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 111             "-XX:+UnlockDiagnosticVMOptions",
 112             "-XX:SharedArchiveFile=./CompressedClassPointers.jsa",
 113             "-Xmx128m",
 114             "-XX:SharedBaseAddress=8g",
 115             "-XX:+PrintCompressedOopsMode",
 116             "-XX:+VerifyBeforeGC",
 117             "-Xshare:dump", "-Xlog:cds");
 118         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 119         if (output.firstMatch("Shared spaces are not supported in this VM") != null) {
 120             return;
 121         }
 122         try {
 123           output.shouldContain("Loading classes to share");
 124           output.shouldHaveExitValue(0);
 125 
 126           pb = ProcessTools.createJavaProcessBuilder(
 127             "-XX:+UnlockDiagnosticVMOptions",
 128             "-XX:SharedArchiveFile=./CompressedClassPointers.jsa",
 129             "-Xmx128m",
 130             "-XX:SharedBaseAddress=8g",
 131             "-XX:+PrintCompressedOopsMode",
 132             "-Xshare:on",
 133             "-version");
 134           output = new OutputAnalyzer(pb.start());
 135           output.shouldContain("sharing");
 136           output.shouldHaveExitValue(0);
 137 
 138         } catch (RuntimeException e) {
 139           output.shouldContain("Unable to use shared archive");
 140           output.shouldHaveExitValue(1);
 141         }
 142     }
 143 
 144     public static void main(String[] args) throws Exception {
 145         if (Platform.isSolaris()) {
 146              String name = System.getProperty("os.version");
 147              if (name.equals("5.10")) {
 148                 throw new SkippedException("Solaris 10 can't mmap compressed oops space without a base");
 149              }
 150         }
 151         smallHeapTest();
 152         smallHeapTestWith1G();
 153         largeHeapTest();
 154         largePagesTest();
 155         heapBaseMinAddressTest();
 156         sharingTest();
 157     }
 158 }