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