JDK-8275735 : [linux] Remove deprecated Metrics api (kernel memory limit)
  • Type: Bug
  • Component: core-libs
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: generic
  • Submitted: 2021-10-21
  • Updated: 2022-04-25
  • Resolved: 2021-11-02
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 18
18 b22Fixed
Related Reports
Relates :  
Description
In the - cgroup v1 specific - Metrics api there is reporting of the set kernel memory limit:

    public long getKernelMemoryLimit() {
        return CgroupV1SubsystemController.longValOrUnlimited(getLongValue(memory, "memory.kmem.limit_in_bytes"));
    }

    public long getTcpMemoryLimit() {
        return CgroupV1SubsystemController.longValOrUnlimited(getLongValue(memory, "memory.kmem.tcp.limit_in_bytes"));
    }

The cgroup v1 API has been deprecated in the Linux kernel and there are container runtime implementations released which already ignore setting a kernel memory limit when being asked to do so via --kernel-memory switch.

Note that it's not supported at all on cgroups v2.

Therefore, this code should get removed as it'll stop doing anything anyway (other than reporting unlimited in any case).

Example docker-only reproducer:
# docker run --kernel-memory=200m --rm -ti fedora:34 cat sys/fs/cgroup/memory/memory.kmem.limit_in_bytes 
WARNING: Specifying a kernel memory limit is deprecated and will be removed in a future release.
9223372036854771712

References:

Kernel commit adding the deprecation:
https://github.com/torvalds/linux/commit/0158115f702b

runc container runtime PR which ignores any setting of --kernel-memory:
https://github.com/opencontainers/runc/pull/2840

podman PR which hides '--kernel-memory' as an option for 'podman run':
https://github.com/containers/podman/pull/12048

OCI runtime spec update discouraging using/implementing it in container runtimes:
https://github.com/opencontainers/runtime-spec/pull/1093
Comments
verified
25-04-2022

Changeset: 9971a2ca Author: Severin Gehwolf <sgehwolf@openjdk.org> Date: 2021-11-02 10:39:41 +0000 URL: https://git.openjdk.java.net/jdk/commit/9971a2cab3892a17f3fd39243df5ecfff5b9f108
02-11-2021

It's not used anywhere other than in this internal api and the tests.
21-10-2021

$ grep -rn 'getKernelMemoryLimit()' src/ src/java.base/linux/classes/jdk/internal/platform/CgroupV1MetricsImpl.java:52: public long getKernelMemoryLimit() { src/java.base/linux/classes/jdk/internal/platform/CgroupV1MetricsImpl.java:53: return metrics.getKernelMemoryLimit(); src/java.base/linux/classes/jdk/internal/platform/cgroupv1/CgroupV1Subsystem.java:335: public long getKernelMemoryLimit() { src/java.base/linux/classes/jdk/internal/platform/CgroupV1Metrics.java:64: public long getKernelMemoryLimit(); $ grep -rn 'getTcpMemoryLimit()' src/ src/java.base/linux/classes/jdk/internal/platform/CgroupV1MetricsImpl.java:72: public long getTcpMemoryLimit() { src/java.base/linux/classes/jdk/internal/platform/CgroupV1MetricsImpl.java:73: return metrics.getTcpMemoryLimit(); src/java.base/linux/classes/jdk/internal/platform/cgroupv1/CgroupV1Subsystem.java:351: public long getTcpMemoryLimit() { src/java.base/linux/classes/jdk/internal/platform/CgroupV1Metrics.java:104: public long getTcpMemoryLimit(); $ grep -rn -E 'getKernelMemoryLimit\(\)|getTcpMemoryLimit\(\)' test test/lib/jdk/test/lib/containers/cgroup/MetricsTesterCgroupV1.java:250: oldVal = metrics.getKernelMemoryLimit(); test/lib/jdk/test/lib/containers/cgroup/MetricsTesterCgroupV1.java:276: oldVal = metrics.getTcpMemoryLimit(); test/jdk/jdk/internal/platform/docker/MetricsMemoryTester.java:128: long kmemlimit = mCgroupV1.getKernelMemoryLimit();
21-10-2021