激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - JVM系列之:再談java中的safepoint說明

JVM系列之:再談java中的safepoint說明

2020-09-15 01:11flydean程序那些事 Java教程

這篇文章主要介紹了JVM系列之:再談java中的safepoint說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

safepoint是什么

java程序里面有很多很多的java線程,每個java線程又有自己的stack,并且共享了heap。這些線程一直運行呀運行,不斷對stack和heap進行操作。

這個時候如果JVM需要對stack和heap做一些操作該怎么辦呢?

比如JVM要進行GC操作,或者要做heap dump等等,這時候如果線程都在對stack或者heap進行修改,那么將不是一個穩(wěn)定的狀態(tài)。GC直接在這種情況下操作stack或者heap,會導(dǎo)致線程的異常。

怎么處理呢?

這個時候safepoint就出場了。

safepoint就是一個安全點,所有的線程執(zhí)行到安全點的時候就會去檢查是否需要執(zhí)行safepoint操作,如果需要執(zhí)行,那么所有的線程都將會等待,直到所有的線程進入safepoint。

然后JVM執(zhí)行相應(yīng)的操作之后,所有的線程再恢復(fù)執(zhí)行。

safepoint的例子

我們舉個例子,一般safepoint比如容易出現(xiàn)在循環(huán)遍歷的情況,還是使用我們之前做null測試用的例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class TestNull {
 
 public static void main(String[] args) throws InterruptedException {
  List<String> list= new ArrayList();
 list.add("www.flydean.com");
  for (int i = 0; i < 10000; i++)
  {
   testMethod(list);
  }
  Thread.sleep(1000);
 }
 
 private static void testMethod(List<String> list)
 {
  list.get(0);
 }
}

運行結(jié)果如下:

JVM系列之:再談java中的safepoint說明

標(biāo)紅的就是傳說中的safepoint。

線程什么時候會進入safepoint

那么線程什么時候會進入safepoint呢?

一般來說,如果線程在競爭鎖被阻塞,IO被阻塞,或者在等待獲得監(jiān)視器鎖狀態(tài)時,線程就處于safepoint狀態(tài)。

如果線程再執(zhí)行JNI代碼的哪一個時刻,java線程也處于safepoint狀態(tài)。因為java線程在執(zhí)行本地代碼之前,需要保存堆棧的狀態(tài),讓后再移交給native方法。

如果java的字節(jié)碼正在執(zhí)行,那么我們不能判斷該線程是不是在safepint上。

safepoint是怎么工作的

如果你使用的是hotspot JVM,那么這個safepoint是一個全局的safepoint,也就是說執(zhí)行Safepoint需要暫停所有的線程。

如果你使用的是Zing,那么可以在線程級別使用safepoint。

我們可以看到生成的匯編語言中safepoint其實是一個test命令。

test指向的是一個特殊的內(nèi)存頁面地址,當(dāng)JVM需要所有的線程都執(zhí)行到safepint的時候,就會對該頁面做一個標(biāo)記。從而通知所有的線程。

我們再用一張圖來詳細說明:

JVM系列之:再談java中的safepoint說明

thread1在收到設(shè)置safepoint之前是一直執(zhí)行的,在收到信號之后還會執(zhí)行一段時間,然后到達Safepint暫停執(zhí)行。

thread2先執(zhí)行了一段時間,然后因為CPU被搶奪,空閑了一段時間,在這段時間里面,thread2收到了設(shè)置safepoint的信號,然后thread2獲得執(zhí)行權(quán)力,接著繼續(xù)執(zhí)行,最后到達safepoint。

thread3是一個native方法,將會一直執(zhí)行,知道safepoint結(jié)束。

thread4也是一個native方法,它和thread3的區(qū)別就在于,thread4在safepoint開始和結(jié)束之間結(jié)束了,需要將控制器轉(zhuǎn)交給普通的java線程,因為這個時候JVM在執(zhí)行Safepoint的操作,所以任然需要暫停執(zhí)行。

在HotSpot VM中,你可以在匯編語言中看到safepoint的兩種形式:'{poll}' 或者 ‘{poll return}' 。

總結(jié)

本文詳細的講解了JVM中Safepoint的作用,希望大家能夠喜歡。

補充知識:JVM源碼分析之安全點safepoint

