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             top_frame = frame(synthetic_sp, synthetic_sp, sender_sp, pc_desc->real_pc(sampled_nm), sampled_nm);
221             in_continuation = is_in_continuation(top_frame, jt);
222             return true;
223           }
224         }
225       }
226     }
227     stream.next(); // skip the SafepointBlob stub frame
228   }
229 
230   assert(!stream.current()->is_safepoint_blob_frame(), "invariant");
231 
232   biased = true;
233 
234   // Search the first frame that is above the sampled sp.
235   for (; !stream.is_done(); stream.next()) {
236     frame* const current = stream.current();
237 
238     if (current->real_fp() <= sampled_sp) {
239       // Continue searching for a matching frame.
240       continue;
241     }
242 
243     if (sampled_nm == nullptr) {
244       // The sample didn't have an nmethod; we decide to trace from its sender.
245       // Another instance of safepoint bias.
246       top_frame = *current;
247       break;
248     }
249 
250     // Check for a matching compiled method.
251     if (current->cb()->as_nmethod_or_null() == sampled_nm) {
252       if (current->pc() != sampled_pc) {
253         // Let's adjust for the safepoint bias if we can.
254         const PcDesc* const pc_desc = get_pc_desc(sampled_nm, sampled_pc);
255         if (is_valid(pc_desc)) {
256           current->adjust_pc(pc_desc->real_pc(sampled_nm));
257           biased = false;
258         }
259       }
260     }
261     // Either a hit or a mismatched sample in which case we trace from the sender.
262     // Yet another instance of safepoint bias,to be addressed with
263     // more exact and stricter versions when parsable blobs become available.
264     top_frame = *current;
265     break;
266   }
267 
268   in_continuation = is_in_continuation(top_frame, jt);
269   return true;
270 }
271 
272 static void record_thread_in_java(const JfrSampleRequest& request, const JfrTicks& now, const JfrThreadLocal* tl, JavaThread* jt, Thread* current) {
273   assert(jt != nullptr, "invariant");
274   assert(tl != nullptr, "invariant");
275   assert(current != nullptr, "invariant");
276 
277   frame top_frame;
278   bool biased = false;
279   bool in_continuation;
280   if (!compute_top_frame(request, top_frame, in_continuation, jt, biased)) {
281     return;
282   }
283 
284   traceid sid;
285   {
286     ResourceMark rm(current);
287     JfrStackTrace stacktrace;
288     if (!stacktrace.record(jt, top_frame, in_continuation, request)) {
289       // Unable to record stacktrace. Fail.
290       return;
291     }
292     sid = JfrStackTraceRepository::add(stacktrace);
293   }
294   assert(sid != 0, "invariant");
295   const traceid tid = in_continuation ? tl->vthread_id_with_epoch_update(jt) : JfrThreadLocal::jvm_thread_id(jt);
296   send_sample_event<EventExecutionSample>(request._sample_ticks, now, sid, tid);
297   if (current == jt) {
298     send_safepoint_latency_event(request, now, sid, jt);
299   }
300 }
301 
302 #ifdef LINUX
303 static void record_cpu_time_thread(const JfrCPUTimeSampleRequest& request, const JfrTicks& now, const JfrThreadLocal* tl, JavaThread* jt, Thread* current) {
304   assert(jt != nullptr, "invariant");
305   assert(tl != nullptr, "invariant");
306   assert(current != nullptr, "invariant");
307   frame top_frame;
308   bool biased = false;
309   bool in_continuation = false;
310   bool could_compute_top_frame = compute_top_frame(request._request, top_frame, in_continuation, jt, biased);
311   const traceid tid = in_continuation ? tl->vthread_id_with_epoch_update(jt) : JfrThreadLocal::jvm_thread_id(jt);
312 
313   if (!could_compute_top_frame) {
314     JfrCPUTimeThreadSampling::send_empty_event(request._request._sample_ticks, tid, request._cpu_time_period);
315     return;
316   }
317   traceid sid;
318   {
319     ResourceMark rm(current);
320     JfrStackTrace stacktrace;
321     if (!stacktrace.record(jt, top_frame, in_continuation, request._request)) {
322       // Unable to record stacktrace. Fail.
323       JfrCPUTimeThreadSampling::send_empty_event(request._request._sample_ticks, tid, request._cpu_time_period);
324       return;
325     }
326     sid = JfrStackTraceRepository::add(stacktrace);
327   }
328   assert(sid != 0, "invariant");
329 
330 
331   JfrCPUTimeThreadSampling::send_event(request._request._sample_ticks, sid, tid, request._cpu_time_period, biased);
332   if (current == jt) {
333     send_safepoint_latency_event(request._request, now, sid, jt);
334   }
335 }
336 #endif
337 
338 static void drain_enqueued_requests(const JfrTicks& now, JfrThreadLocal* tl, JavaThread* jt, Thread* current) {
339   assert(tl != nullptr, "invariant");
340   assert(jt != nullptr, "invariant");
341   assert(current != nullptr, "invariant");
342   assert(jt->jfr_thread_local() == tl, "invariant");
343   assert_lock_strong(tl->sample_monitor());
344   if (tl->has_enqueued_requests()) {
345     for (const JfrSampleRequest& request : *tl->sample_requests()) {
346       record_thread_in_java(request, now, tl, jt, current);
347     }
348     tl->clear_enqueued_requests();
349   }
350   assert(!tl->has_enqueued_requests(), "invariant");
351 }
352 
353 static void drain_enqueued_cpu_time_requests(const JfrTicks& now, JfrThreadLocal* tl, JavaThread* jt, Thread* current, bool lock) {
354   assert(tl != nullptr, "invariant");
355   assert(jt != nullptr, "invariant");
356   assert(current != nullptr, "invariant");
357 #ifdef LINUX
358   tl->set_do_async_processing_of_cpu_time_jfr_requests(false);
359   if (lock) {
360     tl->acquire_cpu_time_jfr_dequeue_lock();
361   }
362   JfrCPUTimeTraceQueue& queue = tl->cpu_time_jfr_queue();
363   for (u4 i = 0; i < queue.size(); i++) {
364     record_cpu_time_thread(queue.at(i), now, tl, jt, current);
365   }
366   queue.clear();
367   assert(queue.is_empty(), "invariant");
368   tl->set_has_cpu_time_jfr_requests(false);
369   if (queue.lost_samples() > 0) {
370     JfrCPUTimeThreadSampling::send_lost_event( now, JfrThreadLocal::thread_id(jt), queue.get_and_reset_lost_samples());
371   }
372   if (lock) {
373     tl->release_cpu_time_jfr_queue_lock();
374   }
375 #endif
376 }
377 
378 // Entry point for a thread that has been sampled in native code and has a pending JFR CPU time request.
379 void JfrThreadSampling::process_cpu_time_request(JavaThread* jt, JfrThreadLocal* tl, Thread* current, bool lock) {
380   assert(jt != nullptr, "invariant");
381 
382   const JfrTicks now = JfrTicks::now();
383   drain_enqueued_cpu_time_requests(now, tl, jt, current, lock);
384 }
385 
386 static void drain_all_enqueued_requests(const JfrTicks& now, JfrThreadLocal* tl, JavaThread* jt, Thread* current) {
387   assert(tl != nullptr, "invariant");
388   assert(jt != nullptr, "invariant");
389   assert(current != nullptr, "invariant");
390   drain_enqueued_requests(now, tl, jt, current);
391   if (tl->has_cpu_time_jfr_requests()) {
392     drain_enqueued_cpu_time_requests(now, tl, jt, current, true);
393   }
394 }
395 
396 // Only entered by the JfrSampler thread.
397 bool JfrThreadSampling::process_native_sample_request(JfrThreadLocal* tl, JavaThread* jt, Thread* sampler_thread) {
398   assert(tl != nullptr, "invairant");
399   assert(jt != nullptr, "invariant");
400   assert(sampler_thread != nullptr, "invariant");
401   assert(sampler_thread->is_JfrSampler_thread(), "invariant");
402   assert(tl == jt->jfr_thread_local(), "invariant");
403   assert(jt != sampler_thread, "only asynchronous processing of native samples");
404   assert(jt->has_last_Java_frame(), "invariant");
405   assert(tl->sample_state() >= NATIVE_SAMPLE, "invariant");
406 
407   assert_lock_strong(Threads_lock);
408 
409   const JfrTicks start_time = JfrTicks::now();
410 
411   traceid tid;
412   traceid sid;
413 
414   {
415     JfrSampleMonitor sm(tl);
416 
417     // Because the thread was in native, it is in a walkable state, because
418     // it will hit a safepoint poll on the way back from native. To ensure timely
419     // progress, any requests in the queue can be safely processed now.
420     drain_enqueued_requests(start_time, tl, jt, sampler_thread);
421     // Process the current stacktrace using the ljf.
422     {
423       ResourceMark rm(sampler_thread);
424       JfrStackTrace stacktrace;
425       const frame top_frame = jt->last_frame();
426       if (!stacktrace.record_inner(jt, top_frame, is_in_continuation(top_frame, jt), 0 /* skip level */)) {
427         // Unable to record stacktrace. Fail.
428         return false;
429       }
430       sid = JfrStackTraceRepository::add(stacktrace);
431     }
432     // Read the tid under the monitor to ensure that if its a virtual thread,
433     // it is not unmounted until we are done with it.
434     tid = JfrThreadLocal::thread_id(jt);
435   }
436 
437   assert(tl->sample_state() == NO_SAMPLE, "invariant");
438   send_sample_event<EventNativeMethodSample>(start_time, start_time, sid, tid);
439   return true;
440 }
441 
442 // Entry point for a sampled thread that discovered pending Jfr Sample Requests as part of a safepoint poll.
443 void JfrThreadSampling::process_sample_request(JavaThread* jt) {
444   assert(JavaThread::current() == jt, "should be current thread");
445   assert(jt->thread_state() == _thread_in_vm || jt->thread_state() == _thread_in_Java, "invariant");
446 
447   const JfrTicks now = JfrTicks::now();
448 
449   JfrThreadLocal* const tl = jt->jfr_thread_local();
450   assert(tl != nullptr, "invariant");
451 
452   MonitorLocker ml(tl->sample_monitor(), Monitor::_no_safepoint_check_flag);
453 
454   for (;;) {
455     const int sample_state = tl->sample_state();
456     if (sample_state == NATIVE_SAMPLE) {
457       tl->set_sample_state(WAITING_FOR_NATIVE_SAMPLE);
458       // Wait until stack trace is processed.
459       ml.wait();
460     } else if (sample_state == JAVA_SAMPLE) {
461       tl->enqueue_request();
462     } else if (sample_state == WAITING_FOR_NATIVE_SAMPLE) {
463       // Handle spurious wakeups. Again wait until stack trace is processed.
464       ml.wait();
465     } else {
466       // State has been processed.
467       break;
468     }
469   }
470   drain_all_enqueued_requests(now, tl, jt, jt);
471 }
472