Skip to content

Commit b6a7367

Browse files
author
David Holmes
committed
8260349: Cannot programmatically retrieve Metaspace max set via JAVA_TOOL_OPTIONS
Reviewed-by: shade, stuefe
1 parent 50f9a70 commit b6a7367

File tree

2 files changed

+123
-3
lines changed

2 files changed

+123
-3
lines changed

src/hotspot/share/services/memoryPool.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -197,8 +197,8 @@ size_t MetaspacePool::used_in_bytes() {
197197
}
198198

199199
size_t MetaspacePool::calculate_max_size() const {
200-
return FLAG_IS_CMDLINE(MaxMetaspaceSize) ? MaxMetaspaceSize :
201-
MemoryUsage::undefined_size();
200+
return !FLAG_IS_DEFAULT(MaxMetaspaceSize) ? MaxMetaspaceSize :
201+
MemoryUsage::undefined_size();
202202
}
203203

204204
CompressedKlassSpacePool::CompressedKlassSpacePool() :
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2021, 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+
/*
25+
* @test
26+
* @bug 8260349
27+
* @summary test that setting via the env-var and options file shows up as expected
28+
* @library /test/lib
29+
* @run driver MaxMetaspaceSizeEnvVarTest
30+
*/
31+
32+
import java.io.PrintWriter;
33+
import java.lang.management.ManagementFactory;
34+
import java.lang.management.MemoryPoolMXBean;
35+
36+
import jdk.test.lib.process.ProcessTools;
37+
import jdk.test.lib.process.OutputAnalyzer;
38+
39+
public class MaxMetaspaceSizeEnvVarTest {
40+
41+
// This is the test class we exec, passing the MaxMetaspaceSize flag
42+
// by different mechanisms.
43+
static class Main {
44+
public static void main(String[] args) throws Exception {
45+
long expected = Long.parseLong(args[0]);
46+
MemoryPoolMXBean metaspaceMemoryPool =
47+
ManagementFactory.getPlatformMXBeans(MemoryPoolMXBean.class)
48+
.stream()
49+
.filter(pool -> "Metaspace".equals(pool.getName()))
50+
.findFirst()
51+
.orElseThrow();
52+
long max = metaspaceMemoryPool.getUsage().getMax();
53+
System.out.println("Metaspace max usage is " + max);
54+
if (max != expected) {
55+
throw new RuntimeException("Metaspace max " + max +
56+
" != " + expected);
57+
}
58+
}
59+
}
60+
61+
static void report(String msg) {
62+
System.out.println(msg);
63+
System.err.println(msg);
64+
}
65+
66+
public static void main(String... args) throws Exception {
67+
final String max = String.valueOf(9 * 1024 * 1024); // 9 MB
68+
final String flagRaw = "MaxMetaspaceSize=" + max;
69+
final String flag = "-XX:" + flagRaw;
70+
final String main = "MaxMetaspaceSizeEnvVarTest$Main";
71+
72+
ProcessBuilder pb = null;
73+
OutputAnalyzer output = null;
74+
75+
int test = 1;
76+
report("Test " + test + ": flag not set");
77+
78+
Main.main(new String[] { "-1" }); // -1 == undefined size
79+
report("------ end Test " + test);
80+
test++;
81+
82+
report("Test " + test + ": normal command-line flag");
83+
pb = ProcessTools.createJavaProcessBuilder(flag, main, max);
84+
output = new OutputAnalyzer(pb.start());
85+
output.shouldHaveExitValue(0);
86+
output.reportDiagnosticSummary();
87+
report("------ end Test " + test);
88+
test++;
89+
90+
String[] envVars = {
91+
"JDK_JAVA_OPTIONS",
92+
"_JAVA_OPTIONS",
93+
"JAVA_TOOL_OPTIONS"
94+
};
95+
96+
for (String envVar : envVars) {
97+
report("Test " + test + ": " + envVar + " env-var");
98+
pb = ProcessTools.createJavaProcessBuilder(main, max);
99+
pb.environment().put(envVar, flag);
100+
output = new OutputAnalyzer(pb.start());
101+
output.shouldHaveExitValue(0);
102+
output.reportDiagnosticSummary();
103+
report("------ end Test " + test);
104+
test++;
105+
}
106+
107+
report("Test " + test + ": .hotspotrc file");
108+
final String rcFile = ".hotspotrc";
109+
final String rcFileFlag = "-XX:Flags=" + rcFile;
110+
111+
PrintWriter pw = new PrintWriter(rcFile);
112+
pw.println(flagRaw);
113+
pw.close();
114+
pb = ProcessTools.createJavaProcessBuilder(rcFileFlag, main, max);
115+
output = new OutputAnalyzer(pb.start());
116+
output.shouldHaveExitValue(0);
117+
output.reportDiagnosticSummary();
118+
report("------ end Test " + test);
119+
}
120+
}

0 commit comments

Comments
 (0)