上周有幸參加了一次關(guān)于JVM的小范圍分享會,聽完R大對虛擬機C2編譯器的講解,我的膝蓋一直是腫的,能記住的實在有點少,能聽進去也不多

1、什么時候進行C2編譯,如何進行C2編譯(這個實在太復(fù)雜)

2、C2編譯的時候,是對整個方法體進行編譯,而不是某個方法段

3、JVM中的safepoint

一直都知道,當(dāng)發(fā)生GC時,正在執(zhí)行Java code的線程必須全部停下來,才可以進行垃圾回收,這就是熟悉的STW(stop the world),但是STW的背后實現(xiàn)原理,比如這些線程如何暫停、又如何恢復(fù)?就比較疑惑了。

然而這一切的一切,都涉及到一個概念safepoint,openjdk的實現(xiàn)位于

openjdk/hotspot/src/share/vm/runtime/safepoint.cpp

什么是safepoint

safepoint可以用在不同地方,比如GC、Deoptimization,在Hotspot VM中,GC safepoint比較常見,需要一個數(shù)據(jù)結(jié)構(gòu)記錄每個線程的調(diào)用棧、寄存器等一些重要的數(shù)據(jù)區(qū)域里什么地方包含了GC管理的指針。

從線程角度看,safepoint可以理解成是在代碼執(zhí)行過程中的一些特殊位置,當(dāng)線程執(zhí)行到這些位置的時候,說明虛擬機當(dāng)前的狀態(tài)是安全的,如果有需要,可以在這個位置暫停,比如發(fā)生GC時,需要暫停暫停所以活動線程,但是線程在這個時刻,還沒有執(zhí)行到一個安全點,所以該線程應(yīng)該繼續(xù)執(zhí)行,到達下一個安全點的時候暫停,等待GC結(jié)束。

什么地方可以放safepoint

下面以Hotspot為例,簡單的說明一下什么地方會放置safepoint

1、理論上,在解釋器的每條字節(jié)碼的邊界都可以放一個safepoint,不過掛在safepoint的調(diào)試符號信息要占用內(nèi)存空間,如果每條機器碼后面都加safepoint的話,需要保存大量的運行時數(shù)據(jù),所以要盡量少放置safepoint,在safepoint會生成polling代碼詢問VM是否要“進入safepoint”,polling操作也是有開銷的,polling操作會在后續(xù)解釋。

2、通過JIT編譯的代碼里,會在所有方法的返回之前,以及所有非counted loop的循環(huán)(無界循環(huán))回跳之前放置一個safepoint,為了防止發(fā)生GC需要STW時,該線程一直不能暫停。另外,JIT編譯器在生成機器碼的同時會為每個safepoint生成一些“調(diào)試符號信息”,為GC生成的符號信息是OopMap,指出棧上和寄存器里哪里有GC管理的指針。

線程如何被掛起

如果觸發(fā)GC動作,VM thread會在VMThread::loop()方法中調(diào)用SafepointSynchronize::begin()方法,最終使所有的線程都進入到safepoint。

?
1
2
3
4
5
6
7
8
9
// Roll all threads forward to a safepoint and suspend them all
void SafepointSynchronize::begin() {
 Thread* myThread = Thread::current();
 assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
 
 if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
  _safepoint_begin_time = os::javaTimeNanos();
  _ts_of_current_safepoint = tty->time_stamp().seconds();
 }

在safepoint實現(xiàn)中,有這樣一段注釋,Java threads可以有多種不同的狀態(tài),所以掛起的機制也不同,一共列舉了5中情況:

1、執(zhí)行Java code

在執(zhí)行字節(jié)碼時會檢查safepoint狀態(tài),因為在begin方法中會調(diào)用Interpreter::notice_safepoints()方法,通知解釋器更新dispatch table,實現(xiàn)如下:

?
1
2
3
4
5
6
7
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));
 }
}

2、執(zhí)行native code

如果VM thread發(fā)現(xiàn)一個Java thread正在執(zhí)行native code,并不會等待該Java thread阻塞,不過當(dāng)該Java thread從native code返回時,必須檢查safepoint狀態(tài),看是否需要進行阻塞。

