简体   繁体   English

WatcherThread如何调用JVM监视例程?

[英]How does WatcherThread invoke JVM monitoring routines?

"VM Periodic Task Thread" “ VM定期任务线程”

Aka the "WatcherThread". 又称为“ WatcherThread”。 This is a VM thread that performs periodic tasks, eg, updating performance counters. 这是一个VM线程,它执行定期任务,例如,更新性能计数器。

see link 链接

Periodic task scheduling of threads, it was founded by WatcherThread, is a singleton object. 由WatcherThread创建的线程的定期任务调度是一个单例对象。

The thread in JVM more frequently used, For example, the running status of the memory monitoring, JVM monitoring regularly. JVM中的线程使用频率更高,例如,内存监视的运行状态,JVM的监视定期。 And we often need to perform some jstat this command for the GC case. 对于GC情况,我们经常需要执行一些jstat这个命令。

As follows: jstat -gcutil 234832507 making this command tells the JVM in the console to print PID: GC 23483, An interval of 250 msec print a, A total of over 7 prints. 如下所示: jstat -gcutil 234832507发出此命令,告诉控制台中的JVM打印PID:GC 23483,间隔为250毫秒,一次打印,总共超过7次打印。

see link 链接

This is what I find in the JVM source code. 这是我在JVM源代码中找到的。

I think when the "VM Periodic Task Thread"(WatcherThread) starts, it should perform "run" function. 我认为,当“ VM周期性任务线程”(WatcherThread)启动时,它应该执行“运行”功能。 The only thing I notice is that it spins in the while loop, but how does WatcherThread invoke JVM monitoring routines, like jstat ? 我唯一注意到的是它在while循环中旋转,但是WatcherThread如何调用jstat类的JVM监视例程? Where is the sub-routine of jstat in this while loop? 此while循环中jstat的子例程在哪里? I am curious how WatcherThread updates performance counters and things like that. 我很好奇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();
  }
}

Obviously JVM does not call jstat or other external utilities. 显然,JVM不会调用jstat或其他外部实用程序。

You are probably looking for StatSampler::collect_sample : 您可能正在寻找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 executes registered instances of PeriodicTask class, and StatSamplerTask is one of such tasks. WatcherThread执行PeriodicTask类的注册实例,而StatSamplerTask是此类任务之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM