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