Navigation Menu

Skip to content

Commit

Permalink
8284950: CgroupV1 detection code should consider memory.swappiness
Browse files Browse the repository at this point in the history
Backport-of: 46d208fb1ce9a3d45bee2afda824f15c84a5e4d2
  • Loading branch information
bobpengxie authored and DamonFool committed Aug 23, 2022
1 parent 2796eca commit b30d962
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 5 deletions.
26 changes: 23 additions & 3 deletions src/hotspot/os/linux/cgroupV1Subsystem_linux.cpp
Expand Up @@ -136,19 +136,39 @@ jlong CgroupV1Subsystem::memory_and_swap_limit_in_bytes() {
const char* matchline = "hierarchical_memsw_limit";
const char* format = "%s " JULONG_FORMAT;
GET_CONTAINER_INFO_LINE(julong, _memory->controller(), "/memory.stat", matchline,
"Hierarchical Memory and Swap Limit is : " JULONG_FORMAT, format, hier_memlimit)
if (hier_memlimit >= _unlimited_memory) {
"Hierarchical Memory and Swap Limit is : " JULONG_FORMAT, format, hier_memswlimit)
if (hier_memswlimit >= _unlimited_memory) {
log_trace(os, container)("Hierarchical Memory and Swap Limit is: Unlimited");
} else {
return (jlong)hier_memlimit;
jlong swappiness = read_mem_swappiness();
if (swappiness == 0) {
const char* matchmemline = "hierarchical_memory_limit";
GET_CONTAINER_INFO_LINE(julong, _memory->controller(), "/memory.stat", matchmemline,
"Hierarchical Memory Limit is : " JULONG_FORMAT, format, hier_memlimit)
log_trace(os, container)("Memory and Swap Limit has been reset to " JULONG_FORMAT " because swappiness is 0", hier_memlimit);
return (jlong)hier_memlimit;
}
return (jlong)hier_memswlimit;
}
}
return (jlong)-1;
} else {
jlong swappiness = read_mem_swappiness();
if (swappiness == 0) {
jlong memlimit = read_memory_limit_in_bytes();
log_trace(os, container)("Memory and Swap Limit has been reset to " JULONG_FORMAT " because swappiness is 0", memlimit);
return memlimit;
}
return (jlong)memswlimit;
}
}

jlong CgroupV1Subsystem::read_mem_swappiness() {
GET_CONTAINER_INFO(julong, _memory->controller(), "/memory.swappiness",
"Swappiness is: " JULONG_FORMAT, JULONG_FORMAT, swappiness);
return swappiness;
}