這里涉及到兩個狀態(tài):Java thread state和safepoint state,兩者之間有著嚴格的讀寫順序,一般可以通過內(nèi)存屏障實現(xiàn),但是性能開銷比較大,Hotspot采用另一種方式,調(diào)用os::serialize_thread_states()把每個線程的狀態(tài)依次寫入到同一個內(nèi)存頁中,實現(xiàn)如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Serialize all thread state variables
void os::serialize_thread_states() {
 // On some platforms such as Solaris & Linux, the time duration of the page
 // permission restoration is observed to be much longer than expected due to
 // scheduler starvation problem etc. To avoid the long synchronization
 // time and expensive page trap spinning, 'SerializePageLock' is used to block
 // the mutator thread if such case is encountered. See bug 6546278 for details.
 Thread::muxAcquire(&SerializePageLock, "serialize_thread_states");
 os::protect_memory((char *)os::get_memory_serialize_page(),
           os::vm_page_size(), MEM_PROT_READ);
 os::protect_memory((char *)os::get_memory_serialize_page(),
           os::vm_page_size(), MEM_PROT_RW);
 Thread::muxRelease(&SerializePageLock);
}

通過VM thread執(zhí)行一系列mprotect os call,保證之前所有線程狀態(tài)的寫入可以被順序執(zhí)行,效率更高。

3、執(zhí)行complied code

如果想進入safepoint,則設(shè)置polling page不可讀,當(dāng)Java thread發(fā)現(xiàn)該內(nèi)存頁不可讀時,最終會被阻塞掛起。在SafepointSynchronize::begin()方法中,通過os::make_polling_page_unreadable()方法設(shè)置polling page為不可讀。

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

方法make_polling_page_unreadable()在不同系統(tǒng)的實現(xiàn)不一樣

linux下實現(xiàn)

?
1
2
3
4
5
// Mark the polling page as unreadable
void os::make_polling_page_unreadable(void) {
 if( !guard_memory((char*)_polling_page, Linux::page_size()) )
  fatal("Could not disable polling page");
};

solaris下實現(xiàn)

?
1
2
3
4
5
// Mark the polling page as unreadable
void os::make_polling_page_unreadable(void) {
 if( mprotect((char *)_polling_page, page_size, PROT_NONE) != 0 )
  fatal("Could not disable polling page");
};

在JIT編譯中,編譯器會把safepoint檢查的操作插入到機器碼指令中,比如下面的指令:

0x01b6d627: call   0x01b2b210         ; OopMap{[60]=Oop off=460}     
                                       ;*invokeinterface size     
                                       ; - Client1::main@113 (line 23)     
                                       ;   {virtual_call}     
 0x01b6d62c: nop                       ; OopMap{[60]=Oop off=461}     
                                       ;*if_icmplt     
                                       ; - Client1::main@118 (line 23)     
 0x01b6d62d: test   %eax,0x160100      ;   {poll}     
 0x01b6d633: mov    0x50(%esp),%esi     
 0x01b6d637: cmp    %eax,%esi

test %eax,0x160100 就是一個檢查polling page是否可讀的操作,如果不可讀,則該線程會被掛起等待。

4、線程處于Block狀態(tài)

即使線程已經(jīng)滿足了block condition,也要等到safepoint operation完成,如GC操作,才能返回。

5、線程正在轉(zhuǎn)換狀態(tài)

會去檢查safepoint狀態(tài),如果需要阻塞,就把自己掛起。

最終實現(xiàn)

當(dāng)線程訪問到被保護的內(nèi)存地址時,會觸發(fā)一個SIGSEGV信號,進而觸發(fā)JVM的signal handler來阻塞這個線程,The GC thread can protect some memory to which all threads in the process can write (using the mprotect system call) so they no longer can. Upon accessing this temporarily forbidden memory, a signal handler kicks in。

再看看底層是如何處理這個SIGSEGV信號,實現(xiàn)位于

?
1
2
3
4
5
6
7
8
9
10
11
hotspot/src/os_cpu/linux_x86/vm/os_linux_x86.cpp
// Check to see if we caught the safepoint code in the
// process of write protecting the memory serialization page.
// It write enables the page immediately after protecting it
// so we can just return to retry the write.
if ((sig == SIGSEGV) &&
  os::is_memory_serialize_page(thread, (address) info->si_addr)) {
 // Block current thread until the memory serialize page permission restored.
 os::block_on_serialize_page_trap();
 return true;
}

執(zhí)行os::block_on_serialize_page_trap()把當(dāng)前線程阻塞掛起。

線程如何恢復(fù)

