Goroutine model and scheduling strategy of go concurrency method

Time:2022-1-2
catalogue
  • Single process operating system
  • Multi thread / multi process operating system
  • 1: N model
  • m: N model
    • goroutine
    • Goroutine early scheduler
    • gmp
    • Scheduler design strategy
      • Reuse thread
      • parallel
      • seize
      • Global queue

Learn Liu danbing’s 8-hour transfer to golang engineer. This section is about the principles

Single process operating system

In the early single process operating system, it can be understood that there is only one time axis, and the CPU executes each process / thread in sequence. In this way, the CPU intelligently processes one instruction, one task and one task at the same time

This will cause the process to block, and the CPU will be stuck in the current process, waiting all the time, and the CPU will be wasted

Multi thread / multi process operating system

The CPU uses the polling mechanism to schedule each process. Each process is allocated a fixed time slice. This time slice is very small. Execute process a first. If a ends, it is OK to switch to the next process B. However, if a does not end in the time slice, the CPU will forcibly switch to the next process B regardless of whether a does not end. By analogy, the CPU avoids blocking a process

However, this problem is that in the process of frequent process switching, process switching will inevitably lead to switching costs. For example, saving the current thread state, system call, context switching of the environment, and various copies will lead to a waste of time. Most of the time is spent on switching. The more processes, the greater the waste of switching

Therefore, the goal of software is to improve CPU utilization

On the other hand, the memory occupation of the process is also a big problem

1: N model

The task of the programmer is to adjust the interface and develop the business in the user state. The kernel state is responsible for adjusting the hardware and system resources. The user thread and the kernel thread are bound one by one. The CPU only needs to manage the kernel thread. Such kernel thread is called thread, and the user thread is called co routine. Thread manages multiple processes by managing the process scheduler, so that the CPU still manages only one thread

Goroutine model and scheduling strategy of go concurrency method

The problem of high CPU consumption is solved when the user switches to the cooperative state, so that only a few CPU resources are consumed

The disadvantage of this is that one collaboration process is blocked and the next collaboration process cannot be executed

m: N model

M threads manage multiple processes through the process scheduler. We can’t optimize the CPU scheduling. This process scheduler is very important

goroutine

In go, the co routine is changed to goroutine. A goroutine only occupies a few KB, so a large number of goroutines can exist. On the other hand, the scheduler of goroutine is very flexible

Goroutine early scheduler

Early goroutine schedulers had a global goroutine queue, which had a lock. To create, destroy and run a goroutine, each thread must first obtain a lock, then execute goroutine, and then return the lock after execution

Such a problem is fierce lock competition

On the other hand, when a thread executes a goroutine, the goroutine creates a new goroutine. In order to ensure concurrency, the new goroutine must be executed, and the newly created goroutine will be executed in the next thread, which actually does not meet the locality principle of the program

In addition, the system calls different threads frequently, which will still cause system overhead

gmp

In the operating system, there is an operating system scheduler, which is used to schedule the CPU to handle different kernel threads. On top of this, each thread has a processor. In this processor, there are various resources of goroutine, heap, stack, data, etc. such a processor is called P. each P manages its own local queue, which stores the goroutine to be processed by P, When goroutine needs to be processed, the thread obtains a P, and P schedules a goroutine to the thread. The thread processes the goroutine. In addition to the local queue of P, there is also a global queue, which also stores the goroutine to be run

Goroutine model and scheduling strategy of go concurrency method

Scheduler design strategy

Reuse thread

work stealing

When a thread is executing a goroutine and there are still goroutines to be executed in its p local queue, other p will steal a goroutine for its thread if it is idle

Goroutine model and scheduling strategy of go concurrency method

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

Goroutine model and scheduling strategy of go concurrency method

hand off

If the goroutine being executed by a p is suddenly blocked, such as waiting for input, then the P thread, that is, the CPU, is not actually used. At this time, a new thread will be created / awakened. At this time, the blocked goroutine will continue to block. The current CPU will change to sleep state, but the physical CPU will be switched to the thread that is not blocked, The rest of the local queues of P and P are directly transferred to the new thread for control. If the previous blocking routine is not blocked, add this goroutine to another queue
Goroutine model and scheduling strategy of go concurrency method

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

Goroutine model and scheduling strategy of go concurrency method

parallel

The number of P can be defined by macro, for example, the number of CPU cores / 2

seize

In the 1:1 model, a process is bound to a thread. When there are other processes that need to be run, the new ones can only wait until the current process releases the thread resources

The goroutine mechanism is that each goroutine has only 10ms. When the time runs out, the new goroutine will seize the CPU. No one has priority. Everyone is very average

Global queue

Goroutine model and scheduling strategy of go concurrency method

If a thread does not have goroutines to execute, it steals from other local queues according to work steeling. If other queues do not have to be stolen, it takes them from the global queue, and other goroutines in the global queue move forward

This is the end of this article on goroutine model and scheduling policy of go concurrency method. For more information about go goroutine model and scheduling policy, please search the previous articles of developpaper or continue to browse the following related articles. I hope you will support developpaper in the future!

Recommended Today

Explanation of websocket heartbeat reconnection

Recently, websocket has been used in the development of applet. The applet provides corresponding native API, which is different from H5 API. Therefore, some mature class libraries of popular H5 are difficult to use, and the native API has some defects, so it implements a set of heartbeat reconnection mechanism by itself. First, let’s briefly […]