1 /*
2 * Copyright (c) 2012, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "classfile/javaThreadStatus.hpp"
26 #include "code/codeCache.inline.hpp"
27 #include "code/debugInfoRec.hpp"
28 #include "code/nmethod.hpp"
29 #include "code/pcDesc.hpp"
30 #include "interpreter/interpreter.hpp"
31 #include "jfr/jfrEvents.hpp"
32 #include "jfr/periodic/sampling/jfrCPUTimeThreadSampler.hpp"
33 #include "jfr/periodic/sampling/jfrSampleMonitor.hpp"
34 #include "jfr/periodic/sampling/jfrSampleRequest.hpp"
35 #include "jfr/periodic/sampling/jfrThreadSampling.hpp"
36 #include "jfr/recorder/stacktrace/jfrStackTrace.hpp"
37 #include "jfr/utilities/jfrTypes.hpp"
38 #include "memory/resourceArea.hpp"
39 #include "oops/method.hpp"
40 #include "runtime/continuation.hpp"
41 #include "runtime/frame.inline.hpp"
42 #include "runtime/javaThread.inline.hpp"
43 #include "runtime/stackFrameStream.inline.hpp"
44
45 template <typename EventType>
46 static inline void send_sample_event(const JfrTicks& start_time, const JfrTicks& end_time, traceid sid, traceid tid) {
47 EventType event(UNTIMED);
48 event.set_starttime(start_time);
49 event.set_endtime(end_time);
50 event.set_sampledThread(tid);
51 event.set_state(static_cast<u8>(JavaThreadStatus::RUNNABLE));
52 event.set_stackTrace(sid);
53 event.commit();
54 }
55
56 static inline void send_safepoint_latency_event(const JfrSampleRequest& request, const JfrTicks& end_time, traceid sid, const JavaThread* jt) {
57 assert(jt != nullptr, "invariant");
58 assert(!jt->jfr_thread_local()->has_cached_stack_trace(), "invariant");
59 EventSafepointLatency event(UNTIMED);
60 event.set_starttime(request._sample_ticks);
61 event.set_endtime(end_time);
62 if (event.should_commit()) {
63 event.set_threadState(_thread_in_Java);
64 jt->jfr_thread_local()->set_cached_stack_trace_id(sid);
65 event.commit();
66 jt->jfr_thread_local()->clear_cached_stack_trace();
67 }
68 }
69
70 static inline bool is_interpreter(const JfrSampleRequest& request) {
71 return request._sample_bcp != nullptr;
72 }
73
74 static inline bool is_in_continuation(const frame& frame, JavaThread* jt) {
75 return JfrThreadLocal::is_vthread(jt) &&
76 (Continuation::is_frame_in_continuation(jt, frame) || Continuation::is_continuation_enterSpecial(frame));
77 }
78
79 // A sampled interpreter frame is handled differently from a sampled compiler frame.
80 //
81 // The JfrSampleRequest description partially describes a _potential_ interpreter Java frame.
82 // It's partial because the sampler thread only sets the fp and bcp fields.
83 //
84 // We want to ensure that what we discovered inside interpreter code _really_ is what we assume, a valid interpreter frame.
85 //
86 // Therefore, instead of letting the sampler thread read what it believes to be a Method*, we delay until we are at a safepoint to ensure the Method* is valid.
87 //
88 // If the JfrSampleRequest represents a valid interpreter frame, the Method* is retrieved and the sender frame is returned per the sender_frame.
89 //
90 // If it is not a valid interpreter frame, then the JfrSampleRequest is invalidated, and the current frame is returned per the sender frame.
91 //
92 static bool compute_sender_frame(JfrSampleRequest& request, frame& sender_frame, bool& in_continuation, JavaThread* jt) {
93 assert(is_interpreter(request), "invariant");
94 assert(jt != nullptr, "invariant");
95 assert(jt->has_last_Java_frame(), "invariant");
96
97 // For a request representing an interpreter frame, request._sample_sp is actually the frame pointer, fp.
98 const void* const sampled_fp = request._sample_sp;
99
100 StackFrameStream stream(jt, false, false);
101
102 // Search for the sampled interpreter frame and get its Method*.
103
104 while (!stream.is_done()) {
105 const frame* const frame = stream.current();
106 assert(frame != nullptr, "invariant");
107 const intptr_t* const real_fp = frame->real_fp();
108 assert(real_fp != nullptr, "invariant");
109 if (real_fp == sampled_fp && frame->is_interpreted_frame()) {
110 Method* const method = frame->interpreter_frame_method();
111 assert(method != nullptr, "invariant");
112 request._sample_pc = method;
113 // Got the Method*. Validate bcp.
114 if (!method->is_native() && !method->contains(static_cast<address>(request._sample_bcp))) {
115 request._sample_bcp = frame->interpreter_frame_bcp();
116 }
117 in_continuation = is_in_continuation(*frame, jt);
118 break;
119 }
120 if (real_fp >= sampled_fp) {
121 // What we sampled is not an official interpreter frame.
122 // Invalidate the sample request and use current.
123 request._sample_bcp = nullptr;
124 sender_frame = *stream.current();
125 in_continuation = is_in_continuation(sender_frame, jt);
126 return true;
127 }
128 stream.next();
129 }
130
131 assert(!stream.is_done(), "invariant");
132
133 // Step to sender.
134 stream.next();
135
136 // If the top frame is in a continuation, check that the sender frame is too.
137 if (in_continuation && !is_in_continuation(*stream.current(), jt)) {
138 // Leave sender frame empty.
139 return true;
140 }
141
142 sender_frame = *stream.current();
143
144 assert(request._sample_pc != nullptr, "invariant");
145 assert(request._sample_bcp != nullptr, "invariant");
146 assert(Method::is_valid_method(static_cast<const Method*>(request._sample_pc)), "invariant");
147 assert(static_cast<const Method*>(request._sample_pc)->is_native() ||
148 static_cast<const Method*>(request._sample_pc)->contains(static_cast<address>(request._sample_bcp)), "invariant");
149 return true;
150 }
151
152 static inline PcDesc* get_pc_desc(nmethod* nm, void* pc) {
153 assert(nm != nullptr, "invariant");
154 assert(pc != nullptr, "invariant");
155 return nm->pc_desc_near(static_cast<address>(pc));
156 }
157
158 static inline bool is_valid(const PcDesc* pc_desc) {
159 return pc_desc != nullptr && pc_desc->scope_decode_offset() != DebugInformationRecorder::serialized_null;
160 }
161
162 static bool compute_top_frame(const JfrSampleRequest& request, frame& top_frame, bool& in_continuation, JavaThread* jt, bool& biased) {
163 assert(jt != nullptr, "invariant");
164
165 if (!jt->has_last_Java_frame()) {
166 return false;
167 }
168
169 if (is_interpreter(request)) {
170 return compute_sender_frame(const_cast<JfrSampleRequest&>(request), top_frame, in_continuation, jt);
171 }
172
173 void* const sampled_pc = request._sample_pc;
174 CodeBlob* sampled_cb;
175 if (sampled_pc == nullptr || (sampled_cb = CodeCache::find_blob(sampled_pc)) == nullptr) {
176 // A biased sample is requested or no code blob.
177 top_frame = jt->last_frame();
178 in_continuation = is_in_continuation(top_frame, jt);
179 biased = true;
180 return true;
181 }
182
183 // We will never describe a sample request that represents an unparsable stub or blob.
184 assert(sampled_cb->frame_complete_offset() != CodeOffsets::frame_never_safe, "invariant");
185
186 const void* const sampled_sp = request._sample_sp;
187 assert(sampled_sp != nullptr, "invariant");
188
189 nmethod* const sampled_nm = sampled_cb->as_nmethod_or_null();
190
191 StackFrameStream stream(jt, false /* update registers */, false /* process frames */);
192
193 if (stream.current()->is_safepoint_blob_frame()) {
194 if (sampled_nm != nullptr) {
195 // Move to the physical sender frame of the SafepointBlob stub frame using the frame size, not the logical iterator.
196 const int safepoint_blob_stub_frame_size = stream.current()->cb()->frame_size();
197 intptr_t* const sender_sp = stream.current()->unextended_sp() + safepoint_blob_stub_frame_size;
198 if (sender_sp > sampled_sp) {
199 const address saved_exception_pc = jt->saved_exception_pc();
200 assert(saved_exception_pc != nullptr, "invariant");
201 const nmethod* const exception_nm = CodeCache::find_blob(saved_exception_pc)->as_nmethod();
202 assert(exception_nm != nullptr, "invariant");
203 if (exception_nm == sampled_nm && sampled_nm->is_at_poll_return(saved_exception_pc)) {
204 // We sit at the poll return site in the sampled compiled nmethod with only the return address on the stack.
205 // The sampled_nm compiled frame is no longer extant, but we might be able to reconstruct a synthetic
206 // compiled frame at this location. We do this by overlaying a reconstructed frame on top of
207 // the huge SafepointBlob stub frame. Of course, the synthetic frame only contains random stack memory,
208 // but it is safe because stack walking cares only about the form of the frame (i.e., an sp and a pc).
209 // We also do not have to worry about stackbanging because we currently have a huge SafepointBlob stub frame
210 // on the stack. For extra assurance, we know that we can create this frame size at this
211 // very location because we just popped such a frame before we hit the return poll site.
212 //
213 // For frames that need stack repair, special care is needed. This is because the general stack-walking code
214 // reads the frame size from the stack, but here that memory is already overwritten by the SafepointBlob.
215 // If we are careful, we don't need to reconstruct a frame that needs stack repair, because we can process
216 // the nmethod directly, unpacking it in the first part of the stack trace.
217 // To accomplish this, we must provide both the PcDesc and the nmethod to the stack-walking code,
218 // which is done by updating the JfrSampleRequest. A special marker, NEEDS_STACK_REPAIR, is set in the bcp field.
219 // In this case, the top_frame becomes the sender frame of the nmethod, similar to how interpreter frames are handled.
220 //
221 // Let's attempt to correct for the safepoint bias
222 PcDesc* const pc_desc = get_pc_desc(sampled_nm, sampled_pc);
223 if (is_valid(pc_desc)) {
224 intptr_t* const synthetic_sp = sender_sp - sampled_nm->frame_size();
225 in_continuation = Continuation::get_continuation_entry_for_sp(jt, synthetic_sp) != nullptr;
226 if (sampled_nm->needs_stack_repair()) {
227 JfrSampleRequest& modified_request = const_cast<JfrSampleRequest&>(request);
228 modified_request._sample_pc = pc_desc;
229 modified_request._sample_sp = sampled_nm;
230 modified_request._sample_bcp = reinterpret_cast<address>(JfrSampleRequestFrameType::NEEDS_STACK_REPAIR);
231 if (!stream.is_done()) {
232 stream.next();
233 // If the needs stack repair frame is in a continuation, check that the sender frame is too.
234 if (in_continuation && !is_in_continuation(*stream.current(), jt)) {
235 // Leave sender frame empty.
236 return true;
237 }
238 // The top_frame becomes the sender of the nmethod that needs stack repair.
239 top_frame = *stream.current();
240 }
241 return true;
242 }
243 intptr_t* const synthetic_fp = sender_sp AARCH64_ONLY( - frame::sender_sp_offset);
244 top_frame = frame(synthetic_sp, synthetic_sp, synthetic_fp, pc_desc->real_pc(sampled_nm), sampled_nm);
245 return true;
246 }
247 }
248 }
249 }
250 stream.next(); // skip the SafepointBlob stub frame
251 }
252
253 assert(!stream.current()->is_safepoint_blob_frame(), "invariant");
254
255 biased = true;
256
257 // Search the first frame that is above the sampled sp.
258 for (; !stream.is_done(); stream.next()) {
259 frame* const current = stream.current();
260
261 if (current->real_fp() <= sampled_sp) {
262 // Continue searching for a matching frame.
263 continue;
264 }
265
266 if (sampled_nm == nullptr) {
267 // The sample didn't have an nmethod; we decide to trace from its sender.
268 // Another instance of safepoint bias.
269 top_frame = *current;
270 break;
271 }
272
273 // Check for a matching compiled method.
274 if (current->cb()->as_nmethod_or_null() == sampled_nm) {
275 if (current->pc() != sampled_pc) {
276 // Let's adjust for the safepoint bias if we can.
277 const PcDesc* const pc_desc = get_pc_desc(sampled_nm, sampled_pc);
278 if (is_valid(pc_desc)) {
279 current->adjust_pc(pc_desc->real_pc(sampled_nm));
280 biased = false;
281 }
282 }
283 }
284 // Either a hit or a mismatched sample in which case we trace from the sender.
285 // Yet another instance of safepoint bias,to be addressed with
286 // more exact and stricter versions when parsable blobs become available.
287 top_frame = *current;
288 break;
289 }
290
291 in_continuation = is_in_continuation(top_frame, jt);
292 return true;
293 }
294
295 static void record_thread_in_java(const JfrSampleRequest& request, const JfrTicks& now, const JfrThreadLocal* tl, JavaThread* jt, Thread* current) {
296 assert(jt != nullptr, "invariant");
297 assert(tl != nullptr, "invariant");
298 assert(current != nullptr, "invariant");
299
300 frame top_frame;
301 bool biased = false;
302 bool in_continuation;
303 if (!compute_top_frame(request, top_frame, in_continuation, jt, biased)) {
304 return;
305 }
306
307 traceid sid;
308 {
309 ResourceMark rm(current);
310 JfrStackTrace stacktrace;
311 if (!stacktrace.record(jt, top_frame, in_continuation, request)) {
312 // Unable to record stacktrace. Fail.
313 return;
314 }
315 sid = JfrStackTraceRepository::add(stacktrace);
316 }
317 assert(sid != 0, "invariant");
318 const traceid tid = in_continuation ? tl->vthread_id_with_epoch_update(jt) : JfrThreadLocal::jvm_thread_id(jt);
319 send_sample_event<EventExecutionSample>(request._sample_ticks, now, sid, tid);
320 if (current == jt) {
321 send_safepoint_latency_event(request, now, sid, jt);
322 }
323 }
324
325 #ifdef LINUX
326 static void record_cpu_time_thread(const JfrCPUTimeSampleRequest& request, const JfrTicks& now, const JfrThreadLocal* tl, JavaThread* jt, Thread* current) {
327 assert(jt != nullptr, "invariant");
328 assert(tl != nullptr, "invariant");
329 assert(current != nullptr, "invariant");
330 frame top_frame;
331 bool biased = false;
332 bool in_continuation = false;
333 bool could_compute_top_frame = compute_top_frame(request._request, top_frame, in_continuation, jt, biased);
334 const traceid tid = in_continuation ? tl->vthread_id_with_epoch_update(jt) : JfrThreadLocal::jvm_thread_id(jt);
335
336 if (!could_compute_top_frame) {
337 JfrCPUTimeThreadSampling::send_empty_event(request._request._sample_ticks, tid, request._cpu_time_period);
338 return;
339 }
340 traceid sid;
341 {
342 ResourceMark rm(current);
343 JfrStackTrace stacktrace;
344 if (!stacktrace.record(jt, top_frame, in_continuation, request._request)) {
345 // Unable to record stacktrace. Fail.
346 JfrCPUTimeThreadSampling::send_empty_event(request._request._sample_ticks, tid, request._cpu_time_period);
347 return;
348 }
349 sid = JfrStackTraceRepository::add(stacktrace);
350 }
351 assert(sid != 0, "invariant");
352
353
354 JfrCPUTimeThreadSampling::send_event(request._request._sample_ticks, sid, tid, request._cpu_time_period, biased);
355 if (current == jt) {
356 send_safepoint_latency_event(request._request, now, sid, jt);
357 }
358 }
359 #endif
360
361 static void drain_enqueued_requests(const JfrTicks& now, JfrThreadLocal* tl, JavaThread* jt, Thread* current) {
362 assert(tl != nullptr, "invariant");
363 assert(jt != nullptr, "invariant");
364 assert(current != nullptr, "invariant");
365 assert(jt->jfr_thread_local() == tl, "invariant");
366 assert_lock_strong(tl->sample_monitor());
367 if (tl->has_enqueued_requests()) {
368 for (const JfrSampleRequest& request : *tl->sample_requests()) {
369 record_thread_in_java(request, now, tl, jt, current);
370 }
371 tl->clear_enqueued_requests();
372 }
373 assert(!tl->has_enqueued_requests(), "invariant");
374 }
375
376 static void drain_enqueued_cpu_time_requests(const JfrTicks& now, JfrThreadLocal* tl, JavaThread* jt, Thread* current, bool lock) {
377 assert(tl != nullptr, "invariant");
378 assert(jt != nullptr, "invariant");
379 assert(current != nullptr, "invariant");
380 #ifdef LINUX
381 tl->set_do_async_processing_of_cpu_time_jfr_requests(false);
382 if (lock) {
383 tl->acquire_cpu_time_jfr_dequeue_lock();
384 }
385 JfrCPUTimeTraceQueue& queue = tl->cpu_time_jfr_queue();
386 for (u4 i = 0; i < queue.size(); i++) {
387 record_cpu_time_thread(queue.at(i), now, tl, jt, current);
388 }
389 queue.clear();
390 assert(queue.is_empty(), "invariant");
391 tl->set_has_cpu_time_jfr_requests(false);
392 if (queue.lost_samples() > 0) {
393 JfrCPUTimeThreadSampling::send_lost_event( now, JfrThreadLocal::thread_id(jt), queue.get_and_reset_lost_samples());
394 queue.resize_if_needed();
395 }
396 if (lock) {
397 tl->release_cpu_time_jfr_queue_lock();
398 }
399 #endif
400 }
401
402 // Entry point for a thread that has been sampled in native code and has a pending JFR CPU time request.
403 void JfrThreadSampling::process_cpu_time_request(JavaThread* jt, JfrThreadLocal* tl, Thread* current, bool lock) {
404 assert(jt != nullptr, "invariant");
405
406 const JfrTicks now = JfrTicks::now();
407 drain_enqueued_cpu_time_requests(now, tl, jt, current, lock);
408 }
409
410 static void drain_all_enqueued_requests(const JfrTicks& now, JfrThreadLocal* tl, JavaThread* jt, Thread* current) {
411 assert(tl != nullptr, "invariant");
412 assert(jt != nullptr, "invariant");
413 assert(current != nullptr, "invariant");
414 drain_enqueued_requests(now, tl, jt, current);
415 if (tl->has_cpu_time_jfr_requests()) {
416 drain_enqueued_cpu_time_requests(now, tl, jt, current, true);
417 }
418 }
419
420 // Only entered by the JfrSampler thread.
421 bool JfrThreadSampling::process_native_sample_request(JfrThreadLocal* tl, JavaThread* jt, Thread* sampler_thread) {
422 assert(tl != nullptr, "invairant");
423 assert(jt != nullptr, "invariant");
424 assert(sampler_thread != nullptr, "invariant");
425 assert(sampler_thread->is_JfrSampler_thread(), "invariant");
426 assert(tl == jt->jfr_thread_local(), "invariant");
427 assert(jt != sampler_thread, "only asynchronous processing of native samples");
428 assert(jt->has_last_Java_frame(), "invariant");
429 assert(tl->sample_state() >= NATIVE_SAMPLE, "invariant");
430
431 assert_lock_strong(Threads_lock);
432
433 const JfrTicks start_time = JfrTicks::now();
434
435 traceid tid;
436 traceid sid;
437
438 {
439 JfrSampleMonitor sm(tl);
440
441 // Because the thread was in native, it is in a walkable state, because
442 // it will hit a safepoint poll on the way back from native. To ensure timely
443 // progress, any requests in the queue can be safely processed now.
444 drain_enqueued_requests(start_time, tl, jt, sampler_thread);
445 // Process the current stacktrace using the ljf.
446 {
447 ResourceMark rm(sampler_thread);
448 JfrStackTrace stacktrace;
449 const frame top_frame = jt->last_frame();
450 if (!stacktrace.record_inner(jt, top_frame, is_in_continuation(top_frame, jt), 0 /* skip level */)) {
451 // Unable to record stacktrace. Fail.
452 return false;
453 }
454 sid = JfrStackTraceRepository::add(stacktrace);
455 }
456 // Read the tid under the monitor to ensure that if its a virtual thread,
457 // it is not unmounted until we are done with it.
458 tid = JfrThreadLocal::thread_id(jt);
459 }
460
461 assert(tl->sample_state() == NO_SAMPLE, "invariant");
462 send_sample_event<EventNativeMethodSample>(start_time, start_time, sid, tid);
463 return true;
464 }
465
466 // Entry point for a sampled thread that discovered pending Jfr Sample Requests as part of a safepoint poll.
467 void JfrThreadSampling::process_sample_request(JavaThread* jt) {
468 assert(JavaThread::current() == jt, "should be current thread");
469 assert(jt->thread_state() == _thread_in_vm || jt->thread_state() == _thread_in_Java, "invariant");
470
471 const JfrTicks now = JfrTicks::now();
472
473 JfrThreadLocal* const tl = jt->jfr_thread_local();
474 assert(tl != nullptr, "invariant");
475
476 MonitorLocker ml(tl->sample_monitor(), Monitor::_no_safepoint_check_flag);
477
478 for (;;) {
479 const int sample_state = tl->sample_state();
480 if (sample_state == NATIVE_SAMPLE) {
481 tl->set_sample_state(WAITING_FOR_NATIVE_SAMPLE);
482 // Wait until stack trace is processed.
483 ml.wait();
484 } else if (sample_state == JAVA_SAMPLE) {
485 tl->enqueue_request();
486 } else if (sample_state == WAITING_FOR_NATIVE_SAMPLE) {
487 // Handle spurious wakeups. Again wait until stack trace is processed.
488 ml.wait();
489 } else {
490 // State has been processed.
491 break;
492 }
493 }
494 drain_all_enqueued_requests(now, tl, jt, jt);
495 }
496