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