Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the essence of Go language scheduling?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "what is the essence of Go language scheduling". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Firstly, the conclusion of this paper is given: the essence of Go scheduling is a production-consumption process.

Producer-consumer

Producer-consumer model

The best thing we can do with Go is to start a goroutine with a go func () {} () to execute tasks concurrently. This is much more convenient than starting a thread to execute a task concurrently with Cmax Candle +. This code actually produces a goroutine, enters the runnable queue, waits and m to find it so that it can be run.

Friends familiar with the GMP model know that goroutine is finally executed on m, because the operating system is not aware of goroutine, it can only perceive threads, and threads can be seen as m.

So, the process of m getting goroutine and running it is a consumption process.

Production-consumption process

Production process-- three-level queue

The produced goroutine needs to be stored in a place that is runnable queue. In Go programs, runnable queues are hierarchical and are divided into three levels:

Three-level runnable queue

Runnext can actually point to only one goroutine, so it is a special queue.

Which runnable queue do you put goroutine in? Depending on the situation.

First, if the runnext is empty, then the goroutine will be successfully put into the runnext, and then it will be run with the highest priority, that is, it will be consumed first.

If the runnext is not empty, it is responsible for kicking out the old goroutine on the runnext and then putting the new goroutine up. Where exactly did you kick it? Score again.

Local queue is an array of size 256 that is actually used as a circular array with head and tail pointers. If the local queue is not satisfied, put the runnext into the local queue;. Otherwise, there are too many goroutine on the local queue of P, indicating that the task of the current P is too heavy and needs to be lightened, so other P assistance is needed. Thus, the runnext and half of the current P goroutine are packaged and thrown into the global queue.

Of course, there are very vivid animations in this part of the course. Here is a screenshot for you to feel:

Producer animation

Consumption process-scheduling cycle

The previous article also talked about the scheduling cycle, which actually means that when the Go program starts, it creates a P equal to the number of CPU cores and creates the initial m, called M0. This M0 will start a scheduling cycle: keep looking for g, execute, and then find g.

The pseudo code goes like this:

Scheduling cycle

As the program runs, more m is created, so more scheduling loops are executed.

The producers over there are constantly producing g, and the scheduling cycle of m here is constantly consuming g, and the whole process is run.

Of course, in the process of looking for g, we also look for it from the third-tier queue above:

First look at runnext, then local queue, then global queue. Of course, if you can't find it, go to another p to steal it.

This is the end of the content of "what is the essence of Go language scheduling". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report