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