Skip to content

Commit

Permalink
change to emit create event in exitsyscall.
Browse files Browse the repository at this point in the history
  • Loading branch information
doujiang24 committed Jun 12, 2022
1 parent aaa997c commit 195d133
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 23 deletions.
9 changes: 9 additions & 0 deletions src/runtime/cgocall.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ func cgocallbackg(fn, frame unsafe.Pointer, ctxt uintptr) {
savedpc := gp.syscallpc
exitsyscall() // coming out of cgo call
gp.m.incgo = false
if gp.m.isextra {
gp.m.cgolevel++
}

osPreemptExtExit(gp.m)

Expand All @@ -237,6 +240,12 @@ func cgocallbackg(fn, frame unsafe.Pointer, ctxt uintptr) {
// This is enforced by checking incgo in the schedule function.

gp.m.incgo = true
if gp.m.isextra {
gp.m.cgolevel--
if gp.m.cgolevel < 0 {
throw("unexpected negative cgolevel")
}
}

if gp.m != checkm {
throw("m changed unexpectedly in cgocallbackg")
Expand Down
61 changes: 44 additions & 17 deletions src/runtime/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1917,11 +1917,6 @@ func oneNewExtraM() {
if raceenabled {
gp.racectx = racegostart(abi.FuncPCABIInternal(newextram) + sys.PCQuantum)
}
if trace.enabled {
traceGoCreate(gp, 0) // no start pc
gp.traceseq++
traceEvent(traceEvGoInSyscall, -1, uint64(gp.goid))
}
// put on allg for garbage collector
allgadd(gp)

Expand Down Expand Up @@ -2515,9 +2510,13 @@ func execute(gp *g, inheritTime bool) {
}

if trace.enabled {
// GoSysExit has to happen when we have a P, but before GoStart.
// So we emit it here.
if gp.syscallsp != 0 && gp.sysblocktraced {
if gp.m.isextra && gp.m.cgolevel == 0 {
// GoCreate happen in needm, but there is no P.
// So we emit it here.
traceGoCreate(gp, 0) // no start pc for locked g in extra M
} else if gp.syscallsp != 0 && gp.sysblocktraced {
// GoSysExit has to happen when we have a P, but before GoStart.
// So we emit it here.
traceGoSysExit(gp.sysexitticks)
}
traceGoStart()
Expand Down Expand Up @@ -3615,7 +3614,14 @@ func reentersyscall(pc, sp uintptr) {
}

if trace.enabled {
systemstack(traceGoSysCall)
if _g_.m.isextra && _g_.m.cgolevel == 0 {
systemstack(func() {
traceGoEnd()
traceProcStop(_g_.m.p.ptr())
})
} else {
systemstack(traceGoSysCall)
}
// systemstack itself clobbers g.sched.{pc,sp} and we might
// need them later when the G is genuinely blocked in a
// syscall
Expand All @@ -3639,10 +3645,19 @@ func reentersyscall(pc, sp uintptr) {
pp.m = 0
_g_.m.oldp.set(pp)
_g_.m.p = 0
atomic.Store(&pp.status, _Psyscall)
if sched.gcwaiting != 0 {
systemstack(entersyscall_gcwait)

if _g_.m.isextra && _g_.m.cgolevel == 0 {
atomic.Store(&pp.status, _Pidle)
systemstack(func() {
handoffp(pp)
})
save(pc, sp)
} else {
atomic.Store(&pp.status, _Psyscall)
if sched.gcwaiting != 0 {
systemstack(entersyscall_gcwait)
save(pc, sp)
}
}

_g_.m.locks--
Expand Down Expand Up @@ -3851,7 +3866,13 @@ func exitsyscallfast(oldp *p) bool {
osyield()
}
}
traceGoSysExit(0)
if _g_.m.isextra && _g_.m.cgolevel == 0 {
// GoCreate happen in needm, but there is no P.
// So we emit it here.
traceGoCreate(_g_, 0) // no start pc for locked g in extra M
} else {
traceGoSysExit(0)
}
}
})
if ok {
Expand All @@ -3874,10 +3895,16 @@ func exitsyscallfast_reacquired() {
// traceGoSysBlock for this syscall was already emitted,
// but here we effectively retake the p from the new syscall running on the same p.
systemstack(func() {
// Denote blocking of the new syscall.
traceGoSysBlock(_g_.m.p.ptr())
// Denote completion of the current syscall.
traceGoSysExit(0)
if _g_.m.isextra && _g_.m.cgolevel == 0 {
// GoCreate happen in needm, but there is no P.
// So we emit it here.
traceGoCreate(_g_, 0) // no start pc for locked g in extra M
} else {
// Denote blocking of the new syscall.
traceGoSysBlock(_g_.m.p.ptr())
// Denote completion of the current syscall.
traceGoSysExit(0)
}
})
}
_g_.m.p.ptr().syscalltick++
Expand Down
1 change: 1 addition & 0 deletions src/runtime/runtime2.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ type m struct {
printlock int8
incgo bool // m is executing a cgo call
isextra bool // m is an extra m
cgolevel int32 // level of cgo call
freeWait uint32 // if == 0, safe to free g0 and delete m (atomic)
fastrand uint64
needextram bool
Expand Down
7 changes: 1 addition & 6 deletions src/runtime/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,12 @@ func StartTrace() error {
// World is stopped, no need to lock.
forEachGRace(func(gp *g) {
status := readgstatus(gp)
if status != _Gdead || (gp.m != nil && gp.m.isextra) {
if status != _Gdead {
gp.traceseq = 0
gp.tracelastp = getg().m.p
// +PCQuantum because traceFrameForPC expects return PCs and subtracts PCQuantum.
id := trace.stackTab.put([]uintptr{startPCforTrace(gp.startpc) + sys.PCQuantum})
traceEvent(traceEvGoCreate, -1, uint64(gp.goid), uint64(id), stackID)

if status == _Gdead {
gp.traceseq++
traceEvent(traceEvGoInSyscall, -1, uint64(gp.goid))
}
}
if status == _Gwaiting {
// traceEvGoWaiting is implied to have seq=1.
Expand Down

0 comments on commit 195d133

Please sign in to comment.