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