Skip to content

Commit 7bf9b52

Browse files
committed
Revert to add missing events.
1 parent 69830d4 commit 7bf9b52

File tree

5 files changed

+40
-89
lines changed

5 files changed

+40
-89
lines changed

src/cmd/trace/trace.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,8 @@ func (ctx *traceContext) buildBranch(parent frameNode, stk []*trace.Frame) int {
11361136
func isSystemGoroutine(entryFn string) bool {
11371137
// This mimics runtime.isSystemGoroutine as closely as
11381138
// possible.
1139-
return entryFn != "runtime.main" && strings.HasPrefix(entryFn, "runtime.")
1139+
// Also, locked g in extra M (with empty entryFn) is system goroutine.
1140+
return entryFn == "" || entryFn != "runtime.main" && strings.HasPrefix(entryFn, "runtime.")
11401141
}
11411142

11421143
// firstTimestamp returns the timestamp of the first event record.

src/runtime/cgocall.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,7 @@ func cgocallbackg(fn, frame unsafe.Pointer, ctxt uintptr) {
226226
savedsp := unsafe.Pointer(gp.syscallsp)
227227
savedpc := gp.syscallpc
228228
exitsyscall() // coming out of cgo call
229-
230-
if gp.m.isextra && gp.m.cgolevel == 0 {
231-
// need a new goid, since the goroutine is reused from dead status.
232-
gp.m.curg.goid = newgoid(gp.m.p.ptr())
233-
234-
if trace.enabled {
235-
// delay emit trace events here, after getting a P.
236-
systemstack(func() {
237-
traceGoCreate(gp, 0) // no start pc for locked g in extra M
238-
traceGoStart()
239-
})
240-
}
241-
}
242-
243229
gp.m.incgo = false
244-
gp.m.cgolevel++
245230

246231
osPreemptExtExit(gp.m)
247232

@@ -251,7 +236,6 @@ func cgocallbackg(fn, frame unsafe.Pointer, ctxt uintptr) {
251236
// The following code must not change to a different m.
252237
// This is enforced by checking incgo in the schedule function.
253238

254-
gp.m.cgolevel--
255239
gp.m.incgo = true
256240

257241
if gp.m != checkm {

src/runtime/proc.go

Lines changed: 29 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,9 +1913,15 @@ func oneNewExtraM() {
19131913
mp.lockedg.set(gp)
19141914
mp.isextra = true
19151915
gp.lockedm.set(mp)
1916+
gp.goid = int64(atomic.Xadd64(&sched.goidgen, 1))
19161917
if raceenabled {
19171918
gp.racectx = racegostart(abi.FuncPCABIInternal(newextram) + sys.PCQuantum)
19181919
}
1920+
if trace.enabled {
1921+
traceGoCreate(gp, 0) // no start pc
1922+
gp.traceseq++
1923+
traceEvent(traceEvGoInSyscall, -1, uint64(gp.goid))
1924+
}
19191925
// put on allg for garbage collector
19201926
allgadd(gp)
19211927

@@ -2509,15 +2515,12 @@ func execute(gp *g, inheritTime bool) {
25092515
}
25102516

25112517
if trace.enabled {
2512-
// delay emit trace events when entering go from c thread at the first level.
2513-
if !_g_.m.isextra || _g_.m.cgolevel != 0 {
2514-
if gp.syscallsp != 0 && gp.sysblocktraced {
2515-
// GoSysExit has to happen when we have a P, but before GoStart.
2516-
// So we emit it here.
2517-
traceGoSysExit(gp.sysexitticks)
2518-
}
2519-
traceGoStart()
2518+
// GoSysExit has to happen when we have a P, but before GoStart.
2519+
// So we emit it here.
2520+
if gp.syscallsp != 0 && gp.sysblocktraced {
2521+
traceGoSysExit(gp.sysexitticks)
25202522
}
2523+
traceGoStart()
25212524
}
25222525

25232526
gogo(&gp.sched)
@@ -3611,18 +3614,8 @@ func reentersyscall(pc, sp uintptr) {
36113614
})
36123615
}
36133616

3614-
// the goroutine is finished when it's the locked g from extra M,
3615-
// and it's returning back to c thread.
3616-
backToCThread := _g_.m.isextra && _g_.m.cgolevel == 0
36173617
if trace.enabled {
3618-
if backToCThread {
3619-
systemstack(func() {
3620-
traceGoEnd()
3621-
traceProcStop(_g_.m.p.ptr())
3622-
})
3623-
} else {
3624-
systemstack(traceGoSysCall)
3625-
}
3618+
systemstack(traceGoSysCall)
36263619
// systemstack itself clobbers g.sched.{pc,sp} and we might
36273620
// need them later when the G is genuinely blocked in a
36283621
// syscall
@@ -3644,28 +3637,12 @@ func reentersyscall(pc, sp uintptr) {
36443637
_g_.sysblocktraced = true
36453638
pp := _g_.m.p.ptr()
36463639
pp.m = 0
3640+
_g_.m.oldp.set(pp)
36473641
_g_.m.p = 0
3648-
3649-
if backToCThread {
3650-
// For a real syscall, we assume it will back to go after a very short time,
3651-
// it's reasonable in most cases. So, keep the P in _Psyscall is a better choice.
3652-
// But in the c to go scene, we can not assume there will be a short time
3653-
// for the next c to go call, to reuse the oldp in exitsyscall.
3654-
// And the Psyscall status P can only be retake by sysmon after 20us ~ 10ms,
3655-
// it means wasting P.
3656-
// So, it's better to handoffp here.
3657-
atomic.Store(&pp.status, _Pidle)
3658-
systemstack(func() {
3659-
handoffp(pp)
3660-
})
3642+
atomic.Store(&pp.status, _Psyscall)
3643+
if sched.gcwaiting != 0 {
3644+
systemstack(entersyscall_gcwait)
36613645
save(pc, sp)
3662-
} else {
3663-
_g_.m.oldp.set(pp)
3664-
atomic.Store(&pp.status, _Psyscall)
3665-
if sched.gcwaiting != 0 {
3666-
systemstack(entersyscall_gcwait)
3667-
save(pc, sp)
3668-
}
36693646
}
36703647

36713648
_g_.m.locks--
@@ -3784,11 +3761,8 @@ func exitsyscall() {
37843761
_g_.m.oldp = 0
37853762
if exitsyscallfast(oldp) {
37863763
if trace.enabled {
3787-
// delay emit trace events when entering go from c thread at the first level.
3788-
if !_g_.m.isextra || _g_.m.cgolevel != 0 {
3789-
if oldp != _g_.m.p.ptr() || _g_.m.syscalltick != _g_.m.p.ptr().syscalltick {
3790-
systemstack(traceGoStart)
3791-
}
3764+
if oldp != _g_.m.p.ptr() || _g_.m.syscalltick != _g_.m.p.ptr().syscalltick {
3765+
systemstack(traceGoStart)
37923766
}
37933767
}
37943768
// There's a cpu for us, so we can run.
@@ -3877,10 +3851,7 @@ func exitsyscallfast(oldp *p) bool {
38773851
osyield()
38783852
}
38793853
}
3880-
// delay emit trace events when entering go from c thread at the first level.
3881-
if !_g_.m.isextra || _g_.m.cgolevel != 0 {
3882-
traceGoSysExit(0)
3883-
}
3854+
traceGoSysExit(0)
38843855
}
38853856
})
38863857
if ok {
@@ -3897,10 +3868,6 @@ func exitsyscallfast(oldp *p) bool {
38973868
//go:nosplit
38983869
func exitsyscallfast_reacquired() {
38993870
_g_ := getg()
3900-
// there is no oldp when entering go from c thread at the first level.
3901-
if _g_.m.isextra && _g_.m.cgolevel == 0 {
3902-
panic("oldp should not existing")
3903-
}
39043871
if _g_.m.syscalltick != _g_.m.p.ptr().syscalltick {
39053872
if trace.enabled {
39063873
// The p was retaken and then enter into syscall again (since _g_.m.syscalltick has changed).
@@ -4103,20 +4070,6 @@ func newproc(fn *funcval) {
41034070
})
41044071
}
41054072

4106-
func newgoid(p *p) int64 {
4107-
if p.goidcache == p.goidcacheend {
4108-
// Sched.goidgen is the last allocated id,
4109-
// this batch must be [sched.goidgen+1, sched.goidgen+GoidCacheBatch].
4110-
// At startup sched.goidgen=0, so main goroutine receives goid=1.
4111-
p.goidcache = atomic.Xadd64(&sched.goidgen, _GoidCacheBatch)
4112-
p.goidcache -= _GoidCacheBatch - 1
4113-
p.goidcacheend = p.goidcache + _GoidCacheBatch
4114-
}
4115-
id := int64(p.goidcache)
4116-
p.goidcache++
4117-
return id
4118-
}
4119-
41204073
// Create a new g in state _Grunnable, starting at fn. callerpc is the
41214074
// address of the go statement that created this. The caller is responsible
41224075
// for adding the new g to the scheduler.
@@ -4180,7 +4133,16 @@ func newproc1(fn *funcval, callergp *g, callerpc uintptr) *g {
41804133
casgstatus(newg, _Gdead, _Grunnable)
41814134
gcController.addScannableStack(_p_, int64(newg.stack.hi-newg.stack.lo))
41824135

4183-
newg.goid = newgoid(_p_)
4136+
if _p_.goidcache == _p_.goidcacheend {
4137+
// Sched.goidgen is the last allocated id,
4138+
// this batch must be [sched.goidgen+1, sched.goidgen+GoidCacheBatch].
4139+
// At startup sched.goidgen=0, so main goroutine receives goid=1.
4140+
_p_.goidcache = atomic.Xadd64(&sched.goidgen, _GoidCacheBatch)
4141+
_p_.goidcache -= _GoidCacheBatch - 1
4142+
_p_.goidcacheend = _p_.goidcache + _GoidCacheBatch
4143+
}
4144+
newg.goid = int64(_p_.goidcache)
4145+
_p_.goidcache++
41844146
if raceenabled {
41854147
newg.racectx = racegostart(callerpc)
41864148
}

src/runtime/runtime2.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,12 @@ type m struct {
540540
printlock int8
541541
incgo bool // m is executing a cgo call
542542
isextra bool // m is an extra m
543-
cgolevel int32 // level of cgo(c to go) calls currently in progress
544543
freeWait uint32 // if == 0, safe to free g0 and delete m (atomic)
545544
fastrand uint64
546545
needextram bool
547546
traceback uint8
548-
ncgocall uint64 // number of cgo(go to c) calls in total
549-
ncgo int32 // number of cgo(go to c) calls currently in progress
547+
ncgocall uint64 // number of cgo calls in total
548+
ncgo int32 // number of cgo calls currently in progress
550549
cgoCallersUse uint32 // if non-zero, cgoCallers in use temporarily
551550
cgoCallers *cgoCallers // cgo traceback if crashing in cgo call
552551
park note

src/runtime/trace.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,17 @@ func StartTrace() error {
225225
// World is stopped, no need to lock.
226226
forEachGRace(func(gp *g) {
227227
status := readgstatus(gp)
228-
if status != _Gdead {
228+
if status != _Gdead || (gp.m != nil && gp.m.isextra) {
229229
gp.traceseq = 0
230230
gp.tracelastp = getg().m.p
231231
// +PCQuantum because traceFrameForPC expects return PCs and subtracts PCQuantum.
232232
id := trace.stackTab.put([]uintptr{startPCforTrace(gp.startpc) + sys.PCQuantum})
233233
traceEvent(traceEvGoCreate, -1, uint64(gp.goid), uint64(id), stackID)
234+
235+
if status == _Gdead {
236+
gp.traceseq++
237+
traceEvent(traceEvGoInSyscall, -1, uint64(gp.goid))
238+
}
234239
}
235240
if status == _Gwaiting {
236241
// traceEvGoWaiting is implied to have seq=1.
@@ -240,7 +245,7 @@ func StartTrace() error {
240245
if status == _Gsyscall {
241246
gp.traceseq++
242247
traceEvent(traceEvGoInSyscall, -1, uint64(gp.goid))
243-
} else {
248+
} else if gp.m == nil || !gp.m.isextra {
244249
gp.sysblocktraced = false
245250
}
246251
})

0 commit comments

Comments
 (0)