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_error(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* jth = JavaThread::cast(context.thread());
179   // Skip sample if we signaled a thread that moved to other state
180   if (!thread_state_in_java(jth)) {
181     return;
182   }
183   JfrGetCallTrace trace(true, jth);
184   frame topframe;
185   if (trace.get_topframe(context.ucontext(), topframe)) {
186     if (_stacktrace.record_thread(*jth, topframe)) {
187       /* If we managed to get a topframe and a stacktrace, create an event
188       * and put it into our array. We can't call Jfr::_stacktraces.add()
189       * here since it would allocate memory using malloc. Doing so while
190       * the stopped thread is inside malloc would deadlock. */
191       _success = true;
192       EventExecutionSample *ev = _closure.next_event();
193       ev->set_starttime(_suspend_time);
194       ev->set_endtime(_suspend_time); // fake to not take an end time
195       ev->set_sampledThread(JFR_THREAD_ID(jth));
196       ev->set_state(static_cast<u8>(java_lang_Thread::get_thread_status(_thread_oop)));
197     }
198   }
199 }
200 
201 void OSThreadSampler::take_sample() {
202   run();
203 }
204 
205 class JfrNativeSamplerCallback : public os::CrashProtectionCallback {
206  public:
207   JfrNativeSamplerCallback(JfrThreadSampleClosure& closure, JavaThread* jt, JfrStackFrame* frames, u4 max_frames) :
208     _closure(closure), _jt(jt), _thread_oop(jt->threadObj()), _stacktrace(frames, max_frames), _success(false) {
209   }
210   virtual void call();
211   bool success() { return _success; }
212   JfrStackTrace& stacktrace() { return _stacktrace; }
213 
214  private:
215   JfrThreadSampleClosure& _closure;
216   JavaThread* _jt;
217   oop _thread_oop;
218   JfrStackTrace _stacktrace;
219   bool _success;
220 };
221 
222 static void write_native_event(JfrThreadSampleClosure& closure, JavaThread* jt, oop thread_oop) {
223   EventNativeMethodSample *ev = closure.next_event_native();
224   ev->set_starttime(JfrTicks::now());
225   ev->set_sampledThread(JFR_THREAD_ID(jt));
226   ev->set_state(static_cast<u8>(java_lang_Thread::get_thread_status(thread_oop)));
227 }
228 
229 void JfrNativeSamplerCallback::call() {
230   // When a thread is only attach it will be native without a last java frame
231   if (!_jt->has_last_Java_frame()) {
232     return;
233   }
234 
235   frame topframe = _jt->last_frame();
236   frame first_java_frame;
237   Method* method = NULL;
238   JfrGetCallTrace gct(false, _jt);
239   if (!gct.find_top_frame(topframe, &method, first_java_frame)) {
240     return;
241   }
242   if (method == NULL) {
243     return;
244   }
245   topframe = first_java_frame;
246   _success = _stacktrace.record_thread(*_jt, topframe);
247   if (_success) {
248     write_native_event(_closure, _jt, _thread_oop);
249   }
250 }
251 
252 bool JfrThreadSampleClosure::sample_thread_in_java(JavaThread* thread, JfrStackFrame* frames, u4 max_frames) {
253   OSThreadSampler sampler(thread, *this, frames, max_frames);
254   sampler.take_sample();
255   /* We don't want to allocate any memory using malloc/etc while the thread
256   * is stopped, so everything is stored in stack allocated memory until this
257   * point where the thread has been resumed again, if the sampling was a success
258   * we need to store the stacktrace in the stacktrace repository and update
259   * the event with the id that was returned. */
260   if (!sampler.success()) {
261     return false;
262   }
263   EventExecutionSample *event = &_events[_added_java - 1];
264   traceid id = JfrStackTraceRepository::add(sampler.stacktrace());
265   assert(id != 0, "Stacktrace id should not be 0");
266   event->set_stackTrace(id);
267   return true;
268 }
269 
270 bool JfrThreadSampleClosure::sample_thread_in_native(JavaThread* thread, JfrStackFrame* frames, u4 max_frames) {
271   JfrNativeSamplerCallback cb(*this, thread, frames, max_frames);
272   if (JfrOptionSet::sample_protection()) {
273     os::ThreadCrashProtection crash_protection;
274     if (!crash_protection.call(cb)) {
275       log_error(jfr)("Thread method sampler crashed for native");
276     }
277   } else {
278     cb.call();
279   }
280   if (!cb.success()) {
281     return false;
282   }
283   EventNativeMethodSample *event = &_events_native[_added_native - 1];
284   traceid id = JfrStackTraceRepository::add(cb.stacktrace());
285   assert(id != 0, "Stacktrace id should not be 0");
286   event->set_stackTrace(id);
287   return true;
288 }
289 
290 static const uint MAX_NR_OF_JAVA_SAMPLES = 5;
291 static const uint MAX_NR_OF_NATIVE_SAMPLES = 1;
292 
293 void JfrThreadSampleClosure::commit_events(JfrSampleType type) {
294   if (JAVA_SAMPLE == type) {
295     assert(_added_java > 0 && _added_java <= MAX_NR_OF_JAVA_SAMPLES, "invariant");
296     for (uint i = 0; i < _added_java; ++i) {
297       _events[i].commit();
298     }
299   } else {
300     assert(NATIVE_SAMPLE == type, "invariant");
301     assert(_added_native > 0 && _added_native <= MAX_NR_OF_NATIVE_SAMPLES, "invariant");
302     for (uint i = 0; i < _added_native; ++i) {
303       _events_native[i].commit();
304     }
305   }
306 }
307 
308 JfrThreadSampleClosure::JfrThreadSampleClosure(EventExecutionSample* events, EventNativeMethodSample* events_native) :
309   _events(events),
310   _events_native(events_native),
311   _self(Thread::current()),
312   _added_java(0),
313   _added_native(0) {
314 }
315 
316 class JfrThreadSampler : public NonJavaThread {
317   friend class JfrThreadSampling;
318  private:
319   Semaphore _sample;
320   Thread* _sampler_thread;
321   JfrStackFrame* const _frames;
322   JavaThread* _last_thread_java;
323   JavaThread* _last_thread_native;
324   size_t _interval_java;
325   size_t _interval_native;
326   int _cur_index;
327   const u4 _max_frames;
328   volatile bool _disenrolled;
329 
330   JavaThread* next_thread(ThreadsList* t_list, JavaThread* first_sampled, JavaThread* current);
331   void task_stacktrace(JfrSampleType type, JavaThread** last_thread);
332   JfrThreadSampler(size_t interval_java, size_t interval_native, u4 max_frames);
333   ~JfrThreadSampler();
334 
335   void start_thread();
336 
337   void enroll();
338   void disenroll();
339   void set_java_interval(size_t interval) { _interval_java = interval; };
340   void set_native_interval(size_t interval) { _interval_native = interval; };
341   size_t get_java_interval() { return _interval_java; };
342   size_t get_native_interval() { return _interval_native; };
343  protected:
344   virtual void post_run();
345  public:
346   virtual const char* name() const { return "JFR Thread Sampler"; }
347   virtual const char* type_name() const { return "JfrThreadSampler"; }
348   bool is_JfrSampler_thread() const { return true; }
349   void run();
350   static Monitor* transition_block() { return JfrThreadSampler_lock; }
351   static void on_javathread_suspend(JavaThread* thread);
352 };
353 
354 static void clear_transition_block(JavaThread* jt) {
355   assert(Threads_lock->owned_by_self(), "Holding the thread table lock.");
356   jt->clear_trace_flag();
357   JfrThreadLocal* const tl = jt->jfr_thread_local();
358   MutexLocker ml(JfrThreadSampler::transition_block(), Mutex::_no_safepoint_check_flag);
359   if (tl->is_trace_block()) {
360     JfrThreadSampler::transition_block()->notify();
361   }
362 }
363 
364 static bool is_excluded(JavaThread* thread) {
365   assert(thread != NULL, "invariant");
366   return thread->is_hidden_from_external_view() || thread->in_deopt_handler() || thread->jfr_thread_local()->is_excluded();
367 }
368 
369 bool JfrThreadSampleClosure::do_sample_thread(JavaThread* thread, JfrStackFrame* frames, u4 max_frames, JfrSampleType type) {
370   assert(Threads_lock->owned_by_self(), "Holding the thread table lock.");
371   if (is_excluded(thread)) {
372     return false;
373   }
374 
375   bool ret = false;
376   thread->set_trace_flag();  // Provides StoreLoad, needed to keep read of thread state from floating up.
377   if (JAVA_SAMPLE == type) {
378     if (thread_state_in_java(thread)) {
379       ret = sample_thread_in_java(thread, frames, max_frames);
380     }
381   } else {
382     assert(NATIVE_SAMPLE == type, "invariant");
383     if (thread_state_in_native(thread)) {
384       ret = sample_thread_in_native(thread, frames, max_frames);
385     }
386   }
387   clear_transition_block(thread);
388   return ret;
389 }
390 
391 JfrThreadSampler::JfrThreadSampler(size_t interval_java, size_t interval_native, u4 max_frames) :
392   _sample(),
393   _sampler_thread(NULL),
394   _frames(JfrCHeapObj::new_array<JfrStackFrame>(max_frames)),
395   _last_thread_java(NULL),
396   _last_thread_native(NULL),
397   _interval_java(interval_java),
398   _interval_native(interval_native),
399   _cur_index(-1),
400   _max_frames(max_frames),
401   _disenrolled(true) {
402 }
403 
404 JfrThreadSampler::~JfrThreadSampler() {
405   JfrCHeapObj::free(_frames, sizeof(JfrStackFrame) * _max_frames);
406 }
407 
408 static inline bool is_released(JavaThread* jt) {
409   return !jt->is_trace_suspend();
410 }
411 
412 void JfrThreadSampler::on_javathread_suspend(JavaThread* thread) {
413   if (is_released(thread)) {
414     return;
415   }
416   JfrThreadLocal* const tl = thread->jfr_thread_local();
417   MonitorLocker ml(transition_block(), Mutex::_no_safepoint_check_flag);
418   tl->set_trace_block();
419   while (!is_released(thread)) {
420     ml.wait();
421   }
422   tl->clear_trace_block();
423 }
424 
425 JavaThread* JfrThreadSampler::next_thread(ThreadsList* t_list, JavaThread* first_sampled, JavaThread* current) {
426   assert(t_list != NULL, "invariant");
427   assert(Threads_lock->owned_by_self(), "Holding the thread table lock.");
428   assert(_cur_index >= -1 && (uint)_cur_index + 1 <= t_list->length(), "invariant");
429   assert((current == NULL && -1 == _cur_index) || (t_list->find_index_of_JavaThread(current) == _cur_index), "invariant");
430   if ((uint)_cur_index + 1 == t_list->length()) {
431     // wrap
432     _cur_index = 0;
433   } else {
434     _cur_index++;
435   }
436   assert(_cur_index >= 0 && (uint)_cur_index < t_list->length(), "invariant");
437   JavaThread* const next = t_list->thread_at(_cur_index);
438   return next != first_sampled ? next : NULL;
439 }
440 
441 void JfrThreadSampler::start_thread() {
442   if (os::create_thread(this, os::os_thread)) {
443     os::start_thread(this);
444   } else {
445     log_error(jfr)("Failed to create thread for thread sampling");
446   }
447 }
448 
449 void JfrThreadSampler::enroll() {
450   if (_disenrolled) {
451     log_trace(jfr)("Enrolling thread sampler");
452     _sample.signal();
453     _disenrolled = false;
454   }
455 }
456 
457 void JfrThreadSampler::disenroll() {
458   if (!_disenrolled) {
459     _sample.wait();
460     _disenrolled = true;
461     log_trace(jfr)("Disenrolling thread sampler");
462   }
463 }
464 
465 static jlong get_monotonic_ms() {
466   return os::javaTimeNanos() / 1000000;
467 }
468 
469 void JfrThreadSampler::run() {
470   assert(_sampler_thread == NULL, "invariant");
471 
472   _sampler_thread = this;
473 
474   jlong last_java_ms = get_monotonic_ms();
475   jlong last_native_ms = last_java_ms;
476   while (true) {
477     if (!_sample.trywait()) {
478       // disenrolled
479       _sample.wait();
480       last_java_ms = get_monotonic_ms();
481       last_native_ms = last_java_ms;
482     }
483     _sample.signal();
484     jlong java_interval = _interval_java == 0 ? max_jlong : MAX2<jlong>(_interval_java, 1);
485     jlong native_interval = _interval_native == 0 ? max_jlong : MAX2<jlong>(_interval_native, 1);
486 
487     jlong now_ms = get_monotonic_ms();
488 
489     /*
490      * Let I be java_interval or native_interval.
491      * Let L be last_java_ms or last_native_ms.
492      * Let N be now_ms.
493      *
494      * Interval, I, might be max_jlong so the addition
495      * could potentially overflow without parenthesis (UB). Also note that
496      * L - N < 0. Avoid UB, by adding parenthesis.
497      */
498     jlong next_j = java_interval + (last_java_ms - now_ms);
499     jlong next_n = native_interval + (last_native_ms - now_ms);
500 
501     jlong sleep_to_next = MIN2<jlong>(next_j, next_n);
502 
503     if (sleep_to_next > 0) {
504       os::naked_short_sleep(sleep_to_next);
505     }
506 
507     if ((next_j - sleep_to_next) <= 0) {
508       task_stacktrace(JAVA_SAMPLE, &_last_thread_java);
509       last_java_ms = get_monotonic_ms();
510     }
511     if ((next_n - sleep_to_next) <= 0) {
512       task_stacktrace(NATIVE_SAMPLE, &_last_thread_native);
513       last_native_ms = get_monotonic_ms();
514     }
515   }
516 }
517 
518 void JfrThreadSampler::post_run() {
519   this->NonJavaThread::post_run();
520   delete this;
521 }
522 
523 
524 void JfrThreadSampler::task_stacktrace(JfrSampleType type, JavaThread** last_thread) {
525   ResourceMark rm;
526   EventExecutionSample samples[MAX_NR_OF_JAVA_SAMPLES];
527   EventNativeMethodSample samples_native[MAX_NR_OF_NATIVE_SAMPLES];
528   JfrThreadSampleClosure sample_task(samples, samples_native);
529 
530   const uint sample_limit = JAVA_SAMPLE == type ? MAX_NR_OF_JAVA_SAMPLES : MAX_NR_OF_NATIVE_SAMPLES;
531   uint num_samples = 0;
532   JavaThread* start = NULL;
533 
534   {
535     elapsedTimer sample_time;
536     sample_time.start();
537     {
538       MutexLocker tlock(Threads_lock);
539       ThreadsListHandle tlh;
540       // Resolve a sample session relative start position index into the thread list array.
541       // In cases where the last sampled thread is NULL or not-NULL but stale, find_index() returns -1.
542       _cur_index = tlh.list()->find_index_of_JavaThread(*last_thread);
543       JavaThread* current = _cur_index != -1 ? *last_thread : NULL;
544 
545       while (num_samples < sample_limit) {
546         current = next_thread(tlh.list(), start, current);
547         if (current == NULL) {
548           break;
549         }
550         if (start == NULL) {
551           start = current;  // remember the thread where we started to attempt sampling
552         }
553         if (current->is_Compiler_thread()) {
554           continue;
555         }
556         if (sample_task.do_sample_thread(current, _frames, _max_frames, type)) {
557           num_samples++;
558         }
559       }
560       *last_thread = current;  // remember the thread we last attempted to sample
561     }
562     sample_time.stop();
563     log_trace(jfr)("JFR thread sampling done in %3.7f secs with %d java %d native samples",
564                    sample_time.seconds(), sample_task.java_entries(), sample_task.native_entries());
565   }
566   if (num_samples > 0) {
567     sample_task.commit_events(type);
568   }
569 }
570 
571 static JfrThreadSampling* _instance = NULL;
572 
573 JfrThreadSampling& JfrThreadSampling::instance() {
574   return *_instance;
575 }
576 
577 JfrThreadSampling* JfrThreadSampling::create() {
578   assert(_instance == NULL, "invariant");
579   _instance = new JfrThreadSampling();
580   return _instance;
581 }
582 
583 void JfrThreadSampling::destroy() {
584   if (_instance != NULL) {
585     delete _instance;
586     _instance = NULL;
587   }
588 }
589 
590 JfrThreadSampling::JfrThreadSampling() : _sampler(NULL) {}
591 
592 JfrThreadSampling::~JfrThreadSampling() {
593   if (_sampler != NULL) {
594     _sampler->disenroll();
595   }
596 }
597 
598 static void log(size_t interval_java, size_t interval_native) {
599   log_trace(jfr)("Updated thread sampler for java: " SIZE_FORMAT "  ms, native " SIZE_FORMAT " ms", interval_java, interval_native);
600 }
601 
602 void JfrThreadSampling::start_sampler(size_t interval_java, size_t interval_native) {
603   assert(_sampler == NULL, "invariant");
604   log_trace(jfr)("Enrolling thread sampler");
605   _sampler = new JfrThreadSampler(interval_java, interval_native, JfrOptionSet::stackdepth());
606   _sampler->start_thread();
607   _sampler->enroll();
608 }
609 
610 void JfrThreadSampling::set_sampling_interval(bool java_interval, size_t period) {
611   size_t interval_java = 0;
612   size_t interval_native = 0;
613   if (_sampler != NULL) {
614     interval_java = _sampler->get_java_interval();
615     interval_native = _sampler->get_native_interval();
616   }
617   if (java_interval) {
618     interval_java = period;
619   } else {
620     interval_native = period;
621   }
622   if (interval_java > 0 || interval_native > 0) {
623     if (_sampler == NULL) {
624       log_trace(jfr)("Creating thread sampler for java:%zu ms, native %zu ms", interval_java, interval_native);
625       start_sampler(interval_java, interval_native);
626     } else {
627       _sampler->set_java_interval(interval_java);
628       _sampler->set_native_interval(interval_native);
629       _sampler->enroll();
630     }
631     assert(_sampler != NULL, "invariant");
632     log(interval_java, interval_native);
633   } else if (_sampler != NULL) {
634     _sampler->disenroll();
635   }
636 }
637 
638 void JfrThreadSampling::set_java_sample_interval(size_t period) {
639   if (_instance == NULL && 0 == period) {
640     return;
641   }
642   instance().set_sampling_interval(true, period);
643 }
644 
645 void JfrThreadSampling::set_native_sample_interval(size_t period) {
646   if (_instance == NULL && 0 == period) {
647     return;
648   }
649   instance().set_sampling_interval(false, period);
650 }
651 
652 void JfrThreadSampling::on_javathread_suspend(JavaThread* thread) {
653   JfrThreadSampler::on_javathread_suspend(thread);
654 }