Java开源工具在linux上的源码分析(四):safe point

开发 后端
safe point 顾明思意,就是安全点,当需要jvm做一些操作的时候,需要把当前正在运行的线程进入一个安全点的状态(也可以说停止状态),这样才能做一些安全的操作,比如线程的dump,堆栈的信息。

safe point 顾明思意,就是安全点,当需要jvm做一些操作的时候,需要把当前正在运行的线程进入一个安全点的状态(也可以说停止状态),这样才能做一些安全的操作,比如线程的dump,堆栈的信息。

在jvm里面通常vm_thread(我们一直在谈论的做一些属于vm 份内事情的线程) 和cms_thread(内存回收的线程)做的操作,是需要将其他的线程通过调用SafepointSynchronize::begin 和 SafepointSynchronize:end来实现让其他的线程进入或者退出safe point 的状态。

通常safepoint 的有三种状态

 

_not_synchronized

说明没有任何打断现在所有线程运行的操作,也就是vm thread, cms thread 没有接到操作的指令

_synchronizing

vm thread,cms thread 接到操作指令,正在等待所有线程进入safe point

_synchronized

所有线程进入safe point, vm thread, cms thread 可以开始指令操作

 

Java线程的状态

通常在java 进程中的Java 的线程有几个不同的状态,如何让这些线程进入safepoint 的状态中,jvm是采用不同的方式

a. 正在解释执行

由于java是解释性语言,而线程在解释java 字节码的时候,需要dispatch table,记录方法地址进行跳转的,那么这样让线程进入停止状态就比较容易了,只要替换掉dispatch table 就可以了,让线程知道当前进入softpoint 状态。

java里会设置3个DispatchTable,  _active_table,  _normal_table, _safept_table

_active_table 正在解释运行的线程使用的dispatch table

_normal_table 就是正常运行的初始化的dispatch table

_safept_table safe point需要的dispatch table

解释运行的线程一直都在使用_active_table,关键处就是在进入saftpoint 的时候,用_safept_table替换_active_table, 在退出saftpoint 的时候,使用_normal_table来替换_active_table。

