Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/containers/docker/TestMemoryAwareness.java
32285 views
1
/*
2
* Copyright (c) 2018, 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
* @summary Test JVM's memory resource awareness when running inside docker container
28
* @library /testlibrary /testlibrary/whitebox
29
* @build AttemptOOM sun.hotspot.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean
30
* @run driver ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
31
* @run driver TestMemoryAwareness
32
*/
33
34
import com.oracle.java.testlibrary.Common;
35
import com.oracle.java.testlibrary.DockerRunOptions;
36
import com.oracle.java.testlibrary.DockerTestUtils;
37
import com.oracle.java.testlibrary.OutputAnalyzer;
38
39
40
public class TestMemoryAwareness {
41
private static final String imageName = Common.imageName("memory");
42
43
public static void main(String[] args) throws Exception {
44
if (!DockerTestUtils.canTestDocker()) {
45
return;
46
}
47
48
Common.prepareWhiteBox();
49
DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
50
51
try {
52
testMemoryLimit("100m", "104857600");
53
testMemoryLimit("500m", "524288000");
54
testMemoryLimit("1g", "1073741824");
55
testMemoryLimit("4g", "4294967296");
56
57
testMemorySoftLimit("500m", "524288000");
58
testMemorySoftLimit("1g", "1073741824");
59
60
// Add extra 10 Mb to allocator limit, to be sure to cause OOM
61
testOOM("256m", 256 + 10);
62
63
testOperatingSystemMXBeanAwareness(
64
"100M", Integer.toString(((int) Math.pow(2, 20)) * 100),
65
"150M", Integer.toString(((int) Math.pow(2, 20)) * (150 - 100))
66
);
67
testOperatingSystemMXBeanAwareness(
68
"128M", Integer.toString(((int) Math.pow(2, 20)) * 128),
69
"256M", Integer.toString(((int) Math.pow(2, 20)) * (256 - 128))
70
);
71
testOperatingSystemMXBeanAwareness(
72
"1G", Integer.toString(((int) Math.pow(2, 20)) * 1024),
73
"1500M", Integer.toString(((int) Math.pow(2, 20)) * (1500 - 1024))
74
);
75
} finally {
76
DockerTestUtils.removeDockerImage(imageName);
77
}
78
}
79
80
81
private static void testMemoryLimit(String valueToSet, String expectedTraceValue)
82
throws Exception {
83
84
Common.logNewTestCase("memory limit: " + valueToSet);
85
86
DockerRunOptions opts = Common.newOpts(imageName)
87
.addDockerOpts("--memory", valueToSet);
88
89
Common.run(opts)
90
.shouldMatch("Memory Limit is:.*" + expectedTraceValue);
91
}
92
93
94
private static void testMemorySoftLimit(String valueToSet, String expectedTraceValue)
95
throws Exception {
96
Common.logNewTestCase("memory soft limit: " + valueToSet);
97
98
DockerRunOptions opts = Common.newOpts(imageName, "PrintContainerInfo");
99
Common.addWhiteBoxOpts(opts);
100
opts.addDockerOpts("--memory-reservation=" + valueToSet);
101
102
Common.run(opts)
103
.shouldMatch("Memory Soft Limit.*" + expectedTraceValue);
104
}
105
106
107
// provoke OOM inside the container, see how VM reacts
108
private static void testOOM(String dockerMemLimit, int sizeToAllocInMb) throws Exception {
109
Common.logNewTestCase("OOM");
110
111
DockerRunOptions opts = Common.newOpts(imageName, "AttemptOOM")
112
.addDockerOpts("--memory", dockerMemLimit, "--memory-swap", dockerMemLimit);
113
opts.classParams.add("" + sizeToAllocInMb);
114
115
DockerTestUtils.dockerRunJava(opts)
116
.shouldHaveExitValue(1)
117
.shouldContain("Entering AttemptOOM main")
118
.shouldNotContain("AttemptOOM allocation successful")
119
.shouldContain("java.lang.OutOfMemoryError");
120
}
121
122
private static void testOperatingSystemMXBeanAwareness(String memoryAllocation, String expectedMemory,
123
String swapAllocation, String expectedSwap) throws Exception {
124
Common.logNewTestCase("Check OperatingSystemMXBean");
125
126
DockerRunOptions opts = Common.newOpts(imageName, "CheckOperatingSystemMXBean")
127
.addDockerOpts(
128
"--memory", memoryAllocation,
129
"--memory-swap", swapAllocation
130
);
131
132
OutputAnalyzer out = DockerTestUtils.dockerRunJava(opts);
133
out.shouldHaveExitValue(0)
134
.shouldContain("Checking OperatingSystemMXBean")
135
.shouldContain("OperatingSystemMXBean.getTotalPhysicalMemorySize: " + expectedMemory)
136
.shouldMatch("OperatingSystemMXBean\\.getFreePhysicalMemorySize: [1-9][0-9]+");
137
// in case of warnings like : "Your kernel does not support swap limit capabilities
138
// or the cgroup is not mounted. Memory limited without swap."
139
// the getTotalSwapSpaceSize and getFreeSwapSpaceSize return the system
140
// values as the container setup isn't supported in that case.
141
try {
142
out.shouldContain("OperatingSystemMXBean.getTotalSwapSpaceSize: " + expectedSwap);
143
} catch(RuntimeException ex) {
144
out.shouldMatch("OperatingSystemMXBean.getTotalSwapSpaceSize: [0-9]+");
145
}
146
147
try {
148
out.shouldMatch("OperatingSystemMXBean\\.getFreeSwapSpaceSize: [1-9][0-9]+");
149
} catch(RuntimeException ex) {
150
out.shouldMatch("OperatingSystemMXBean\\.getFreeSwapSpaceSize: 0");
151
}
152
}
153
154
}
155
156