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