1 /*
  2  * Copyright (c) 2012, 2021, 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 "precompiled.hpp"
 26 #include "jfr/jfrEvents.hpp"
 27 #include "jfr/recorder/jfrRecorder.hpp"
 28 #include "jfr/periodic/sampling/jfrCallTrace.hpp"
 29 #include "jfr/periodic/sampling/jfrThreadSampler.hpp"
 30 #include "jfr/recorder/service/jfrOptionSet.hpp"
 31 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
 32 #include "jfr/support/jfrThreadId.hpp"
 33 #include "jfr/support/jfrThreadLocal.hpp"
 34 #include "jfr/utilities/jfrTime.hpp"
 35 #include "jfrfiles/jfrEventClasses.hpp"
 36 #include "logging/log.hpp"
 37 #include "runtime/frame.inline.hpp"
 38 #include "runtime/os.hpp"
 39 #include "runtime/semaphore.hpp"
 40 #include "runtime/thread.inline.hpp"
 41 #include "runtime/threadSMR.hpp"
 42 
 43 enum JfrSampleType {
 44   NO_SAMPLE = 0,
 45   JAVA_SAMPLE = 1,
 46   NATIVE_SAMPLE = 2
 47 };
 48 
 49 static bool thread_state_in_java(JavaThread* thread) {
 50   assert(thread != NULL, "invariant");
 51   switch(thread->thread_state()) {
 52     case _thread_new:
 53     case _thread_uninitialized:
 54     case _thread_new_trans:
 55     case _thread_in_vm_trans:
 56     case _thread_blocked_trans:
 57     case _thread_in_native_trans:
 58     case _thread_blocked:
 59     case _thread_in_vm:
 60     case _thread_in_native:
 61     case _thread_in_Java_trans:
 62       break;
 63     case _thread_in_Java:
 64       return true;
 65     default:
 66       ShouldNotReachHere();
 67       break;
 68   }
 69   return false;
 70 }
 71 
 72 static bool thread_state_in_native(JavaThread* thread) {
 73   assert(thread != NULL, "invariant");
 74   switch(thread->thread_state()) {
 75     case _thread_new:
 76     case _thread_uninitialized:
 77     case _thread_new_trans:
 78     case _thread_blocked_trans:
 79     case _thread_blocked:
 80     case _thread_in_vm:
 81     case _thread_in_vm_trans:
 82     case _thread_in_Java_trans:
 83     case _thread_in_Java:
 84     case _thread_in_native_trans:
 85       break;
 86     case _thread_in_native:
 87       return true;
 88     default:
 89       ShouldNotReachHere();
 90       break;
 91   }
 92   return false;
 93 }
 94 
 95 class JfrThreadSampleClosure {
 96  public:
 97   JfrThreadSampleClosure(EventExecutionSample* events, EventNativeMethodSample* events_native);
 98   ~JfrThreadSampleClosure() {}
 99   EventExecutionSample* next_event() { return &_events[_added_java++]; }
100   EventNativeMethodSample* next_event_native() { return &_events_native[_added_native++]; }
101   void commit_events(JfrSampleType type);
102   bool do_sample_thread(JavaThread* thread, JfrStackFrame* frames, u4 max_frames, JfrSampleType type);
103   uint java_entries() { return _added_java; }
104   uint native_entries() { return _added_native; }
105 
106  private:
107   bool sample_thread_in_java(JavaThread* thread, JfrStackFrame* frames, u4 max_frames);
108   bool sample_thread_in_native(JavaThread* thread, JfrStackFrame* frames, u4 max_frames);
109   EventExecutionSample* _events;
110   EventNativeMethodSample* _events_native;
111   Thread* _self;
112   uint _added_java;
113   uint _added_native;
114 };
115 
116 class OSThreadSampler : public os::SuspendedThreadTask {
117  public:
118   OSThreadSampler(JavaThread* thread,
119                   JfrThreadSampleClosure& closure,
120                   JfrStackFrame *frames,
121                   u4 max_frames) : os::SuspendedThreadTask((Thread*)thread),
122     _success(false),
123     _thread_oop(thread->threadObj()),
124     _stacktrace(frames, max_frames),
125     _closure(closure),
126     _suspend_time() {}
127 
128   void take_sample();
129   void do_task(const os::SuspendedThreadTaskContext& context);
130   void protected_task(const os::SuspendedThreadTaskContext& context);
131   bool success() const { return _success; }
132   const JfrStackTrace& stacktrace() const { return _stacktrace; }
133 
134  private:
135   bool _success;
136   oop _thread_oop;
137   JfrStackTrace _stacktrace;
138   JfrThreadSampleClosure& _closure;
139   JfrTicks _suspend_time;
140 };
141 
142 class OSThreadSamplerCallback : public os::CrashProtectionCallback {
143  public:
144   OSThreadSamplerCallback(OSThreadSampler& sampler, const os::SuspendedThreadTaskContext &context) :
145     _sampler(sampler), _context(context) {
146   }
147   virtual void call() {
148     _sampler.protected_task(_context);
149   }
150  private:
151   OSThreadSampler& _sampler;
152   const os::SuspendedThreadTaskContext& _context;
153 };
154 
155 void OSThreadSampler::do_task(const os::SuspendedThreadTaskContext& context) {
156 #ifndef ASSERT
157   guarantee(JfrOptionSet::sample_protection(), "Sample Protection should be on in product builds");
158 #endif
159   assert(_suspend_time.value() == 0, "already timestamped!");
160   _suspend_time = JfrTicks::now();
161 
162   if (JfrOptionSet::sample_protection()) {
163     OSThreadSamplerCallback cb(*this, context);
164     os::ThreadCrashProtection crash_protection;
165     if (!crash_protection.call(cb)) {
166       log_debug(jfr)("Thread method sampler crashed");
167     }
168   } else {
169     protected_task(context);
170   }
171 }
172 
173 /*
174 * From this method and down the call tree we attempt to protect against crashes
175 * using a signal handler / __try block. Don't take locks, rely on destructors or
176 * leave memory (in case of signal / exception) in an inconsistent state. */
177 void OSThreadSampler::protected_task(const os::SuspendedThreadTaskContext& context) {
178   JavaThread* const jt = JavaThread::cast(context.thread());
179   // Skip sample if we signaled a thread that moved to other state
180   if (!thread_state_in_java(jt)) {
181     return;
182   }
183   JfrGetCallTrace trace(true, jt);
184   frame topframe;
185   if (trace.get_topframe(context.ucontext(), topframe)) {
186     bool virtual_thread;
187     if (_stacktrace.record_async(jt, topframe, &virtual_thread)) {
188       /* If we managed to get a topframe and a stacktrace, create an event
189       * and put it into our array. We can't call Jfr::_stacktraces.add()
190       * here since it would allocate memory using malloc. Doing so while
191       * the stopped thread is inside malloc would deadlock. */
192       _success = true;
193       EventExecutionSample *ev = _closure.next_event();
194       ev->set_starttime(_suspend_time);
195       ev->set_endtime(_suspend_time); // fake to not take an end time
196       ev->set_sampledThread(virtual_thread ? JFR_THREAD_ID(jt) : JFR_VM_THREAD_ID(jt));
197       ev->set_state(static_cast<u8>(java_lang_Thread::get_thread_status(_thread_oop)));
198     }
199   }
200 }
201 
202 void OSThreadSampler::take_sample() {
203   run();
204 }
205 
206 class JfrNativeSamplerCallback : public os::CrashProtectionCallback {
207  public:
208   JfrNativeSamplerCallback(JfrThreadSampleClosure& closure, JavaThread* jt, JfrStackFrame* frames, u4 max_frames) :
209     _closure(closure), _jt(jt), _thread_oop(jt->threadObj()), _stacktrace(frames, max_frames), _success(false) {
210   }
211   virtual void call();
212   bool success() { return _success; }
213   JfrStackTrace& stacktrace() { return _stacktrace; }
214 
215  private:
216   JfrThreadSampleClosure& _closure;
217   JavaThread* _jt;
218   oop _thread_oop;
219   JfrStackTrace _stacktrace;
220   bool _success;
221 };
222 
223 static void write_native_event(JfrThreadSampleClosure& closure, bool virtual_thread, JavaThread* jt, oop thread_oop) {
224   EventNativeMethodSample *ev = closure.next_event_native();
225   ev->set_starttime(JfrTicks::now());
226   ev->set_sampledThread(virtual_thread ? JFR_THREAD_ID(jt) : JFR_VM_THREAD_ID(jt));
227   ev->set_state(static_cast<u8>(java_lang_Thread::get_thread_status(thread_oop)));
228 }
229 
230 void JfrNativeSamplerCallback::call() {
231   // When a thread is only attach it will be native without a last java frame
232   if (!_jt->has_last_Java_frame()) {
233     return;
234   }
235 
236   frame topframe = _jt->last_frame();
237   frame first_java_frame;
238   Method* method = NULL;
239   JfrGetCallTrace gct(false, _jt);
240   if (!gct.find_top_frame(topframe, &method, first_java_frame)) {
241     return;
242   }
243   if (method == NULL) {
244     return;
245   }
246   topframe = first_java_frame;
247   bool virtual_thread;
248   _success = _stacktrace.record_async(_jt, topframe, &virtual_thread);
249   if (_success) {
250     write_native_event(_closure, virtual_thread, _jt, _thread_oop);
251   }
252 }
253 
254 bool JfrThreadSampleClosure::sample_thread_in_java(JavaThread* thread, JfrStackFrame* frames, u4 max_frames) {
255   OSThreadSampler sampler(thread, *this, frames, max_frames);
256   sampler.take_sample();
257   /* We don't want to allocate any memory using malloc/etc while the thread
258   * is stopped, so everything is stored in stack allocated memory until this
259   * point where the thread has been resumed again, if the sampling was a success
260   * we need to store the stacktrace in the stacktrace repository and update
261   * the event with the id that was returned. */
262   if (!sampler.success()) {
263     return false;
264   }
265   EventExecutionSample *event = &_events[_added_java - 1];
266   traceid id = JfrStackTraceRepository::add(sampler.stacktrace());
267   assert(id != 0, "Stacktrace id should not be 0");
268   event->set_stackTrace(id);
269   return true;
270 }
271 
272 bool JfrThreadSampleClosure::sample_thread_in_native(JavaThread* thread, JfrStackFrame* frames, u4 max_frames) {
273   JfrNativeSamplerCallback cb(*this, thread, frames, max_frames);
274   if (JfrOptionSet::sample_protection()) {
275     os::ThreadCrashProtection crash_protection;
276     if (!crash_protection.call(cb)) {
277       log_error(jfr)("Thread method sampler crashed for native");
278     }
279   } else {
280     cb.call();
281   }
282   if (!cb.success()) {
283     return false;
284   }
285   EventNativeMethodSample *event = &_events_native[_added_native - 1];
286   traceid id = JfrStackTraceRepository::add(cb.stacktrace());
287   assert(id != 0, "Stacktrace id should not be 0");
288   event->set_stackTrace(id);
289   return true;
290 }
291 
292 static const uint MAX_NR_OF_JAVA_SAMPLES = 5;
293 static const uint MAX_NR_OF_NATIVE_SAMPLES = 1;
294 
295 void JfrThreadSampleClosure::commit_events(JfrSampleType type) {
296   if (JAVA_SAMPLE == type) {
297     assert(_added_java > 0 && _added_java <= MAX_NR_OF_JAVA_SAMPLES, "invariant");
298     for (uint i = 0; i < _added_java; ++i) {
299       _events[i].commit();
300     }
301   } else {
302     assert(NATIVE_SAMPLE == type, "invariant");
303     assert(_added_native > 0 && _added_native <= MAX_NR_OF_NATIVE_SAMPLES, "invariant");
304     for (uint i = 0; i < _added_native; ++i) {
305       _events_native[i].commit();
306     }
307   }
308 }
309 
310 JfrThreadSampleClosure::JfrThreadSampleClosure(EventExecutionSample* events, EventNativeMethodSample* events_native) :
311   _events(events),
312   _events_native(events_native),
313   _self(Thread::current()),
314   _added_java(0),
315   _added_native(0) {
316 }
317 
318 class JfrThreadSampler : public NonJavaThread {
319   friend class JfrThreadSampling;
320  private:
321   Semaphore _sample;
322   Thread* _sampler_thread;
323   JfrStackFrame* const _frames;
324   JavaThread* _last_thread_java;
325   JavaThread* _last_thread_native;
326   size_t _interval_java;
327   size_t _interval_native;
328   int _cur_index;
329   const u4 _max_frames;
330   volatile bool _disenrolled;
331 
332   JavaThread* next_thread(ThreadsList* t_list, JavaThread* first_sampled, JavaThread* current);
333   void task_stacktrace(JfrSampleType type, JavaThread** last_thread);
334   JfrThreadSampler(size_t interval_java, size_t interval_native, u4 max_frames);
335   ~JfrThreadSampler();
336 
337   void start_thread();
338 
339   void enroll();
340   void disenroll();
341   void set_java_interval(size_t interval) { _interval_java = interval; };
342   void set_native_interval(size_t interval) { _interval_native = interval; };
343   size_t get_java_interval() { return _interval_java; };
344   size_t get_native_interval() { return _interval_native; };
345  protected:
346   virtual void post_run();
347  public:
348   virtual const char* name() const { return "JFR Thread Sampler"; }
349   virtual const char* type_name() const { return "JfrThreadSampler"; }
350   bool is_JfrSampler_thread() const { return true; }
351   void run();
352   static Monitor* transition_block() { return JfrThreadSampler_lock; }
353   static void on_javathread_suspend(JavaThread* thread);
354 };
355 
356 static void clear_transition_block(JavaThread* jt) {
357   assert(Threads_lock->owned_by_self(), "Holding the thread table lock.");
358   jt->clear_trace_flag();
359   JfrThreadLocal* const tl = jt->jfr_thread_local();
360   MutexLocker ml(JfrThreadSampler::transition_block(), Mutex::_no_safepoint_check_flag);
361   if (tl->is_trace_block()) {
362     JfrThreadSampler::transition_block()->notify();
363   }
364 }
365 
366 static bool is_excluded(JavaThread* thread) {
367   assert(thread != NULL, "invariant");
368   return thread->is_hidden_from_external_view() || thread->in_deopt_handler() || thread->jfr_thread_local()->is_excluded();
369 }
370 
371 bool JfrThreadSampleClosure::do_sample_thread(JavaThread* thread, JfrStackFrame* frames, u4 max_frames, JfrSampleType type) {
372   assert(Threads_lock->owned_by_self(), "Holding the thread table lock.");
373   if (is_excluded(thread)) {
374     return false;
375   }
376 
377   bool ret = false;
378   thread->set_trace_flag();  // Provides StoreLoad, needed to keep read of thread state from floating up.
379   if (JAVA_SAMPLE == type) {
380     if (thread_state_in_java(thread)) {
381       ret = sample_thread_in_java(thread, frames, max_frames);
382     }
383   } else {
384     assert(NATIVE_SAMPLE == type, "invariant");
385     if (thread_state_in_native(thread)) {
386       ret = sample_thread_in_native(thread, frames, max_frames);
387     }
388   }
389   clear_transition_block(thread);
390   return ret;
391 }
392 
393 JfrThreadSampler::JfrThreadSampler(size_t interval_java, size_t interval_native, u4 max_frames) :
394   _sample(),
395   _sampler_thread(NULL),
396   _frames(JfrCHeapObj::new_array<JfrStackFrame>(max_frames)),
397   _last_thread_java(NULL),
398   _last_thread_native(NULL),
399   _interval_java(interval_java),
400   _interval_native(interval_native),
401   _cur_index(-1),
402   _max_frames(max_frames),
403   _disenrolled(true) {
404 }
405 
406 JfrThreadSampler::~JfrThreadSampler() {
407   JfrCHeapObj::free(_frames, sizeof(JfrStackFrame) * _max_frames);
408 }
409 
410 static inline bool is_released(JavaThread* jt) {
411   return !jt->is_trace_suspend();
412 }
413 
414 void JfrThreadSampler::on_javathread_suspend(JavaThread* thread) {
415   if (is_released(thread)) {
416     return;
417   }
418   JfrThreadLocal* const tl = thread->jfr_thread_local();
419   MonitorLocker ml(transition_block(), Mutex::_no_safepoint_check_flag);
420   tl->set_trace_block();
421   while (!is_released(thread)) {
422     ml.wait();
423   }
424   tl->clear_trace_block();
425 }
426 
427 JavaThread* JfrThreadSampler::next_thread(ThreadsList* t_list, JavaThread* first_sampled, JavaThread* current) {
428   assert(t_list != NULL, "invariant");
429   assert(Threads_lock->owned_by_self(), "Holding the thread table lock.");
430   assert(_cur_index >= -1 && (uint)_cur_index + 1 <= t_list->length(), "invariant");
431   assert((current == NULL && -1 == _cur_index) || (t_list->find_index_of_JavaThread(current) == _cur_index), "invariant");
432   if ((uint)_cur_index + 1 == t_list->length()) {
433     // wrap
434     _cur_index = 0;
435   } else {
436     _cur_index++;
437   }
438   assert(_cur_index >= 0 && (uint)_cur_index < t_list->length(), "invariant");
439   JavaThread* const next = t_list->thread_at(_cur_index);
440   return next != first_sampled ? next : NULL;
441 }
442 
443 void JfrThreadSampler::start_thread() {
444   if (os::create_thread(this, os::os_thread)) {
445     os::start_thread(this);
446   } else {
447     log_error(jfr)("Failed to create thread for thread sampling");
448   }
449 }
450 
451 void JfrThreadSampler::enroll() {
452   if (_disenrolled) {
453     log_trace(jfr)("Enrolling thread sampler");
454     _sample.signal();
455     _disenrolled = false;
456   }
457 }
458 
459 void JfrThreadSampler::disenroll() {
460   if (!_disenrolled) {
461     _sample.wait();
462     _disenrolled = true;
463     log_trace(jfr)("Disenrolling thread sampler");
464   }
465 }
466 
467 static jlong get_monotonic_ms() {
468   return os::javaTimeNanos() / 1000000;
469 }
470 
471 void JfrThreadSampler::run() {
472   assert(_sampler_thread == NULL, "invariant");
473 
474   _sampler_thread = this;
475 
476   jlong last_java_ms = get_monotonic_ms();
477   jlong last_native_ms = last_java_ms;
478   while (true) {
479     if (!_sample.trywait()) {
480       // disenrolled
481       _sample.wait();
482       last_java_ms = get_monotonic_ms();
483       last_native_ms = last_java_ms;
484     }
485     _sample.signal();
486     jlong java_interval = _interval_java == 0 ? max_jlong : MAX2<jlong>(_interval_java, 1);
487     jlong native_interval = _interval_native == 0 ? max_jlong : MAX2<jlong>(_interval_native, 1);
488 
489     jlong now_ms = get_monotonic_ms();
490 
491     /*
492      * Let I be java_interval or native_interval.
493      * Let L be last_java_ms or last_native_ms.
494      * Let N be now_ms.
495      *
496      * Interval, I, might be max_jlong so the addition
497      * could potentially overflow without parenthesis (UB). Also note that
498      * L - N < 0. Avoid UB, by adding parenthesis.
499      */
500     jlong next_j = java_interval + (last_java_ms - now_ms);
501     jlong next_n = native_interval + (last_native_ms - now_ms);
502 
503     jlong sleep_to_next = MIN2<jlong>(next_j, next_n);
504 
505     if (sleep_to_next > 0) {
506       os::naked_short_sleep(sleep_to_next);
507     }
508 
509     if ((next_j - sleep_to_next) <= 0) {
510       task_stacktrace(JAVA_SAMPLE, &_last_thread_java);
511       last_java_ms = get_monotonic_ms();
512     }
513     if ((next_n - sleep_to_next) <= 0) {
514       task_stacktrace(NATIVE_SAMPLE, &_last_thread_native);
515       last_native_ms = get_monotonic_ms();
516     }
517   }
518 }
519 
520 void JfrThreadSampler::post_run() {
521   this->NonJavaThread::post_run();
522   delete this;
523 }
524 
525 
526 void JfrThreadSampler::task_stacktrace(JfrSampleType type, JavaThread** last_thread) {
527   ResourceMark rm;
528   EventExecutionSample samples[MAX_NR_OF_JAVA_SAMPLES];
529   EventNativeMethodSample samples_native[MAX_NR_OF_NATIVE_SAMPLES];
530   JfrThreadSampleClosure sample_task(samples, samples_native);
531 
532   const uint sample_limit = JAVA_SAMPLE == type ? MAX_NR_OF_JAVA_SAMPLES : MAX_NR_OF_NATIVE_SAMPLES;
533   uint num_samples = 0;
534   JavaThread* start = NULL;
535 
536   {
537     elapsedTimer sample_time;
538     sample_time.start();
539     {
540       MutexLocker tlock(Threads_lock);
541       ThreadsListHandle tlh;
542       // Resolve a sample session relative start position index into the thread list array.
543       // In cases where the last sampled thread is NULL or not-NULL but stale, find_index() returns -1.
544       _cur_index = tlh.list()->find_index_of_JavaThread(*last_thread);
545       JavaThread* current = _cur_index != -1 ? *last_thread : NULL;
546 
547       while (num_samples < sample_limit) {
548         current = next_thread(tlh.list(), start, current);
549         if (current == NULL) {
550           break;
551         }
552         if (start == NULL) {
553           start = current;  // remember the thread where we started to attempt sampling
554         }
555         if (current->is_Compiler_thread()) {
556           continue;
557         }
558         if (sample_task.do_sample_thread(current, _frames, _max_frames, type)) {
559           num_samples++;
560         }
561       }
562       *last_thread = current;  // remember the thread we last attempted to sample
563     }
564     sample_time.stop();
565     log_trace(jfr)("JFR thread sampling done in %3.7f secs with %d java %d native samples",
566                    sample_time.seconds(), sample_task.java_entries(), sample_task.native_entries());
567   }
568   if (num_samples > 0) {
569     sample_task.commit_events(type);
570   }
571 }
572 
573 static JfrThreadSampling* _instance = NULL;
574 
575 JfrThreadSampling& JfrThreadSampling::instance() {
576   return *_instance;
577 }
578 
579 JfrThreadSampling* JfrThreadSampling::create() {
580   assert(_instance == NULL, "invariant");
581   _instance = new JfrThreadSampling();
582   return _instance;
583 }
584 
585 void JfrThreadSampling::destroy() {
586   if (_instance != NULL) {
587     delete _instance;
588     _instance = NULL;
589   }
590 }
591 
592 JfrThreadSampling::JfrThreadSampling() : _sampler(NULL) {}
593 
594 JfrThreadSampling::~JfrThreadSampling() {
595   if (_sampler != NULL) {
596     _sampler->disenroll();
597   }
598 }
599 
600 static void log(size_t interval_java, size_t interval_native) {
601   log_trace(jfr)("Updated thread sampler for java: " SIZE_FORMAT "  ms, native " SIZE_FORMAT " ms", interval_java, interval_native);
602 }
603 
604 void JfrThreadSampling::start_sampler(size_t interval_java, size_t interval_native) {
605   assert(_sampler == NULL, "invariant");
606   log_trace(jfr)("Enrolling thread sampler");
607   _sampler = new JfrThreadSampler(interval_java, interval_native, JfrOptionSet::stackdepth());
608   _sampler->start_thread();
609   _sampler->enroll();
610 }
611 
612 void JfrThreadSampling::set_sampling_interval(bool java_interval, size_t period) {
613   size_t interval_java = 0;
614   size_t interval_native = 0;
615   if (_sampler != NULL) {
616     interval_java = _sampler->get_java_interval();
617     interval_native = _sampler->get_native_interval();
618   }
619   if (java_interval) {
620     interval_java = period;
621   } else {
622     interval_native = period;
623   }
624   if (interval_java > 0 || interval_native > 0) {
625     if (_sampler == NULL) {
626       log_trace(jfr)("Creating thread sampler for java:%zu ms, native %zu ms", interval_java, interval_native);
627       start_sampler(interval_java, interval_native);
628     } else {
629       _sampler->set_java_interval(interval_java);
630       _sampler->set_native_interval(interval_native);
631       _sampler->enroll();
632     }
633     assert(_sampler != NULL, "invariant");
634     log(interval_java, interval_native);
635   } else if (_sampler != NULL) {
636     _sampler->disenroll();
637   }
638 }
639 
640 void JfrThreadSampling::set_java_sample_interval(size_t period) {
641   if (_instance == NULL && 0 == period) {
642     return;
643   }
644   instance().set_sampling_interval(true, period);
645 }
646 
647 void JfrThreadSampling::set_native_sample_interval(size_t period) {
648   if (_instance == NULL && 0 == period) {
649     return;
650   }
651   instance().set_sampling_interval(false, period);
652 }
653 
654 void JfrThreadSampling::on_javathread_suspend(JavaThread* thread) {
655   JfrThreadSampler::on_javathread_suspend(thread);
656 }