Skip to content

Commit db95626

Browse files
prattmicgopherbot
authored andcommitted
runtime: rename ncpu to numCPUStartup
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>
1 parent 76e7bfb commit db95626

23 files changed

+54
-43
lines changed

src/runtime/debug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func GOMAXPROCS(n int) int {
4040
// at process startup. Changes to operating system CPU allocation after
4141
// process startup are not reflected.
4242
func NumCPU() int {
43-
return int(ncpu)
43+
return int(numCPUStartup)
4444
}
4545

4646
// NumCgoCall returns the number of cgo calls made by the current process.

src/runtime/heapdump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ func dumpparams() {
536536
dumpint(uint64(arenaEnd))
537537
dumpstr(goarch.GOARCH)
538538
dumpstr(buildVersion)
539-
dumpint(uint64(ncpu))
539+
dumpint(uint64(numCPUStartup))
540540
}
541541

542542
func itab_callback(tab *itab) {

src/runtime/lock_spinbit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func lock2(l *mutex) {
175175
// On uniprocessors, no point spinning.
176176
// On multiprocessors, spin for mutexActiveSpinCount attempts.
177177
spin := 0
178-
if ncpu > 1 {
178+
if numCPUStartup > 1 {
179179
spin = mutexActiveSpinCount
180180
}
181181

src/runtime/mgc.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,10 @@ func gcStart(trigger gcTrigger) {
727727
systemstack(gcResetMarkState)
728728

729729
work.stwprocs, work.maxprocs = gomaxprocs, gomaxprocs
730-
if work.stwprocs > ncpu {
731-
// This is used to compute CPU time of the STW phases,
732-
// so it can't be more than ncpu, even if GOMAXPROCS is.
733-
work.stwprocs = ncpu
730+
if work.stwprocs > numCPUStartup {
731+
// This is used to compute CPU time of the STW phases, so it
732+
// can't be more than the CPU count, even if GOMAXPROCS is.
733+
work.stwprocs = numCPUStartup
734734
}
735735
work.heap0 = gcController.heapLive.Load()
736736
work.pauseNS = 0

src/runtime/os3_solaris.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func osinit() {
137137
// before calling minit on m0.
138138
asmcgocall(unsafe.Pointer(abi.FuncPCABI0(miniterrno)), unsafe.Pointer(&libc____errno))
139139

140-
ncpu = getncpu()
140+
numCPUStartup = getCPUCount()
141141
if physPageSize == 0 {
142142
physPageSize = getPageSize()
143143
}

src/runtime/os_aix.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,14 @@ func osinit() {
9797
// before calling minit on m0.
9898
miniterrno()
9999

100-
ncpu = int32(sysconf(__SC_NPROCESSORS_ONLN))
100+
numCPUStartup = getCPUCount()
101101
physPageSize = sysconf(__SC_PAGE_SIZE)
102102
}
103103

104+
func getCPUCount() int32 {
105+
return int32(sysconf(__SC_NPROCESSORS_ONLN))
106+
}
107+
104108
// newosproc0 is a version of newosproc that can be called before the runtime
105109
// is initialized.
106110
//

src/runtime/os_darwin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func osinit() {
144144
// pthread_create delayed until end of goenvs so that we
145145
// can look at the environment first.
146146

147-
ncpu = getncpu()
147+
numCPUStartup = getCPUCount()
148148
physPageSize = getPageSize()
149149

150150
osinit_hack()
@@ -168,7 +168,7 @@ const (
168168
_HW_PAGESIZE = 7
169169
)
170170

171-
func getncpu() int32 {
171+
func getCPUCount() int32 {
172172
// Use sysctl to fetch hw.ncpu.
173173
mib := [2]uint32{_CTL_HW, _HW_NCPU}
174174
out := uint32(0)

src/runtime/os_dragonfly.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const (
7878

7979
var sigset_all = sigset{[4]uint32{^uint32(0), ^uint32(0), ^uint32(0), ^uint32(0)}}
8080

81-
func getncpu() int32 {
81+
func getCPUCount() int32 {
8282
mib := [2]uint32{_CTL_HW, _HW_NCPU}
8383
out := uint32(0)
8484
nout := unsafe.Sizeof(out)
@@ -174,7 +174,7 @@ func newosproc(mp *m) {
174174
}
175175

176176
func osinit() {
177-
ncpu = getncpu()
177+
numCPUStartup = getCPUCount()
178178
if physPageSize == 0 {
179179
physPageSize = getPageSize()
180180
}

src/runtime/os_freebsd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const (
9191
func cpuset_getaffinity(level int, which int, id int64, size int, mask *byte) int32
9292

9393
//go:systemstack
94-
func getncpu() int32 {
94+
func getCPUCount() int32 {
9595
// Use a large buffer for the CPU mask. We're on the system
9696
// stack, so this is fine, and we can't allocate memory for a
9797
// dynamically-sized buffer at this point.
@@ -276,7 +276,7 @@ func libpreinit() {
276276
}
277277

278278
func osinit() {
279-
ncpu = getncpu()
279+
numCPUStartup = getCPUCount()
280280
if physPageSize == 0 {
281281
physPageSize = getPageSize()
282282
}

src/runtime/os_freebsd_arm.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ func checkgoarm() {
2828
exit(1)
2929
}
3030

31-
// osinit not called yet, so ncpu not set: must use getncpu directly.
32-
if getncpu() > 1 && goarm < 7 {
31+
// osinit not called yet, so numCPUStartup not set: must use
32+
// getCPUCount directly.
33+
if getCPUCount() > 1 && goarm < 7 {
3334
print("runtime: this system has multiple CPUs and must use\n")
3435
print("atomic synchronization instructions. Recompile using GOARM=7.\n")
3536
exit(1)

0 commit comments

Comments
 (0)