可以超过计算机的核数吗?
一般可以设置CPU Core两倍压榨性能,最新go1.14没有限制,以前版本有个常量_MaxGomaxprocs作为最大值限制。
https://golang.org/src/runtime/proc.go L563-L569,ncpu应该是cpu数量,然后获得环境变量,最后procresize函数去设置gomaxprocs变量。
procs := ncpu
if n, ok := atoi32(gogetenv("GOMAXPROCS")); ok && n > 0 {
procs = n
}
if procresize(procs) != nil {
throw("unknown runnable goroutine during bootstrap")
}
procresize函数定义在L4154,处理p的扩容或者缩容,最后面保存nprocs数量到全局变量gomaxprocs,就是全部p的数量,gomaxprocs还是有个数量限制的,int32的最大值。
var int32p *int32 = &gomaxprocs // make compiler check that gomaxprocs is an int32
atomic.Store((*uint32)(unsafe.Pointer(int32p)), uint32(nprocs))
在https://golang.org/src/runtime/runtime2.go L1012定义了切片allp保存全部p,全部p的数量就是gomaxprocs的值。
var (
allglen uintptr
allm *m
allp []*p // len(allp) == gomaxprocs; may change at safe points, otherwise immutable
allpLock mutex // Protects P-less reads of allp and all writes
gomaxprocs int32
2 回答1.4k 阅读
2 回答1.3k 阅读
2 回答1.2k 阅读
2 回答1.3k 阅读
3 回答836 阅读
2 回答1k 阅读
2 回答980 阅读
gomaxprocs 是可以超过最大cpu 数的。设置过多也是没有意义的,因为是cpu在跑你的服务,P只是个媒介而已.
业务建议: 涉及到cgo 较多的服务,可以适当加大P的数量。