1 /*
   2 * Copyright (c) 2013, 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 import java.util.regex.Matcher;
  25 import java.util.regex.Pattern;
  26 import java.util.ArrayList;
  27 import java.util.Arrays;
  28 
  29 import com.oracle.java.testlibrary.*;
  30 import sun.hotspot.WhiteBox;
  31 
  32 class CompressedOopsErgoPrinter {
  33   public static void main(String[] args) throws Exception {
  34     WhiteBox wb = WhiteBox.getWhiteBox();
  35     wb.printCompressedOopsHeapInfo();
  36   }
  37 }
  38 
  39 final class HeapForCompressedOopsValues {
  40   public long maxHeapForCompressedOops;
  41 }
  42 
  43 class TestUseCompressedOopsErgoTools {
  44 
  45   public static boolean is64bitVM() {
  46     String val = System.getProperty("sun.arch.data.model");
  47     if (val == null) {
  48       throw new RuntimeException("Could not read sun.arch.data.model");
  49     }
  50     if (val.equals("64")) {
  51       return true;
  52     } else if (val.equals("32")) {
  53       return false;
  54     }
  55     throw new RuntimeException("Unexpected value " + val + " of sun.arch.data.model");
  56   }
  57 
  58   private static String[] join(String[] part1, String part2) {
  59     ArrayList<String> result = new ArrayList<String>();
  60     result.addAll(Arrays.asList(part1));
  61     result.add(part2);
  62     return result.toArray(new String[0]);
  63   }
  64 
  65   public static void checkCompressedOopsErgo(String[] gcflags) throws Exception {
  66     HeapForCompressedOopsValues v = new HeapForCompressedOopsValues();
  67 
  68     getMaxHeapForCompressedOops(gcflags, v);
  69 
  70     checkUseCompressedOops(gcflags, v.maxHeapForCompressedOops, true);
  71     checkUseCompressedOops(gcflags, v.maxHeapForCompressedOops - 1, true);
  72     checkUseCompressedOops(gcflags, v.maxHeapForCompressedOops + 1, false);
  73 
  74     // the use of HeapBaseMinAddress should not change the outcome
  75     checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), v.maxHeapForCompressedOops, true);
  76     checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), v.maxHeapForCompressedOops - 1, true);
  77     checkUseCompressedOops(join(gcflags, "-XX:HeapBaseMinAddress=32G"), v.maxHeapForCompressedOops + 1, false);
  78 
  79     // use a different object alignment
  80     getMaxHeapForCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), v);
  81 
  82     checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), v.maxHeapForCompressedOops, true);
  83     checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), v.maxHeapForCompressedOops - 1, true);
  84     checkUseCompressedOops(join(gcflags, "-XX:ObjectAlignmentInBytes=16"), v.maxHeapForCompressedOops + 1, false);
  85 
  86     // use a different ClassMetaspaceSize
  87     getMaxHeapForCompressedOops(join(gcflags, "-XX:ClassMetaspaceSize=1G"), v);
  88 
  89     checkUseCompressedOops(join(gcflags, "-XX:ClassMetaspaceSize=1G"), v.maxHeapForCompressedOops, true);
  90     checkUseCompressedOops(join(gcflags, "-XX:ClassMetaspaceSize=1G"), v.maxHeapForCompressedOops - 1, true);
  91     checkUseCompressedOops(join(gcflags, "-XX:ClassMetaspaceSize=1G"), v.maxHeapForCompressedOops + 1, false);
  92   }
  93 
  94   private static void checkUseCompressedOops(String[] args, long heapsize, boolean expectUseCompressedOops) throws Exception {
  95      ArrayList<String> finalargs = new ArrayList<String>();
  96      finalargs.addAll(Arrays.asList(args));
  97      finalargs.add("-Xmx" + heapsize);
  98      finalargs.add("-XX:+PrintFlagsFinal");
  99      finalargs.add("-version");
 100 
 101      String output = expectValid(finalargs.toArray(new String[0]));
 102 
 103      boolean actualUseCompressedOops = getFlagBoolValue(" UseCompressedOops", output);
 104 
 105      if (expectUseCompressedOops != actualUseCompressedOops) {
 106        throw new RuntimeException("Expected use of compressed oops: " + expectUseCompressedOops + " but was: " + actualUseCompressedOops);
 107      }
 108   }
 109 
 110   private static long valueAfter(String source, String match) {
 111     int start = source.indexOf(match) + match.length();
 112     String tail = source.substring(start).split(" ")[0];
 113     return Long.parseLong(tail);
 114   }
 115 
 116   private static void getMaxHeapForCompressedOops(String[] args, HeapForCompressedOopsValues val) throws Exception {
 117     ArrayList<String> finalargs = new ArrayList<String>();
 118 
 119     String[] testVMopts = new String[] {
 120       "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI",
 121       "-Xbootclasspath/a:.",
 122       "-cp", System.getProperty("java.class.path"),
 123     };
 124 
 125     finalargs.addAll(Arrays.asList(args));
 126     finalargs.addAll(Arrays.asList(testVMopts));
 127     finalargs.add(CompressedOopsErgoPrinter.class.getName());
 128 
 129     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(finalargs.toArray(new String[0]));
 130     OutputAnalyzer output = new OutputAnalyzer(pb.start());
 131     output.shouldHaveExitValue(0);
 132 
 133     // the output we watch for has the following format:
 134     //
 135     // "Max heap for compressed oops X Max heap alignment Y ClassMetaspaceSize Z Page size A"
 136     //
 137     // where X, Y, Z and A are sizes in bytes.
 138 
 139     Matcher m = Pattern.compile("Max heap for compressed oops \\d+ ClassMetaspaceSize \\d+").
 140       matcher(output.getStdout());
 141     if (!m.find()) {
 142       throw new RuntimeException("Could not find heap size string.");
 143     }
 144     String match = m.group();
 145 
 146     // parse actual values
 147     val.maxHeapForCompressedOops = valueAfter(match, "Max heap for compressed oops ");
 148   }
 149 
 150   private static boolean getFlagBoolValue(String flag, String where) {
 151     Matcher m = Pattern.compile(flag + "\\s+:?= (true|false)").matcher(where);
 152     if (!m.find()) {
 153       throw new RuntimeException("Could not find value for flag " + flag + " in output string");
 154     }
 155     String match = m.group();
 156     return match.substring(match.lastIndexOf(" ") + 1, match.length()).equals("true");
 157   }
 158 
 159   private static void shouldContainOrNot(OutputAnalyzer output, boolean contains, String message) throws Exception {
 160     if (contains) {
 161       output.shouldContain(message);
 162     } else {
 163       output.shouldNotContain(message);
 164     }
 165   }
 166 
 167   private static String expect(String[] flags, boolean hasWarning, boolean hasError, int errorcode) throws Exception {
 168     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(flags);
 169     OutputAnalyzer output = new OutputAnalyzer(pb.start());
 170     output.shouldHaveExitValue(errorcode);
 171     return output.getStdout();
 172   }
 173 
 174   private static String expectValid(String[] flags) throws Exception {
 175     return expect(flags, false, false, 0);
 176   }
 177 }
 178