gpt4 book ai didi

java - GCLocker 中的慢路径和快路径是什么?

转载 作者:行者123 更新时间:2023-11-30 10:21:05 26 4
gpt4 key购买 nike

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

  2. 这两个概念有什么区别?

我从 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);

最佳答案

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

  • 快速路径,当 _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 方法才会承受临界区的开销。

关于java - GCLocker 中的慢路径和快路径是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47984937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com