El Psy Congroo

Safepoint学习笔记

safepoint

Safepoints: Meaning, Side Effects and Overheads
Counted/Uncounted loops, Safepoints and OSR Compilation
JVM的Stop The World,安全点,黑暗的地底世界

When at a safepoint, the thread’s representation of it’s Java machine state is well described, and can be safely manipulated and observed by other threads in the JVM.

安全点是指线程执行过程中一些特定的位置,在这些位置上线程的JVM状态是确定的(JIT会在safepoint上生成OopMap帮助确定GC Roots),因此可以进行GC等操作

safepoint日志

  • 打印日志
    -XX:+PrintSafepointStatistics -XX:PrintSafepointStatisticsCount=1
    -XX:+PrintGCApplicationStoppedTime #打印停顿时间

  • 日志样例

    vmop [threads: total initially_running wait_to_block] [time: spin block sync cleanup vmop] page_trap_count
    6.763: PrintThreads [ 11 1 1 ] [ 1 0 1 0 0 ] 1

    • 第一段是时间戳,VM Operation的类型,以及线程概况
      • total: 安全点里的总线程数
      • initially_running: 安全点时开始时正在运行状态的线程数
      • wait_to_block: 在VM Operation开始前需要等待其暂停的线程数
    • 第二段是到达安全点时的各个阶段以及执行操作所花的时间,其中最重要的是vmop
      • spin: 等待线程响应safepoint号召的时间
      • block: 暂停所有线程所用的时间
      • sync: 等于 spin+block,这是从开始到进入安全点所耗的时间,可用于判断进入安全点耗时
      • cleanup: 清理所用时间
      • vmop: 真正执行VM Operation的时间

safepoint poll

  • 采用主动式中断(Voluntary Suspension)进入safepoint

  • 检查方式
    JIT方式下直接插入检查代码,解释器执行时监听safepoint请求,有请求时让线程进行safepoint检查

  • safepoint检查点

    • between any 2 bytecodes (interpreter)
    • ‘uncounted’ loops back edge (compiled)
    • method exit/enter (compiled)
  • 影响进入safepoint的情况

    • large methods
    • long running ‘counted’ loops
    • page faults/thread suspension
  • counted & uncounted loop
    counted loop jvm认为比较短的循环,因此编译时不会加入safepoint检查
    uncounted loop 编译时会在每次循环中加入safepoint检查

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    // counted = reps is int/short/byte
    for (int i = 0; i < reps; i++) {}
    // uncounted
    for (long l = 0; l < int_reps; i++) {}
    // counted
    while (i < reps) { i++; }
    // uncounted
    while (i++ < reps) {}

safepoint operation

  1. Garbage collection pauses
  2. Code deoptimization
  3. Flushing code cache
  4. Class redefinition (e.g. hot swap or instrumentation)
  5. Biased lock revocation
  6. Various debug operation (e.g. deadlock check or stacktrace dump)

safe-region

safepoint处理运行中的thread,safe-region处理等待中的线程,线程block或sleep前标识进入safe-region,唤醒后准备离开safe-region前,先检查是否能离开,例如GC是否完成,如果没有,则继续等待

safepoint bias on profiling

热点方法中的safepoint检查点可能被优化掉,导致profile时只能统计到热点方法的外部方法

JNI code

JNI code运行时线程处于safepoint,如果需要访问或修改Java state(通过JNI API),则这部分API调用会在离开safepoint时执行,并再次进入safepoint返回给JNI code

总结

  • 分别考虑Safepoint/Safepoint poll/Safepoint operation的开销和性能影响
  • 任何一个没有成功进入safepoint的线程都会阻塞整个JVM
  • 通过日志观察停顿时间,如果有异常,增加明细日志