1 /*
   2  * Copyright (c) 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  * @library /testlibrary
  27  */
  28 import java.util.ArrayList;
  29 import java.util.Collections;
  30 import com.oracle.java.testlibrary.*;
  31 
  32 public class UseCompressedOops {
  33 
  34     public static void main(String[] args) throws Exception {
  35 
  36         if (Platform.is64bit()) {
  37             // Explicitly turn of compressed oops
  38             testCompressedOops("-XX:-UseCompressedOops", "-Xmx32m")
  39                 .shouldNotContain("Compressed Oops")
  40                 .shouldHaveExitValue(0);
  41 
  42             // Compressed oops should be on by default
  43             testCompressedOops("-Xmx32m")
  44                 .shouldContain("Compressed Oops mode")
  45                 .shouldHaveExitValue(0);
  46 
  47             // Explicly enabling compressed oops
  48             testCompressedOops("-XX:+UseCompressedOops", "-Xmx32m")
  49                 .shouldContain("Compressed Oops mode")
  50                 .shouldHaveExitValue(0);
  51 
  52             // Larger than 4gb heap should result in zero based with shift 3
  53             testCompressedOops("-XX:+UseCompressedOops", "-Xmx5g")
  54                 .shouldContain("Zero based")
  55                 .shouldContain("Oop shift amount: 3")
  56                 .shouldHaveExitValue(0);
  57 
  58             // Skip the following three test cases if we're on OSX or Solaris Sparc.
  59             //
  60             // OSX doesn't seem to care about HeapBaseMinAddress and Solaris Sparc
  61             // puts the heap way up, forcing different behaviour. 
  62             
  63             if (!Platform.isOSX() && !(Platform.isSolaris() && Platform.isSparc())) {
  64                 // Small heap above 4gb should result in zero based with shift 3
  65                 testCompressedOops("-XX:+UseCompressedOops", "-Xmx32m", "-XX:HeapBaseMinAddress=4g")
  66                     .shouldContain("Zero based")
  67                     .shouldContain("Oop shift amount: 3")
  68                     .shouldHaveExitValue(0);
  69 
  70                 // Small heap above 32gb should result in non-zero based with shift 3
  71                 testCompressedOops("-XX:+UseCompressedOops", "-Xmx32m", "-XX:HeapBaseMinAddress=32g")
  72                     .shouldContain("Non-zero based")
  73                     .shouldContain("Oop shift amount: 3")
  74                     .shouldHaveExitValue(0);
  75                     
  76                 // 32gb heap with heap base above 64gb and object alignment set to 16 bytes should result 
  77                 // in non-zero based with shift 4
  78                 testCompressedOops("-XX:+UseCompressedOops", "-Xmx32g", "-XX:ObjectAlignmentInBytes=16", 
  79                                "-XX:HeapBaseMinAddress=64g")
  80                     .shouldContain("Non-zero based")
  81                     .shouldContain("Oop shift amount: 4")
  82                     .shouldHaveExitValue(0);
  83             }
  84             
  85             // Explicitly enabling compressed oops with 32gb heap should result a warning 
  86             testCompressedOops("-XX:+UseCompressedOops", "-Xmx32g")
  87                 .shouldContain("Max heap size too large for Compressed Oops")
  88                 .shouldHaveExitValue(0);
  89 
  90             // 32gb heap should not result a warning 
  91             testCompressedOops("-Xmx32g")
  92                 .shouldNotContain("Max heap size too large for Compressed Oops")
  93                 .shouldHaveExitValue(0);
  94 
  95             // Explicitly enabling compressed oops with 32gb heap and object 
  96             // alignment set to 8 byte should result a warning 
  97             testCompressedOops("-XX:+UseCompressedOops", "-Xmx32g", "-XX:ObjectAlignmentInBytes=8")
  98                 .shouldContain("Max heap size too large for Compressed Oops")
  99                 .shouldHaveExitValue(0);
 100 
 101             // 64gb heap and object alignment set to 16 bytes should result in a warning
 102             testCompressedOops("-XX:+UseCompressedOops", "-Xmx64g", "-XX:ObjectAlignmentInBytes=16")
 103                 .shouldContain("Max heap size too large for Compressed Oops")
 104                 .shouldHaveExitValue(0);
 105 
 106             // 32gb heap with object alignment set to 16 bytes should result in zero based with shift 4
 107             testCompressedOops("-XX:+UseCompressedOops", "-Xmx32g", "-XX:ObjectAlignmentInBytes=16")
 108                 .shouldContain("Zero based")
 109                 .shouldContain("Oop shift amount: 4")
 110                 .shouldHaveExitValue(0);
 111 
 112         } else {
 113             // Compressed oops should only apply to 64bit platforms
 114             testCompressedOops("-XX:+UseCompressedOops", "-Xmx32m")
 115                 .shouldContain("Unrecognized VM option 'UseCompressedOops'")
 116                 .shouldHaveExitValue(1);
 117         }
 118     }
 119 
 120     private static OutputAnalyzer testCompressedOops(String... flags) throws Exception {
 121         ArrayList<String> args = new ArrayList<>();
 122 
 123         // Always run with these three:
 124         args.add("-XX:+UnlockDiagnosticVMOptions");
 125         args.add("-XX:+PrintCompressedOopsMode");
 126         args.add("-Xms32m");
 127 
 128         // Add the extra flags
 129         Collections.addAll(args, flags);
 130 
 131         args.add("-version");
 132 
 133         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args.toArray(new String[0]));
 134         return new OutputAnalyzer(pb.start());
 135     }
 136 }