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

Example Analysis of goroutine Model and scheduling Strategy in Go concurrency

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

Share

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

This article shares with you the content of the sample analysis of the goroutine model and scheduling strategy in Go concurrency. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Single process operating system

In the early single-process operating system, it can be understood that there is only one timeline, and CPU executes each process / thread sequentially. In this way of sequential execution, CPU intelligently processes one instruction and one task at the same time.

If this causes the process to block, CPU will be stuck in the current process, waiting all the time, and CPU will be wasted.

Multithreaded / multiprocess operating system

CPU uses polling mechanism to schedule each process, each process allocates a fixed time slice, this time slice is very small, execute process A first, if An ends, then no problem switches to the next process B, but if the time slice A does not end, CPU, regardless of A does not end, will be forced to switch to the next process B, and so on, CPU avoids blocking a certain process

But the problem is that in the process of frequently switching processes, process switching will inevitably lead to switching costs, such as saving the current thread state, system calls, context switching of the environment, and various copy replication will lead to a waste of time. Most of the time is spent on switching, the more the number of processes, the greater the waste of switching.

Therefore, the goal of the software is to improve CPU utilization.

On the other hand, the memory footprint of processes is also a big problem.

1RU N model

The programmer's task is to adjust the interface and develop the business in the user mode, and the kernel mode is responsible for adjusting the hardware and system resources. The user thread and the kernel thread are bound one by one, and the CPU only needs to manage the kernel thread. The kernel thread is called thread, and the user thread is called co-routine,thread to manage multiple coprograms by managing the co-program scheduler, so CPU still manages only one thread.

In this way, the concurrency is realized in the user mode, and the CPU does not need to be switched. It only consumes very few resources when the cooperative program is switched, and the problem of high consumption scheduling of CPU is solved.

The disadvantage is that one co-program is blocked and the next one cannot be executed.

MRU N model

M threads manage multiple co-programs through the co-program scheduler. We can't do the scheduling optimization of CPU, so this co-program scheduler is very important.

Goroutine

In go, the co-program co-routine is changed to goroutine, and a goroutine occupies only a few kb, so there can be a large number of goroutine. On the other hand, the scheduler of goroutine is very flexible.

Goroutine early scheduler

The early goroutine scheduler had a global goroutine queue, which had a lock. Each thread to create, destroy, and run a goroutine first acquired a lock, then executed the goroutine, and returned the lock after execution.

The 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 is placed on the next thread to execute, which actually does not satisfy the locality principle of the program.

In addition, it will cause system overhead when the system calls different threads frequently.

GMP

In the operating system, there is an operating system scheduler used to schedule CPU to handle different kernel threads. On top of this, each thread has a processor, which contains various resources of goroutine, stack, data, etc., such a processor is called P, and each P manages its own local queue, in which the thread acquires a P when the goroutine,goroutine to be processed by the P is to be processed. P dispatches a goroutine to the thread, and the thread processes the goroutine. In addition to the local queue of P, there is also a global queue in which the goroutine to be run is stored.

Scheduler design strategy reuse thread

Work stealing

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

↓↓↓↓

Hand off

If a goroutine that P is executing is suddenly blocked, such as waiting for input, then the thread of P, that is, the CPU, is not actually used, and a new thread is created / awakened. At this time, the blocked goroutine continues to block, and the current CPU goes to sleep, but switches the physical CPU to a non-blocking thread. The rest of the local queues of P and P are transferred directly to the new thread for control. If the previous blocking routine is not blocked again, then the goroutine is added to another queue.

↓↓↓↓

Parallelism

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

Preemption

In the 1:1 model, a co-program is bound to a thread, and when there is another co-program to run, the newcomer will have to wait unless the thread resource is released.

The mechanism of goroutine is that there is only 10ms for each goroutine. After running out of time, the new goroutine will definitely preempt this CPU. No one has priority, and everyone is very average.

Global queue

If a thread does not have a goroutine to execute, it steals from another local queue according to work stealing. If there is no other queue to steal, it will take it from the global queue. Other goroutine in the global queue will be moved forward.

Thank you for reading! This is the end of the article on "example analysis of goroutine model and scheduling strategy in Go concurrency". I hope the above content can be of some help to you, so that 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.

Share To

Development

Wechat

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

12
Report