Skip to content

Commit 6c7bea6

Browse files
committed
runtime: replace sched.mcount int32 with sched.mnext int64
Currently, since Ms never exit, the number of Ms, the number of Ms ever created, and the ID of the next M are all the same and must be small. That's about to change, so rename sched.mcount to sched.mnext to make it clear it's the number of Ms ever created (and the ID of the next M), change its type to int64, and use mcount() for the number of Ms. In the next commit, mcount() will become slightly less trivial. For #20395. Change-Id: I9af34d36bd72416b5656555d16e8085076f1b196 Reviewed-on: https://go-review.googlesource.com/68750 Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent d7ac5ce commit 6c7bea6

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

src/runtime/proc.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ func dumpgstatus(gp *g) {
518518

519519
func checkmcount() {
520520
// sched lock is held
521-
if sched.mcount > sched.maxmcount {
521+
if mcount() > sched.maxmcount {
522522
print("runtime: program exceeds ", sched.maxmcount, "-thread limit\n")
523523
throw("thread exhaustion")
524524
}
@@ -533,8 +533,11 @@ func mcommoninit(mp *m) {
533533
}
534534

535535
lock(&sched.lock)
536-
mp.id = sched.mcount
537-
sched.mcount++
536+
if sched.mnext+1 < sched.mnext {
537+
throw("runtime: thread ID overflow")
538+
}
539+
mp.id = sched.mnext
540+
sched.mnext++
538541
checkmcount()
539542

540543
mp.fastrand[0] = 1597334677 * uint32(mp.id)
@@ -3374,7 +3377,7 @@ func gcount() int32 {
33743377
}
33753378

33763379
func mcount() int32 {
3377-
return sched.mcount
3380+
return int32(sched.mnext)
33783381
}
33793382

33803383
var prof struct {
@@ -3854,7 +3857,7 @@ func acquirep1(_p_ *p) {
38543857
throw("acquirep: already in go")
38553858
}
38563859
if _p_.m != 0 || _p_.status != _Pidle {
3857-
id := int32(0)
3860+
id := int64(0)
38583861
if _p_.m != 0 {
38593862
id = _p_.m.ptr().id
38603863
}
@@ -3915,12 +3918,12 @@ func checkdead() {
39153918
return
39163919
}
39173920

3918-
run := sched.mcount - sched.nmidle - sched.nmidlelocked - sched.nmsys
3921+
run := mcount() - sched.nmidle - sched.nmidlelocked - sched.nmsys
39193922
if run > 0 {
39203923
return
39213924
}
39223925
if run < 0 {
3923-
print("runtime: checkdead: nmidle=", sched.nmidle, " nmidlelocked=", sched.nmidlelocked, " mcount=", sched.mcount, " nmsys=", sched.nmsys, "\n")
3926+
print("runtime: checkdead: nmidle=", sched.nmidle, " nmidlelocked=", sched.nmidlelocked, " mcount=", mcount(), " nmsys=", sched.nmsys, "\n")
39243927
throw("checkdead: inconsistent counts")
39253928
}
39263929

@@ -4234,7 +4237,7 @@ func schedtrace(detailed bool) {
42344237
}
42354238

42364239
lock(&sched.lock)
4237-
print("SCHED ", (now-starttime)/1e6, "ms: gomaxprocs=", gomaxprocs, " idleprocs=", sched.npidle, " threads=", sched.mcount, " spinningthreads=", sched.nmspinning, " idlethreads=", sched.nmidle, " runqueue=", sched.runqsize)
4240+
print("SCHED ", (now-starttime)/1e6, "ms: gomaxprocs=", gomaxprocs, " idleprocs=", sched.npidle, " threads=", mcount(), " spinningthreads=", sched.nmspinning, " idlethreads=", sched.nmidle, " runqueue=", sched.runqsize)
42384241
if detailed {
42394242
print(" gcwaiting=", sched.gcwaiting, " nmidlelocked=", sched.nmidlelocked, " stopwait=", sched.stopwait, " sysmonwait=", sched.sysmonwait, "\n")
42404243
}
@@ -4246,7 +4249,7 @@ func schedtrace(detailed bool) {
42464249
h := atomic.Load(&_p_.runqhead)
42474250
t := atomic.Load(&_p_.runqtail)
42484251
if detailed {
4249-
id := int32(-1)
4252+
id := int64(-1)
42504253
if mp != nil {
42514254
id = mp.id
42524255
}
@@ -4294,11 +4297,11 @@ func schedtrace(detailed bool) {
42944297
gp := allgs[gi]
42954298
mp := gp.m
42964299
lockedm := gp.lockedm.ptr()
4297-
id1 := int32(-1)
4300+
id1 := int64(-1)
42984301
if mp != nil {
42994302
id1 = mp.id
43004303
}
4301-
id2 := int32(-1)
4304+
id2 := int64(-1)
43024305
if lockedm != nil {
43034306
id2 = lockedm.id
43044307
}

src/runtime/runtime2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ type m struct {
399399
caughtsig guintptr // goroutine running during fatal signal
400400
p puintptr // attached p for executing go code (nil if not executing go code)
401401
nextp puintptr
402-
id int32
402+
id int64
403403
mallocing int32
404404
throwing int32
405405
preemptoff string // if != "", keep curg running on this m
@@ -531,7 +531,7 @@ type schedt struct {
531531
midle muintptr // idle m's waiting for work
532532
nmidle int32 // number of idle m's waiting for work
533533
nmidlelocked int32 // number of locked m's waiting for work
534-
mcount int32 // number of m's that have been created
534+
mnext int64 // number of m's that have been created and next M ID
535535
maxmcount int32 // maximum number of m's allowed (or die)
536536
nmsys int32 // number of system m's not counted for deadlock
537537

src/runtime/signal_sighandler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func sighandler(sig uint32, info *siginfo, ctxt unsafe.Pointer, gp *g) {
111111

112112
if docrash {
113113
crashing++
114-
if crashing < sched.mcount-int32(extraMCount) {
114+
if crashing < mcount()-int32(extraMCount) {
115115
// There are other m's that need to dump their stacks.
116116
// Relay SIGQUIT to the next m by sending it to the current process.
117117
// All m's that have already received SIGQUIT have signal masks blocking

0 commit comments

Comments
 (0)