G/P/M

G(Goroutine) Status

const ( _Gidle = iota // 0 _Grunnable // 1 _Grunning // 2 _Gsyscall // 3 _Gwaiting // 4 _Gmoribund_unused // 5 _Gdead // 6 )

P(Processor) Status GOMAXPROCS个P

const ( // P status _Pidle = iota _Prunning // Only this P is allowed to change from _Prunning. _Psyscall _Pgcstop _Pdead )

M(Machine) OS线程

go 入口

// Create a new g running fn with siz bytes of arguments. // Put it on the queue of g’s waiting to run. // The compiler turns a go statement into a call to this. // Cannot split the stack because it assumes that the arguments // are available sequentially after &fn; they would not be // copied if a stack split occurred. //go:nosplit func newproc(siz int32, fn funcval) { argp := add(unsafe.Pointer(&fn), sys.PtrSize) pc := getcallerpc(unsafe.Pointer(&siz)) systemstack(func() { newproc1(fn, (uint8)(argp), siz, 0, pc) }) }