有了begin方法,自然有對應(yīng)的end方法,在SafepointSynchronize::end()中,會最終喚醒所有掛起等待的線程,大概實現(xiàn)如下:

1、重新設(shè)置pooling page為可讀

?
1
2
3
4
5
if (PageArmed) {
  // Make polling safepoint aware
  os::make_polling_page_readable();
  PageArmed = 0 ;
 }

2、設(shè)置解釋器為ignore_safepoints,實現(xiàn)如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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));
  }
 }
}

3、喚醒所有掛起等待的線程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Start suspended threads
  for(JavaThread *current = Threads::first(); current; current = current->next()) {
   // A problem occurring on Solaris is when attempting to restart threads
   // the first #cpus - 1 go well, but then the VMThread is preempted when we get
   // to the next one (since it has been running the longest). We then have
   // to wait for a cpu to become available before we can continue restarting
   // threads.
   // FIXME: This causes the performance of the VM to degrade when active and with
   // large numbers of threads. Apparently this is due to the synchronous nature
   // of suspending threads.
   //
   // TODO-FIXME: the comments above are vestigial and no longer apply.
   // Furthermore, using solaris' schedctl in this particular context confers no benefit
   if (VMThreadHintNoPreempt) {
    os::hint_no_preempt();
   }
   ThreadSafepointState* cur_state = current->safepoint_state();
   assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
   cur_state->restart();
   assert(cur_state->is_running(), "safepoint state has not been reset");
  }

對JVM性能有什么影響

通過設(shè)置JVM參數(shù) -XX:+PrintGCApplicationStoppedTime, 可以打出系統(tǒng)停止的時間,大概如下:

?
1
2
3
4
5
6
7
8
Total time for which application threads were stopped: 0.0051000 seconds
Total time for which application threads were stopped: 0.0041930 seconds
Total time for which application threads were stopped: 0.0051210 seconds
Total time for which application threads were stopped: 0.0050940 seconds
Total time for which application threads were stopped: 0.0058720 seconds
Total time for which application threads were stopped: 5.1298200 seconds
Total time for which application threads were stopped: 0.0197290 seconds
Total time for which application threads were stopped: 0.0087590 seconds

從上面數(shù)據(jù)可以發(fā)現(xiàn),有一次暫停時間特別長,達到了5秒多,這在線上環(huán)境肯定是無法忍受的,那么是什么原因?qū)е碌哪兀?/p>

一個大概率的原因是當(dāng)發(fā)生GC時,有線程遲遲進入不到safepoint進行阻塞,導(dǎo)致其他已經(jīng)停止的線程也一直等待,VM Thread也在等待所有的Java線程掛起才能開始GC,這里需要分析業(yè)務(wù)代碼中是否存在有界的大循環(huán)邏輯,可能在JIT優(yōu)化時,這些循環(huán)操作沒有插入safepoint檢查。

以上這篇JVM系列之:再談java中的safepoint說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/superfjj/article/details/107855767

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 娇妻被各种姿势c到高潮小说 | 羞羞色院91精品网站 | 在线一级片 | 久久久中精品2020中文 | 美女在线视频一区二区 | 国产精品自在线拍 | 国产精品欧美日韩一区二区 | 日韩视| 91精品久久久久久久久久久 | 美女黄网站免费观看 | 特级毛片全部免费播放器 | 欧美一区高清 | 一本色道精品久久一区二区三区 | 成人性生活视频在线播放 | 国产精品久久久久无码av | 亚洲精品久久久久久久久久久 | 久草在线资源观看 | 福利在线免费 | 久久久久九九九女人毛片 | 亚洲精品午夜国产va久久成人 | 91精品观看91久久久久久国产 | 欧美另类69xxxxx 视频 | 日本人乱人乱亲乱色视频观看 | 污版视频在线观看 | 国产噜噜噜噜久久久久久久久 | aa国产视频一区二区 | 久久久久久久久国产精品 | 国产精品久久久久久久久久 | 中文字幕精品久久 | 日韩精品一区二区免费视频 | 欧美一级视频网站 | 激情综合婷婷久久 | 91精品久久久久久久久 | 久色乳综合思思在线视频 | 免费视频爱爱太爽了 | 欧美激情精品久久久久久久久久 | 色播亚洲 | 草莓福利视频在线观看 | 日韩精品一区二区在线 | 蜜桃91麻豆 | 鲁丝一区二区三区不属 |