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