Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ddprof-lib/src/main/cpp/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,12 @@ Error Profiler::start(Arguments &args, bool reset) {
_thread_filter.remove(slot_id); // Remove from filtering initially (matches onThreadStart behavior)
}

// Per-recording jvmtistacks options are parsed after the VM probe ran, so
// the extension may still be unpublished here; initialize and publish it
// now (idempotent, quiet when the extension is absent).
if (args._jvmtistacks) {
VM::enableRequestStackTrace();
}
_cpu_engine = selectCpuEngine(args);
_wall_engine = selectWallEngine(args);
_cstack = args._cstack;
Expand Down Expand Up @@ -1963,6 +1969,12 @@ Error Profiler::check(Arguments &args) {

error = checkJvmCapabilities();

// Match Profiler::start(): a check command with jvmtistacks must report the
// same engine availability a subsequent start would see, which requires the
// extension to be initialized first (idempotent, quiet when absent).
if (args._jvmtistacks) {
VM::enableRequestStackTrace();
}
if (!error && (args._event != NULL || args._cpu >= 0)) {
_cpu_engine = selectCpuEngine(args);
error = _cpu_engine->check(args);
Expand Down
68 changes: 59 additions & 9 deletions ddprof-lib/src/main/cpp/vmEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ bool VM::_is_adaptive_gc_boundary_flag_set = false;

jvmtiExtensionFunction VM::_request_stack_trace = nullptr;
jvmtiExtensionFunction VM::_init_request_stack_trace = nullptr;
jvmtiExtensionFunction VM::_discovered_request_stack_trace = nullptr;

jvmtiError(JNICALL *VM::_orig_RedefineClasses)(jvmtiEnv *, jint,
const jvmtiClassDefinition *);
Expand Down Expand Up @@ -398,17 +399,28 @@ bool VM::initLibrary(JavaVM *vm) {
void VM::probeJFRRequestStackTrace() {
jint ext_count = 0;
jvmtiExtensionFunctionInfo *ext_functions = nullptr;
// The extension function pointers are collected into locals and published
// only after InitializeRequestStackTrace has succeeded (see below), so a
// concurrent reader of _request_stack_trace never observes it non-null
// while the extension is still uninitialized.
jvmtiExtensionFunction request_fn = nullptr;
jvmtiExtensionFunction init_fn = nullptr;
if (_jvmti->GetExtensionFunctions(&ext_count, &ext_functions) == 0) {
for (jint i = 0; i < ext_count; i++) {
if (strcmp(ext_functions[i].id,
"com.sun.hotspot.functions.RequestStackTrace") == 0) {
__atomic_store_n(&_request_stack_trace, ext_functions[i].func, __ATOMIC_RELEASE);
request_fn = ext_functions[i].func;
} else if (strcmp(ext_functions[i].id,
"com.sun.hotspot.functions.InitializeRequestStackTrace") == 0) {
_init_request_stack_trace = ext_functions[i].func;
init_fn = ext_functions[i].func;
}
for (jint j = 0; j < ext_functions[i].param_count; j++) {
_jvmti->Deallocate((unsigned char *)ext_functions[i].params[j].name);
// The JVMTI spec leaves params NULL when param_count is 0; guard
// against conformance-defect JVMs reporting param_count > 0 with a
// NULL params array as well.
if (ext_functions[i].params != nullptr) {
for (jint j = 0; j < ext_functions[i].param_count; j++) {
_jvmti->Deallocate((unsigned char *)ext_functions[i].params[j].name);
}
}
_jvmti->Deallocate((unsigned char *)ext_functions[i].params);
_jvmti->Deallocate((unsigned char *)ext_functions[i].errors);
Expand All @@ -418,33 +430,71 @@ void VM::probeJFRRequestStackTrace() {
_jvmti->Deallocate((unsigned char *)ext_functions);
}

// Discovery is unconditional: per-recording jvmtistacks options are parsed
// after this probe ran (the JNI load path calls initProfilerBridge() before
// any recording command), so a later start command must be able to enable
// the extension via enableRequestStackTrace() from the recorded pointers.
__atomic_store_n(&_discovered_request_stack_trace, request_fn, __ATOMIC_RELEASE);
__atomic_store_n(&_init_request_stack_trace, init_fn, __ATOMIC_RELEASE);

if (_agent_args._jvmtistacks) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve activation from per-recording jvmtistacks options

When the profiler is loaded through the normal JNI path, JavaProfiler_init0() calls initProfilerBridge() before any recording arguments are parsed, so _agent_args._jvmtistacks retains its default false value. This condition now prevents _request_stack_trace from ever being published, and the bridge is not probed again when a later start,jvmtistacks=true command reaches Profiler::selectCpuEngine(). Consequently, JNI-loaded users requesting JVMTI stacks at recording start always see canRequestStackTrace() == false and silently fall back to ASGCT; initialization/publication needs to be deferred to the recording option path or remain available after the initial probe.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed — the JNI load path (JavaProfiler_init0 → initProfilerBridge) probes before any recording options are parsed, so _agent_args._jvmtistacks was still false and a per-recording start,jvmtistacks=true could never publish the extension.

Fixed in f0085fe: the probe now records the discovered extension function pointers unconditionally, and a new VM::enableRequestStackTrace() (idempotent, quiet when the extension is absent) initializes and publishes lazily. Profiler::start() and Profiler::check() call it when args._jvmtistacks is set, so per-recording options enable the extension at recording time. Covered by new gtest cases (LazyEnableAfterJvmtistacksDisabledProbe, LazyEnableQuietWhenExtensionAbsent, LazyEnableIdempotentAfterPublication); full :ddprof-lib:gtestDebug 1024 passed / 0 failed.

if (_request_stack_trace == nullptr || _init_request_stack_trace == nullptr) {
if (request_fn == nullptr || init_fn == nullptr) {
Log::warn("jvmtistacks requested but HotSpot RequestStackTrace extension "
"is not available on this JVM");
Counters::increment(JVMTI_STACKS_INIT_FAILED);
} else {
initializeRequestStackTrace();
enableRequestStackTrace();
}
}
}

// Must not be called from a signal handler — invokes JVMTI which is not async-signal-safe.
bool VM::initializeRequestStackTrace() {
if (__atomic_load_n(&_request_stack_trace, __ATOMIC_RELAXED) == nullptr || _init_request_stack_trace == nullptr) {
// The caller verifies the presence of the request function before publishing
// it; here only the init function must be set.
jvmtiExtensionFunction init_fn = __atomic_load_n(&_init_request_stack_trace, __ATOMIC_ACQUIRE);
if (init_fn == nullptr) {
return false;
}
jvmtiError rc = _init_request_stack_trace(_jvmti);
jvmtiError rc = init_fn(_jvmti);
if (rc == JVMTI_ERROR_NONE) {
Counters::increment(JVMTI_STACKS_INIT_OK);
return true;
}
Log::warn("InitializeRequestStackTrace failed: %d", rc);
__atomic_store_n(&_request_stack_trace, (jvmtiExtensionFunction)nullptr, __ATOMIC_RELEASE);
// No rollback needed: _request_stack_trace is published by the caller only
// after initialization succeeds, so it is still null here.
Counters::increment(JVMTI_STACKS_INIT_FAILED);
return false;
}

// Must not be called from a signal handler — invokes JVMTI which is not async-signal-safe.
bool VM::enableRequestStackTrace() {
if (canRequestStackTrace()) {
return true;
}
// The request function was never discovered (or only one of the two
// extensions exists): the probe already warned on an explicit jvmtistacks
// request, so stay quiet here — a recording command that did not ask for
// jvmtistacks must not produce warnings at all.
if (__atomic_load_n(&_discovered_request_stack_trace, __ATOMIC_ACQUIRE) == nullptr ||
__atomic_load_n(&_init_request_stack_trace, __ATOMIC_ACQUIRE) == nullptr) {
return false;
}
// Publication order as in the probe: _init_request_stack_trace is already
// stored; the RELEASE store of _request_stack_trace below pairs with the
// ACQUIRE loads in canRequestStackTrace()/requestStackTrace(), so any
// reader that observes the request function also observes the initialized
// init function.
if (!initializeRequestStackTrace()) {
return false;
}
__atomic_store_n(&_request_stack_trace,
__atomic_load_n(&_discovered_request_stack_trace, __ATOMIC_ACQUIRE),
__ATOMIC_RELEASE);
return true;
}

// JVMTI delivers ONE callback per event slot; both trackers need the
// GarbageCollectionFinish signal (liveness GC epochs + reference-chain pass
// scheduling), so this vmEntry-level forwarder fans the single slot out to
Expand Down
26 changes: 21 additions & 5 deletions ddprof-lib/src/main/cpp/vmEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,22 @@ class VM {
static bool _is_adaptive_gc_boundary_flag_set;
static CodeCache *_libjvm;

// HotSpot JFR async stack-trace extension (optional, JDK 27+).
// _request_stack_trace is atomic (RELEASE/ACQUIRE) because canRequestStackTrace()
// is called from signal handlers; _init_request_stack_trace is plain because it
// is only ever read by initializeRequestStackTrace(), called once from the same
// init thread before any signal handlers are installed.
// HotSpot JFR async stack-trace extension (optional, JDK 27+). All three
// fields are atomic (RELEASE store / ACQUIRE load). Publication order is
// load-bearing: _init_request_stack_trace is stored first and
// _request_stack_trace only after InitializeRequestStackTrace succeeded, so
// canRequestStackTrace() (called from signal handlers) observing a non-null
// _request_stack_trace is guaranteed to also observe the initialized
// _init_request_stack_trace, and true means the extension is ready to use,
// not merely present.
// The probe records the discovered function pointers unconditionally;
// publication and initialization happen when jvmtistacks is requested —
// at probe time for agent options, or lazily from a recording command via
// enableRequestStackTrace() (the JNI load path probes before any recording
// options are parsed, so per-recording jvmtistacks=true must enable there).
static jvmtiExtensionFunction _request_stack_trace;
static jvmtiExtensionFunction _init_request_stack_trace;
static jvmtiExtensionFunction _discovered_request_stack_trace;

static jvmtiError(JNICALL *_orig_RedefineClasses)(
jvmtiEnv *, jint, const jvmtiClassDefinition *);
Expand Down Expand Up @@ -233,6 +242,13 @@ class VM {
// Must not be called from a signal handler — invokes JVMTI which is not async-signal-safe.
static bool initializeRequestStackTrace();

// Must not be called from a signal handler — invokes JVMTI which is not async-signal-safe.
// Initializes and publishes the RequestStackTrace extension if it was
// discovered by the probe and is not published yet; idempotent. Quiet when
// the extension is absent (the probe already warned); returns whether the
// extension is usable afterwards.
static bool enableRequestStackTrace();

static jvmtiError requestStackTrace(void* ucontext, jlong user_data) {
jvmtiExtensionFunction fn = __atomic_load_n(&_request_stack_trace, __ATOMIC_ACQUIRE);
if (!fn) return JVMTI_ERROR_NOT_AVAILABLE;
Expand Down
8 changes: 8 additions & 0 deletions ddprof-lib/src/test/cpp/referenceChainsTestAccessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class VMTestAccessor {
public:
static jvmtiEnv* getJvmti() { return VM::_jvmti; }
static void setJvmti(jvmtiEnv* env) { VM::_jvmti = env; }
static jvmtiExtensionFunction getRequestStackTrace() { return VM::_request_stack_trace; }
static void setRequestStackTrace(jvmtiExtensionFunction fn) { VM::_request_stack_trace = fn; }
static jvmtiExtensionFunction getInitRequestStackTrace() { return VM::_init_request_stack_trace; }
static void setInitRequestStackTrace(jvmtiExtensionFunction fn) { VM::_init_request_stack_trace = fn; }
static jvmtiExtensionFunction getDiscoveredRequestStackTrace() { return VM::_discovered_request_stack_trace; }
static void setDiscoveredRequestStackTrace(jvmtiExtensionFunction fn) { VM::_discovered_request_stack_trace = fn; }
static bool enableRequestStackTrace() { return VM::enableRequestStackTrace(); }
static void probeJFRRequestStackTrace() { VM::probeJFRRequestStackTrace(); }
// VM::jni() goes through VM::_vm, and VM::hotspot_version() reports -1 unless VM::_hotspot is
// set - both are needed to drive LivenessTracker::start() against a mock JVM.
static JavaVM* getVm() { return VM::_vm; }
Expand Down
Loading
Loading