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