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                 // Small heap above 4gb should result in zero based with shift 3
  67                 testCompressedOops("-XX:+UseCompressedOops", "-Xmx32m", "-XX:HeapBaseMinAddress=4g")
  68                     .shouldContain("Zero based")
  69                     .shouldContain("Oop shift amount: 3")
  70                     .shouldHaveExitValue(0);
  71 
  72                 // Small heap above 32gb should result in non-zero based with shift 3
  73                 testCompressedOops("-XX:+UseCompressedOops", "-Xmx32m", "-XX:HeapBaseMinAddress=32g")
  74                     .shouldContain("Non-zero based")
  75                     .shouldContain("Oop shift amount: 3")
  76                     .shouldHaveExitValue(0);
  77 
  78                 // 32gb heap with heap base above 64gb and object alignment set to 16 bytes should result
  79                 // in non-zero based with shift 4
  80                 testCompressedOops("-XX:+UseCompressedOops", "-Xmx32g", "-XX:ObjectAlignmentInBytes=16",
  81                                "-XX:HeapBaseMinAddress=64g")
  82                     .shouldContain("Non-zero based")
  83                     .shouldContain("Oop shift amount: 4")
  84                     .shouldHaveExitValue(0);
  85 
  86                 // 32gb heap with object alignment set to 16 bytes should result in zero based with shift 4
  87                 testCompressedOops("-XX:+UseCompressedOops", "-Xmx32g", "-XX:ObjectAlignmentInBytes=16")
  88                     .shouldContain("Zero based")
  89                     .shouldContain("Oop shift amount: 4")
  90                     .shouldHaveExitValue(0);
  91             }
  92 
  93             // Explicitly enabling compressed oops with 32gb heap should result a warning
  94             testCompressedOops("-XX:+UseCompressedOops", "-Xmx32g")
  95                 .shouldContain("Max heap size too large for Compressed Oops")
  96                 .shouldHaveExitValue(0);
  97 
  98             // 32gb heap should not result a warning
  99             testCompressedOops("-Xmx32g")
 100                 .shouldNotContain("Max heap size too large for Compressed Oops")
 101                 .shouldHaveExitValue(0);
 102 
 103             // Explicitly enabling compressed oops with 32gb heap and object
 104             // alignment set to 8 byte should result a warning
 105             testCompressedOops("-XX:+UseCompressedOops", "-Xmx32g", "-XX:ObjectAlignmentInBytes=8")
 106                 .shouldContain("Max heap size too large for Compressed Oops")
 107                 .shouldHaveExitValue(0);
 108 
 109             // 64gb heap and object alignment set to 16 bytes should result in a warning
 110             testCompressedOops("-XX:+UseCompressedOops", "-Xmx64g", "-XX:ObjectAlignmentInBytes=16")
 111                 .shouldContain("Max heap size too large for Compressed Oops")
 112                 .shouldHaveExitValue(0);
 113 
 114         } else {
 115             // Compressed oops should only apply to 64bit platforms
 116             testCompressedOops("-XX:+UseCompressedOops", "-Xmx32m")
 117                 .shouldContain("Unrecognized VM option 'UseCompressedOops'")
 118                 .shouldHaveExitValue(1);
 119         }
 120     }
 121 
 122     private static OutputAnalyzer testCompressedOops(String... flags) throws Exception {
 123         ArrayList<String> args = new ArrayList<>();
 124 
 125         // Always run with these three:
 126         args.add("-XX:+UnlockDiagnosticVMOptions");
 127         args.add("-XX:+PrintCompressedOopsMode");
 128         args.add("-Xms32m");
 129 
 130         // Add the extra flags
 131         Collections.addAll(args, flags);
 132 
 133         args.add("-version");
 134 
 135         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args.toArray(new String[0]));
 136         return new OutputAnalyzer(pb.start());
 137     }
 138 }