JDK-8242480 : Negative value may be returned by getFreeSwapSpaceSize() in the docker
  • Type: Bug
  • Component: core-svc
  • Sub-Component: java.lang.management
  • Affected Version: 8,11,15
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: generic
  • Submitted: 2020-04-10
  • Updated: 2022-03-16
  • Resolved: 2020-04-18
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 11 JDK 13 JDK 15 JDK 8 Other
11.0.10-oracleFixed 13.0.6Fixed 15 b20Fixed 8u281Fixed openjdk8u282Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
Negative values were returned by getFreeSwapSpaceSize() in our docker testing.
The reason is that current implementation doesn't consider the situation when memory.limit_in_bytes == memory.memsw.limit_in_bytes, which means do not use the swap space at all.

In src/jdk.management/unix/classes/com/sun/management/internal/OperatingSystemImpl.java, let's see
------------------------------------------------
 71     public long getFreeSwapSpaceSize() {
 72         if (containerMetrics != null) {
 73             long memSwapLimit = containerMetrics.getMemoryAndSwapLimit();
 74             long memLimit = containerMetrics.getMemoryLimit();
 75             if (memSwapLimit >= 0 && memLimit >= 0) {
 76                 for (int attempt = 0; attempt < MAX_ATTEMPTS_NUMBER; attempt++) {
 77                     long memSwapUsage = containerMetrics.getMemoryAndSwapUsage();
 78                     long memUsage = containerMetrics.getMemoryUsage();
 79                     if (memSwapUsage > 0 && memUsage > 0) {
 80                         // We read "memory usage" and "memory and swap usage" not atomically,
 81                         // and it's possible to get the negative value when subtracting these two.
 82                         // If this happens just retry the loop for a few iterations.
 83                         if ((memSwapUsage - memUsage) >= 0) {
 84                             return memSwapLimit - memLimit - (memSwapUsage - memUsage);
 85                         }
 86                     }
 87                 }
 88             }
 89         }
 90         return getFreeSwapSpaceSize0();
 91     }
------------------------------------------------
If memSwapLimit (@line 73) equals memLimit (@line 74), then getFreeSwapSpaceSize() may return a negative value @line 84.
Comments
Fix request (13u) Requesting backport to 13u for parity with 11u. The patch applies cleanly. Tested with container tests, the only failure is containers/docker/TestMemoryAwareness.java which is fixed by follow-up JDK-8246648, the plan is to push them together.
03-12-2020

Fix Request (OpenJDK 8u): Please approve backporting this to OpenJDK 8u for Oracle JDK parity. It depends on JDK-8221710 (in openjdk8u282) for the DockerTestUtils.RETAIN_IMAGE_AFTER_TEST symbol. And on 8-only bug JDK-8257397 fixing a runtime issue for the included test. A follow-up is JDK-8246648 which is also waiting for approval. The patch did not apply cleanly due to some test differences. It was reviewed by Paul Hohensee and Andrew Hughes. Testing: new regression test and container tests. Risk seems low. webrev: https://cr.openjdk.java.net/~sgehwolf/webrevs/JDK-8242480/jdk8/02/webrev/ RFR: https://mail.openjdk.java.net/pipermail/jdk8u-dev/2020-November/012921.html
30-11-2020

Comment.java change needs to be split out into its own bug. It is unrelated to this change.
30-11-2020

Fix request (11u) -- will label after testing completed. I would like to downport this for parity with 11.0.10-oracle. Applies clean.
01-10-2020

URL: https://hg.openjdk.java.net/jdk/jdk/rev/ed5d312d82b6 User: jiefu Date: 2020-04-18 01:12:28 +0000
18-04-2020

RFR: https://mail.openjdk.java.net/pipermail/serviceability-dev/2020-April/031067.html
10-04-2020