GCLocker中缓慢而快速的路径是什么?

时间:2017-12-27 00:05:43

标签: java jvm jvm-hotspot

  1. GCLocker中针对HotSpot JVM中JNI关键区域的慢速和快速路径是什么?

  2. 这两个概念之间的区别是什么?

  3. 我从class GCLocker找到了代码评论。

      // JNI critical regions are the only participants in this scheme
      // because they are, by spec, well bounded while in a critical region.
      //
      // Each of the following two method is split into a fast path and a
      // slow path. JNICritical_lock is only grabbed in the slow path.
      // _needs_gc is initially false and every java thread will go
      // through the fast path, which simply increments or decrements the
      // current thread's critical count.  When GC happens at a safepoint,
      // GCLocker::is_active() is checked. Since there is no safepoint in
      // the fast path of lock_critical() and unlock_critical(), there is
      // no race condition between the fast path and GC. After _needs_gc
      // is set at a safepoint, every thread will go through the slow path
      // after the safepoint.  Since after a safepoint, each of the
      // following two methods is either entered from the method entry and
      // falls into the slow path, or is resumed from the safepoints in
      // the method, which only exist in the slow path. So when _needs_gc
      // is set, the slow path is always taken, till _needs_gc is cleared.
      static void lock_critical(JavaThread* thread);
      static void unlock_critical(JavaThread* thread);
    

1 个答案:

答案 0 :(得分:1)

您引用的引用中的答案 ,所以我不确定您还在寻找什么。

  • _needs_gc == false时,快速路径只是递增/递减计数器 - Thread::_jni_active_critical
  • _needs_gc == true通过全局锁(互斥锁)时,慢速路径。在最后一个线程离开关键区域后,需要使用互斥锁来确保调用GC。

看来你已经有了HotSpot资源,所以请看一下gcLocker.inline.hpp中的实现:

inline void GC_locker::lock_critical(JavaThread* thread) {
  if (!thread->in_critical()) {
    if (needs_gc()) {
      // jni_lock call calls enter_critical under the lock so that the
      // global lock count and per thread count are in agreement.
      jni_lock(thread);   <-- SLOW PATH
      return;
    }
    increment_debug_jni_lock_count();
  }
  thread->enter_critical();  <-- FAST PATH
}

分成快/慢路径的想法是在不请求GC时尽快进入/离开JNI关键区域。只有在需要GC时,JNI方法才能承受关键部分的开销。

相关问题