WatcherThread如何调用JVM监视例程?

时间:2017-12-22 14:15:57

标签: java garbage-collection jvm jvm-hotspot

“VM Periodic Task Thread”

  

阿卡的“WatcherThread”。这是一个定期执行的VM线程   任务,例如,更新性能计数器。

请参阅link

  

由WatcherThread创建的线程的定期任务调度是一个单例对象。

     

JVM中的线程更常用,例如,内存监控的运行状态,JVM定期监控。我们经常需要为GC案例执行一些jstat此命令。

     

如下:jstat -gcutil 234832507发出此命令告诉控制台中的JVM打印PID:GC 23483,打印间隔为250毫秒a,总共打印超过7张。

请参阅link

这是我在JVM源代码中找到的。

我认为当“VM Periodic Task Thread”(WatcherThread)启动时,它应该执行“run”功能。 我唯一注意到它在while循环中旋转, 但WatcherThread如何调用JVM监视例程,如jstat? 这个while循环中jstat的子例程在哪里? 我很好奇WatcherThread如何更新性能计数器等等。

void WatcherThread::run() {
  assert(this == watcher_thread(), "just checking");

  this->record_stack_base_and_size();
  this->set_native_thread_name(this->name());
  this->set_active_handles(JNIHandleBlock::allocate_block());

  while (true) {
    assert(watcher_thread() == Thread::current(), "thread consistency check");
    assert(watcher_thread() == this, "thread consistency check");

    // Calculate how long it'll be until the next PeriodicTask work
    // should be done, and sleep that amount of time.
    int time_waited = sleep();  // return 50

    if (is_error_reported()) {
      // A fatal error has happened, the error handler(VMError::report_and_die)
      // should abort JVM after creating an error log file. However in some
      // rare cases, the error handler itself might deadlock. Here we try to
      // kill JVM if the fatal error handler fails to abort in 2 minutes.
      //
      // This code is in WatcherThread because WatcherThread wakes up
      // periodically so the fatal error handler doesn't need to do anything;
      // also because the WatcherThread is less likely to crash than other
      // threads.

      for (;;) {
        if (!ShowMessageBoxOnError
            && (OnError == NULL || OnError[0] == '\0')
            && Arguments::abort_hook() == NULL) {
          os::sleep(this, (jlong)ErrorLogTimeout * 1000, false); // in seconds
          fdStream err(defaultStream::output_fd());
          err.print_raw_cr("# [ timer expired, abort... ]");
          // skip atexit/vm_exit/vm_abort hooks
          os::die();
        }

        // Wake up 5 seconds later, the fatal handler may reset OnError or
        // ShowMessageBoxOnError when it is ready to abort.
        os::sleep(this, 5 * 1000, false);
      }
    }

    if (_should_terminate) {
      // check for termination before posting the next tick
      break;
    }

    PeriodicTask::real_time_tick(time_waited);
  }

  // Signal that it is terminated
  {
    MutexLockerEx mu(Terminator_lock, Mutex::_no_safepoint_check_flag);
    _watcher_thread = NULL;
    Terminator_lock->notify();
  }
}

1 个答案:

答案 0 :(得分:1)

显然,JVM不会调用jstat或其他外部实用程序。

您可能正在寻找StatSampler::collect_sample

/*
 * the collect_sample() method is the method invoked by the
 * WatcherThread via the PeriodicTask::task() method. This method
 * is responsible for collecting data samples from sampled
 * PerfData instances every PerfDataSamplingInterval milliseconds.
 * It is also responsible for logging the requested set of
 * PerfData instances every _sample_count milliseconds. While
 * logging data, it will output a column header after every _print_header
 * rows of data have been logged.
 */
void StatSampler::collect_sample() {

WatcherThread执行PeriodicTask类的已注册实例,而StatSamplerTask是此类任务之一。