diff options
| author | Andrei Vagin <avagin@google.com> | 2019-03-29 10:43:31 -0700 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2019-04-09 07:45:26 +0000 |
| commit | 4166ff42c09cae4ca9e15154627e7cfc80586c65 (patch) | |
| tree | 135a22462d208dc313e3dd18369ae457362a6786 /src/runtime/proc.go | |
| parent | 08e1823a632783e3f71b358f2f546ab0f13a6d98 (diff) | |
| download | go-4166ff42c09cae4ca9e15154627e7cfc80586c65.tar.xz | |
runtime: preempt a goroutine which calls a lot of short system calls
A goroutine should be preempted if it runs for 10ms without blocking.
We found that this doesn't work for goroutines which call short system calls.
For example, the next program can stuck for seconds without this fix:
$ cat main.go
package main
import (
"runtime"
"syscall"
)
func main() {
runtime.GOMAXPROCS(1)
c := make(chan int)
go func() {
c <- 1
for {
t := syscall.Timespec{
Nsec: 300,
}
if true {
syscall.Nanosleep(&t, nil)
}
}
}()
<-c
}
$ time go run main.go
real 0m8.796s
user 0m0.367s
sys 0m0.893s
Updates #10958
Change-Id: Id3be54d3779cc28bfc8b33fe578f13778f1ae2a2
Reviewed-on: https://go-review.googlesource.com/c/go/+/170138
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
Run-TryBot: Dmitry Vyukov <dvyukov@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/runtime/proc.go')
| -rw-r--r-- | src/runtime/proc.go | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/src/runtime/proc.go b/src/runtime/proc.go index 6b5b3e2b2b..29763d328a 100644 --- a/src/runtime/proc.go +++ b/src/runtime/proc.go @@ -4380,10 +4380,24 @@ func retake(now int64) uint32 { } pd := &_p_.sysmontick s := _p_.status + sysretake := false + if s == _Prunning || s == _Psyscall { + // Preempt G if it's running for too long. + t := int64(_p_.schedtick) + if int64(pd.schedtick) != t { + pd.schedtick = uint32(t) + pd.schedwhen = now + } else if pd.schedwhen+forcePreemptNS <= now { + preemptone(_p_) + // In case of syscall, preemptone() doesn't + // work, because there is no M wired to P. + sysretake = true + } + } if s == _Psyscall { // Retake P from syscall if it's there for more than 1 sysmon tick (at least 20us). t := int64(_p_.syscalltick) - if int64(pd.syscalltick) != t { + if !sysretake && int64(pd.syscalltick) != t { pd.syscalltick = uint32(t) pd.syscallwhen = now continue @@ -4412,18 +4426,6 @@ func retake(now int64) uint32 { } incidlelocked(1) lock(&allpLock) - } else if s == _Prunning { - // Preempt G if it's running for too long. - t := int64(_p_.schedtick) - if int64(pd.schedtick) != t { - pd.schedtick = uint32(t) - pd.schedwhen = now - continue - } - if pd.schedwhen+forcePreemptNS > now { - continue - } - preemptone(_p_) } } unlock(&allpLock) |
