Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/containers/docker/TestJFREvents.java
64475 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.buildJdkContainerImage(imageName);
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
// Try the memory sizes that were verified by testMemory tests before.
87
int maxNrOfAvailableCpus = availableCPUs - 1;
88
for (int cpus = 1; cpus < maxNrOfAvailableCpus; cpus *= 2) {
89
for (int mem : new int[]{ 200, 500, 1024 }) {
90
testContainerInfo(cpus, mem);
91
}
92
}
93
}
94
95
private static void testContainerInfo(int expectedCPUs, int expectedMemoryMB) throws Exception {
96
Common.logNewTestCase("ContainerInfo: --cpus = " + expectedCPUs + " --memory=" + expectedMemoryMB + "m");
97
String eventName = "jdk.ContainerConfiguration";
98
long expectedSlicePeriod = 100000; // default slice period
99
long expectedMemoryLimit = expectedMemoryMB * 1024 * 1024;
100
101
String cpuCountFld = "effectiveCpuCount";
102
String cpuQuotaFld = "cpuQuota";
103
String cpuSlicePeriodFld = "cpuSlicePeriod";
104
String memoryLimitFld = "memoryLimit";
105
106
DockerTestUtils.dockerRunJava(
107
commonDockerOpts()
108
.addDockerOpts("--cpus=" + expectedCPUs)
109
.addDockerOpts("--memory=" + expectedMemoryMB + "m")
110
.addClassOptions(eventName))
111
.shouldHaveExitValue(0)
112
.shouldContain(cpuCountFld + " = " + expectedCPUs)
113
.shouldContain(cpuSlicePeriodFld + " = " + expectedSlicePeriod)
114
.shouldContain(cpuQuotaFld + " = " + expectedCPUs * expectedSlicePeriod)
115
.shouldContain(memoryLimitFld + " = " + expectedMemoryLimit);
116
}
117
118
private static void testCpuUsage() throws Exception {
119
Common.logNewTestCase("CPU Usage");
120
String eventName = "jdk.ContainerCPUUsage";
121
122
String cpuTimeFld = "cpuTime";
123
String cpuUserTimeFld = "cpuUserTime";
124
String cpuSystemTimeFld = "cpuSystemTime";
125
126
DockerTestUtils.dockerRunJava(
127
commonDockerOpts()
128
.addClassOptions(eventName, "period=endChunk"))
129
.shouldHaveExitValue(0)
130
.shouldNotContain(cpuTimeFld + " = " + 0)
131
.shouldNotContain(cpuUserTimeFld + " = " + 0)
132
.shouldNotContain(cpuSystemTimeFld + " = " + 0);
133
}
134
135
private static void testMemoryUsage() throws Exception {
136
Common.logNewTestCase("Memory Usage");
137
String eventName = "jdk.ContainerMemoryUsage";
138
139
String memoryFailCountFld = "memoryFailCount";
140
String memoryUsageFld = "memoryUsage";
141
String swapMemoryUsageFld = "swapMemoryUsage";
142
143
DockerTestUtils.dockerRunJava(
144
commonDockerOpts()
145
.addClassOptions(eventName, "period=endChunk"))
146
.shouldHaveExitValue(0)
147
.shouldContain(memoryFailCountFld)
148
.shouldContain(memoryUsageFld)
149
.shouldContain(swapMemoryUsageFld);
150
}
151
152
private static void testIOUsage() throws Exception {
153
Common.logNewTestCase("I/O Usage");
154
String eventName = "jdk.ContainerIOUsage";
155
156
String serviceRequestsFld = "serviceRequests";
157
String dataTransferredFld = "dataTransferred";
158
159
DockerTestUtils.dockerRunJava(
160
commonDockerOpts()
161
.addClassOptions(eventName, "period=endChunk"))
162
.shouldHaveExitValue(0)
163
.shouldContain(serviceRequestsFld)
164
.shouldContain(dataTransferredFld);
165
}
166
167
private static void testCpuThrottling() throws Exception {
168
Common.logNewTestCase("CPU Throttling");
169
String eventName = "jdk.ContainerCPUThrottling";
170
171
String cpuElapsedSlicesFld = "cpuElapsedSlices";
172
String cpuThrottledSlicesFld = "cpuThrottledSlices";
173
String cpuThrottledTimeFld = "cpuThrottledTime";
174
175
DockerTestUtils.dockerRunJava(
176
commonDockerOpts()
177
.addClassOptions(eventName, "period=endChunk"))
178
.shouldHaveExitValue(0)
179
.shouldContain(cpuElapsedSlicesFld)
180
.shouldContain(cpuThrottledSlicesFld)
181
.shouldContain(cpuThrottledTimeFld);
182
}
183
184
185
private static void testMemory(String valueToSet, String expectedValue) throws Exception {
186
Common.logNewTestCase("Memory: --memory = " + valueToSet);
187
DockerTestUtils.dockerRunJava(
188
commonDockerOpts()
189
.addDockerOpts("--memory=" + valueToSet)
190
.addClassOptions("jdk.PhysicalMemory"))
191
.shouldHaveExitValue(0)
192
.shouldContain("totalSize = " + expectedValue);
193
}
194
195
196
private static void testProcessInfo() throws Exception {
197
Common.logNewTestCase("ProcessInfo");
198
DockerTestUtils.dockerRunJava(
199
commonDockerOpts()
200
.addClassOptions("jdk.SystemProcess"))
201
.shouldHaveExitValue(0)
202
.shouldContain("pid = 1");
203
}
204
205
private static DockerRunOptions commonDockerOpts() {
206
return new DockerRunOptions(imageName, "/jdk/bin/java", "JfrReporter")
207
.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/")
208
.addJavaOpts("-cp", "/test-classes/");
209
}
210
211
212
private static void testEnvironmentVariables() throws Exception {
213
Common.logNewTestCase("EnvironmentVariables");
214
215
List<String> cmd = DockerTestUtils.buildJavaCommand(
216
commonDockerOpts()
217
.addClassOptions("jdk.InitialEnvironmentVariable"));
218
219
ProcessBuilder pb = new ProcessBuilder(cmd);
220
// Container has JAVA_HOME defined via the Dockerfile; make sure
221
// it is reported by JFR event.
222
// Environment variable set in host system should not be visible inside a container,
223
// and should not be reported by JFR.
224
pb.environment().put(TEST_ENV_VARIABLE, TEST_ENV_VALUE);
225
226
System.out.println("[COMMAND]\n" + Utils.getCommandLine(pb));
227
OutputAnalyzer out = new OutputAnalyzer(pb.start());
228
System.out.println("[STDERR]\n" + out.getStderr());
229
System.out.println("[STDOUT]\n" + out.getStdout());
230
231
out.shouldHaveExitValue(0)
232
.shouldContain("key = JAVA_HOME")
233
.shouldContain("value = /jdk")
234
.shouldNotContain(TEST_ENV_VARIABLE)
235
.shouldNotContain(TEST_ENV_VALUE);
236
}
237
}
238
239