Skip to content

Commit

Permalink
docs: update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
suyuan32 committed Apr 30, 2024
1 parent dbc5f80 commit a6e2657
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/en/guide/concepts/golang/20-gmp.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ The GMP model is the core model of the Go language scheduler. It serves as the f
| Preemptive Scheduling | In goroutines, a goroutine must voluntarily yield the CPU for the next goroutine to use it. Goroutines in Go are limited to using the CPU for a maximum of 10ms, after which they are preempted to allow other goroutines to run. This prevents any single goroutine from monopolizing the CPU for an extended period. |
| Thread Reuse | Go's scheduler reuses threads instead of creating new ones each time. This reduces the overhead of thread creation and destruction, improving performance. <br>- **Work Stealing**: When an `M` has no runnable `G`s, it tries to steal half of the `G`s from another `P`'s local queue instead of destroying the `M`. <br>- **Handoff Mechanism**: When a `G` blocks due to a system call, the associated `M` releases its bound `P` for other `M`s to use. |
| Parallelism | By configuring the number of `P`s using `GOMAXPROCS`, Go achieves parallel execution. The number of `P`s determines the degree of parallelism, and setting it equal to the number of CPU cores achieves maximum parallelism. |
| Global Queue | If an `M` cannot steal a `G` from another `M`'s bound `P`, it retrieves a `G` from the global queue and places it in its local queue. |
| Global Queue | When there is no executable `G` in the local queue, `M` will first go to the global queue to try to obtain `G`. If there is no `G` to be run in the global queue, `M` will try to steal it from the local queue of other `P` to take `G` |

:::

Expand All @@ -128,8 +128,7 @@ The GMP model is the core model of the Go language scheduler. It serves as the f
- The `P` acquires a `G` and executes it within an `M`.
- If the `G` blocks due to a system call, the `M` is put to sleep, and another `M` takes over the `P`. If no `M` is available, a new `M` is created.
3. **Fetching a `G`**:
- If the local queue is empty, the `M` attempts to steal a `G` from another `P`'s local queue.
- If stealing fails, the `M` retrieves a `G` from the global queue.
- If `G` in the local queue has been executed, try to obtain `G` from the global queue. If there is no executable `G` in the global queue, steal `G` from the local queue of other `P`

:::

Expand Down
4 changes: 2 additions & 2 deletions src/guide/concepts/golang/20-gmp.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ GMP 模型是 Go 语言调度器的核心模型,它是 Go 语言调度器的
| 抢占式调度 | 在协程中需要一个协程主动让出 CPU 下一个协程才能使用 CPU, 而 Goroutine 规定一个 Goroutine 每次最多只能占用 10ms 的 CPU,然后就要切换到下一个, 防止其他协程长时间不被执行 |
| 复用线程 | Go 语言的调度器会复用线程,而不是每次都创建新的线程,这样可以减少线程创建和销毁的开销,提高性能。 <br>- **工作偷取(Work stealing)**:当 `M` 没有可运行的 `G` 时,会尝试从其他线程绑定的 `P` 的本地队列中偷取一半的 `G`来运行,而不是销毁 `M` <br>- **挂起机制(Hand off)**: 当 `G` 由于系统调用而阻塞时, `M` 会释放绑定的 `P` 供其他 `M` 使用 |
| 并行 | 通过 `GOMAXPROCS` 配置 `P` 的数量,从而实现并行执行,`P` 的数量决定了并行度,`P` 的数量等于 CPU 核数时,可以实现最大并行度。 |
| 全局队列 | 如果 `M` 无法从其他 `M` 绑定的 `P` 偷取到 `G`, 则会从全局队列中获取 `G`,放入本地队列。 |
| 全局队列 | 当本地队列中没有可运行的 `G``M` 会先去全局队列尝试获取 `G`, 若全局队列中没有待运行的 `G` 则会尝试去其他 `P` 的本地队列中偷取 `G` |

:::

Expand All @@ -131,7 +131,7 @@ GMP 模型是 Go 语言调度器的核心模型,它是 Go 语言调度器的

- 获取 `G`

若本地队列中 `G` 已经执行完,则尝试从其他 `P` 的本地队列中偷取 `G`若偷取失败则从全局队列中获取 `G`
若本地队列中 `G` 已经执行完,则尝试从从全局队列中获取 `G`,若全局队列中没有可运行的 `G` 则从其他 `P` 的本地队列中偷取 `G`

:::

Expand Down

0 comments on commit a6e2657

Please sign in to comment.