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