jlong CgroupV1Subsystem::memory_soft_limit_in_bytes() {
GET_CONTAINER_INFO(julong, _memory->controller(), "/memory.soft_limit_in_bytes",
"Memory Soft Limit is: " JULONG_FORMAT, JULONG_FORMAT, memsoftlimit);
Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/os/linux/cgroupV1Subsystem_linux.hpp
Expand Up @@ -108,6 +108,8 @@ class CgroupV1Subsystem: public CgroupSubsystem {

char * pids_max_val();

jlong read_mem_swappiness();

public:
CgroupV1Subsystem(CgroupV1Controller* cpuset,
CgroupV1Controller* cpu,
Expand Down
Expand Up @@ -150,8 +150,9 @@ private static CgroupV1Subsystem initSubSystem(Map<String, CgroupInfo> infos) {
}

private static boolean getSwapEnabled(CgroupV1MemorySubSystemController controller) {
long retval = getLongValue(controller, "memory.memsw.limit_in_bytes");
return retval > 0;
long memswBytes = getLongValue(controller, "memory.memsw.limit_in_bytes");
long swappiness = getLongValue(controller, "memory.swappiness");
return (memswBytes > 0 && swappiness > 0);
}


Expand Down
114 changes: 114 additions & 0 deletions test/hotspot/jtreg/containers/docker/TestMemoryWithCgroupV1.java
@@ -0,0 +1,114 @@
/*
* Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import jdk.test.lib.containers.docker.Common;
import jdk.test.lib.containers.docker.DockerTestUtils;
import jdk.test.lib.containers.docker.DockerRunOptions;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import jdk.internal.platform.Metrics;

/*
* @test
* @key cgroups
* @requires os.family == "linux"
* @modules java.base/jdk.internal.platform
* @library /test/lib
* @build sun.hotspot.WhiteBox PrintContainerInfo CheckOperatingSystemMXBean
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar whitebox.jar sun.hotspot.WhiteBox
* @run main TestMemoryWithCgroupV1
*/
public class TestMemoryWithCgroupV1 {

private static final String imageName = Common.imageName("memory");

public static void main(String[] args) throws Exception {
// If cgroups is not configured, report success.
Metrics metrics = Metrics.systemMetrics();
if (metrics == null) {
System.out.println("TEST PASSED!!!");
return;
}
if ("cgroupv1".equals(metrics.getProvider())) {
if (!DockerTestUtils.canTestDocker()) {
return;
}

Common.prepareWhiteBox();
DockerTestUtils.buildJdkContainerImage(imageName);

try {
testMemoryLimitWithSwappiness("100M", "150M", "100.00M",
Integer.toString(((int) Math.pow(2, 20)) * 150),
Integer.toString(((int) Math.pow(2, 20)) * 100));
testOSBeanSwappinessMemory("200m", "250m", "0", "0");
} finally {
DockerTestUtils.removeDockerImage(imageName);
}
} else {
System.out.println("Memory swappiness not supported with cgroups v2. Test skipped.");
}
System.out.println("TEST PASSED!!!");
}

private static void testMemoryLimitWithSwappiness(String dockerMemLimit, String dockerSwapMemLimit,
String expectedLimit, String expectedReadLimit, String expectedResetLimit)
throws Exception {
Common.logNewTestCase("Test print_container_info()");

DockerRunOptions opts = Common.newOpts(imageName, "PrintContainerInfo").addJavaOpts("-XshowSettings:system");
opts.addDockerOpts("--memory", dockerMemLimit, "--memory-swappiness", "0", "--memory-swap", dockerSwapMemLimit);
Common.addWhiteBoxOpts(opts);

OutputAnalyzer out = Common.run(opts);
out.shouldContain("Memory and Swap Limit is: " + expectedReadLimit)
.shouldContain(
"Memory and Swap Limit has been reset to " + expectedResetLimit + " because swappiness is 0")
.shouldContain("Memory & Swap Limit: " + expectedLimit);
}

private static void testOSBeanSwappinessMemory(String memoryAllocation, String swapAllocation,
String swappiness, String expectedSwap) throws Exception {
Common.logNewTestCase("Check OperatingSystemMXBean");
DockerRunOptions opts = Common.newOpts(imageName, "CheckOperatingSystemMXBean")
.addDockerOpts(
"--memory", memoryAllocation,
"--memory-swappiness", swappiness,
"--memory-swap", swapAllocation)
// CheckOperatingSystemMXBean uses Metrics (jdk.internal.platform) for
// diagnostics
.addJavaOpts("--add-exports")
.addJavaOpts("java.base/jdk.internal.platform=ALL-UNNAMED");
OutputAnalyzer out = DockerTestUtils.dockerRunJava(opts);
// in case of warnings like : "Your kernel does not support swap limit
// capabilities or the cgroup is not mounted. Memory limited without swap."
// the getTotalSwapSpaceSize and getFreeSwapSpaceSize return the system
// values as the container setup isn't supported in that case.
try {
out.shouldContain("OperatingSystemMXBean.getTotalSwapSpaceSize: " + expectedSwap);
} catch (RuntimeException ex) {
out.shouldMatch("OperatingSystemMXBean.getTotalSwapSpaceSize: [0-9]+");
}
}

}

1 comment on commit b30d962

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.