-
Notifications
You must be signed in to change notification settings - Fork 11
Refactor stackwalker #451
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
Open
zhengyu123
wants to merge
10
commits into
main
Choose a base branch
from
zgu/refactor-stackwalk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactor stackwalker #451
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
df4d238
v0
zhengyu123 1088c74
v1
zhengyu123 7f303a7
v2
zhengyu123 9a9de6c
Fix VMMethod::validatedId() crash
zhengyu123 6a5924b
Revert previous commit
zhengyu123 44fb879
Fix memory ordering and add comment
zhengyu123 a839b3c
Merge branch 'main' into zgu/refactor-stackwalk
zhengyu123 b035e63
Code review
zhengyu123 f9f527f
Code review
zhengyu123 8fabf90
Review by Opus 4.6
zhengyu123 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #ifndef _FRAMES_H | ||
| #define _FRAMES_H | ||
|
|
||
| #include <jni.h> | ||
| #include "vmEntry.h" | ||
|
|
||
| inline int makeFrame(ASGCT_CallFrame *frames, jint type, jmethodID id) { | ||
| frames[0].bci = type; | ||
| frames[0].method_id = id; | ||
| return 1; | ||
| } | ||
|
|
||
| inline int makeFrame(ASGCT_CallFrame *frames, jint type, | ||
| const char *id) { | ||
| return makeFrame(frames, type, (jmethodID)id); | ||
| } | ||
|
|
||
| #endif // _FRAMES_H |
Large diffs are not rendered by default.
Oops, something went wrong.
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,32 @@ | ||
| /* | ||
| * Copyright The async-profiler authors | ||
| * Copyright 2026, Datadog, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef _HOTSPOT_HOTSPOTSUPPORT_H | ||
| #define _HOTSPOT_HOTSPOTSUPPORT_H | ||
|
|
||
| #include "stackWalker.h" | ||
|
|
||
| class ProfiledThread; | ||
|
|
||
| class HotspotSupport { | ||
| private: | ||
| static int walkVM(void* ucontext, ASGCT_CallFrame* frames, int max_depth, | ||
| StackWalkFeatures features, EventType event_type, | ||
| const void* pc, uintptr_t sp, uintptr_t fp, int lock_index, bool* truncated); | ||
| static int walkVM(void* ucontext, ASGCT_CallFrame* frames, int max_depth, | ||
| StackWalkFeatures features, EventType event_type, | ||
| int lock_index, bool* truncated = nullptr); | ||
|
|
||
| static int getJavaTraceAsync(void *ucontext, ASGCT_CallFrame *frames, | ||
| int max_depth, StackContext *java_ctx, | ||
| bool *truncated); | ||
|
|
||
| public: | ||
| static void checkFault(ProfiledThread* thrd = nullptr); | ||
| static int walkJavaStack(StackWalkRequest& request); | ||
| }; | ||
|
|
||
| #endif // _HOTSPOT_HOTSPOTSUPPORT_H |
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,51 @@ | ||
| /* | ||
| * Copyright 2026, Datadog, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include "jitCodeCache.h" | ||
|
|
||
| #include "hotspot/vmStructs.h" | ||
|
|
||
| SpinLock JitCodeCache::_stubs_lock; | ||
| CodeCache JitCodeCache::_runtime_stubs("[stubs]"); | ||
| std::atomic<const void *> JitCodeCache::_call_stub_begin = { nullptr }; | ||
| std::atomic<const void *> JitCodeCache::_call_stub_end = { nullptr }; | ||
|
|
||
| // CompiledMethodLoad is also needed to enable DebugNonSafepoints info by | ||
| // default | ||
| void JNICALL JitCodeCache::CompiledMethodLoad(jvmtiEnv *jvmti, jmethodID method, | ||
| jint code_size, const void *code_addr, | ||
| jint map_length, | ||
| const jvmtiAddrLocationMap *map, | ||
| const void *compile_info) { | ||
| CodeHeap::updateBounds(code_addr, (const char *)code_addr + code_size); | ||
| } | ||
|
|
||
| void JNICALL JitCodeCache::DynamicCodeGenerated(jvmtiEnv *jvmti, const char *name, | ||
| const void *address, jint length) { | ||
| _stubs_lock.lock(); | ||
| _runtime_stubs.add(address, length, name, true); | ||
| _stubs_lock.unlock(); | ||
|
|
||
| if (name[0] == 'I' && strcmp(name, "Interpreter") == 0) { | ||
| CodeHeap::setInterpreterStart(address); | ||
| } else if (strcmp(name, "call_stub") == 0) { | ||
| _call_stub_begin.store(address, std::memory_order_relaxed); | ||
| // This fence ensures that _call_stub_begin is visible before _call_stub_end, so that isCallStub() works correctly | ||
| std::atomic_thread_fence(std::memory_order_release); | ||
| _call_stub_end.store((const char *)address + length, std::memory_order_relaxed); | ||
| } | ||
|
|
||
| CodeHeap::updateBounds(address, (const char *)address + length); | ||
| } | ||
|
|
||
| CodeBlob* JitCodeCache::findRuntimeStub(const void *address) { | ||
| CodeBlob *stub = nullptr; | ||
| _stubs_lock.lockShared(); | ||
| if (_runtime_stubs.contains(address)) { | ||
| stub = _runtime_stubs.findBlobByAddress(address); | ||
| } | ||
| _stubs_lock.unlockShared(); | ||
| return stub; | ||
| } |
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,41 @@ | ||
| /* | ||
| * Copyright 2026, Datadog, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef _HOTSPOT_JITCODECACHE_H | ||
| #define _HOTSPOT_JITCODECACHE_H | ||
|
|
||
| #include <atomic> | ||
|
|
||
| #include "codeCache.h" | ||
| #include "spinLock.h" | ||
|
|
||
| // The class tracks JIT-compiled code and ranges | ||
| class JitCodeCache { | ||
| private: | ||
| static SpinLock _stubs_lock; | ||
| static CodeCache _runtime_stubs; | ||
| static std::atomic<const void *> _call_stub_begin; | ||
| static std::atomic<const void *> _call_stub_end; | ||
|
|
||
| public: | ||
| static void JNICALL CompiledMethodLoad(jvmtiEnv *jvmti, jmethodID method, | ||
| jint code_size, const void *code_addr, | ||
| jint map_length, | ||
| const jvmtiAddrLocationMap *map, | ||
| const void *compile_info); | ||
| static void JNICALL DynamicCodeGenerated(jvmtiEnv *jvmti, const char *name, | ||
| const void *address, jint length); | ||
|
|
||
| static inline bool isCallStub(const void *address) { | ||
| const void* stub_end = _call_stub_end.load(std::memory_order_acquire); | ||
| return stub_end != nullptr && | ||
| address >= _call_stub_begin.load(std::memory_order_relaxed) && | ||
| address < stub_end; | ||
| } | ||
|
|
||
| static CodeBlob* findRuntimeStub(const void *address); | ||
| }; | ||
|
|
||
| #endif // _HOTSPOT_JITCODECACHE_H |
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,56 @@ | ||
| /* | ||
| * Copyright 2026, Datadog, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include "jvmSupport.h" | ||
|
|
||
| #include "asyncSampleMutex.h" | ||
| #include "frames.h" | ||
| #include "os.h" | ||
| #include "profiler.h" | ||
| #include "thread.h" | ||
|
|
||
| #include "hotspot/hotspotSupport.h" | ||
|
|
||
| #include <jni.h> | ||
|
|
||
| int JVMSupport::walkJavaStack(StackWalkRequest& request) { | ||
| if (VM::isHotspot()) { | ||
| return HotspotSupport::walkJavaStack(request); | ||
| } else if (VM::isOpenJ9() || VM::isZing()) { | ||
| assert(request.event_type == BCI_CPU || request.event_type == BCI_WALL); | ||
| return asyncGetCallTrace(request.frames, request.max_depth, request.ucontext); | ||
| } | ||
| assert(false && "Unsupported JVM"); | ||
| return 0; | ||
| } | ||
|
|
||
| int JVMSupport::asyncGetCallTrace(ASGCT_CallFrame *frames, int max_depth, void* ucontext) { | ||
| JNIEnv *jni = VM::jni(); | ||
| if (jni == nullptr) { | ||
| return 0; | ||
| } | ||
|
|
||
| AsyncSampleMutex mutex(ProfiledThread::currentSignalSafe()); | ||
| if (!mutex.acquired()) { | ||
| return 0; | ||
| } | ||
|
|
||
| JitWriteProtection jit(false); | ||
| // AsyncGetCallTrace writes to ASGCT_CallFrame array | ||
| ASGCT_CallTrace trace = {jni, 0, frames}; | ||
| VM::_asyncGetCallTrace(&trace, max_depth, ucontext); | ||
| if (trace.num_frames > 0) { | ||
| return trace.num_frames; | ||
| } | ||
|
|
||
| const char* err_string = Profiler::asgctError(trace.num_frames); | ||
| if (err_string == NULL) { | ||
| // No Java stack, because thread is not in Java context | ||
| return 0; | ||
| } | ||
|
|
||
| Profiler::instance()->incFailure(-trace.num_frames); | ||
| return makeFrame(frames, BCI_ERROR, err_string); | ||
| } | ||
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,30 @@ | ||
| /* | ||
| * Copyright 2026, Datadog, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef _JVMSUPPORT_H | ||
| #define _JVMSUPPORT_H | ||
|
|
||
| #include "stackWalker.h" | ||
| #include "vmEntry.h" | ||
|
|
||
| // Stack recovery techniques used to workaround AsyncGetCallTrace flaws. | ||
| // Can be disabled with 'safemode' option. | ||
| enum StackRecovery { | ||
| UNKNOWN_JAVA = (1 << 0), | ||
| POP_STUB = (1 << 1), | ||
| POP_METHOD = (1 << 2), | ||
| LAST_JAVA_PC = (1 << 4), | ||
| GC_TRACES = (1 << 5), | ||
| PROBE_SP = 0x100, | ||
| }; | ||
|
|
||
|
|
||
| class JVMSupport { | ||
| static int asyncGetCallTrace(ASGCT_CallFrame *frames, int max_depth, void* ucontext); | ||
| public: | ||
| static int walkJavaStack(StackWalkRequest& request); | ||
| }; | ||
|
|
||
| #endif // _JVMSUPPORT_H |
Oops, something went wrong.
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.