1 /*
   2  * Copyright (c) 2013, 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 import jdk.test.lib.Asserts;
  25 import jdk.test.lib.process.ProcessTools;
  26 import jdk.test.lib.process.OutputAnalyzer;
  27 
  28 /*
  29  * @test TestMetaspaceSizeFlags
  30  * @key gc
  31  * @bug 8024650
  32  * @summary Test that metaspace size flags can be set correctly
  33  * @library /test/lib
  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   }
  51 
  52   private static void testMaxMetaspaceSizeEQMetaspaceSize(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  53     MetaspaceFlags mf = runAndGetValue(maxMetaspaceSize, metaspaceSize);
  54     Asserts.assertEQ(maxMetaspaceSize, metaspaceSize);
  55     Asserts.assertEQ(mf.maxMetaspaceSize, maxMetaspaceSize);
  56     Asserts.assertEQ(mf.metaspaceSize, metaspaceSize);
  57   }
  58 
  59   private static void testMaxMetaspaceSizeLTMetaspaceSize(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  60     MetaspaceFlags mf = runAndGetValue(maxMetaspaceSize, metaspaceSize);
  61     Asserts.assertEQ(mf.maxMetaspaceSize, maxMetaspaceSize);
  62     Asserts.assertEQ(mf.metaspaceSize, maxMetaspaceSize);
  63   }
  64 
  65   private static void testMaxMetaspaceSizeGTMetaspaceSize(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  66     MetaspaceFlags mf = runAndGetValue(maxMetaspaceSize, metaspaceSize);
  67     Asserts.assertGT(maxMetaspaceSize, metaspaceSize);
  68     Asserts.assertGT(mf.maxMetaspaceSize, mf.metaspaceSize);
  69     Asserts.assertEQ(mf.maxMetaspaceSize, maxMetaspaceSize);
  70     Asserts.assertEQ(mf.metaspaceSize, metaspaceSize);
  71   }
  72 
  73   private static MetaspaceFlags runAndGetValue(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  74     OutputAnalyzer output = run(maxMetaspaceSize, metaspaceSize);
  75     output.shouldNotMatch("Error occurred during initialization of VM\n.*");
  76 
  77     String stringMaxMetaspaceSize = output.firstMatch(".* MaxMetaspaceSize .* = (\\d+).*", 1);
  78     String stringMetaspaceSize = output.firstMatch(".* MetaspaceSize .* = (\\d+).*", 1);
  79 
  80     return new MetaspaceFlags(Long.parseLong(stringMaxMetaspaceSize),
  81                               Long.parseLong(stringMetaspaceSize));
  82   }
  83 
  84   private static OutputAnalyzer run(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  85     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  86         "-XX:MaxMetaspaceSize=" + maxMetaspaceSize,
  87         "-XX:MetaspaceSize=" + metaspaceSize,
  88         "-XX:-UseLargePages", // Prevent us from using 2GB large pages on solaris + sparc.
  89         "-XX:+PrintFlagsFinal",
  90         "-version");
  91     return new OutputAnalyzer(pb.start());
  92   }
  93 
  94   private static class MetaspaceFlags {
  95     public long maxMetaspaceSize;
  96     public long metaspaceSize;
  97     public MetaspaceFlags(long maxMetaspaceSize, long metaspaceSize) {
  98       this.maxMetaspaceSize = maxMetaspaceSize;
  99       this.metaspaceSize = metaspaceSize;
 100     }
 101   }
 102 }