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