On Fri, 20 Nov 2026 11:30:46 GMT, Markus Grönlund <[email protected]> wrote:

>> Greetings,
>> 
>> ClassLoadingService records a class load during 
>> ClassFileParser::fill_instance_klass(), before the parsed class has been 
>> accepted into the class hierarchy.
>> 
>> Consequently, its counters include implementation artifacts that are not 
>> newly loaded classes:
>> 
>> * Scratch classes created during redefinition and retransformation.
>> * Discarded class definitions, such as losing candidates from competing 
>> parallel class-loading attempts.
>> 
>> These artifacts are reclaimed without corresponding class-unload accounting. 
>> The cumulative loaded-class count is therefore inflated, and the reported 
>> currently loaded count can grow even when the underlying metadata is 
>> reclaimed.
>> This affects consumers such as ClassLoadingMXBean and JFR’s class-loading 
>> statistics, potentially giving a false indication of a class leak.
>> 
>> Fix
>> Move the tally into InstanceKlass::add_to_hierarchy(), where only accepted 
>> classes enter the hierarchy. Remove the separate CDS load notification to 
>> avoid double-counting, while preserving shared class counts through 
>> in_aot_cache().
>> 
>> This corresponds to the actual installation of loaded classes, rather than 
>> class-file parsing. 
>> 
>> Noticed while working on 
>> [JDK-8392362](https://bugs.openjdk.org/browse/JDK-8392362)
>> 
>> Testing:
>> 
>> - tiers 1, 2, 3
>> - jdk_svc
>> - jdk_jfr
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Markus Grönlund has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   Synchronize class-loading counter access and stabilize JFR snapshots

Serguei’s observation that the class-loading counters need a consistent 
snapshot led down an unexpected rabbit hole.

The snapshot uses a **Read, Read, Validate (RRV)** pattern that relies on 
monotonic cumulative counters. But unfolding `PerfCounter::inc()` reveals:


inline void inc() { (*(jlong*)_valuep)++; }


This is a plain read–modify–write with no internal locking or atomic 
operations. Inspecting its callers showed class-loading increments also lacked 
writer serialization. Monotonicity could therefore not be assumed, because 
overlapping increments can lose updates, and a delayed writer can overwrite a 
newer count with an older one.

There are two distinct problems:

- **Write–write data races (UB)** from concurrent non-atomic accesses.
- **Logical races** from overlapping read–modify–write sequences.

For these counters, atomic loads and stores fix the data races. Writer 
serialization protects the complete increment by moving:


ClassLoadingService::notify_class_loaded(this, in_aot_cache());


inside the existing `Compile_lock` scope in `add_to_hierarchy()`, immediately 
after setting the class state to `loaded`.

`ClassLoadingService::notify_class_unloaded()` already runs under 
`ClassLoaderDataGraph_lock` or at a safepoint.

The reader side exposes another problem:


inline jlong get_value() { return *(jlong*)_valuep; }


Readers do not acquire the writers’ locks, so these plain reads cause 
**write–read data races (UB)**. The class-count getters also need atomic loads 
paired with atomic stores on the writer side.

Finally, the JFR periodic snapshot will use an explicit 
`OrderAccess::loadload()` between its initial and validation reads, preserving 
the ordering required by RRV.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/33973#issuecomment-7039540254

Reply via email to