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