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