Java開源工具在linux上的源碼分析(四):safe point
safe point 顧明思意,就是安全點(diǎn),當(dāng)需要jvm做一些操作的時(shí)候,需要把當(dāng)前正在運(yùn)行的線程進(jìn)入一個(gè)安全點(diǎn)的狀態(tài)(也可以說停止?fàn)顟B(tài)),這樣才能做一些安全的操作,比如線程的dump,堆棧的信息。
在jvm里面通常vm_thread(我們一直在談?wù)摰淖鲆恍儆趘m 份內(nèi)事情的線程) 和cms_thread(內(nèi)存回收的線程)做的操作,是需要將其他的線程通過調(diào)用SafepointSynchronize::begin 和 SafepointSynchronize:end來實(shí)現(xiàn)讓其他的線程進(jìn)入或者退出safe point 的狀態(tài)。
通常safepoint 的有三種狀態(tài)
_not_synchronized |
說明沒有任何打斷現(xiàn)在所有線程運(yùn)行的操作,也就是vm thread, cms thread 沒有接到操作的指令 |
_synchronizing |
vm thread,cms thread 接到操作指令,正在等待所有線程進(jìn)入safe point |
_synchronized |
所有線程進(jìn)入safe point, vm thread, cms thread 可以開始指令操作 |
Java線程的狀態(tài)
通常在java 進(jìn)程中的Java 的線程有幾個(gè)不同的狀態(tài),如何讓這些線程進(jìn)入safepoint 的狀態(tài)中,jvm是采用不同的方式
a. 正在解釋執(zhí)行
由于java是解釋性語言,而線程在解釋java 字節(jié)碼的時(shí)候,需要dispatch table,記錄方法地址進(jìn)行跳轉(zhuǎn)的,那么這樣讓線程進(jìn)入停止?fàn)顟B(tài)就比較容易了,只要替換掉dispatch table 就可以了,讓線程知道當(dāng)前進(jìn)入softpoint 狀態(tài)。
java里會(huì)設(shè)置3個(gè)DispatchTable, _active_table, _normal_table, _safept_table
_active_table 正在解釋運(yùn)行的線程使用的dispatch table
_normal_table 就是正常運(yùn)行的初始化的dispatch table
_safept_table safe point需要的dispatch table
解釋運(yùn)行的線程一直都在使用_active_table,關(guān)鍵處就是在進(jìn)入saftpoint 的時(shí)候,用_safept_table替換_active_table, 在退出saftpoint 的時(shí)候,使用_normal_table來替換_active_table。
具體實(shí)現(xiàn)可以查看源碼
- 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. 運(yùn)行在native code
如果線程運(yùn)行在native code的時(shí)候,vm thread 是不需要等待線程執(zhí)行完的,只需要在從native code 返回的時(shí)候去判斷一下 _state 的狀態(tài)就可以了。
在方法體里就是前面博客也出現(xiàn)過的 SafepointSynchronize::do_call_back()
- inline static bool do_call_back() {
- return (_state != _not_synchronized);
- }
判斷了_state 不是_not_synchronized狀態(tài)
為了能讓線程從native code 回到j(luò)ava 的時(shí)候?yàn)榱四茏x到/設(shè)置正確線程的狀態(tài),通常的解決方法使用memory barrier,java 使用OrderAccess::fence(); 在匯編里使用__asm__ volatile ("lock; addl $0,0(%%rsp)" : : : "cc", "memory"); 保證從內(nèi)存里讀到正確的值,但是這種方法嚴(yán)重影響系統(tǒng)的性能,于是java使用了每個(gè)線程都有獨(dú)立的內(nèi)存頁來設(shè)置狀態(tài)。通過使用使用參數(shù)-XX:+UseMembar 參數(shù)使用memory barrier,默認(rèn)是不打開的,也就是使用獨(dú)立的內(nèi)存頁來設(shè)置狀態(tài)。
c. 運(yùn)行編譯的代碼
1. Poling page 頁面
Poling page是在jvm初始化啟動(dòng)的時(shí)候會(huì)初始化的一個(gè)單獨(dú)的內(nèi)存頁面,這個(gè)頁面是讓運(yùn)行的編譯過的代碼的線程進(jìn)入停止?fàn)顟B(tài)的關(guān)鍵。
在linux里面使用了mmap初始化,源碼如下
- address polling_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
2. 編譯
java 的JIT 會(huì)直接編譯一些熱門的源碼到機(jī)器碼,直接執(zhí)行而不需要在解釋執(zhí)行從而提高效率,在編譯的代碼中,當(dāng)函數(shù)或者方法塊返回的時(shí)候會(huì)去訪問一個(gè)內(nèi)存poling頁面。
x86架構(gòu)下
- 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 函數(shù)源碼中
- if (UseCompilerSafepoints && DeferPollingPageLoopCount < 0) {
- // Make polling safepoint aware
- guarantee (PageArmed == 0, "invariant") ;
- PageArmed = 1 ;
- os::make_polling_page_unreadable();
- }
這里提到了2個(gè)參數(shù) UseCompilerSafepoints 和 DeferPollingPageLoopCount ,在默認(rèn)的情況下這2個(gè)參數(shù)是true和-1
函數(shù)體將會(huì)調(diào)用os:make_polling_page_unreadable();在linux os 下具體實(shí)現(xiàn)是調(diào)用了mprotect(bottom,size,prot) 使polling 內(nèi)存頁變成不可讀。
3. 信號(hào)
到當(dāng)編譯好的程序嘗試在去訪問這個(gè)不可讀的polling頁面的時(shí)候,在系統(tǒng)級(jí)別會(huì)產(chǎn)生一個(gè)錯(cuò)誤信號(hào)SIGSEGV, 可以參考筆者的一篇博客中曾經(jīng)講過java 的信號(hào)處理,可以知道信號(hào)SIGSEGV的處理函數(shù)在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 詳細(xì)程序可見shareRuntime_x86_64.cpp
回到safepoint.cpp中,SafepointSynchronize::handle_polling_page_exception通過取出線程的safepoint_stat,調(diào)用函數(shù)void ThreadSafepointState::handle_polling_page_exception,***通過調(diào)用SafepointSynchronize::block(thread()); 來block當(dāng)前線程。
d. block 狀態(tài)
當(dāng)線程進(jìn)入block狀態(tài)的時(shí)候,繼續(xù)保持block狀態(tài)。
原文鏈接:http://blog.csdn.net/raintungli/article/details/7162468
【系列文章】