In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to realize stop the world in Go language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement stop the world in Go language".
Stop the World (Stop the world)
Stopping the program means stopping all running goroutine. Here is a simple program to execute STW:
Func main () {runtime.GC ()}
Running the garbage collector triggers two phases of STW.
For more information about the garbage collector cycle, I suggest you read my other article, "Go: how does the garbage collector mark memory? ①"
Step 1: preempt all running goroutine:
Goroutine preemption
Once the goroutine is preempted, they will stop at the safe point. At the same time, the P processor marks (running code or in the free list) as stopped so that no code is running:
P marked as stopped
The Go scheduler then runs, separating each M from its P, and putting it in the free list:
M has been moved to idle list
About the goroutine M running on each, they will wait in the global queue:
Goroutine waits in the global queue
Then, once the world stops, only the only active goroutine can run safely and start the whole world when the work is done. The following tracking diagram will help you understand when this phase occurred:
Track the "STW" phase
System call
The "STW" phase may also affect system calls because they may be returned at STW time. Let's take an example of intensive execution of system calls and see how it handles it:
Func main () {var wg sync.WaitGroup wg.Add (10) for I: = 0; I
< 10; i++ { go func() { http.Get(`https://httpstat.us/200`) wg.Done() }() } wg.Wait() } 这是跟踪:In the STW phase, the system call is ending. However, since there are no P available (as described in the previous section, they are all marked as stopped), goroutine will be placed in the global queue and run later when the world recovers.
Delay time
The third step of "STW" involves separating all M from their P. However, Go will wait for them to stop on their own: medium calls to syscall while the scheduler is running. Waiting for the goroutine to be preempted should be quick, but in some cases it may cause some delay. Let's take an extreme case as an example:
Func main () {var t int for i: = 0 runtime.GC I < 20; iTunes + {go func () {for I: = 0 Bizi < 1000000000; iTunes + {tact +}} ()} runtime.GC ()}
In this case, the "Stop the World" phase takes 2.6 seconds:
At this point, I believe you have a deeper understanding of "how to achieve stop the world in Go language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.