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