On Mon, 2 Nov 2026 15:06:43 GMT, Andrei Pangin <[email protected]> wrote:

> A change in JDK-8364258 broke CPU-time sampling for threads that call JNI 
> `AttachCurrentThread` or `AttachCurrentThreadAsDaemon` while profiler is 
> active. This PR restores the correct behavior, allowing JNI-attached threads 
> to be sampled, whether they attach before or after enabling profiler.
> 
> When a native thread is attached to the JVM, it goes directly to 
> `_thread_in_vm` state, but `JfrThreadLocal::on_start` enables CPU-time 
> sampling only for `_thread_new` threads. The fix is to add an explicit check 
> for `is_attaching_via_jni()`.
> 
> Testing: all `jdk_jfr` tests, including new `TestCPUTimeSamplingJNIAttach`.
> 
> ---------
> - [x] I confirm that I make this contribution in accordance with the [OpenJDK 
> Interim AI Policy](https://openjdk.org/legal/ai).

I think there is more that needs fixing here. We should only be calling this 
`on_start` method from where we call `Jfr::on_thread_start` - which is during 
attach or during the initial part of `run()` for created Java threads. So the 
fact we are in this code should mean that we have to enable the sampling 
unconditionally. The current logic for new threads seems incorrect because we 
call `on_thread_start` after switching to `_thread_in_vm`:

// Thread is now sufficiently initialized to be handled by the safepoint code 
as being
  // in the VM. Change thread state from _thread_new to _thread_in_vm
  assert(this->thread_state() == _thread_new, "wrong thread state");
  set_thread_state(_thread_in_vm);

  // Before a thread is on the threads list it is always safe, so after leaving 
the
  // _thread_new we should emit a instruction barrier. The distance to modified 
code
  // from here is probably far enough, but this is consistent and safe.
  OrderAccess::cross_modify_fence();

  assert(JavaThread::current() == this, "sanity check");
  assert(!Thread::current()->owns_locks(), "sanity check");

  JFR_ONLY(Jfr::on_thread_start(this);)

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

Changes requested by dholmes (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/33678#pullrequestreview-5743607684

Reply via email to