WatcherThread如何调用JVM监视例程?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WatcherThread如何调用JVM监视例程?相关的知识,希望对你有一定的参考价值。

“VM周期性任务线程”

Aka是“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] == '')
            && 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或其他外部实用程序。

你可能正在寻找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是其中一项任务。

以上是关于WatcherThread如何调用JVM监视例程?的主要内容,如果未能解决你的问题,请参考以下文章

在JAVA中以编程方式监视JVM的堆栈区域? [复制]

如何从 unix shell 调用带有参数的子例程

如何让 VBA 子例程调用将数组传递给子例程中的另一个函数的函数

如何在远程主机上调用 perl 子例程(可能通过系统命令)?

如何在由 MPI 并行化的 fortran 中调用子例程?

如何从 Spring/Hibernate 调用存储的例程/函数?