Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/containers/docker/TestMemoryAwareness.java
40943 views
1
/*
2
* Copyright (c) 2017, 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
/*
26
* @test
27
* @key cgroups
28
* @summary Test JVM's memory resource awareness when running inside docker container
29
* @requires docker.support
30
* @library /test/lib
31
* @modules java.base/jdk.internal.misc
32
* java.base/jdk.internal.platform
33
* java.management
34
* jdk.jartool/sun.tools.jar
35
* @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean
36
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox
37
* @run driver TestMemoryAwareness
38
*/
39
import jdk.test.lib.containers.docker.Common;
40
import jdk.test.lib.containers.docker.DockerRunOptions;
41
import jdk.test.lib.containers.docker.DockerTestUtils;
42
import jdk.test.lib.process.OutputAnalyzer;
43
44
public class TestMemoryAwareness {
45
private static final String imageName = Common.imageName("memory");
46
47
public static void main(String[] args) throws Exception {
48
if (!DockerTestUtils.canTestDocker()) {
49
return;
50
}
51
52
Common.prepareWhiteBox();
53
DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
54
55
try {
56
testMemoryLimit("100m", "104857600");
57
testMemoryLimit("500m", "524288000");
58
testMemoryLimit("1g", "1073741824");
59
testMemoryLimit("4g", "4294967296");
60
61
testMemorySoftLimit("500m", "524288000");
62
testMemorySoftLimit("1g", "1073741824");
63
64
// Add extra 10 Mb to allocator limit, to be sure to cause OOM
65
testOOM("256m", 256 + 10);
66
67
testOperatingSystemMXBeanAwareness(
68
"100M", Integer.toString(((int) Math.pow(2, 20)) * 100),
69
"150M", Integer.toString(((int) Math.pow(2, 20)) * (150 - 100))
70
);
71
testOperatingSystemMXBeanAwareness(
72
"128M", Integer.toString(((int) Math.pow(2, 20)) * 128),
73
"256M", Integer.toString(((int) Math.pow(2, 20)) * (256 - 128))
74
);
75
testOperatingSystemMXBeanAwareness(
76
"1G", Integer.toString(((int) Math.pow(2, 20)) * 1024),
77
"1500M", Integer.toString(((int) Math.pow(2, 20)) * (1500 - 1024))
78
);
79
} finally {
80
if (!DockerTestUtils.RETAIN_IMAGE_AFTER_TEST) {
81
DockerTestUtils.removeDockerImage(imageName);
82
}
83
}
84
}
85
86
87
private static void testMemoryLimit(String valueToSet, String expectedTraceValue)
88
throws Exception {
89
90
Common.logNewTestCase("memory limit: " + valueToSet);
91
92
DockerRunOptions opts = Common.newOpts(imageName)
93
.addDockerOpts("--memory", valueToSet);
94
95
Common.run(opts)
96
.shouldMatch("Memory Limit is:.*" + expectedTraceValue);
97
}
98
99
100
private static void testMemorySoftLimit(String valueToSet, String expectedTraceValue)
101
throws Exception {
102
Common.logNewTestCase("memory soft limit: " + valueToSet);
103
104
DockerRunOptions opts = Common.newOpts(imageName, "PrintContainerInfo");
105
Common.addWhiteBoxOpts(opts);
106
opts.addDockerOpts("--memory-reservation=" + valueToSet);
107
108
Common.run(opts)
109
.shouldMatch("Memory Soft Limit.*" + expectedTraceValue);
110
}
111
112
113
// provoke OOM inside the container, see how VM reacts
114
private static void testOOM(String dockerMemLimit, int sizeToAllocInMb) throws Exception {
115
Common.logNewTestCase("OOM");
116
117
// add "--memory-swappiness 0" so as to disable anonymous page swapping.
118
DockerRunOptions opts = Common.newOpts(imageName, "AttemptOOM")
119
.addDockerOpts("--memory", dockerMemLimit, "--memory-swappiness", "0", "--memory-swap", dockerMemLimit);
120
opts.classParams.add("" + sizeToAllocInMb);
121
122
// make sure we avoid inherited Xmx settings from the jtreg vmoptions
123
// set Xmx ourselves instead
124
System.out.println("sizeToAllocInMb is:" + sizeToAllocInMb + " sizeToAllocInMb/2 is:" + sizeToAllocInMb/2);
125
String javaHeapSize = sizeToAllocInMb/2 + "m";
126
opts.addJavaOptsAppended("-Xmx" + javaHeapSize);
127
128
OutputAnalyzer out = DockerTestUtils.dockerRunJava(opts);
129
130
if (out.getExitValue() == 0) {
131
throw new RuntimeException("We exited successfully, but we wanted to provoke an OOM inside the container");
132
}
133
134
out.shouldContain("Entering AttemptOOM main")
135
.shouldNotContain("AttemptOOM allocation successful")
136
.shouldContain("java.lang.OutOfMemoryError");
137
}
138
139
private static void testOperatingSystemMXBeanAwareness(String memoryAllocation, String expectedMemory,
140
String swapAllocation, String expectedSwap) throws Exception {
141
Common.logNewTestCase("Check OperatingSystemMXBean");
142
143
DockerRunOptions opts = Common.newOpts(imageName, "CheckOperatingSystemMXBean")
144
.addDockerOpts(
145
"--memory", memoryAllocation,
146
"--memory-swap", swapAllocation
147
)
148
// CheckOperatingSystemMXBean uses Metrics (jdk.internal.platform) for
149
// diagnostics
150
.addJavaOpts("--add-exports")
151
.addJavaOpts("java.base/jdk.internal.platform=ALL-UNNAMED");
152
153
OutputAnalyzer out = DockerTestUtils.dockerRunJava(opts);
154
out.shouldHaveExitValue(0)
155
.shouldContain("Checking OperatingSystemMXBean")
156
.shouldContain("OperatingSystemMXBean.getTotalPhysicalMemorySize: " + expectedMemory)
157
.shouldContain("OperatingSystemMXBean.getTotalMemorySize: " + expectedMemory)
158
.shouldMatch("OperatingSystemMXBean\\.getFreeMemorySize: [1-9][0-9]+")
159
.shouldMatch("OperatingSystemMXBean\\.getFreePhysicalMemorySize: [1-9][0-9]+");
160
161
// in case of warnings like : "Your kernel does not support swap limit capabilities
162
// or the cgroup is not mounted. Memory limited without swap."
163
// the getTotalSwapSpaceSize and getFreeSwapSpaceSize return the system
164
// values as the container setup isn't supported in that case.
165
try {
166
out.shouldContain("OperatingSystemMXBean.getTotalSwapSpaceSize: " + expectedSwap);
167
} catch(RuntimeException ex) {
168
out.shouldMatch("OperatingSystemMXBean.getTotalSwapSpaceSize: [0-9]+");
169
}
170
171
try {
172
out.shouldMatch("OperatingSystemMXBean\\.getFreeSwapSpaceSize: [1-9][0-9]+");
173
} catch(RuntimeException ex) {
174
out.shouldMatch("OperatingSystemMXBean\\.getFreeSwapSpaceSize: 0");
175
}
176
}
177
178
}
179
180