Java WatcherThread如何调用JVM监视例程?

Java WatcherThread如何调用JVM监视例程?,java,garbage-collection,jvm,jvm-hotspot,Java,Garbage Collection,Jvm,Jvm Hotspot,“VM定期任务线程” 又名“观察线程”。这是一个执行定期任务的VM线程 任务,例如更新性能计数器 看 由WatcherThread创建的线程周期任务调度是一个单例对象 线程在JVM中使用的频率更高,例如,内存的运行状态监控,JVM的定期监控。对于GC案例,我们通常需要执行一些jstat这个命令 如下所示:jstat-gcutil 234832507发出此命令告诉控制台中的JVM打印PID:GC 23483,间隔250毫秒打印a,总共打印7次以上 看 这是我在JVM源代码中找到的 我认为当“VM周

“VM定期任务线程”

又名“观察线程”。这是一个执行定期任务的VM线程 任务,例如更新性能计数器

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

线程在JVM中使用的频率更高,例如,内存的运行状态监控,JVM的定期监控。对于GC案例,我们通常需要执行一些
jstat
这个命令

如下所示:
jstat-gcutil 234832507
发出此命令告诉控制台中的JVM打印PID:GC 23483,间隔250毫秒打印a,总共打印7次以上

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

我认为当“VM周期性任务线程”(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();
  }
}

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

您可能正在寻找:


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

jstat
是一个命令行工具。我确信JVM不会调用外部命令行工具。这个循环显然在每个常规迭代中调用
PeriodicTask::real\u time\u tick
。除此之外,你引用了一段文字,没有提及其来源。谁在什么背景下写的?@Holger我添加了参考链接。我在
PeriodicTask::real\u time\u tick
中发现,它提供了一些有关计时器的会计信息。最值得注意的是
\u tasks[index]->如果挂起则执行(延迟时间)
。它似乎在
task()
中完成了一些真正的工作。知道了!我认为它最终调用<代码> STATSAMPLReTask::任务< /代码>,如果你做了一个示例。你真的考虑第二个来源来获取可行的信息吗?我仍然想知道“制造”和“制造”之间的区别是什么,在那篇破译之前,原文是否更有意义…@Holger这对我来说乍一看也很奇怪。
/*
 * 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() {