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