Skip to content

Commit 9d17e17

Browse files
committed
runtime: capture runtimeInitTime after nanotime is initialized
CL 36428 changed the way nanotime works so on Darwin and Windows it now depends on runtime.startNano, which is computed at runtime.init time. Unfortunately, the `runtimeInitTime = nanotime()` initialization happened *before* runtime.init, so on these platforms runtimeInitTime is set incorrectly. The one (and only) consequence of this is that the start time printed in gctrace lines is bogus: gc 1 18446653480.186s 0%: 0.092+0.47+0.038 ms clock, 0.37+0.15/0.81/1.8+0.15 ms cpu, 4->4->1 MB, 5 MB goal, 8 P To fix this, this commit moves the runtimeInitTime initialization to shortly after runtime.init, at which point nanotime is safe to use. This also requires changing the condition in newproc1 that currently uses runtimeInitTime != 0 simply to detect whether or not the main M has started. Since runtimeInitTime could genuinely be 0 now, this introduces a separate flag to newproc1. Fixes #21554. Change-Id: Id874a4b912d3fa3d22f58d01b31ffb3548266d3b Reviewed-on: https://go-review.googlesource.com/58690 Run-TryBot: Austin Clements <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rick Hudson <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 05ff6bf commit 9d17e17

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/runtime/proc.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ var main_init_done chan bool
9696
//go:linkname main_main main.main
9797
func main_main()
9898

99+
// mainStarted indicates that the main M has started.
100+
var mainStarted bool
101+
99102
// runtimeInitTime is the nanotime() at which the runtime started.
100103
var runtimeInitTime int64
101104

@@ -119,8 +122,8 @@ func main() {
119122
maxstacksize = 250000000
120123
}
121124

122-
// Record when the world started.
123-
runtimeInitTime = nanotime()
125+
// Allow newproc to start new Ms.
126+
mainStarted = true
124127

125128
systemstack(func() {
126129
newm(sysmon, nil)
@@ -148,6 +151,10 @@ func main() {
148151
}
149152
}()
150153

154+
// Record when the world started. Must be after runtime_init
155+
// because nanotime on some platforms depends on startNano.
156+
runtimeInitTime = nanotime()
157+
151158
gcenable()
152159

153160
main_init_done = make(chan bool)
@@ -3029,7 +3036,7 @@ func newproc1(fn *funcval, argp *uint8, narg int32, nret int32, callerpc uintptr
30293036
}
30303037
runqput(_p_, newg, true)
30313038

3032-
if atomic.Load(&sched.npidle) != 0 && atomic.Load(&sched.nmspinning) == 0 && runtimeInitTime != 0 {
3039+
if atomic.Load(&sched.npidle) != 0 && atomic.Load(&sched.nmspinning) == 0 && mainStarted {
30333040
wakep()
30343041
}
30353042
_g_.m.locks--

0 commit comments

Comments
 (0)