Home  >  Article  >  Backend Development  >  When and Why Does the Go Scheduler Allocate New M and P Processors?

When and Why Does the Go Scheduler Allocate New M and P Processors?

Linda Hamilton
Linda HamiltonOriginal
2024-10-30 17:36:03894browse

When and Why Does the Go Scheduler Allocate New M and P Processors?

When and Why the Go Scheduler Allocates New M and P Processors

Understanding the Go runtime's scheduling model is crucial for optimizing application performance. One key aspect of this model is the allocation of M (machines) and P (processors).

When Are M and P Created?

M processors are created when:

  • A new goroutine is started ( unless there is a spare idle M)
  • An existing M blocks on an A system call (creates a new operating system thread known as an M)

P processors are created when all the local runqueues are full one goroutine is running its neighbor goroutines ready to run are put in a global queue and it contains a single p.

Blocking Tasks and P Reuse

In the provided test code, the goroutines perform blocking database operations. Blocking tasks remove M processors from P processors.

In this case, creating new M processors is necessary to handle the blocked goroutines. The initial 8 (number of virtual cores) M processors will not be sufficient for the second batch. New M processors will be allocated as needed.

Additional 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 When and Why Does the Go Scheduler Allocate New M and P Processors?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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