In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use source code to analyze safe point on linux. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Safe point is a safe point. When you need jvm to do something, you need to put the currently running thread into a safe point state (or stop state), so that you can do some safe operations, such as thread dump, stack information.
In jvm, what is usually done by vm_thread (the thread that we have been talking about doing something that belongs to vm) and cms_thread (the thread that reclaims memory) is that other threads need to be called SafepointSynchronize::begin and SafepointSynchronize:end to get other threads into or out of safe point.
There are usually three states of safepoint
_ not_synchronized
It means that there is no interruption of any operation running by all threads, that is, vm thread, and cms thread has not received instructions for the operation.
_ synchronizing
Vm thread,cms thread received the operation instruction and is waiting for all threads to enter safe point
_ synchronized
All threads enter safe point, vm thread, and cms thread to start instruction operations.
State of the Java thread
Usually the threads of Java in the java process have several different states. How to get these threads into the state of safepoint, jvm uses different ways.
a. Interpreting execution
Because java is an interpretive language, and when a thread interprets the java bytecode, it needs dispatch table to record the method address to jump, so it is easier for the thread to enter the stop state. Just replace the dispatch table and let the thread know that it is currently in the softpoint state.
Three DispatchTable, _ active_table, _ normal_table and _ safept_table will be set in java
_ active_table is explaining the dispatch table used by the running thread
_ normal_table is the initialized dispatch table that runs normally
_ dispatch table required by safept_table safe point
The running thread of the explanation uses _ active_table all the time, and the key point is to replace _ active_table with _ safept_table when entering saftpoint and _ normal_table to replace _ active_table when exiting saftpoint.
For the specific implementation, you can view the source code.
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. Run on native code
If the thread is running on native code, vm thread does not need to wait for the thread to finish execution, just need to determine the state of _ state when returning from native code.
In the method body is SafepointSynchronize::do_call_back (), which has also appeared in previous blogs.
Inline static bool do_call_back () {return (_ state! = _ not_synchronized);}
Determined that _ state is not a _ not_synchronized state
In order to enable a thread to read / set the correct thread state when returning from native code to java, the usual solution is to use memory barrier,java using OrderAccess::fence (); in assembly, use _ _ asm__ volatile ("lock; addl $0asm__ volatile 0 (% rsp)": "cc", "memory") Ensure that the correct value is read from memory, but this method seriously affects the performance of the system, so java uses a separate memory page for each thread to set the state. By using the use parameter-XX:+UseMembar parameter to use memory barrier, it is not opened by default, that is, a separate memory page is used to set the state.
c. Run compiled code
1. Poling page page
Poling page is a separate memory page that is initialized when the jvm initialization starts, and this page is the key to bringing the thread of running compiled code into a stopped state.
Mmap initialization is used in linux. The source code is as follows
Address polling_page = (address):: mmap (NULL, Linux::page_size (), PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS,-1,0)
two。 Compile
Java's JIT compiles some popular source code directly into machine code and executes it directly without interpreting it to improve efficiency. In compiled code, a memory poling page is accessed when a function or method block is returned.
Under x86 architecture
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);}
In the SafepointSynchronize::begin function source code mentioned earlier
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);}. }
In the system of linux x86 and 64 bit, the address of poll stub is SafepointSynchronize::handle_polling_page_exception. The detailed program can be seen in shareRuntime_x86_64.cpp.
Back in safepoint.cpp, SafepointSynchronize::handle_polling_page_exception block the current thread by fetching the thread's safepoint_stat and calling the function void ThreadSafepointState::handle_polling_page_exception,*** by calling SafepointSynchronize::block (thread ());.
D. block status
When the thread enters the block state, it continues to maintain the block state.
On how to use source code analysis on the linux safe point to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.