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 com.oracle.java.testlibrary.Asserts;
  25 import com.oracle.java.testlibrary.OutputAnalyzer;
  26 import com.oracle.java.testlibrary.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  */
  35 public class TestMetaspaceSizeFlags {
  36   public static final long K = 1024L;
  37   public static final long M = 1024L * K;
  38     
  39   // HotSpot uses a number of different values to align memory size flags.
  40   // This is currently the largest alignment (unless huge large pages are used).
  41   public static final long MAX_ALIGNMENT = 32 * M;
  42   
  43   public static void main(String [] args) throws Exception {
  44     testMaxMetaspaceSizeEQMetaspaceSize(MAX_ALIGNMENT, MAX_ALIGNMENT);
  45     // 8024650: MaxMetaspaceSize was adjusted instead of MetaspaceSize.
  46     testMaxMetaspaceSizeLTMetaspaceSize(MAX_ALIGNMENT, MAX_ALIGNMENT * 2);
  47     testMaxMetaspaceSizeGTMetaspaceSize(MAX_ALIGNMENT * 2, MAX_ALIGNMENT);
  48   }
  49 
  50   private static void testMaxMetaspaceSizeEQMetaspaceSize(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  51     MetaspaceFlags mf = runAndGetValue(maxMetaspaceSize, metaspaceSize);
  52     Asserts.assertEQ(maxMetaspaceSize, metaspaceSize);
  53     Asserts.assertEQ(mf.maxMetaspaceSize, maxMetaspaceSize);
  54     Asserts.assertEQ(mf.metaspaceSize, metaspaceSize);
  55   }
  56 
  57   private static void testMaxMetaspaceSizeLTMetaspaceSize(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  58     MetaspaceFlags mf = runAndGetValue(maxMetaspaceSize, metaspaceSize);
  59     Asserts.assertEQ(mf.maxMetaspaceSize, maxMetaspaceSize);
  60     Asserts.assertEQ(mf.metaspaceSize, maxMetaspaceSize);
  61   }
  62 
  63   private static void testMaxMetaspaceSizeGTMetaspaceSize(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  64     MetaspaceFlags mf = runAndGetValue(maxMetaspaceSize, metaspaceSize);
  65     Asserts.assertGT(maxMetaspaceSize, metaspaceSize);
  66     Asserts.assertGT(mf.maxMetaspaceSize, mf.metaspaceSize);
  67     Asserts.assertEQ(mf.maxMetaspaceSize, maxMetaspaceSize);
  68     Asserts.assertEQ(mf.metaspaceSize, metaspaceSize);
  69   }
  70   
  71   private static MetaspaceFlags runAndGetValue(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  72     OutputAnalyzer output = run(maxMetaspaceSize, metaspaceSize);
  73     output.shouldNotMatch("Error occurred during initialization of VM\n.*");
  74   
  75     String stringMaxMetaspaceSize = output.firstMatch(".* MaxMetaspaceSize .* := (\\d+).*", 1);
  76     String stringMetaspaceSize = output.firstMatch(".* MetaspaceSize .* := (\\d+).*", 1);
  77  
  78     return new MetaspaceFlags(Long.parseLong(stringMaxMetaspaceSize),
  79                               Long.parseLong(stringMetaspaceSize));
  80   }
  81 
  82   private static OutputAnalyzer run(long maxMetaspaceSize, long metaspaceSize) throws Exception {
  83     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  84         "-XX:MaxMetaspaceSize=" + maxMetaspaceSize,
  85         "-XX:MetaspaceSize=" + metaspaceSize,
  86         "-XX:-UseLargePages", // Prevent us from using 2GB large pages on solaris + sparc.
  87         "-XX:+PrintFlagsFinal",
  88         "-version");
  89     return new OutputAnalyzer(pb.start());
  90   }
  91 
  92   private static class MetaspaceFlags {
  93     public long maxMetaspaceSize;
  94     public long metaspaceSize;
  95     public MetaspaceFlags(long maxMetaspaceSize, long metaspaceSize) {
  96       this.maxMetaspaceSize = maxMetaspaceSize;
  97       this.metaspaceSize = metaspaceSize;
  98     }
  99   }
 100 }