具体实现可以查看源码

  1. void TemplateInterpreter::notice_safepoints() {  
  2.   if (!_notice_safepoints) {  
  3.     // switch to safepoint dispatch table  
  4.     _notice_safepoints = true;  
  5.     copy_table((address*)&_safept_table, (address*)&_active_table, sizeof(_active_table) / sizeof(address));  
  6.   }  
  7. }  
  8.  
  9. // switch from the dispatch table which notices safepoints back to the  
  10. // normal dispatch table.  So that we can notice single stepping points,  
  11. // keep the safepoint dispatch table if we are single stepping in JVMTI.  
  12. // Note that the should_post_single_step test is exactly as fast as the  
  13. // JvmtiExport::_enabled test and covers both cases.  
  14. void TemplateInterpreter::ignore_safepoints() {  
  15.   if (_notice_safepoints) {  
  16.     if (!JvmtiExport::should_post_single_step()) {  
  17.       // switch to normal dispatch table  
  18.       _notice_safepoints = false;  
  19.       copy_table((address*)&_normal_table, (address*)&_active_table, sizeof(_active_table) / sizeof(address));  
  20.     }  
  21.   }  

b. 运行在native code

如果线程运行在native code的时候,vm thread 是不需要等待线程执行完的,只需要在从native code 返回的时候去判断一下 _state 的状态就可以了。

在方法体里就是前面博客也出现过的 SafepointSynchronize::do_call_back()

  1. inline static bool do_call_back() {  
  2.   return (_state != _not_synchronized);  

判断了_state 不是_not_synchronized状态

为了能让线程从native code 回到java 的时候为了能读到/设置正确线程的状态,通常的解决方法使用memory barrier,java 使用OrderAccess::fence(); 在汇编里使用__asm__ volatile ("lock; addl $0,0(%%rsp)" : : : "cc", "memory"); 保证从内存里读到正确的值,但是这种方法严重影响系统的性能,于是java使用了每个线程都有独立的内存页来设置状态。通过使用使用参数-XX:+UseMembar 参数使用memory barrier,默认是不打开的,也就是使用独立的内存页来设置状态。

c. 运行编译的代码

1. Poling page 页面

Poling page是在jvm初始化启动的时候会初始化的一个单独的内存页面,这个页面是让运行的编译过的代码的线程进入停止状态的关键。

在linux里面使用了mmap初始化,源码如下

  1. address polling_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -10); 

2. 编译

java 的JIT 会直接编译一些热门的源码到机器码,直接执行而不需要在解释执行从而提高效率,在编译的代码中,当函数或者方法块返回的时候会去访问一个内存poling页面。

x86架构下

  1. void LIR_Assembler::return_op(LIR_Opr result) {  
  2.   assert(result->is_illegal() || !result->is_single_cpu() || result->as_register() == rax, "word returns are in rax,");  
  3.   if (!result->is_illegal() && result->is_float_kind() && !result->is_xmm_register()) {  
  4.     assert(result->fpu() == 0"result must already be on TOS");  
  5.   }  
  6.  
  7.   // Pop the stack before the safepoint code  
  8.   __ remove_frame(initial_frame_size_in_bytes());  
  9.  
  10.   bool result_is_oop = result->is_valid() ? result->is_oop() : false;  
  11.  
  12.   // Note: we do not need to round double result; float result has the right precision  
  13.   // the poll sets the condition code, but no data registers  
  14.   AddressLiteral polling_page(os::get_polling_page() + (SafepointPollOffset % os::vm_page_size()),  
  15.                               relocInfo::poll_return_type);  
  16.  
  17.   // NOTE: the requires that the polling page be reachable else the reloc  
  18.   // goes to the movq that loads the address and not the faulting instruction  
  19.   // which breaks the signal handler code  
  20.  
  21.   __ test32(rax, polling_page);  
  22.  
  23.   __ ret(0);  

在前面提到的SafepointSynchronize::begin 函数源码中

  1. if (UseCompilerSafepoints && DeferPollingPageLoopCount < 0) {  
  2.   // Make polling safepoint aware  
  3.   guarantee (PageArmed == 0"invariant") ;  
  4.   PageArmed = 1 ;  
  5.   os::make_polling_page_unreadable();  

这里提到了2个参数 UseCompilerSafepoints 和 DeferPollingPageLoopCount ,在默认的情况下这2个参数是true和-1

函数体将会调用os:make_polling_page_unreadable();在linux os 下具体实现是调用了mprotect(bottom,size,prot) 使polling 内存页变成不可读。

3. 信号

到当编译好的程序尝试在去访问这个不可读的polling页面的时候,在系统级别会产生一个错误信号SIGSEGV, 可以参考笔者的一篇博客中曾经讲过java 的信号处理,可以知道信号SIGSEGV的处理函数在x86体系下见下源码:

  1. JVM_handle_linux_signal(int sig,  
  2.                         siginfo_t* info,  
  3.                         void* ucVoid,  
  4.                         int abort_if_unrecognized){  
  5.    ....  
  6.    if (sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) {  
  7.         stub = SharedRuntime::get_poll_stub(pc);  
  8.       }   
  9.    ....  

在linux x86,64 bit的体系中,poll stub 的地址 就是 SafepointSynchronize::handle_polling_page_exception 详细程序可见shareRuntime_x86_64.cpp

回到safepoint.cpp中,SafepointSynchronize::handle_polling_page_exception通过取出线程的safepoint_stat,调用函数void ThreadSafepointState::handle_polling_page_exception,***通过调用SafepointSynchronize::block(thread()); 来block当前线程。

 

d. block 状态

当线程进入block状态的时候,继续保持block状态。

原文链接:http://blog.csdn.net/raintungli/article/details/7162468

【系列文章】

  1. Java开源工具在linux上的源码分析(一):跟踪方式
  2. Java开源工具在linux上的源码分析(二):信号处理
  3. Java开源工具在linux上的源码分析(三):执行的线程vm thread
  4. Java开源工具在linux上的源码分析(五):-F参数的bug
  5. Java开源工具在linux上的源码分析(六):符号表的读取
责任编辑:林师授 来源: raintungli的博客
相关推荐

2012-03-02 12:14:19

JavaJstackJmap

2012-03-02 12:20:21

Javajmapjstack

2012-03-02 12:25:07

Javajmapjstack

2012-03-02 12:38:49

Javajmapjstack

2012-03-02 13:29:38

Javajmapjstack

2022-06-26 18:09:43

Linux开源

2019-10-16 17:00:51

LinuxUbuntuVMware

2020-05-09 12:01:40

Linux开源软件SDN

2010-01-27 09:58:59

Linuxunix程序日志

2022-02-18 15:19:52

日志收集工具开源

2021-03-09 11:25:04

Linux开源工具服务器

2019-08-01 09:52:46

LinuxNetData性能监控工具

2018-05-30 09:00:00

2012-05-22 00:28:21

JavaJava开源开源工具

2021-08-31 09:41:57

LinuxiPhone开源工具

2021-09-01 09:47:25

Linux 工具 开发

2022-06-06 14:20:25

个人财务开源预算

2019-05-23 14:36:24

LinuxSOSReportxsos

2017-01-12 15:58:17

Linux死锁分析方法

2021-11-25 09:25:51

Linux服务器开源工具
点赞
收藏

51CTO技术栈公众号