1 /*
   2  * Copyright (c) 2014, 2018, 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 /test/lib
  29  * @modules java.base/jdk.internal.misc
  30  *          java.management
  31  * @build sun.hotspot.WhiteBox
  32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  33  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. UseCompressedOops
  34  */
  35 import java.util.ArrayList;
  36 import java.util.Collections;
  37 import jdk.test.lib.Platform;
  38 import jdk.test.lib.process.ProcessTools;
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 
  41 import sun.hotspot.code.Compiler;
  42 
  43 public class UseCompressedOops {
  44 
  45     public static void main(String[] args) throws Exception {
  46         testCompressedOopsModesGCs();
  47         testCompressedOopsModesGCs("-XX:+UseLargePages");
  48     }
  49 
  50     public static void testCompressedOopsModesGCs(String... flags) throws Exception {
  51         ArrayList<String> args = new ArrayList<>();
  52         Collections.addAll(args, flags);
  53 
  54         // Test default.
  55         testCompressedOopsModes(args);
  56         // Test GCs.
  57         testCompressedOopsModes(args, "-XX:+UseG1GC");
  58         if (!Compiler.isGraalEnabled()) { // Graal does not support CMS
  59             testCompressedOopsModes(args, "-XX:+UseConcMarkSweepGC");
  60         }
  61         testCompressedOopsModes(args, "-XX:+UseSerialGC");
  62         testCompressedOopsModes(args, "-XX:+UseParallelGC");
  63         testCompressedOopsModes(args, "-XX:+UseParallelOldGC");
  64     }
  65 
  66     public static void testCompressedOopsModes(ArrayList<String> flags1, String... flags2) throws Exception {
  67         ArrayList<String> args = new ArrayList<>();
  68         args.addAll(flags1);
  69         Collections.addAll(args, flags2);
  70 
  71         if (Platform.is64bit()) {
  72             // Explicitly turn of compressed oops
  73             testCompressedOops(args, "-XX:-UseCompressedOops", "-Xmx32m")
  74                 .shouldNotContain("Compressed Oops")
  75                 .shouldHaveExitValue(0);
  76 
  77             // Compressed oops should be on by default
  78             testCompressedOops(args, "-Xmx32m")
  79                 .shouldContain("Compressed Oops mode")
  80                 .shouldHaveExitValue(0);
  81 
  82             // Explicly enabling compressed oops
  83             testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx32m")
  84                 .shouldContain("Compressed Oops mode")
  85                 .shouldHaveExitValue(0);
  86 
  87             // Skip the following three test cases if we're on OSX or Solaris.
  88             //
  89             // OSX doesn't seem to care about HeapBaseMinAddress and Solaris
  90             // puts the heap way up, forcing different behaviour.
  91             if (!Platform.isOSX() && !Platform.isSolaris()) {
  92                 // Larger than 4gb heap should result in zero based with shift 3
  93                 testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx5g")
  94                     .shouldContain("Zero based")
  95                     .shouldContain("Oop shift amount: 3")
  96                     .shouldHaveExitValue(0);
  97 
  98                 // Larger than 3gb heap and HeapBaseMinAddress=1g should result in zero based with shift 3
  99                 testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx3200m", "-XX:HeapBaseMinAddress=1g")
 100                     .shouldContain("Zero based")
 101                     .shouldContain("Oop shift amount: 3")
 102                     .shouldHaveExitValue(0);
 103 
 104                 // Small heap above 4gb should result in zero based with shift 3
 105                 testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx32m", "-XX:HeapBaseMinAddress=4g")
 106                     .shouldContain("Zero based")
 107                     .shouldContain("Oop shift amount: 3")
 108                     .shouldHaveExitValue(0);
 109 
 110                 // Small heap above 32gb should result in non-zero based with shift 3
 111                 testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx32m", "-XX:HeapBaseMinAddress=32g")
 112                     .shouldContain("Non-zero disjoint base")
 113                     .shouldContain("Oop shift amount: 3")
 114                     .shouldHaveExitValue(0);
 115 
 116                 // Small heap above 32gb should result in non-zero based with shift 3
 117                 testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx32m", "-XX:HeapBaseMinAddress=72704m")
 118                     .shouldContain("Non-zero based")
 119                     .shouldContain("Oop shift amount: 3")
 120                     .shouldHaveExitValue(0);
 121 
 122                 // 32gb heap with heap base above 64gb and object alignment set to 16 bytes should result
 123                 // in non-zero based with shift 4
 124                 testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx32g", "-XX:ObjectAlignmentInBytes=16",
 125                                "-XX:HeapBaseMinAddress=64g")
 126                     .shouldContain("Non-zero disjoint base")
 127                     .shouldContain("Oop shift amount: 4")
 128                     .shouldHaveExitValue(0);
 129 
 130                 // 32gb heap with object alignment set to 16 bytes should result in zero based with shift 4
 131                 testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx32g", "-XX:ObjectAlignmentInBytes=16")
 132                     .shouldContain("Zero based")
 133                     .shouldContain("Oop shift amount: 4")
 134                     .shouldHaveExitValue(0);
 135             }
 136 
 137             // This is a pathologic case for the heap allocation algorithm. Regression test.
 138             // HeapBaseMinAddress must be 2g and should not be set on the command line.
 139             testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx2g")
 140                 .shouldNotContain("Max heap size too large for Compressed Oops")
 141                 .shouldHaveExitValue(0);
 142             testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx29g", "-XX:CompressedClassSpaceSize=1g")
 143                 .shouldNotContain("Max heap size too large for Compressed Oops")
 144                 .shouldHaveExitValue(0);
 145 
 146             // Explicitly enabling compressed oops with 32gb heap should result a warning
 147             testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx32g")
 148                 .shouldContain("Max heap size too large for Compressed Oops")
 149                 .shouldHaveExitValue(0);
 150 
 151             // 32gb heap should not result a warning
 152             testCompressedOops(args, "-Xmx32g")
 153                 .shouldNotContain("Max heap size too large for Compressed Oops")
 154                 .shouldHaveExitValue(0);
 155 
 156             // Explicitly enabling compressed oops with 32gb heap and object
 157             // alignment set to 8 byte should result a warning
 158             testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx32g", "-XX:ObjectAlignmentInBytes=8")
 159                 .shouldContain("Max heap size too large for Compressed Oops")
 160                 .shouldHaveExitValue(0);
 161 
 162             // 64gb heap and object alignment set to 16 bytes should result in a warning
 163             testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx64g", "-XX:ObjectAlignmentInBytes=16")
 164                 .shouldContain("Max heap size too large for Compressed Oops")
 165                 .shouldHaveExitValue(0);
 166 
 167         } else {
 168             // Compressed oops should only apply to 64bit platforms
 169             testCompressedOops(args, "-XX:+UseCompressedOops", "-Xmx32m")
 170                 .shouldContain("Unrecognized VM option 'UseCompressedOops'")
 171                 .shouldHaveExitValue(1);
 172         }
 173     }
 174 
 175     private static OutputAnalyzer testCompressedOops(ArrayList<String> flags1, String... flags2) throws Exception {
 176         ArrayList<String> args = new ArrayList<>();
 177 
 178         // Always run with these three:
 179         args.add("-XX:+PrintCompressedOopsMode");
 180         args.add("-Xms32m");
 181 
 182         // Add the extra flags
 183         args.addAll(flags1);
 184         Collections.addAll(args, flags2);
 185 
 186         args.add("-version");
 187 
 188         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args.toArray(new String[0]));
 189         return new OutputAnalyzer(pb.start());
 190     }
 191 }