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