Skip to content

Commit 36e703a

Browse files
committed
8321931: memory_swap_current_in_bytes reports 0 as "unlimited"
Backport-of: 7777eb5e15b9f08cdc621c84ff38c72334388b56
1 parent 95e6cbd commit 36e703a

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

src/hotspot/os/linux/osContainer_linux.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ jlong OSContainer::pids_current() {
138138

139139
void OSContainer::print_container_helper(outputStream* st, jlong j, const char* metrics) {
140140
st->print("%s: ", metrics);
141-
if (j > 0) {
141+
if (j >= 0) {
142142
if (j >= 1024) {
143143
st->print_cr(UINT64_FORMAT " k", uint64_t(j) / 1024);
144144
} else {
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2024, Red Hat, Inc.
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+
* @summary Test container info for cgroup v2
29+
* @requires docker.support
30+
* @library /test/lib
31+
* @modules java.base/jdk.internal.misc
32+
* java.management
33+
* jdk.jartool/sun.tools.jar
34+
* @build CheckContainerized jdk.test.whitebox.WhiteBox PrintContainerInfo
35+
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar jdk.test.whitebox.WhiteBox
36+
* @run driver TestContainerInfo
37+
*/
38+
import jtreg.SkippedException;
39+
import jdk.test.lib.containers.docker.Common;
40+
import jdk.test.lib.containers.docker.DockerTestUtils;
41+
import jdk.test.lib.containers.docker.DockerRunOptions;
42+
import jdk.test.lib.process.OutputAnalyzer;
43+
import jdk.test.lib.process.ProcessTools;
44+
45+
46+
public class TestContainerInfo {
47+
private static final String imageName = Common.imageName("container-info");
48+
49+
public static void main(String[] args) throws Exception {
50+
if (!DockerTestUtils.canTestDocker()) {
51+
return;
52+
}
53+
54+
Common.prepareWhiteBox();
55+
DockerTestUtils.buildJdkContainerImage(imageName);
56+
57+
try {
58+
testPrintContainerInfoWithoutSwap();
59+
} finally {
60+
DockerTestUtils.removeDockerImage(imageName);
61+
}
62+
}
63+
64+
private static void testPrintContainerInfoWithoutSwap() throws Exception {
65+
Common.logNewTestCase("Test print_container_info() - without swap");
66+
67+
DockerRunOptions opts = Common.newOpts(imageName, "PrintContainerInfo")
68+
.addDockerOpts("--memory=500m")
69+
.addDockerOpts("--memory-swap=500m"); // no swap
70+
Common.addWhiteBoxOpts(opts);
71+
72+
OutputAnalyzer out = Common.run(opts);
73+
checkContainerInfo(out);
74+
}
75+
76+
private static void shouldMatchWithValue(OutputAnalyzer output, String match, String value) {
77+
output.shouldContain(match);
78+
String str = output.getOutput();
79+
for (String s : str.split(System.lineSeparator())) {
80+
if (s.contains(match)) {
81+
if (!s.contains(value)) {
82+
throw new RuntimeException("memory_swap_current_in_bytes NOT " + value + "! Line was : " + s);
83+
}
84+
}
85+
}
86+
}
87+
88+
private static void checkContainerInfo(OutputAnalyzer out) throws Exception {
89+
String str = out.getOutput();
90+
if (str.contains("cgroupv2")) {
91+
shouldMatchWithValue(out, "memory_swap_max_limit_in_bytes", "0");
92+
shouldMatchWithValue(out, "memory_swap_current_in_bytes", "0");
93+
} else {
94+
throw new SkippedException("This test is cgroups v2 specific, skipped on cgroups v1");
95+
}
96+
}
97+
}

0 commit comments

Comments
 (0)