How Does the Go Scheduler Create New M and P When Goroutines Perform Blocking Operations?

Patricia Arquette
Release: 2024-10-30 02:18:28
Original
437 people have browsed it

How Does the Go Scheduler Create New M and P When Goroutines Perform Blocking Operations?

When Go Scheduler Creates New M and P in the GMP Model

In Go's GMP (Goroutine, Machine, Processor) model, the scheduler manages the creation of M (Machine) and P (Processor) based on specific conditions.

M Creation

M is created in response to specific events, such as:

  • Blocking Operations: When a goroutine performs a blocking operation (e.g., I/O, syscall), the M associated with that goroutine is blocked. To continue executing the goroutine, the scheduler creates a new M.
  • Scheduling Pipelines: When the global G queue, which stores goroutines waiting for execution, has more goroutines than the number of available M, the scheduler creates new M to execute the goroutines.

P Creation

P is created at program startup based on the GOMAXPROCS environment variable, which specifies the maximum number of available P. The default value is the number of logical CPUs on the system.

Example Analysis

In your example code, you have two batches of goroutines running database operations. Each goroutine performs a blocking I/O operation.

  • First Batch: The scheduler will create 8 M (since you have 8 virtual cores) and 1 P to run the first batch of goroutines. Each M will execute a goroutine from the local queue of P.
  • Second Batch: Since the blocking operations cause the initial M to become blocked, the scheduler will create new M for the remaining goroutines in the second batch. The M will be associated with a new P, even though the number of P remains at 1.

Therefore, in your case, the scheduler will create more than 8 M for the second batch of goroutines because the operations are blocking. The P will be limited to 1 based on the GOMAXPROCS value, but M will be created dynamically as needed.

Additional Resources

For further understanding, refer to the following resources:

  • https://www.programmersought.com/article/79557885527/
  • https://blog.golang.org/go-goroutine-os-thread-and-cpu-management

The above is the detailed content of How Does the Go Scheduler Create New M and P When Goroutines Perform Blocking Operations?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!