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