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