Skip to content

Commit

Permalink
8253797: [cgroups v2] Account for the fact that swap accounting is di…
Browse files Browse the repository at this point in the history
…sabled on some systems

Reviewed-by: hseigel
  • Loading branch information
jerboaa committed Dec 10, 2020
1 parent 6be1f56 commit 6693611
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,21 @@ public class CgroupV2Subsystem implements CgroupSubsystem {
private static final int PER_CPU_SHARES = 1024;
private static final String MAX_VAL = "max";
private static final Object EMPTY_STR = "";
private static final long NO_SWAP = 0;

private CgroupV2Subsystem(CgroupSubsystemController unified) {
this.unified = unified;
}

private long getLongVal(String file) {
private long getLongVal(String file, long defaultValue) {
return CgroupSubsystemController.getLongValue(unified,
file,
CgroupV2SubsystemController::convertStringToLong,
CgroupSubsystem.LONG_RETVAL_UNLIMITED);
defaultValue);
}

private long getLongVal(String file) {
return getLongVal(file, CgroupSubsystem.LONG_RETVAL_UNLIMITED);
}

private static CgroupV2Subsystem initSubsystem() {
Expand Down Expand Up @@ -289,6 +294,11 @@ public long getTcpMemoryUsage() {
@Override
public long getMemoryAndSwapLimit() {
String strVal = CgroupSubsystemController.getStringValue(unified, "memory.swap.max");
// We only get a null string when file memory.swap.max doesn't exist.
// In that case we return the memory limit without any swap.
if (strVal == null) {
return getMemoryLimit();
}
long swapLimit = limitFromString(strVal);
if (swapLimit >= 0) {
long memoryLimit = getMemoryLimit();
Expand All @@ -307,9 +317,14 @@ public long getMemoryAndSwapLimit() {
*/
@Override
public long getMemoryAndSwapUsage() {
long swapUsage = getLongVal("memory.swap.current");
long memoryUsage = getMemoryUsage();
return memoryUsage + swapUsage;
if (memoryUsage >= 0) {
// If file memory.swap.current doesn't exist, only return the regular
// memory usage (without swap). Thus, use default value of NO_SWAP.
long swapUsage = getLongVal("memory.swap.current", NO_SWAP);
return memoryUsage + swapUsage;
}
return memoryUsage; // case of no memory limits
}

@Override
Expand Down
42 changes: 25 additions & 17 deletions test/lib/jdk/test/lib/containers/cgroup/MetricsTesterCgroupV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,33 @@ public void testMemorySubsystem() {
fail("memory.stat[sock]", oldVal, newVal);
}

oldVal = metrics.getMemoryAndSwapLimit();
long valSwap = getLongLimitValueFromFile("memory.swap.max");
long valMemory = getLongLimitValueFromFile("memory.max");
if (valSwap == UNLIMITED) {
newVal = valSwap;
long memAndSwapLimit = metrics.getMemoryAndSwapLimit();
long memLimit = metrics.getMemoryLimit();
// Only test swap memory limits if we can. On systems with swapaccount=0
// we cannot, as swap limits are disabled.
if (memAndSwapLimit <= memLimit) {
System.out.println("No swap memory limits, test case(s) skipped");
} else {
assert valMemory >= 0;
newVal = valSwap + valMemory;
}
if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
fail("memory.swap.max", oldVal, newVal);
}
oldVal = memAndSwapLimit;
long valSwap = getLongLimitValueFromFile("memory.swap.max");
long valMemory = getLongLimitValueFromFile("memory.max");
if (valSwap == UNLIMITED) {
newVal = valSwap;
} else {
assert valMemory >= 0;
newVal = valSwap + valMemory;
}
if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
fail("memory.swap.max", oldVal, newVal);
}

oldVal = metrics.getMemoryAndSwapUsage();
long swapUsage = getLongValueFromFile("memory.swap.current");
long memUsage = getLongValueFromFile("memory.current");
newVal = swapUsage + memUsage;
if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
fail("memory.swap.current", oldVal, newVal);
oldVal = metrics.getMemoryAndSwapUsage();
long swapUsage = getLongValueFromFile("memory.swap.current");
long memUsage = getLongValueFromFile("memory.current");
newVal = swapUsage + memUsage;
if (!CgroupMetricsTester.compareWithErrorMargin(oldVal, newVal)) {
fail("memory.swap.current", oldVal, newVal);
}
}

oldVal = metrics.getMemorySoftLimit();
Expand Down

3 comments on commit 6693611

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@ebaron
Copy link
Member

@ebaron ebaron commented on 6693611 Apr 1, 2022

Choose a reason for hiding this comment

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

/backport jdk11u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 6693611 Apr 1, 2022

Choose a reason for hiding this comment

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

@ebaron the backport was successfully created on the branch ebaron-backport-66936111 in my personal fork of openjdk/jdk11u-dev. To create a pull request with this backport targeting openjdk/jdk11u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 66936111 from the openjdk/jdk repository.

The commit being backported was authored by Severin Gehwolf on 10 Dec 2020 and was reviewed by Harold Seigel.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk11u-dev:

$ git fetch https://github.com/openjdk-bots/jdk11u-dev ebaron-backport-66936111:ebaron-backport-66936111
$ git checkout ebaron-backport-66936111
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk11u-dev ebaron-backport-66936111

Please sign in to comment.