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