]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: rename ncpu to numCPUStartup
authorMichael Pratt <mpratt@google.com>
Tue, 13 May 2025 17:12:47 +0000 (13:12 -0400)
committerGopher Robot <gobot@golang.org>
Mon, 19 May 2025 19:47:30 +0000 (12:47 -0700)
ncpu is the total logical CPU count at startup. It is never updated. For
#73193, we will start using updated CPU counts for updated GOMAXPROCS,
making the ncpu name a bit ambiguous. Change to a less ambiguous name.

While we're at it, give the OS specific lookup functions a common name,
so it can be used outside of osinit later.

For #73193.

Change-Id: I6a6a636cf21cc60de36b211f3c374080849fc667
Reviewed-on: https://go-review.googlesource.com/c/go/+/672277
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>

23 files changed:
src/runtime/debug.go
src/runtime/heapdump.go
src/runtime/lock_spinbit.go
src/runtime/mgc.go
src/runtime/os3_solaris.go
src/runtime/os_aix.go
src/runtime/os_darwin.go
src/runtime/os_dragonfly.go
src/runtime/os_freebsd.go
src/runtime/os_freebsd_arm.go
src/runtime/os_illumos.go
src/runtime/os_linux.go
src/runtime/os_netbsd.go
src/runtime/os_netbsd_arm.go
src/runtime/os_only_solaris.go
src/runtime/os_openbsd.go
src/runtime/os_openbsd_arm.go
src/runtime/os_plan9.go
src/runtime/os_wasm.go
src/runtime/os_windows.go
src/runtime/proc.go
src/runtime/runtime2.go
src/runtime/vgetrandom_linux.go

