-
Notifications
You must be signed in to change notification settings - Fork 5.8k
8354897: Support Soft/Weak Reference in AOT cache #24757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
iklam
wants to merge
18
commits into
openjdk:master
from
iklam:8354897-soft-weak-references-in-aot-cache
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a23331a
8354897: Support Soft/Weak Reference in AOT cache (imported from Leyd…
c6beb85
Added code to process Universe::reference_pending_list()
iklam 2d82e36
Avoid the need to Universe::reference_pending_list() and remove assum…
iklam dc5ad26
Added missing files
iklam 2544cc7
cleaned up test cases
iklam 6678482
Merge branch 'master' into 8354897-soft-weak-references-in-aot-cache
iklam 1898399
Work around hotspot/jtreg/sources/TestNoNULL.java
iklam 73786f2
Fixed error when running "make test JTREG=AOT_JDK=true TEST=open/test…
iklam 2c31ca7
Refactored code
iklam 643eec2
Fixed windows path issues
iklam bba16ee
@fisk comment
iklam a25aab0
@DanHeidinga comments
iklam 331acfa
@fisk offline comments -- tighten up and simplify eligibility check; …
iklam 7021656
Merge branch 'master' into 8354897-soft-weak-references-in-aot-cache
iklam 01f0ea0
@fisk comment -- use proper HeapAccess to load referent; Also refacto…
iklam 4aa6874
Renamed the internal field ReferenceQueue.NULL to NULL_QUEUE to avoid…
iklam ec735cb
@xmas92 comments
iklam f8aff12
@AlanBateman comments
iklam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
/* | ||
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
* | ||
*/ | ||
|
||
#include "cds/aotReferenceObjSupport.hpp" | ||
#include "cds/heapShared.hpp" | ||
#include "classfile/javaClasses.hpp" | ||
#include "classfile/symbolTable.hpp" | ||
#include "classfile/systemDictionary.hpp" | ||
#include "classfile/vmSymbols.hpp" | ||
#include "logging/log.hpp" | ||
#include "memory/resourceArea.hpp" | ||
#include "memory/universe.hpp" | ||
#include "oops/oop.inline.hpp" | ||
#include "oops/oopHandle.inline.hpp" | ||
#include "runtime/fieldDescriptor.inline.hpp" | ||
#include "runtime/javaCalls.hpp" | ||
#include "utilities/resourceHash.hpp" | ||
|
||
// Handling of java.lang.ref.Reference objects in the AOT cache | ||
// ============================================================ | ||
// | ||
// When AOTArtifactFinder finds an oop which is a instance of java.lang.ref.Reference: | ||
// | ||
// - We check if the oop is eligible to be stored in the AOT cache. If not, the AOT cache | ||
// creation fails -- see AOTReferenceObjSupport::check_if_ref_obj() | ||
// | ||
// - Otherwise, we store the oop into the AOT cache, but we unconditionally reset its | ||
// "next" and "discovered" fields to null. Otherwise, if AOTArtifactFinder follows these | ||
// fields, it may found unrelated objects that we don't intend to cache. | ||
// | ||
// Eligibility | ||
// =========== | ||
// | ||
// [1] A reference that does not require special clean up (i.e., Reference::queue == ReferenceQueue.NULL_QUEUE) | ||
// is eligible. | ||
// | ||
// [2] A reference that REQUIRE specials clean up (i.e., Reference::queue != ReferenceQueue.NULL_QUEUE) | ||
// is eligible ONLY if its referent is not null. | ||
// | ||
// As of this version, the only oops in group [2] that can be found by AOTArtifactFinder are | ||
// the keys used by ReferencedKeyMap in the implementation of MethodType::internTable. | ||
// stabilize_cached_reference_objects() ensures that all keys found by AOTArtifactFinder are eligible. | ||
// | ||
// The purpose of the error check in check_if_ref_obj() is to guard against changes in the JDK core | ||
// libs that might introduce new types of oops in group [2] into the AOT cache. | ||
// | ||
// Reasons for the eligibility restrictions | ||
// ======================================== | ||
// | ||
// Reference handling is complex. In this version, we implement only enough functionality to support | ||
// the use of Weak/Soft references used by java.lang.invoke. | ||
// | ||
// We intend to evolve the implementation in the future by | ||
// -- implementing more assemblySetup() operations for other use cases, and/or | ||
// -- relaxing the eligibility restrictions. | ||
// | ||
// | ||
// null referents for group [1] | ||
// ============================ | ||
// | ||
// Any cached reference R1 of group [1] is allowed to have a null referent. | ||
// This can happen in the following situations: | ||
// (a) R1.clear() was called by Java code during the assembly phase. | ||
// (b) The referent has been collected, and R1 is in the "pending" state. | ||
// In case (b), the "next" and "discovered" fields of the cached copy of R1 will | ||
// be set to null. During the production run: | ||
// - It would appear to the Java program as if immediately during VM start-up, the referent | ||
// was collected and ReferenceThread completed processing of R1. | ||
// - It would appear to the GC as if immediately during VM start-up, the Java program called | ||
// R1.clear(). | ||
|
||
#if INCLUDE_CDS_JAVA_HEAP | ||
|
||
class KeepAliveObjectsTable : public ResourceHashtable<oop, bool, | ||
36137, // prime number | ||
AnyObj::C_HEAP, | ||
mtClassShared, | ||
HeapShared::oop_hash> {}; | ||
|
||
static KeepAliveObjectsTable* _keep_alive_objs_table; | ||
static OopHandle _keep_alive_objs_array; | ||
static OopHandle _null_queue; | ||
|
||
bool AOTReferenceObjSupport::is_enabled() { | ||
// For simplicity, AOTReferenceObjSupport is enabled only when dumping method handles. | ||
// Otherwise we won't see Reference objects in the AOT cache. Let's be conservative now. | ||
return CDSConfig::is_dumping_method_handles(); | ||
} | ||
|
||
void AOTReferenceObjSupport::initialize(TRAPS) { | ||
if (!AOTReferenceObjSupport::is_enabled()) { | ||
return; | ||
} | ||
|
||
TempNewSymbol class_name = SymbolTable::new_symbol("java/lang/ref/ReferenceQueue"); | ||
Klass* k = SystemDictionary::resolve_or_fail(class_name, true, CHECK); | ||
InstanceKlass* ik = InstanceKlass::cast(k); | ||
ik->initialize(CHECK); | ||
|
||
TempNewSymbol field_name = SymbolTable::new_symbol("NULL_QUEUE"); | ||
fieldDescriptor fd; | ||
bool found = ik->find_local_field(field_name, vmSymbols::referencequeue_signature(), &fd); | ||
precond(found); | ||
precond(fd.is_static()); | ||
|
||
_null_queue = OopHandle(Universe::vm_global(), ik->java_mirror()->obj_field(fd.offset())); | ||
} | ||
|
||
// Ensure that all group [2] references found by AOTArtifactFinder are eligible. | ||
void AOTReferenceObjSupport::stabilize_cached_reference_objects(TRAPS) { | ||
if (AOTReferenceObjSupport::is_enabled()) { | ||
// This assert means that the MethodType and MethodTypeForm tables won't be | ||
// updated concurrently, so we can remove GC'ed entries ... | ||
assert(CDSConfig::allow_only_single_java_thread(), "Required"); | ||
|
||
{ | ||
TempNewSymbol method_name = SymbolTable::new_symbol("assemblySetup"); | ||
JavaValue result(T_VOID); | ||
JavaCalls::call_static(&result, vmClasses::MethodType_klass(), | ||
method_name, | ||
vmSymbols::void_method_signature(), | ||
CHECK); | ||
} | ||
|
||
{ | ||
Symbol* cds_name = vmSymbols::jdk_internal_misc_CDS(); | ||
Klass* cds_klass = SystemDictionary::resolve_or_fail(cds_name, true /*throw error*/, CHECK); | ||
TempNewSymbol method_name = SymbolTable::new_symbol("getKeepAliveObjects"); | ||
TempNewSymbol method_sig = SymbolTable::new_symbol("()[Ljava/lang/Object;"); | ||
JavaValue result(T_OBJECT); | ||
JavaCalls::call_static(&result, cds_klass, method_name, method_sig, CHECK); | ||
|
||
_keep_alive_objs_array = OopHandle(Universe::vm_global(), result.get_oop()); | ||
} | ||
} | ||
} | ||
|
||
void AOTReferenceObjSupport::init_keep_alive_objs_table() { | ||
assert_at_safepoint(); // _keep_alive_objs_table uses raw oops | ||
oop a = _keep_alive_objs_array.resolve(); | ||
if (a != nullptr) { | ||
precond(a->is_objArray()); | ||
precond(AOTReferenceObjSupport::is_enabled()); | ||
objArrayOop array = objArrayOop(a); | ||
|
||
_keep_alive_objs_table = new (mtClass)KeepAliveObjectsTable(); | ||
for (int i = 0; i < array->length(); i++) { | ||
oop obj = array->obj_at(i); | ||
_keep_alive_objs_table->put(obj, true); // The array may have duplicated entries but that's OK. | ||
} | ||
} | ||
} | ||
|
||
// Returns true IFF obj is an instance of java.lang.ref.Reference. If so, perform extra eligibility checks. | ||
bool AOTReferenceObjSupport::check_if_ref_obj(oop obj) { | ||
// We have a single Java thread. This means java.lang.ref.Reference$ReferenceHandler thread | ||
// is not running. Otherwise the checks for next/discovered may not work. | ||
precond(CDSConfig::allow_only_single_java_thread()); | ||
assert_at_safepoint(); // _keep_alive_objs_table uses raw oops | ||
|
||
if (obj->klass()->is_subclass_of(vmClasses::Reference_klass())) { | ||
precond(AOTReferenceObjSupport::is_enabled()); | ||
precond(JavaClasses::is_supported_for_archiving(obj)); | ||
precond(_keep_alive_objs_table != nullptr); | ||
|
||
// GC needs to know about this load, It will keep referent alive until the current safepoint ends. | ||
oop referent = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(obj, java_lang_ref_Reference::referent_offset()); | ||
|
||
oop queue = obj->obj_field(java_lang_ref_Reference::queue_offset()); | ||
oop next = java_lang_ref_Reference::next(obj); | ||
oop discovered = java_lang_ref_Reference::discovered(obj); | ||
bool needs_special_cleanup = (queue != _null_queue.resolve()); | ||
|
||
// If you see the errors below, you probably modified the implementation of java.lang.invoke. | ||
// Please check the comments at the top of this file. | ||
if (needs_special_cleanup && (referent == nullptr || !_keep_alive_objs_table->contains(referent))) { | ||
ResourceMark rm; | ||
|
||
log_error(cds, heap)("Cannot archive reference object " PTR_FORMAT " of class %s", | ||
p2i(obj), obj->klass()->external_name()); | ||
log_error(cds, heap)("referent = " PTR_FORMAT | ||
", queue = " PTR_FORMAT | ||
", next = " PTR_FORMAT | ||
", discovered = " PTR_FORMAT, | ||
p2i(referent), p2i(queue), p2i(next), p2i(discovered)); | ||
log_error(cds, heap)("This object requires special clean up as its queue is not ReferenceQueue::N" "ULL (" | ||
PTR_FORMAT ")", p2i(_null_queue.resolve())); | ||
log_error(cds, heap)("%s", (referent == nullptr) ? | ||
"referent cannot be null" : "referent is not registered with CDS.keepAlive()"); | ||
HeapShared::debug_trace(); | ||
MetaspaceShared::unrecoverable_writing_error(); | ||
} | ||
|
||
if (log_is_enabled(Info, cds, ref)) { | ||
ResourceMark rm; | ||
log_info(cds, ref)("Reference obj:" | ||
" r=" PTR_FORMAT | ||
" q=" PTR_FORMAT | ||
" n=" PTR_FORMAT | ||
" d=" PTR_FORMAT | ||
" %s", | ||
p2i(referent), | ||
p2i(queue), | ||
p2i(next), | ||
p2i(discovered), | ||
obj->klass()->external_name()); | ||
} | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
bool AOTReferenceObjSupport::skip_field(int field_offset) { | ||
return (field_offset == java_lang_ref_Reference::next_offset() || | ||
field_offset == java_lang_ref_Reference::discovered_offset()); | ||
} | ||
|
||
#endif // INCLUDE_CDS_JAVA_HEAP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
* | ||
*/ | ||
|
||
#ifndef SHARE_CDS_AOTREFERENCEOBJSUPPORT_HPP | ||
#define SHARE_CDS_AOTREFERENCEOBJSUPPORT_HPP | ||
|
||
#include "memory/allStatic.hpp" | ||
#include "oops/oopsHierarchy.hpp" | ||
#include "utilities/exceptions.hpp" | ||
|
||
// Support for ahead-of-time allocated instances of java.lang.ref.Reference | ||
|
||
class AOTReferenceObjSupport : AllStatic { | ||
|
||
public: | ||
static void initialize(TRAPS); | ||
static void stabilize_cached_reference_objects(TRAPS); | ||
static void init_keep_alive_objs_table() NOT_CDS_JAVA_HEAP_RETURN; | ||
static bool check_if_ref_obj(oop obj); | ||
static bool skip_field(int field_offset); | ||
static bool is_enabled(); | ||
}; | ||
|
||
#endif // SHARE_CDS_AOTREFERENCEOBJSUPPORT_HPP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be guarded with
INCLUDE_CDS_JAVA_HEAP
somehow. I guess the cleanest way would be to useNOT_CDS_JAVA_HEAP_RETURN
inaotReferenceObjSupport.hpp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added
NOT_CDS_JAVA_HEAP_RETURN
to the declaration ofinit_keep_alive_objs_table
. All the other functions are called only insideINCLUDE_CDS_JAVA_HEAP
blocks, so I didn't change them to avoid cluttering the code.