-
Notifications
You must be signed in to change notification settings - Fork 5.9k
8352738: Implement JEP 520: JFR Method Timing and Tracing #25306
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
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2e5ade8
Initial
egahlin 489fd83
Reviewer feedback
egahlin c274da4
Atomic::load_acquire(&_method_tracer_state);
egahlin 585a929
Merge
egahlin 5f5a42a
Remove METHOD_FLAG_USED_PREVIOUS_EPOCH_BIT(method)
egahlin 4b28ae9
Improve comment
egahlin 5378722
Fix comment
egahlin 2ca64a1
Merge
egahlin 73025f1
Add back sticky bit
egahlin 06ad6fa
Fix indentation and synchronized
egahlin 31d602b
Atomic::release_store
egahlin 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
187 changes: 187 additions & 0 deletions
187
src/hotspot/share/jfr/instrumentation/jfrClassTransformer.cpp
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,187 @@ | ||
/* | ||
* Copyright (c) 2016, 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 "classfile/classFileParser.hpp" | ||
#include "classfile/classFileStream.hpp" | ||
#include "classfile/classLoadInfo.hpp" | ||
#include "classfile/javaClasses.inline.hpp" | ||
#include "classfile/symbolTable.hpp" | ||
#include "jfr/instrumentation/jfrClassTransformer.hpp" | ||
#include "jfr/recorder/service/jfrOptionSet.hpp" | ||
#include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp" | ||
#include "logging/log.hpp" | ||
#include "memory/allocation.inline.hpp" | ||
#include "memory/resourceArea.hpp" | ||
#include "oops/instanceKlass.hpp" | ||
#include "oops/klass.inline.hpp" | ||
#include "prims/jvmtiRedefineClasses.hpp" | ||
#include "prims/jvmtiThreadState.hpp" | ||
#include "runtime/handles.inline.hpp" | ||
#include "runtime/javaThread.hpp" | ||
#include "utilities/exceptions.hpp" | ||
#include "utilities/globalDefinitions.hpp" | ||
#include "utilities/growableArray.hpp" | ||
#include "utilities/macros.hpp" | ||
|
||
static void log_pending_exception(oop throwable) { | ||
assert(throwable != nullptr, "invariant"); | ||
oop msg = java_lang_Throwable::message(throwable); | ||
if (msg != nullptr) { | ||
char* text = java_lang_String::as_utf8_string(msg); | ||
if (text != nullptr) { | ||
log_error(jfr, system) ("%s", text); | ||
} | ||
} | ||
} | ||
|
||
// On initial class load. | ||
void JfrClassTransformer::cache_class_file_data(InstanceKlass* new_ik, const ClassFileStream* new_stream, const JavaThread* thread) { | ||
assert(new_ik != nullptr, "invariant"); | ||
assert(new_stream != nullptr, "invariant"); | ||
assert(thread != nullptr, "invariant"); | ||
assert(!thread->has_pending_exception(), "invariant"); | ||
if (!JfrOptionSet::allow_retransforms()) { | ||
return; | ||
} | ||
const jint stream_len = new_stream->length(); | ||
JvmtiCachedClassFileData* p = | ||
(JvmtiCachedClassFileData*)NEW_C_HEAP_ARRAY_RETURN_NULL(u1, offset_of(JvmtiCachedClassFileData, data) + stream_len, mtInternal); | ||
if (p == nullptr) { | ||
log_error(jfr, system)("Allocation using C_HEAP_ARRAY for %zu bytes failed in JfrEventClassTransformer::cache_class_file_data", | ||
static_cast<size_t>(offset_of(JvmtiCachedClassFileData, data) + stream_len)); | ||
return; | ||
} | ||
p->length = stream_len; | ||
memcpy(p->data, new_stream->buffer(), stream_len); | ||
new_ik->set_cached_class_file(p); | ||
} | ||
|
||
InstanceKlass* JfrClassTransformer::create_instance_klass(InstanceKlass*& ik, ClassFileStream* stream, bool is_initial_load, JavaThread* thread) { | ||
if (stream == nullptr) { | ||
if (is_initial_load) { | ||
log_error(jfr, system)("JfrClassTransformer: unable to create ClassFileStream for %s", ik->external_name()); | ||
} | ||
return nullptr; | ||
} | ||
InstanceKlass* const new_ik = create_new_instance_klass(ik, stream, thread); | ||
if (new_ik == nullptr) { | ||
if (is_initial_load) { | ||
log_error(jfr, system)("JfrClassTransformer: unable to create InstanceKlass for %s", ik->external_name()); | ||
} | ||
} | ||
return new_ik; | ||
} | ||
|
||
void JfrClassTransformer::copy_traceid(const InstanceKlass* ik, const InstanceKlass* new_ik) { | ||
assert(ik != nullptr, "invariant"); | ||
assert(new_ik != nullptr, "invariant"); | ||
new_ik->set_trace_id(ik->trace_id()); | ||
assert(TRACE_ID(ik) == TRACE_ID(new_ik), "invariant"); | ||
} | ||
|
||
InstanceKlass* JfrClassTransformer::create_new_instance_klass(InstanceKlass* ik, ClassFileStream* stream, TRAPS) { | ||
assert(stream != nullptr, "invariant"); | ||
ResourceMark rm(THREAD); | ||
ClassLoaderData* const cld = ik->class_loader_data(); | ||
Handle pd(THREAD, ik->protection_domain()); | ||
Symbol* const class_name = ik->name(); | ||
ClassLoadInfo cl_info(pd); | ||
ClassFileParser new_parser(stream, | ||
class_name, | ||
cld, | ||
&cl_info, | ||
ClassFileParser::INTERNAL, // internal visibility | ||
THREAD); | ||
if (HAS_PENDING_EXCEPTION) { | ||
log_pending_exception(PENDING_EXCEPTION); | ||
CLEAR_PENDING_EXCEPTION; | ||
return nullptr; | ||
} | ||
const ClassInstanceInfo* cl_inst_info = cl_info.class_hidden_info_ptr(); | ||
InstanceKlass* const new_ik = new_parser.create_instance_klass(false, *cl_inst_info, THREAD); | ||
if (HAS_PENDING_EXCEPTION) { | ||
log_pending_exception(PENDING_EXCEPTION); | ||
CLEAR_PENDING_EXCEPTION; | ||
return nullptr; | ||
} | ||
assert(new_ik != nullptr, "invariant"); | ||
assert(new_ik->name() != nullptr, "invariant"); | ||
assert(ik->name() == new_ik->name(), "invariant"); | ||
return new_ik; | ||
} | ||
|
||
// Redefining / retransforming? | ||
const Klass* JfrClassTransformer::find_existing_klass(const InstanceKlass* ik, JavaThread* thread) { | ||
assert(ik != nullptr, "invariant"); | ||
assert(thread != nullptr, "invariant"); | ||
JvmtiThreadState* const state = thread->jvmti_thread_state(); | ||
return state != nullptr ? klass_being_redefined(ik, state) : nullptr; | ||
egahlin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
const Klass* JfrClassTransformer::klass_being_redefined(const InstanceKlass* ik, JvmtiThreadState* state) { | ||
assert(ik != nullptr, "invariant"); | ||
assert(state != nullptr, "invariant"); | ||
const GrowableArray<Klass*>* const redef_klasses = state->get_classes_being_redefined(); | ||
if (redef_klasses == nullptr || redef_klasses->is_empty()) { | ||
return nullptr; | ||
} | ||
for (int i = 0; i < redef_klasses->length(); ++i) { | ||
const Klass* const existing_klass = redef_klasses->at(i); | ||
assert(existing_klass != nullptr, "invariant"); | ||
if (ik->name() == existing_klass->name() && ik->class_loader_data() == existing_klass->class_loader_data()) { | ||
// 'ik' is a scratch klass. Return the klass being redefined. | ||
return existing_klass; | ||
} | ||
} | ||
return nullptr; | ||
} | ||
|
||
// On redefine / retransform, in case an agent modified the class, the original bytes are cached onto the scratch klass. | ||
void JfrClassTransformer::transfer_cached_class_file_data(InstanceKlass* ik, InstanceKlass* new_ik, const ClassFileParser& parser, JavaThread* thread) { | ||
assert(ik != nullptr, "invariant"); | ||
assert(new_ik != nullptr, "invariant"); | ||
JvmtiCachedClassFileData* const p = ik->get_cached_class_file(); | ||
if (p != nullptr) { | ||
new_ik->set_cached_class_file(p); | ||
ik->set_cached_class_file(nullptr); | ||
return; | ||
} | ||
// No cached classfile indicates that no agent modified the klass. | ||
// This means that the parser is holding the original bytes. Hence, we cache it onto the scratch klass. | ||
const ClassFileStream* const stream = parser.clone_stream(); | ||
cache_class_file_data(new_ik, stream, thread); | ||
} | ||
|
||
void JfrClassTransformer::rewrite_klass_pointer(InstanceKlass*& ik, InstanceKlass* new_ik, ClassFileParser& parser, const JavaThread* thread) { | ||
assert(ik != nullptr, "invariant"); | ||
assert(new_ik != nullptr, "invariant"); | ||
assert(thread != nullptr, "invariant"); | ||
assert(TRACE_ID(ik) == TRACE_ID(new_ik), "invariant"); | ||
assert(!thread->has_pending_exception(), "invariant"); | ||
// Assign original InstanceKlass* back onto "its" parser object for proper destruction. | ||
parser.set_klass_to_deallocate(ik); | ||
// Finally rewrite the original pointer to the newly created InstanceKlass. | ||
ik = new_ik; | ||
} | ||
|
52 changes: 52 additions & 0 deletions
52
src/hotspot/share/jfr/instrumentation/jfrClassTransformer.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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_JFR_INSTRUMENTATION_JFRCLASSTRANSFORMER_HPP | ||
#define SHARE_JFR_INSTRUMENTATION_JFRCLASSTRANSFORMER_HPP | ||
|
||
#include "memory/allStatic.hpp" | ||
#include "utilities/exceptions.hpp" | ||
|
||
class ClassFileParser; | ||
class ClassFileStream; | ||
class InstanceKlass; | ||
|
||
/* | ||
* Contains common functionality used by method and event instrumentation. | ||
*/ | ||
class JfrClassTransformer : AllStatic { | ||
private: | ||
static InstanceKlass* create_new_instance_klass(InstanceKlass* ik, ClassFileStream* stream, TRAPS); | ||
static const Klass* klass_being_redefined(const InstanceKlass* ik, JvmtiThreadState* state); | ||
|
||
public: | ||
static const Klass* find_existing_klass(const InstanceKlass* ik, JavaThread* thread); | ||
static InstanceKlass* create_instance_klass(InstanceKlass*& ik, ClassFileStream* stream, bool is_initial_load, JavaThread* thread); | ||
static void copy_traceid(const InstanceKlass* ik, const InstanceKlass* new_ik); | ||
static void transfer_cached_class_file_data(InstanceKlass* ik, InstanceKlass* new_ik, const ClassFileParser& parser, JavaThread* thread); | ||
static void rewrite_klass_pointer(InstanceKlass*& ik, InstanceKlass* new_ik, ClassFileParser& parser, const JavaThread* thread); | ||
static void cache_class_file_data(InstanceKlass* new_ik, const ClassFileStream* new_stream, const JavaThread* thread); | ||
}; | ||
|
||
#endif // SHARE_JFR_INSTRUMENTATION_JFRCLASSTRANSFORMER_HPP |
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.
Uh oh!
There was an error while loading. Please reload this page.