8299858: [Metrics] Swap memory limit reported incorrectly when too large

Reviewed-by: stuefe
This commit is contained in:
Severin Gehwolf 2023-01-26 14:50:46 +00:00
parent 28545dcf2b
commit 64ddf9536f
3 changed files with 67 additions and 3 deletions

View file

@ -122,9 +122,11 @@ public class CgroupMetrics implements Metrics {
@Override
public long getMemoryLimit() {
long subsMem = subsystem.getMemoryLimit();
long systemTotal = getTotalMemorySize0();
assert(systemTotal > 0);
// Catch the cgroup memory limit exceeding host physical memory.
// Treat this as unlimited.
if (subsMem >= getTotalMemorySize0()) {
if (subsMem >= systemTotal) {
return CgroupSubsystem.LONG_RETVAL_UNLIMITED;
}
return subsMem;
@ -142,7 +144,15 @@ public class CgroupMetrics implements Metrics {
@Override
public long getMemoryAndSwapLimit() {
return subsystem.getMemoryAndSwapLimit();
long totalSystemMemSwap = getTotalMemorySize0() + getTotalSwapSize0();
assert(totalSystemMemSwap > 0);
// Catch the cgroup memory and swap limit exceeding host physical swap
// and memory. Treat this case as unlimited.
long subsSwapMem = subsystem.getMemoryAndSwapLimit();
if (subsSwapMem >= totalSystemMemSwap) {
return CgroupSubsystem.LONG_RETVAL_UNLIMITED;
}
return subsSwapMem;
}
@Override
@ -185,5 +195,6 @@ public class CgroupMetrics implements Metrics {
private static native boolean isUseContainerSupport();
private static native long getTotalMemorySize0();
private static native long getTotalSwapSize0();
}

View file

@ -23,6 +23,7 @@
* questions.
*/
#include <unistd.h>
#include <sys/sysinfo.h>
#include "jni.h"
#include "jvm.h"
@ -43,3 +44,15 @@ Java_jdk_internal_platform_CgroupMetrics_getTotalMemorySize0
jlong page_size = sysconf(_SC_PAGESIZE);
return pages * page_size;
}
JNIEXPORT jlong JNICALL
Java_jdk_internal_platform_CgroupMetrics_getTotalSwapSize0
(JNIEnv *env, jclass ignored)
{
struct sysinfo si;
int retval = sysinfo(&si);
if (retval < 0) {
return 0; // syinfo failed, treat as no swap
}
return (jlong)si.totalswap;
}