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           // For frames that need stack repair we skip this trick. This is because the stack walking code reads
216           // the frame size from the stack, but the memory has already been overwritten by the SafepointBlob.
217           //
218           // Let's attempt to correct for the safepoint bias.
219           const PcDesc* const pc_desc = get_pc_desc(sampled_nm, sampled_pc);
220           if (is_valid(pc_desc) && !sampled_nm->needs_stack_repair()) {
221             intptr_t* const synthetic_sp = sender_sp - sampled_nm->frame_size();
222             top_frame = frame(synthetic_sp, synthetic_sp, sender_sp, pc_desc->real_pc(sampled_nm), sampled_nm);
223             in_continuation = is_in_continuation(top_frame, jt);
224             return true;
225           }
226         }
227       }
228     }
229     stream.next(); // skip the SafepointBlob stub frame
230   }
231 
232   assert(!stream.current()->is_safepoint_blob_frame(), "invariant");
233 
234   biased = true;
235 
236   // Search the first frame that is above the sampled sp.
237   for (; !stream.is_done(); stream.next()) {
238     frame* const current = stream.current();
239 
240     if (current->real_fp() <= sampled_sp) {
241       // Continue searching for a matching frame.
242       continue;
243     }
244 
245     if (sampled_nm == nullptr) {
246       // The sample didn't have an nmethod; we decide to trace from its sender.
247       // Another instance of safepoint bias.
248       top_frame = *current;
249       break;
250     }
251 
252     // Check for a matching compiled method.
253     if (current->cb()->as_nmethod_or_null() == sampled_nm) {
254       if (current->pc() != sampled_pc) {
255         // Let's adjust for the safepoint bias if we can.
256         const PcDesc* const pc_desc = get_pc_desc(sampled_nm, sampled_pc);
257         if (is_valid(pc_desc)) {
258           current->adjust_pc(pc_desc->real_pc(sampled_nm));
259           biased = false;
260         }
261       }
262     }
263     // Either a hit or a mismatched sample in which case we trace from the sender.
264     // Yet another instance of safepoint bias,to be addressed with
265     // more exact and stricter versions when parsable blobs become available.
266     top_frame = *current;
267     break;
268   }
269 
270   in_continuation = is_in_continuation(top_frame, jt);
271   return true;
272 }
273 
274 static void record_thread_in_java(const JfrSampleRequest& request, const JfrTicks& now, const JfrThreadLocal* tl, JavaThread* jt, Thread* current) {
275   assert(jt != nullptr, "invariant");
276   assert(tl != nullptr, "invariant");
277   assert(current != nullptr, "invariant");
278 
279   frame top_frame;
280   bool biased = false;
281   bool in_continuation;
282   if (!compute_top_frame(request, top_frame, in_continuation, jt, biased)) {
283     return;
284   }
285 
286   traceid sid;
287   {
288     ResourceMark rm(current);
289     JfrStackTrace stacktrace;
290     if (!stacktrace.record(jt, top_frame, in_continuation, request)) {
291       // Unable to record stacktrace. Fail.
292       return;
293     }
294     sid = JfrStackTraceRepository::add(stacktrace);
295   }
296   assert(sid != 0, "invariant");
297   const traceid tid = in_continuation ? tl->vthread_id_with_epoch_update(jt) : JfrThreadLocal::jvm_thread_id(jt);
298   send_sample_event<EventExecutionSample>(request._sample_ticks, now, sid, tid);
299   if (current == jt) {
300     send_safepoint_latency_event(request, now, sid, jt);
301   }
302 }
303 
304 #ifdef LINUX
305 static void record_cpu_time_thread(const JfrCPUTimeSampleRequest& request, const JfrTicks& now, const JfrThreadLocal* tl, JavaThread* jt, Thread* current) {
306   assert(jt != nullptr, "invariant");
307   assert(tl != nullptr, "invariant");
308   assert(current != nullptr, "invariant");
309   frame top_frame;
310   bool biased = false;
311   bool in_continuation = false;
312   bool could_compute_top_frame = compute_top_frame(request._request, top_frame, in_continuation, jt, biased);
313   const traceid tid = in_continuation ? tl->vthread_id_with_epoch_update(jt) : JfrThreadLocal::jvm_thread_id(jt);
314 
315   if (!could_compute_top_frame) {
316     JfrCPUTimeThreadSampling::send_empty_event(request._request._sample_ticks, tid, request._cpu_time_period);
317     return;
318   }
319   traceid sid;
320   {
321     ResourceMark rm(current);
322     JfrStackTrace stacktrace;
323     if (!stacktrace.record(jt, top_frame, in_continuation, request._request)) {
324       // Unable to record stacktrace. Fail.
325       JfrCPUTimeThreadSampling::send_empty_event(request._request._sample_ticks, tid, request._cpu_time_period);
326       return;
327     }
328     sid = JfrStackTraceRepository::add(stacktrace);
329   }
330   assert(sid != 0, "invariant");
331 
332 
333   JfrCPUTimeThreadSampling::send_event(request._request._sample_ticks, sid, tid, request._cpu_time_period, biased);
334   if (current == jt) {
335     send_safepoint_latency_event(request._request, now, sid, jt);
336   }
337 }
338 #endif
339 
340 static void drain_enqueued_requests(const JfrTicks& now, JfrThreadLocal* tl, JavaThread* jt, Thread* current) {
341   assert(tl != nullptr, "invariant");
342   assert(jt != nullptr, "invariant");
343   assert(current != nullptr, "invariant");
344   assert(jt->jfr_thread_local() == tl, "invariant");
345   assert_lock_strong(tl->sample_monitor());
346   if (tl->has_enqueued_requests()) {
347     for (const JfrSampleRequest& request : *tl->sample_requests()) {
348       record_thread_in_java(request, now, tl, jt, current);
349     }
350     tl->clear_enqueued_requests();
351   }
352   assert(!tl->has_enqueued_requests(), "invariant");
353 }
354 
355 static void drain_enqueued_cpu_time_requests(const JfrTicks& now, JfrThreadLocal* tl, JavaThread* jt, Thread* current, bool lock) {
356   assert(tl != nullptr, "invariant");
357   assert(jt != nullptr, "invariant");
358   assert(current != nullptr, "invariant");
359 #ifdef LINUX
360   tl->set_do_async_processing_of_cpu_time_jfr_requests(false);
361   if (lock) {
362     tl->acquire_cpu_time_jfr_dequeue_lock();
363   }
364   JfrCPUTimeTraceQueue& queue = tl->cpu_time_jfr_queue();
365   for (u4 i = 0; i < queue.size(); i++) {
366     record_cpu_time_thread(queue.at(i), now, tl, jt, current);
367   }
368   queue.clear();
369   assert(queue.is_empty(), "invariant");
370   tl->set_has_cpu_time_jfr_requests(false);
371   if (queue.lost_samples() > 0) {
372     JfrCPUTimeThreadSampling::send_lost_event( now, JfrThreadLocal::thread_id(jt), queue.get_and_reset_lost_samples());
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