Java開源工具在linux上的源碼分析(四):safe point
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。
具體實現可以查看源碼
- void TemplateInterpreter::notice_safepoints() {
- if (!_notice_safepoints) {
- // switch to safepoint dispatch table
- _notice_safepoints = true;
- copy_table((address*)&_safept_table, (address*)&_active_table, sizeof(_active_table) / sizeof(address));
- }
- }
- // switch from the dispatch table which notices safepoints back to the
- // normal dispatch table. So that we can notice single stepping points,
- // keep the safepoint dispatch table if we are single stepping in JVMTI.
- // Note that the should_post_single_step test is exactly as fast as the
- // JvmtiExport::_enabled test and covers both cases.
- void TemplateInterpreter::ignore_safepoints() {
- if (_notice_safepoints) {
- if (!JvmtiExport::should_post_single_step()) {
- // switch to normal dispatch table
- _notice_safepoints = false;
- copy_table((address*)&_normal_table, (address*)&_active_table, sizeof(_active_table) / sizeof(address));
- }
- }
- }
b. 運行在native code
如果線程運行在native code的時候,vm thread 是不需要等待線程執行完的,只需要在從native code 返回的時候去判斷一下 _state 的狀態就可以了。
在方法體里就是前面博客也出現過的 SafepointSynchronize::do_call_back()
- inline static bool do_call_back() {
- 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初始化,源碼如下
- address polling_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
2. 編譯
java 的JIT 會直接編譯一些熱門的源碼到機器碼,直接執行而不需要在解釋執行從而提高效率,在編譯的代碼中,當函數或者方法塊返回的時候會去訪問一個內存poling頁面。
x86架構下
- void LIR_Assembler::return_op(LIR_Opr result) {
- assert(result->is_illegal() || !result->is_single_cpu() || result->as_register() == rax, "word returns are in rax,");
- if (!result->is_illegal() && result->is_float_kind() && !result->is_xmm_register()) {
- assert(result->fpu() == 0, "result must already be on TOS");
- }
- // Pop the stack before the safepoint code
- __ remove_frame(initial_frame_size_in_bytes());
- bool result_is_oop = result->is_valid() ? result->is_oop() : false;
- // Note: we do not need to round double result; float result has the right precision
- // the poll sets the condition code, but no data registers
- AddressLiteral polling_page(os::get_polling_page() + (SafepointPollOffset % os::vm_page_size()),
- relocInfo::poll_return_type);
- // NOTE: the requires that the polling page be reachable else the reloc
- // goes to the movq that loads the address and not the faulting instruction
- // which breaks the signal handler code
- __ test32(rax, polling_page);
- __ ret(0);
- }
在前面提到的SafepointSynchronize::begin 函數源碼中
- if (UseCompilerSafepoints && DeferPollingPageLoopCount < 0) {
- // Make polling safepoint aware
- guarantee (PageArmed == 0, "invariant") ;
- PageArmed = 1 ;
- 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體系下見下源碼:
- JVM_handle_linux_signal(int sig,
- siginfo_t* info,
- void* ucVoid,
- int abort_if_unrecognized){
- ....
- if (sig == SIGSEGV && os::is_poll_address((address)info->si_addr)) {
- stub = SharedRuntime::get_poll_stub(pc);
- }
- ....
- }
在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
【系列文章】