Skip to content

Commit

Permalink
more fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
doujiang24 committed Jun 22, 2022
1 parent b9e7765 commit b93187b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/runtime/cgocall.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ func cgocallbackg(fn, frame unsafe.Pointer, ctxt uintptr) {
exitsyscall() // coming out of cgo call

if gp.m.isextra && gp.m.cgolevel == 0 {
// need a new goid, since the goroutine is reused from dead status.
gp.m.curg.goid = newgoid(gp.m.p.ptr())

if trace.enabled {
Expand Down
16 changes: 13 additions & 3 deletions src/runtime/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3611,8 +3611,11 @@ func reentersyscall(pc, sp uintptr) {
})
}

// the goroutine is finished when it's the locked g from extra M,
// and it's returning back to c thread.
backToCThread := _g_.m.isextra && _g_.m.cgolevel == 0
if trace.enabled {
if _g_.m.isextra && _g_.m.cgolevel == 0 {
if backToCThread {
systemstack(func() {
traceGoEnd()
traceProcStop(_g_.m.p.ptr())
Expand Down Expand Up @@ -3643,7 +3646,14 @@ func reentersyscall(pc, sp uintptr) {
pp.m = 0
_g_.m.p = 0

if _g_.m.isextra && _g_.m.cgolevel == 0 {
if backToCThread {
// For a real syscall, we can assume it will back to go in a very short time,
// at least in most cases. So, keep the P in _Psyscall may be a better choice.
// But in the c to go scene, we can not assume there will be a short time
// for the next c to go call, to reuse the oldp in exitsyscall.
// And the Psyscall status P can only be retake by sysmon after 20us ~ 10ms,
// means wasting P.
// So, it's better to handoff the P here.
atomic.Store(&pp.status, _Pidle)
systemstack(func() {
handoffp(pp)
Expand All @@ -3668,7 +3678,7 @@ func reentersyscall(pc, sp uintptr) {
//go:nosplit
//go:linkname entersyscall
func entersyscall() {
reentersyscall(getcallerpc(), getcallersp(), false)
reentersyscall(getcallerpc(), getcallersp())
}

func entersyscall_sysmon() {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func StartTrace() error {
if status == _Gsyscall {
gp.traceseq++
traceEvent(traceEvGoInSyscall, -1, uint64(gp.goid))
} else if gp.m == nil || !gp.m.isextra {
} else {
gp.sysblocktraced = false
}
})
Expand Down

0 comments on commit b93187b

Please sign in to comment.