Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/containers/docker/TestPids.java
64493 views
1
/*
2
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3
* Copyright (c) 2021 SAP SE. All rights reserved.
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
*
6
* This code is free software; you can redistribute it and/or modify it
7
* under the terms of the GNU General Public License version 2 only, as
8
* published by the Free Software Foundation.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*/
24
25
26
/*
27
* @test
28
* @key cgroups
29
* @summary Test JVM's awareness of pids controller
30
* @requires docker.support
31
* @library /test/lib
32
* @modules java.base/jdk.internal.misc
33
* java.management
34
* @build sun.hotspot.WhiteBox PrintContainerInfo
35
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox
36
* @run driver TestPids
37
*/
38
import java.util.List;
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.Asserts;
43
import jdk.test.lib.Container;
44
import jdk.test.lib.Platform;
45
import jdk.test.lib.Utils;
46
47
public class TestPids {
48
private static final String imageName = Common.imageName("pids");
49
private static final boolean IS_PODMAN = Container.ENGINE_COMMAND.contains("podman");
50
private static final int UNLIMITED_PIDS_PODMAN = 0;
51
private static final int UNLIMITED_PIDS_DOCKER = -1;
52
53
static final String warning_kernel_no_pids_support = "WARNING: Your kernel does not support pids limit capabilities";
54
55
public static void main(String[] args) throws Exception {
56
if (!DockerTestUtils.canTestDocker()) {
57
return;
58
}
59
60
Common.prepareWhiteBox();
61
DockerTestUtils.buildJdkContainerImage(imageName);
62
63
try {
64
testPids();
65
} finally {
66
if (!DockerTestUtils.RETAIN_IMAGE_AFTER_TEST) {
67
DockerTestUtils.removeDockerImage(imageName);
68
}
69
}
70
}
71
72
private static void testPids() throws Exception {
73
System.out.println("Testing pids controller ...");
74
testPids("400");
75
testPids("800");
76
testPids("2000");
77
testPids("Unlimited");
78
}
79
80
private static DockerRunOptions commonOpts() {
81
DockerRunOptions opts = new DockerRunOptions(imageName, "/jdk/bin/java", "PrintContainerInfo");
82
opts.addDockerOpts("--volume", Utils.TEST_CLASSES + ":/test-classes/");
83
opts.addJavaOpts("-Xlog:os+container=trace", "-cp", "/test-classes/");
84
Common.addWhiteBoxOpts(opts);
85
return opts;
86
}
87
88
private static void checkResult(List<String> lines, String lineMarker, String expectedValue) {
89
boolean lineMarkerFound = false;
90
91
for (String line : lines) {
92
if (line.contains(warning_kernel_no_pids_support)) {
93
System.out.println("Docker pids limitation seems not to work, avoiding check");
94
return;
95
}
96
97
if (line.contains(lineMarker)) {
98
lineMarkerFound = true;
99
String[] parts = line.split(":");
100
System.out.println("DEBUG: line = " + line);
101
System.out.println("DEBUG: parts.length = " + parts.length);
102
if (expectedValue.equals("any_integer")) {
103
Asserts.assertEquals(parts.length, 2);
104
String ivalue = parts[1].replaceAll("\\s","");
105
try {
106
int ai = Integer.parseInt(ivalue);
107
System.out.println("Found " + lineMarker + " with value: " + ai + ". PASS.");
108
} catch (NumberFormatException ex) {
109
throw new RuntimeException("Could not convert " + ivalue + " to an integer, log line was " + line);
110
}
111
break;
112
}
113
114
Asserts.assertEquals(parts.length, 2);
115
String actual = parts[1].replaceAll("\\s","");
116
// Unlimited pids leads on some setups not to "max" in the output, but to a high number
117
if (expectedValue.equals("max")) {
118
if (actual.equals("max")) {
119
System.out.println("Found expected max for unlimited pids value.");
120
} else {
121
try {
122
int ai = Integer.parseInt(actual);
123
if (ai > 20000) {
124
System.out.println("Limit value " + ai + " got accepted as unlimited, log line was " + line);
125
} else {
126
throw new RuntimeException("Limit value " + ai + " is not accepted as unlimited, log line was " + line);
127
}
128
} catch (NumberFormatException ex) {
129
throw new RuntimeException("Could not convert " + actual + " to an integer, log line was " + line);
130
}
131
}
132
} else {
133
Asserts.assertEquals(actual, expectedValue);
134
}
135
break;
136
}
137
}
138
Asserts.assertTrue(lineMarkerFound);
139
}
140
141
private static void testPids(String value) throws Exception {
142
Common.logNewTestCase("pids controller test, limiting value = " + value);
143
144
DockerRunOptions opts = commonOpts();
145
if (value.equals("Unlimited")) {
146
int unlimited = IS_PODMAN ? UNLIMITED_PIDS_PODMAN : UNLIMITED_PIDS_DOCKER;
147
opts.addDockerOpts("--pids-limit=" + unlimited);
148
} else {
149
opts.addDockerOpts("--pids-limit="+value);
150
}
151
152
List<String> lines = Common.run(opts).asLines();
153
if (value.equals("Unlimited")) {
154
checkResult(lines, "Maximum number of tasks is: ", "max");
155
} else {
156
checkResult(lines, "Maximum number of tasks is: ", value);
157
}
158
// current number of tasks value is hard to predict, so better expect no value
159
checkResult(lines, "Current number of tasks is: ", "any_integer");
160
}
161
162
}
163
164