207

Go 1.14 新特性之 Goroutine 抢占式调度

 4 years ago
source link: https://studygolang.com/articles/28321
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

代码示例

有这样一段 Go 代码,在程序执行之初将 P 设置到数量为 1 ,有两个 goroutine,一个是 main ,一个是执行死循环的匿名函数:

package main

import (
    "fmt"
    "runtime"
    "time"
)

func main() {
    runtime.GOMAXPROCS(1)

    fmt.Println("The program starts ...")

    go func() {
        for {
        }
    }()

    time.Sleep(time.Second)
    fmt.Println("I got scheduled!")
}

我们分析一下程序执行过程,设置 P 数量以后,执行打印 The program starts ... ,之后将匿名 goroutine 加入调度队列,执行 Sleep 操作,在 sleep 过程中调度器会将 main goroutine 从唯一 P 中让出,执行匿名 goroutine,而这个 goroutine 是无限循环,并且中间没有函数调用,导致调度器无法插手把它让出继续执行 main ,所以程序打印完 The program starts ... 之后会一直挂着,并不会打印 I got scheduled!

无函数调用的死循环 goroutine 会一直占据一个 P,GC 需要等待所有 goroutine 停止才得以执行,从而会导致 GC 延迟。如果程序中因无意出现这种死循环 goroutine 而造成 bug,很难排查。

Go 1.14 之前一直是上述的执行过程,协程之间的调度是非抢占式的。 Go 1.14 引入了基于系统信号的异步抢占调度 ,这样,像上面的无函数调用的死循环 goroutine 也可以被抢占了,从而将 main goroutine 重新调度回 P 执行,最终会打印 I got scheduled!

验证

我们创建一个项目目录,并在里面创建两个文件: main.goDockerfilemain.go 就是上面贴的代码, Dockerfile 内容如下:

ARG GO_VERSION
FROM golang:${GO_VERSION}
COPY ./main.go /app/
CMD ["go", "run", "/app/main.go"]

我们通过构建参数来指定基础镜像 golang 的版本( 1.131.14 ),然后将 main.go 拷贝进镜像,然后执行。

执行构建:

$ docker build -t app13 --build-arg GO_VERSION=1.13 .
$ docker build -t app14 --build-arg GO_VERSION=1.14 .

对比执行:

$ docker run -it --rm app13:latest

The program starts ...
$ docker run -it --rm app14:latest

The program starts ...
I got scheduled!

执行 app13 被阻塞住,不会打印 I got scheduled! ,而执行 app14 则可以。

参考

关于新特性抢占式调度更详尽的解读,请参考如下链接:

欢迎关注我们的微信公众号,每天学习Go知识

FveQFjN.jpg!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK