JDK-8238356 : CodeHeap::blob_count() overestimates the number of blobs
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 9,10,11,12,13,14,15
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2020-01-31
  • Updated: 2020-06-05
  • Resolved: 2020-02-19
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 14 JDK 15
11.0.8-oracleFixed 13.0.4Fixed 14.0.2Fixed 15 b11Fixed
Description
While working on JDK-8238247, I realized that CodeHeap::blob_count() miscalculates the blob count. If you look at the usages of CodeHeap::_blob_count field, it is only incremented, never decremented. Which is almost fine, but it leads to blob_count miscalculation: when block is freed and put on free list and re-acquired from the free list, it is counted as new blob all over again.

So, either way should be done:
 a) Decrement _blob_count on addition to the free list, which would make CodeHeap::blob_count() report the number of alive blobs;
 b) Stop incrementing blob_count on removal from the free list, which would make CodeHeap::blob_count() report the number of alive+free blobs.

Sample patch for option (a):

diff -r e4e6c1846d31 src/hotspot/share/memory/heap.cpp
--- a/src/hotspot/share/memory/heap.cpp Fri Jan 31 19:00:14 2020 +0100
+++ b/src/hotspot/share/memory/heap.cpp Fri Jan 31 19:00:17 2020 +0100
@@ -609,10 +609,13 @@
 void CodeHeap::add_to_freelist(HeapBlock* a) {
   FreeBlock* b = (FreeBlock*)a;
   size_t  bseg = segment_for(b);
   _freelist_length++;
 
+  _blob_count--;
+  assert(_blob_count >= 0, "sanity");
+
   assert(b != _freelist, "cannot be removed twice");
 
   // Mark as free and update free space count
   _freelist_segments += b->length();
   b->set_free();

Comments
Fix request (13u): The original change applies cleanly, tier1 tests pass fine.
05-06-2020

Fix Request (JDK 14.0.2) This patch fixes diagnostic information and has already been backported to JDK 11u. The fix is low risk and applies cleanly to JDK 14.0.2. In addition to CI testing through all tiers in JDK 15 and 11u, additional testing will be executed in JDK 14.0.2 before pushing.
06-03-2020

Fix Request (11u) This improves diagnostics (that counter is fed into JFR?) and keeps codebases in sync (I see 11.0.8-oracle). Patch applies cleanly to 11u, passes tier{1,2,3} tests.
29-02-2020

The code in question seems to be added by JDK-8067378, confidential. It does not seem to be in 8u, so it is probably unaffected.
28-02-2020

URL: https://hg.openjdk.java.net/jdk/jdk/rev/dbe22aa857d9 User: rraghavan Date: 2020-02-19 09:33:43 +0000
19-02-2020