index c477e2b9f6e4a02289b099252b818393e338dec9..57e9ba8d7d4b440b93b6742af5bb30a6f39a8a29 100644 (file)
@@ -40,7 +40,7 @@ func GOMAXPROCS(n int) int {
 // at process startup. Changes to operating system CPU allocation after
 // process startup are not reflected.
 func NumCPU() int {
-       return int(ncpu)
+       return int(numCPUStartup)
 }
 
 // NumCgoCall returns the number of cgo calls made by the current process.
index 6e6b58edd5ec34f67dc23c865871923ac4179506..5476035b2ea5e1bd8a58e6ea411605193237580c 100644 (file)
@@ -536,7 +536,7 @@ func dumpparams() {
        dumpint(uint64(arenaEnd))
        dumpstr(goarch.GOARCH)
        dumpstr(buildVersion)
-       dumpint(uint64(ncpu))
+       dumpint(uint64(numCPUStartup))
 }
 
 func itab_callback(tab *itab) {
index f90698a4c943d4314828331ecd73cffd22bec79d..039ea6f5656a8aa45bac2100790c1a2c2179a60f 100644 (file)
@@ -175,7 +175,7 @@ func lock2(l *mutex) {
        // On uniprocessors, no point spinning.
        // On multiprocessors, spin for mutexActiveSpinCount attempts.
        spin := 0
-       if ncpu > 1 {
+       if numCPUStartup > 1 {
                spin = mutexActiveSpinCount
        }
 
index f96dbadd0139f07e5aef3616ecd07f87b24eb322..84aa1105d8702f57b8e5230324ab5e8bc508b460 100644 (file)
@@ -727,10 +727,10 @@ func gcStart(trigger gcTrigger) {
        systemstack(gcResetMarkState)
 
        work.stwprocs, work.maxprocs = gomaxprocs, gomaxprocs
-       if work.stwprocs > ncpu {
-               // This is used to compute CPU time of the STW phases,
-               // so it can't be more than ncpu, even if GOMAXPROCS is.
-               work.stwprocs = ncpu
+       if work.stwprocs > numCPUStartup {
+               // This is used to compute CPU time of the STW phases, so it
+               // can't be more than the CPU count, even if GOMAXPROCS is.
+               work.stwprocs = numCPUStartup
        }
        work.heap0 = gcController.heapLive.Load()
        work.pauseNS = 0
index ded821b2e66f817a19e26c24166153bf42159f77..116995e5f6a1aff9c0336c7b4820ea58d3551fe6 100644 (file)
@@ -137,7 +137,7 @@ func osinit() {
        // before calling minit on m0.
        asmcgocall(unsafe.Pointer(abi.FuncPCABI0(miniterrno)), unsafe.Pointer(&libc____errno))
 
-       ncpu = getncpu()
+       numCPUStartup = getCPUCount()
        if physPageSize == 0 {
                physPageSize = getPageSize()
        }
index 1b483c2a7e98567410a6041e292cfd00a7cc77a3..3847b7671ab6f23fd50ec22889a19daa54916b30 100644 (file)
@@ -97,10 +97,14 @@ func osinit() {
        // before calling minit on m0.
        miniterrno()
 
-       ncpu = int32(sysconf(__SC_NPROCESSORS_ONLN))
+       numCPUStartup = getCPUCount()
        physPageSize = sysconf(__SC_PAGE_SIZE)
 }
 
+func getCPUCount() int32 {
+       return int32(sysconf(__SC_NPROCESSORS_ONLN))
+}
+
 // newosproc0 is a version of newosproc that can be called before the runtime
 // is initialized.
 //
index 6eab3b5c3ddba63178a712fc798062fd58769baf..5aef34ff8fec8b6bb86d8c648e08c93f84bff57f 100644 (file)
@@ -144,7 +144,7 @@ func osinit() {
        // pthread_create delayed until end of goenvs so that we
        // can look at the environment first.
 
-       ncpu = getncpu()
+       numCPUStartup = getCPUCount()
        physPageSize = getPageSize()
 
        osinit_hack()
@@ -168,7 +168,7 @@ const (
        _HW_PAGESIZE = 7
 )
 
-func getncpu() int32 {
+func getCPUCount() int32 {
        // Use sysctl to fetch hw.ncpu.
        mib := [2]uint32{_CTL_HW, _HW_NCPU}
        out := uint32(0)
index 9b3235084d7c4fd1264424b9c82466e0aaaac6c5..e22fd9b42f45d9c9f99c49cfaf9a218a9ca62f0b 100644 (file)
@@ -78,7 +78,7 @@ const (
 
 var sigset_all = sigset{[4]uint32{^uint32(0), ^uint32(0), ^uint32(0), ^uint32(0)}}
 
-func getncpu() int32 {
+func getCPUCount() int32 {
        mib := [2]uint32{_CTL_HW, _HW_NCPU}
        out := uint32(0)
        nout := unsafe.Sizeof(out)
@@ -174,7 +174,7 @@ func newosproc(mp *m) {
 }
 
 func osinit() {
-       ncpu = getncpu()
+       numCPUStartup = getCPUCount()
        if physPageSize == 0 {
                physPageSize = getPageSize()
        }
index 1002185f993a9a7f7efc04929a263b1158684f44..ab859cfb47f7c11f1a13e5def9a8a269f1c15d3c 100644 (file)
@@ -91,7 +91,7 @@ const (
 func cpuset_getaffinity(level int, which int, id int64, size int, mask *byte) int32
 
 //go:systemstack
-func getncpu() int32 {
+func getCPUCount() int32 {
        // Use a large buffer for the CPU mask. We're on the system
        // stack, so this is fine, and we can't allocate memory for a
        // dynamically-sized buffer at this point.
@@ -276,7 +276,7 @@ func libpreinit() {
 }
 
 func osinit() {
-       ncpu = getncpu()
+       numCPUStartup = getCPUCount()
        if physPageSize == 0 {
                physPageSize = getPageSize()
        }
index 5f6bf46798c9ee561b1f623c6cbec724188dfe33..5a6c60210af98835233f5fcba6c2c2e81a24792b 100644 (file)
@@ -28,8 +28,9 @@ func checkgoarm() {
                exit(1)
        }
 
-       // osinit not called yet, so ncpu not set: must use getncpu directly.
-       if getncpu() > 1 && goarm < 7 {
+       // osinit not called yet, so numCPUStartup not set: must use
+       // getCPUCount directly.
+       if getCPUCount() > 1 && goarm < 7 {
                print("runtime: this system has multiple CPUs and must use\n")
                print("atomic synchronization instructions. Recompile using GOARM=7.\n")
                exit(1)
index c3c3e4e6d56df56d29462553efbf24c57d6c3285..c4bc9fe9c069979b08cffcf54711e0b2ceb77aa4 100644 (file)
@@ -87,7 +87,7 @@ func getcpucap() uint64 {
        return capval
 }
 
-func getncpu() int32 {
+func getCPUCount() int32 {
        n := int32(sysconf(__SC_NPROCESSORS_ONLN))
        if n < 1 {
                return 1
index 3071e3220216a362eef8107382cd262e8a8dfebd..54f98ef4f84326b72bf12f455e2732365b4ca15f 100644 (file)
@@ -101,7 +101,7 @@ func futexwakeup(addr *uint32, cnt uint32) {
        *(*int32)(unsafe.Pointer(uintptr(0x1006))) = 0x1006
 }
 
-func getproccount() int32 {
+func getCPUCount() int32 {
        // This buffer is huge (8 kB) but we are on the system stack
        // and there should be plenty of space (64 kB).
        // Also this is a leaf, so we're not holding up the memory for long.
@@ -354,7 +354,7 @@ func getHugePageSize() uintptr {
 }
 
 func osinit() {
-       ncpu = getproccount()
+       numCPUStartup = getCPUCount()
        physHugePageSize = getHugePageSize()
        osArchInit()
        vgetrandomInit()
index a06e5febbd5a75a200abe1642555526c3b429ea1..342ede9c5348b4b35b8efc9c0713cdc8fec21fea 100644 (file)
@@ -120,7 +120,7 @@ func sysctlInt(mib []uint32) (int32, bool) {
        return out, true
 }
 
-func getncpu() int32 {
+func getCPUCount() int32 {
        if n, ok := sysctlInt([]uint32{_CTL_HW, _HW_NCPUONLINE}); ok {
                return int32(n)
        }
@@ -264,7 +264,7 @@ func netbsdMstart0() {
 }
 
 func osinit() {
-       ncpu = getncpu()
+       numCPUStartup = getCPUCount()
        if physPageSize == 0 {
                physPageSize = getPageSize()
        }
index 7494a387e33b99abdc44fc9edc3dbc307de0888c..884b18bdd400ce53803970c9ec40c2243c444770 100644 (file)
@@ -21,8 +21,9 @@ func lwp_mcontext_init(mc *mcontextt, stk unsafe.Pointer, mp *m, gp *g, fn uintp
 func checkgoarm() {
        // TODO(minux): FP checks like in os_linux_arm.go.
 
-       // osinit not called yet, so ncpu not set: must use getncpu directly.
-       if getncpu() > 1 && goarm < 7 {
+       // osinit not called yet, so numCPUStartup not set: must use
+       // getCPUCount directly.
+       if getCPUCount() > 1 && goarm < 7 {
                print("runtime: this system has multiple CPUs and must use\n")
                print("atomic synchronization instructions. Recompile using GOARM=7.\n")
                exit(1)
index 0c72500674f72e1bbb48b22dbcbb0ee915ff7c3b..aa7b8faf007d6a8df7ea741060079938dc9e9cb6 100644 (file)
@@ -8,7 +8,7 @@
 
 package runtime
 
-func getncpu() int32 {
+func getCPUCount() int32 {
        n := int32(sysconf(__SC_NPROCESSORS_ONLN))
        if n < 1 {
                return 1
index 4ce4c3c58d35df6d984ca6c583c8e5c111aea063..02846851d65e9c6379e52cbdd31aa621e018536b 100644 (file)
@@ -63,7 +63,7 @@ func internal_cpu_sysctlUint64(mib []uint32) (uint64, bool) {
        return sysctlUint64(mib)
 }
 
-func getncpu() int32 {
+func getCPUCount() int32 {
        // Try hw.ncpuonline first because hw.ncpu would report a number twice as
        // high as the actual CPUs running on OpenBSD 6.4 with hyperthreading
        // disabled (hw.smt=0). See https://golang.org/issue/30127
@@ -135,7 +135,7 @@ func semawakeup(mp *m) {
 }
 
 func osinit() {
-       ncpu = getncpu()
+       numCPUStartup = getCPUCount()
        physPageSize = getPageSize()
 }
 
index d5dc8cb4792406e4484504297bf5c8a176508c8a..ba33d4d5bb268b13b5ed115e789cb485cd875b99 100644 (file)
@@ -7,8 +7,9 @@ package runtime
 func checkgoarm() {
        // TODO(minux): FP checks like in os_linux_arm.go.
 
-       // osinit not called yet, so ncpu not set: must use getncpu directly.
-       if getncpu() > 1 && goarm < 7 {
+       // osinit not called yet, so numCPUStartup not set: must use
+       // getCPUCount directly.
+       if getCPUCount() > 1 && goarm < 7 {
                print("runtime: this system has multiple CPUs and must use\n")
                print("atomic synchronization instructions. Recompile using GOARM=7.\n")
                exit(1)
index a0ad9060c5f9e61021548c74658f4e84e906d135..72a86579854b5ce929f68886f389a5c1099deaf3 100644 (file)
@@ -230,7 +230,7 @@ func mdestroy(mp *m) {
 
 var sysstat = []byte("/dev/sysstat\x00")
 
-func getproccount() int32 {
+func getCPUCount() int32 {
        var buf [2048]byte
        fd := open(&sysstat[0], _OREAD|_OCEXEC, 0)
        if fd < 0 {
@@ -330,7 +330,7 @@ var (
 func osinit() {
        physPageSize = getPageSize()
        initBloc()
-       ncpu = getproccount()
+       numCPUStartup = getCPUCount()
        getg().m.procid = getpid()
 
        fd := open(&bintimeDev[0], _OREAD|_OCEXEC, 0)
index 8046caf45ef133edc2c14b4d2be57089535a549f..15137cc13f3329c31e674fcc86cc386fb04768ca 100644 (file)
@@ -14,10 +14,14 @@ func osinit() {
        physPageSize = 64 * 1024
        initBloc()
        blocMax = uintptr(currentMemory()) * physPageSize // record the initial linear memory size
-       ncpu = 1
+       numCPUStartup = getCPUCount()
        getg().m.procid = 2
 }
 
+func getCPUCount() int32 {
+       return 1
+}
+
 const _SIGSEGV = 0xb
 
 func sigpanic() {
index a84832e0ce35556c56fef6314bb142d678a3f1da..8f77cd50f87714b05a57aaa6df414ecf6eca1d94 100644 (file)
@@ -322,7 +322,7 @@ func monitorSuspendResume() {
                uintptr(unsafe.Pointer(&params)), uintptr(unsafe.Pointer(&handle)))
 }
 
-func getproccount() int32 {
+func getCPUCount() int32 {
        var mask, sysmask uintptr
        ret := stdcall3(_GetProcessAffinityMask, currentProcess, uintptr(unsafe.Pointer(&mask)), uintptr(unsafe.Pointer(&sysmask)))
        if ret != 0 {
@@ -486,7 +486,7 @@ func osinit() {
        initSysDirectory()
        initLongPathSupport()
 
-       ncpu = getproccount()
+       numCPUStartup = getCPUCount()
 
        physPageSize = getPageSize()
 
index 5d3e4e495340edd8cbaabb29d017a6d6f04082bd..f48373fe7c74d6c1d6bfdf05c69582191dfccae9 100644 (file)
@@ -900,7 +900,7 @@ func schedinit() {
 
        lock(&sched.lock)
        sched.lastpoll.Store(nanotime())
-       procs := ncpu
+       procs := numCPUStartup
        if n, ok := strconv.Atoi32(gogetenv("GOMAXPROCS")); ok && n > 0 {
                procs = n
        }
@@ -7230,7 +7230,7 @@ func internal_sync_runtime_canSpin(i int) bool {
        // GOMAXPROCS>1 and there is at least one other running P and local runq is empty.
        // As opposed to runtime mutex we don't do passive spinning here,
        // because there can be work on global runq or on other Ps.
-       if i >= active_spin || ncpu <= 1 || gomaxprocs <= sched.npidle.Load()+sched.nmspinning.Load()+1 {
+       if i >= active_spin || numCPUStartup <= 1 || gomaxprocs <= sched.npidle.Load()+sched.nmspinning.Load()+1 {
                return false
        }
        if p := getg().m.p.ptr(); !runqempty(p) {
index 920437882daf47e837b268c468270c7cc40192b6..2c213d0de447c29c1829740a8caadea668bd7ca1 100644 (file)
@@ -1196,12 +1196,12 @@ var isIdleInSynctest = [len(waitReasonStrings)]bool{
 }
 
 var (
-       allm       *m
-       gomaxprocs int32
-       ncpu       int32
-       forcegc    forcegcstate
-       sched      schedt
-       newprocs   int32
+       allm          *m
+       gomaxprocs    int32
+       numCPUStartup int32
+       forcegc       forcegcstate
+       sched         schedt
+       newprocs      int32
 )
 
 var (
index 6ab12e3c67b07a300aa2930556231a8c3475c5c1..8f39f6d3a6a0fe9cc1ee6b6c753591a196b87689 100644 (file)
@@ -46,7 +46,7 @@ func vgetrandomInit() {
 func vgetrandomGetState() uintptr {
        lock(&vgetrandomAlloc.statesLock)
        if len(vgetrandomAlloc.states) == 0 {
-               num := uintptr(ncpu) // Just a reasonable size hint to start.
+               num := uintptr(numCPUStartup) // Just a reasonable size hint to start.
                stateSizeCacheAligned := (vgetrandomAlloc.stateSize + cpu.CacheLineSize - 1) &^ (cpu.CacheLineSize - 1)
                allocSize := (num*stateSizeCacheAligned + physPageSize - 1) &^ (physPageSize - 1)
                num = (physPageSize / stateSizeCacheAligned) * (allocSize / physPageSize)