RFR: 8352251: Implement Cooperative JFR Sampling [v16]
Markus Grönlund
mgronlun at openjdk.org
Tue May 6 09:44:16 UTC 2025
On Tue, 6 May 2025 07:40:25 GMT, Markus Grönlund <mgronlun at openjdk.org> wrote:
> > > > Isn't this problem already solved by using the `_last_sender_Java_fp`? Why do we need both, the frame pop before the safepoint check and the _last_sender_Java_fp trick?
> >
> >
> > >
> >
> >
> > > Because the sampler fetches the CPU context for threads running in state "_thread_in_Java". Without it, you can sample after the safepoint poll test is issued, but before the frame is popped. That sampled frame will represent something now being removed, and if the sender issues another call, also overwritten.
> >
> >
> > >
> >
> >
> > > It may help to think about how the JIT methods works - they pop their frames before issuing the method return safepoint poll test.
> >
> >
> > Ok. This makes sense. Thanks for the explanation!
> > Just trying to understand: What if we have the frame pop before the safepoint check like in JIT compiled code. Do we still need the `_last_sender_Java_fp` trick in this case (assuming we handle it like in the JIT compiled case and use `StackWatermarkSet::after_unwind` in `InterpreterRuntime::at_unwind`)?
>
> Sorry Martin, I did not read your reply detailed enough. That is surely an interesting idea - I did not want to shake things too much, but now I will attempt to try it.
>
> Could solve the trick quite naturally, as you say.
I remember now why I designed it this way. The reason is the other part of the solution, the hook to process enqueued sample requests. As you can see, in Interpreter::unwind(), there is a check for processing sample requests, like:
JFR_ONLY(Jfr::check_and_process_sample_request(current);)
The invariant here is that the frame about to be popped could have been sampled; therefore, an ljf at this point must "cover" it for stackwalking to locate it (it must be above or equal). If we pop before testing the safepoint poll, that frame is gone (now below the saved ljf).
Comparing again with a JIT frame, to be more exact, it is not true that the compiled frame is popped before the method return poll check: Specifically, only the explicit frame size is popped before issuing the return poll test. That is, the frame's return address is still on the stack. The reason this works is because JIT frame unwind test the sp. Therefore, it is possible capture the top frame even though it has been (partially) popped. We reconstruct it in the safepoint handler using the java_thread->saved_exception_pc().
-------------
PR Comment: https://git.openjdk.org/jdk/pull/24296#issuecomment-2853907459
More information about the hotspot-jfr-dev
mailing list