golang_runtime_reading icon indicating copy to clipboard operation
golang_runtime_reading copied to clipboard

sysmon是不是和某个M共用?该M也进行执行G?

Open JayenLee opened this issue 5 years ago • 0 comments

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.13.6 我在阅读您的blog 《深入golang-runtime的调度》看到 main goroutine启动的时候是在m0中启动,然后在执行proc.go main函数中有一个newm(sysmon, nil) 好像是新建一个m线程去执行sysmon监控,也就是说sysmon是不是独立一个线程运行? 但是我在测试调度的时候发现,如果gorouine里面做了一个无限循环,在用户main函数循环内创建多个goroutine(超过runtime.GOMAXPROCS个)发现在最终调用的时候就只要runtime.GOMAXPROCS个被执行,另外其他的goroutine并没有被执行,如果按照源代码中以及您的分析,应该会被sysmon进行抢占,把P和m剥离再创建新的m线程去执行其他的goroutine,但是从结果来看,好像sysmon并不是独立线程而是和某个m共用了,导致m如果被占用就无法做出抢占和调度,不知道是否是这个原因?代码如下:

package main
import (
	"fmt"
	"sync"
)

var wg sync.WaitGroup

func process(id int) {
	fmt.Printf("id: %d\n", id)
	for {
		// time.Sleep(time.Second)
	}
	// wg.Done()
}
func main() {
	runtime.GOMAXPROCS(4)
	// println(runtime.NumCPU())
	 wg.Add(1)
	 for i := 0; i < 10; i++ {
	 	go process(i)
	 }
	wg.Wait()
}

最终输出的只要 4个

JayenLee avatar Feb 09 '20 16:02 JayenLee