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