1 /*
   2  * Copyright (c) 2013, 2015, 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 jdk.test.lib.Asserts;
  25 import jdk.test.lib.OutputAnalyzer;
  26 import jdk.test.lib.ProcessTools;
  27 
  28 /*
  29  * @test TestMetaspaceSizeFlags
  30  * @key gc
  31  * @bug 8024650
  32  * @summary Test that metaspace size flags can be set correctly
  33  * @library /testlibrary
  34  * @modules java.base/jdk.internal.misc
  35  *          java.management
  36  */
  37 public class TestMetaspaceSizeFlags {
  38   public static final long K = 1024L;
  39   public static final long M = 1024L * K;
  40 
  41   // HotSpot uses a number of different values to align memory size flags.
  42   // This is currently the largest alignment (unless huge large pages are used).
  43   public static final long MAX_ALIGNMENT = 32 * M;
  44 
  45   public static void main(String [] args) throws Exception {
  46     testMaxMetaspaceSizeEQMetaspaceSize(MAX_ALIGNMENT, MAX_ALIGNMENT);
  47     // 8024650: MaxMetaspaceSize was adjusted instead of MetaspaceSize.
  48     testMaxMetaspaceSizeLTMetaspaceSize(MAX_ALIGNMENT, MAX_ALIGNMENT * 2);
  49     testMaxMetaspaceSizeGTMetaspaceSize(MAX_ALIGNMENT * 2, MAX_ALIGNMENT);
  50     testTooSmallInitialMetaspace(0, 0);
  51     testTooSmallInitialMetaspace(0, MAX_ALIGNMENT);
  52     testTooSmallInitialMetaspace(MAX_ALIGNMENT, 0);
  53   }
  54 
  55   private static void testMaxMetaspaceSizeEQMetaspaceSize(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  56     MetaspaceFlags mf = runAndGetValue(maxMetaspaceSize, metaspaceSize);
  57     Asserts.assertEQ(maxMetaspaceSize, metaspaceSize);
  58     Asserts.assertEQ(mf.maxMetaspaceSize, maxMetaspaceSize);
  59     Asserts.assertEQ(mf.metaspaceSize, metaspaceSize);
  60   }
  61 
  62   private static void testMaxMetaspaceSizeLTMetaspaceSize(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  63     MetaspaceFlags mf = runAndGetValue(maxMetaspaceSize, metaspaceSize);
  64     Asserts.assertEQ(mf.maxMetaspaceSize, maxMetaspaceSize);
  65     Asserts.assertEQ(mf.metaspaceSize, maxMetaspaceSize);
  66   }
  67 
  68   private static void testMaxMetaspaceSizeGTMetaspaceSize(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  69     MetaspaceFlags mf = runAndGetValue(maxMetaspaceSize, metaspaceSize);
  70     Asserts.assertGT(maxMetaspaceSize, metaspaceSize);
  71     Asserts.assertGT(mf.maxMetaspaceSize, mf.metaspaceSize);
  72     Asserts.assertEQ(mf.maxMetaspaceSize, maxMetaspaceSize);
  73     Asserts.assertEQ(mf.metaspaceSize, metaspaceSize);
  74   }
  75 
  76   private static void testTooSmallInitialMetaspace(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  77     OutputAnalyzer output = run(maxMetaspaceSize, metaspaceSize);
  78     output.shouldContain("Too small initial Metaspace size");
  79   }
  80 
  81   private static MetaspaceFlags runAndGetValue(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  82     OutputAnalyzer output = run(maxMetaspaceSize, metaspaceSize);
  83     output.shouldNotMatch("Error occurred during initialization of VM\n.*");
  84 
  85     String stringMaxMetaspaceSize = output.firstMatch(".* MaxMetaspaceSize .* := (\\d+).*", 1);
  86     String stringMetaspaceSize = output.firstMatch(".* MetaspaceSize .* := (\\d+).*", 1);
  87 
  88     return new MetaspaceFlags(Long.parseLong(stringMaxMetaspaceSize),
  89                               Long.parseLong(stringMetaspaceSize));
  90   }
  91 
  92   private static OutputAnalyzer run(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  93     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  94         "-XX:MaxMetaspaceSize=" + maxMetaspaceSize,
  95         "-XX:MetaspaceSize=" + metaspaceSize,
  96         "-XX:-UseLargePages", // Prevent us from using 2GB large pages on solaris + sparc.
  97         "-XX:+PrintFlagsFinal",
  98         "-version");
  99     return new OutputAnalyzer(pb.start());
 100   }
 101 
 102   private static class MetaspaceFlags {
 103     public long maxMetaspaceSize;
 104     public long metaspaceSize;
 105     public MetaspaceFlags(long maxMetaspaceSize, long metaspaceSize) {
 106       this.maxMetaspaceSize = maxMetaspaceSize;
 107       this.metaspaceSize = metaspaceSize;
 108     }
 109   }
 110 }