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