Today’s protagonist is an extension question (question) of the all-purpose GMP model question in the Go interview, that is "GMP model, why is there P ? "
Further deliberation behind the question, in fact, the essence of this interview question is to ask: "GMP model, why can't G and M be directly bound? There is also an additional P. It's so troublesome, why is it, what problem is it trying to solve? "
In this article, Jianyu will take you to explore the reasons for the changes in GM and GMP models.static void schedule(G *gp) { ... schedlock(); if(gp != nil) { ... switch(gp->status){ case Grunnable: case Gdead: // Shouldn't have been running! runtime·throw("bad gp->status in sched"); case Grunning: gp->status = Grunnable; gput(gp); break; } gp = nextgandunlock(); gp->readyonstop = 0; gp->status = Grunning; m->curg = gp; gp->m = m; ... runtime·gogo(&gp->sched, 0); }
schedlock
method to obtain the global lock. gput
method to save the current Goroutine running status and other information for subsequent use. nextgandunlock
method to find the next runnable Goroutine and release the global lock for other schedulers to use. runtime·gogo
method to run the next Goroutine to be executed just obtained and enter the next round of scheduling. By analyzing the scheduler source code of Go1.0.1, we can find a more interesting point. That is the scheduler itself (schedule method). Under normal processes, it will not return, that is, it will not end the main process.
He will continuously run the scheduling process. After GoroutineA is completed, it will start looking for GoroutineB. When B is found, it will The completed scheduling right of A is handed over to B, allowing GoroutineB to start being scheduled, that is, running.
Of course, there are also Gs that are blocked (Blocked). Suppose G is making some system or network calls, which will cause G to stall. At this time, M (system thread) will be put back in the kernel queue, waiting for a new round of wake-up.
On the surface, the GM model seems to be indestructible and flawless. But why change it?
In 2012, Dmitry Vyukov published the article "Scalable Go Scheduler Design Doc", which is still the main target of major research articles on the Go scheduler. He described the overall reasons and considerations in the article. The following content refers to this article.
The current Goroutine scheduler (referring to the GM model of Go1.0) limits the scalability of concurrent programs written in Go, especially high-throughput servers and parallel computing programs.
The implementation has the following problems:
In order to solve the above many problems of the GM model, in Go1.1, Dmitry Vyukov worked on the GM model Based on this, a new P (Processor) component is added. And implemented the Work Stealing algorithm to solve some newly generated problems.
GMP model, in the previous article "Go group friends asked: What is the appropriate number of Goroutines to control, will it affect GC and scheduling? has been explained in ".
# Friends who think it’s good can pay attention to it, I won’t repeat it here.
What changes will it bring after adding P? Let’s talk about it more explicitly.
Each P has its own local queue, which greatly reduces the direct dependence on the global queue. The result is a reduction in lock competition. The bulk of the performance overhead of the GM model is lock competition.
On the relative balance of each P, the Work Stealing algorithm is also implemented in the GMP model. If the local queue of P is empty, it will be removed from the global queue. Or steal the runnable G from the local queue of other P to run, reducing idling and improving resource utilization.
At this time, some friends may be confused. If you want To implement the local queue and Work Stealing algorithm, why not add it directly to M? M can still achieve similar functions. Why add another P component?
Combined with the positioning of M (system thread), if you do this, there are the following problems.
"GMP model, why is there P?" This question is like a system design understanding, because now many people will memorize the GMP model or go through it in an instant style in order to cope with the interview. And understanding the real reasons behind it is what we need to learn and understand.
Only by knowing what is happening and why is it happening can we break the situation.
The above is the detailed content of Goodbye Go interviewer: GMP model, why is there P?. For more information, please follow other related articles on the PHP Chinese website!