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