Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/containers/docker/TestJFREvents.java
40942 views
1
/*
2
* Copyright (c) 2019, 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
25
/*
26
* @test
27
* @key cgroups
28
* @summary Ensure that certain JFR events return correct results for resource values
29
* when run inside Docker container, such as available CPU and memory.
30
* Also make sure that PIDs are based on value provided by container,
31
* not by the host system.
32
* @requires (docker.support & os.maxMemory >= 2g)
33
* @library /test/lib
34
* @modules java.base/jdk.internal.misc
35
* java.management
36
* jdk.jartool/sun.tools.jar
37
* @build JfrReporter
38
* @run driver TestJFREvents
39
*/
40
import java.util.List;
41
import jdk.test.lib.containers.docker.Common;
42
import jdk.test.lib.containers.docker.DockerRunOptions;
43
import jdk.test.lib.containers.docker.DockerTestUtils;
44
import jdk.test.lib.Asserts;
45
import jdk.test.lib.process.OutputAnalyzer;
46
import jdk.test.lib.Utils;
47
48
49
public class TestJFREvents {
50
private static final String imageName = Common.imageName("jfr-events");
51
private static final String TEST_ENV_VARIABLE = "UNIQUE_VARIABLE_ABC592903XYZ";
52
private static final String TEST_ENV_VALUE = "unique_value_abc592903xyz";
53
private static final int availableCPUs = Runtime.getRuntime().availableProcessors();
54
55
public static void main(String[] args) throws Exception {
56
System.out.println("Test Environment: detected availableCPUs = " + availableCPUs);
57
if (!DockerTestUtils.canTestDocker()) {
58
return;
59
}
60
61
DockerTestUtils.buildJdkDockerImage(imageName, "Dockerfile-BasicTest", "jdk-docker");
62
63
try {
64
65
long MB = 1024*1024;
66
testMemory("200m", "" + 200*MB);
67
testMemory("500m", "" + 500*MB);
68
testMemory("1g", "" + 1024*MB);
69
70
testProcessInfo();
71
72
testEnvironmentVariables();
73
74
containerInfoTestCase();
75
testCpuUsage();
76
testCpuThrottling();
77
testMemoryUsage();
78
testIOUsage();
79
} finally {
80
DockerTestUtils.removeDockerImage(imageName);
81
}
82
}
83
84
private static void containerInfoTestCase() throws Exception {
85
// leave one CPU for system and tools, otherwise this test may be unstable
86
int maxNrOfAvailableCpus = availableCPUs - 1;
87
for (int i=1; i < maxNrOfAvailableCpus; i = i * 2) {
88
for (int j=64; j <= 256; j *= 2) {
89
testContainerInfo(i, j);
90
}
91
}
92
}
93
94
private static void testContainerInfo(int expectedCPUs, int expectedMemoryMB) throws Exception {
95
Common.logNewTestCase("ContainerInfo: --cpus = " + expectedCPUs + " --memory=" + expectedMemoryMB + "m");
96
String eventName = "jdk.ContainerConfiguration";
97
long expectedSlicePeriod = 100000; // default slice period
98
long expectedMemoryLimit = expectedMemoryMB * 1024 * 1024;
99
100
String cpuCountFld = "effectiveCpuCount";
101
String cpuQuotaFld = "cpuQuota";
102
String cpuSlicePeriodFld = "cpuSlicePeriod";
103
String memoryLimitFld = "memoryLimit";
104
105
DockerTestUtils.dockerRunJava(
106
commonDockerOpts()
107
.addDockerOpts("--cpus=" + expectedCPUs)
108
.addDockerOpts("--memory=" + expectedMemoryMB + "m")
109
.addClassOptions(eventName))
110
.shouldHaveExitValue(0)
111
.shouldContain(cpuCountFld + " = " + expectedCPUs)
112
.shouldContain(cpuSlicePeriodFld + " = " + expectedSlicePeriod)
113
.shouldContain(cpuQuotaFld + " = " + expectedCPUs * expectedSlicePeriod)
114
.shouldContain(memoryLimitFld + " = " + expectedMemoryLimit);
115
}
116
117
private static void testCpuUsage() throws Exception {
118
Common.logNewTestCase("CPU Usage");
119
String eventName = "jdk.ContainerCPUUsage";
120
121
String cpuTimeFld = "cpuTime";
122
String cpuUserTimeFld = "cpuUserTime";
123
String cpuSystemTimeFld = "cpuSystemTime";
124
125
DockerTestUtils.dockerRunJava(
126
commonDockerOpts()
127
.addClassOptions(eventName, "period=endChunk"))
128
.shouldHaveExitValue(0)
129
.shouldNotContain(cpuTimeFld + " = " + 0)
130
.shouldNotContain(cpuUserTimeFld + " = " + 0)
131
.shouldNotContain(cpuSystemTimeFld + " = " + 0);
132
}
133
134
private static void testMemoryUsage() throws Exception {
135
Common.logNewTestCase("Memory Usage");
136
String eventName = "jdk.ContainerMemoryUsage";
137
138
String memoryFailCountFld = "memoryFailCount";
139
String memoryUsageFld = "memoryUsage";
140
String swapMemoryUsageFld = "swapMemoryUsage";
141
142
DockerTestUtils.dockerRunJava(
143
commonDockerOpts()
144
.addClassOptions(eventName, "period=endChunk"))
145
.shouldHaveExitValue(0)
146
.shouldContain(memoryFailCountFld)
147
.shouldContain(memoryUsageFld)
148
.shouldContain(swapMemoryUsageFld);
149
}
150
151
private static void testIOUsage() throws Exception {
152
Common.logNewTestCase("I/O Usage");
153
String eventName = "jdk.ContainerIOUsage";
154
155
String serviceRequestsFld = "serviceRequests";
156
String dataTransferredFld = "dataTransferred";
157
158
DockerTestUtils.dockerRunJava(
159
commonDockerOpts()
160
.addClassOptions(eventName, "period=endChunk"))
161
.shouldHaveExitValue(0)
162
.shouldContain(serviceRequestsFld)
163
.shouldContain(dataTransferredFld);
164
}
165
166
private static void testCpuThrottling() throws Exception {
167
Common.logNewTestCase("CPU Throttling");
168
String eventName = "jdk.ContainerCPUThrottling";
169
170
String cpuElapsedSlicesFld = "cpuElapsedSlices";
171
String cpuThrottledSlicesFld = "cpuThrottledSlices";
172
String cpuThrottledTimeFld = "cpuThrottledTime";
173
174
DockerTestUtils.dockerRunJava(
175
commonDockerOpts()
176
.addClassOptions(eventName, "period=endChunk"))
177
.shouldHaveExitValue(0)
178
.shouldContain(cpuElapsedSlicesFld)
179
.shouldContain(cpuThrottledSlicesFld)
180
.shouldContain(cpuThrottledTimeFld);
181
}
182
183
184
private static void testMemory(String valueToSet, String expectedValue) throws Exception {
185
Common.logNewTestCase("Memory: --memory = " + valueToSet);
186
DockerTestUtils.dockerRunJava(
187
commonDockerOpts()
188
.addDockerOpts("--memory=" + valueToSet)
189
.addClassOptions("jdk.PhysicalMemory"))
190
.shouldHaveExitValue(0)
191
.shouldContain("totalSize = " + expectedValue);
192
}
193
194
195
private static void testProcessInfo() throws Exception {
196
Common.logNewTestCase("ProcessInfo");
197
DockerTestUtils.dockerRunJava(
198
commonDockerOpts()
199
.addClassOptions("jdk.SystemProcess"))
200
.shouldHaveExitValue(0)
201
.shouldContain("pid = 1");
202
}
203
204
private static DockerRunOptions commonDockerOpts() {
205
return new DockerRunOptions(imageName, "/jdk/bin/java", "JfrReporter")
206
.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
207
.addJavaOpts("-cp", "/test-classes/");
208
}
209
210
211
private static void testEnvironmentVariables() throws Exception {
212
Common.logNewTestCase("EnvironmentVariables");
213
214
List<String> cmd = DockerTestUtils.buildJavaCommand(
215
commonDockerOpts()
216
.addClassOptions("jdk.InitialEnvironmentVariable"));
217
218
ProcessBuilder pb = new ProcessBuilder(cmd);
219
// Container has JAVA_HOME defined via the Dockerfile; make sure
220
// it is reported by JFR event.
221
// Environment variable set in host system should not be visible inside a container,
222
// and should not be reported by JFR.
223
pb.environment().put(TEST_ENV_VARIABLE, TEST_ENV_VALUE);
224
225
System.out.println("[COMMAND]\n" + Utils.getCommandLine(pb));
226
OutputAnalyzer out = new OutputAnalyzer(pb.start());
227
System.out.println("[STDERR]\n" + out.getStderr());
228
System.out.println("[STDOUT]\n" + out.getStdout());
229
230
out.shouldHaveExitValue(0)
231
.shouldContain("key = JAVA_HOME")
232
.shouldContain("value = /jdk")
233
.shouldNotContain(TEST_ENV_VARIABLE)
234
.shouldNotContain(TEST_ENV_VALUE);
235
}
236
}
237
238