2016-03-01 22:57:46 +00:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
2015-12-21 10:29:21 -05:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"runtime"
|
runtime: fix goroutine priority elevation
Currently it's possible for user code to exploit the high scheduler
priority of the GC worker in conjunction with the runnext optimization
to elevate a user goroutine to high priority so it will always run
even if there are other runnable goroutines.
For example, if a goroutine is in a tight allocation loop, the
following can happen:
1. Goroutine 1 allocates, triggering a GC.
2. G 1 attempts an assist, but fails and blocks.
3. The scheduler runs the GC worker, since it is high priority.
Note that this also starts a new scheduler quantum.
4. The GC worker does enough work to satisfy the assist.
5. The GC worker readies G 1, putting it in runnext.
6. GC finishes and the scheduler runs G 1 from runnext, giving it
the rest of the GC worker's quantum.
7. Go to 1.
Even if there are other goroutines on the run queue, they never get a
chance to run in the above sequence. This requires a confluence of
circumstances that make it unlikely, though not impossible, that it
would happen in "real" code. In the test added by this commit, we
force this confluence by setting GOMAXPROCS to 1 and GOGC to 1 so it's
easy for the test to repeated trigger GC and wake from a blocked
assist.
We fix this by making GC always put user goroutines at the end of the
run queue, instead of in runnext. This makes it so user code can't
piggy-back on the GC's high priority to make a user goroutine act like
it has high priority. The only other situation where GC wakes user
goroutines is waking all blocked assists at the end, but this uses the
global run queue and hence doesn't have this problem.
Fixes #15706.
Change-Id: I1589dee4b7b7d0c9c8575ed3472226084dfce8bc
Reviewed-on: https://go-review.googlesource.com/23172
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-05-17 18:46:03 -04:00
|
|
|
"runtime/debug"
|
|
|
|
|
"sync/atomic"
|
2015-12-21 10:29:21 -05:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
register("GCFairness", GCFairness)
|
runtime: fix goroutine priority elevation
Currently it's possible for user code to exploit the high scheduler
priority of the GC worker in conjunction with the runnext optimization
to elevate a user goroutine to high priority so it will always run
even if there are other runnable goroutines.
For example, if a goroutine is in a tight allocation loop, the
following can happen:
1. Goroutine 1 allocates, triggering a GC.
2. G 1 attempts an assist, but fails and blocks.
3. The scheduler runs the GC worker, since it is high priority.
Note that this also starts a new scheduler quantum.
4. The GC worker does enough work to satisfy the assist.
5. The GC worker readies G 1, putting it in runnext.
6. GC finishes and the scheduler runs G 1 from runnext, giving it
the rest of the GC worker's quantum.
7. Go to 1.
Even if there are other goroutines on the run queue, they never get a
chance to run in the above sequence. This requires a confluence of
circumstances that make it unlikely, though not impossible, that it
would happen in "real" code. In the test added by this commit, we
force this confluence by setting GOMAXPROCS to 1 and GOGC to 1 so it's
easy for the test to repeated trigger GC and wake from a blocked
assist.
We fix this by making GC always put user goroutines at the end of the
run queue, instead of in runnext. This makes it so user code can't
piggy-back on the GC's high priority to make a user goroutine act like
it has high priority. The only other situation where GC wakes user
goroutines is waking all blocked assists at the end, but this uses the
global run queue and hence doesn't have this problem.
Fixes #15706.
Change-Id: I1589dee4b7b7d0c9c8575ed3472226084dfce8bc
Reviewed-on: https://go-review.googlesource.com/23172
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-05-17 18:46:03 -04:00
|
|
|
register("GCFairness2", GCFairness2)
|
2015-12-21 10:29:21 -05:00
|
|
|
register("GCSys", GCSys)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GCSys() {
|
|
|
|
|
runtime.GOMAXPROCS(1)
|
|
|
|
|
memstats := new(runtime.MemStats)
|
|
|
|
|
runtime.GC()
|
|
|
|
|
runtime.ReadMemStats(memstats)
|
|
|
|
|
sys := memstats.Sys
|
|
|
|
|
|
|
|
|
|
runtime.MemProfileRate = 0 // disable profiler
|
|
|
|
|
|
|
|
|
|
itercount := 100000
|
|
|
|
|
for i := 0; i < itercount; i++ {
|
|
|
|
|
workthegc()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Should only be using a few MB.
|
|
|
|
|
// We allocated 100 MB or (if not short) 1 GB.
|
|
|
|
|
runtime.ReadMemStats(memstats)
|
|
|
|
|
if sys > memstats.Sys {
|
|
|
|
|
sys = 0
|
|
|
|
|
} else {
|
|
|
|
|
sys = memstats.Sys - sys
|
|
|
|
|
}
|
|
|
|
|
if sys > 16<<20 {
|
|
|
|
|
fmt.Printf("using too much memory: %d bytes\n", sys)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Printf("OK\n")
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-06 22:57:35 -07:00
|
|
|
var sink []byte
|
|
|
|
|
|
2015-12-21 10:29:21 -05:00
|
|
|
func workthegc() []byte {
|
2018-07-06 22:57:35 -07:00
|
|
|
sink = make([]byte, 1029)
|
|
|
|
|
return sink
|
2015-12-21 10:29:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GCFairness() {
|
|
|
|
|
runtime.GOMAXPROCS(1)
|
|
|
|
|
f, err := os.Open("/dev/null")
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
// This test tests what it is intended to test only if writes are fast.
|
|
|
|
|
// If there is no /dev/null, we just don't execute the test.
|
|
|
|
|
fmt.Println("OK")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
|
go func() {
|
|
|
|
|
for {
|
|
|
|
|
f.Write([]byte("."))
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
fmt.Println("OK")
|
|
|
|
|
}
|
runtime: fix goroutine priority elevation
Currently it's possible for user code to exploit the high scheduler
priority of the GC worker in conjunction with the runnext optimization
to elevate a user goroutine to high priority so it will always run
even if there are other runnable goroutines.
For example, if a goroutine is in a tight allocation loop, the
following can happen:
1. Goroutine 1 allocates, triggering a GC.
2. G 1 attempts an assist, but fails and blocks.
3. The scheduler runs the GC worker, since it is high priority.
Note that this also starts a new scheduler quantum.
4. The GC worker does enough work to satisfy the assist.
5. The GC worker readies G 1, putting it in runnext.
6. GC finishes and the scheduler runs G 1 from runnext, giving it
the rest of the GC worker's quantum.
7. Go to 1.
Even if there are other goroutines on the run queue, they never get a
chance to run in the above sequence. This requires a confluence of
circumstances that make it unlikely, though not impossible, that it
would happen in "real" code. In the test added by this commit, we
force this confluence by setting GOMAXPROCS to 1 and GOGC to 1 so it's
easy for the test to repeated trigger GC and wake from a blocked
assist.
We fix this by making GC always put user goroutines at the end of the
run queue, instead of in runnext. This makes it so user code can't
piggy-back on the GC's high priority to make a user goroutine act like
it has high priority. The only other situation where GC wakes user
goroutines is waking all blocked assists at the end, but this uses the
global run queue and hence doesn't have this problem.
Fixes #15706.
Change-Id: I1589dee4b7b7d0c9c8575ed3472226084dfce8bc
Reviewed-on: https://go-review.googlesource.com/23172
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-05-17 18:46:03 -04:00
|
|
|
|
|
|
|
|
func GCFairness2() {
|
|
|
|
|
// Make sure user code can't exploit the GC's high priority
|
|
|
|
|
// scheduling to make scheduling of user code unfair. See
|
|
|
|
|
// issue #15706.
|
|
|
|
|
runtime.GOMAXPROCS(1)
|
|
|
|
|
debug.SetGCPercent(1)
|
|
|
|
|
var count [3]int64
|
|
|
|
|
var sink [3]interface{}
|
|
|
|
|
for i := range count {
|
|
|
|
|
go func(i int) {
|
|
|
|
|
for {
|
|
|
|
|
sink[i] = make([]byte, 1024)
|
|
|
|
|
atomic.AddInt64(&count[i], 1)
|
|
|
|
|
}
|
|
|
|
|
}(i)
|
|
|
|
|
}
|
|
|
|
|
// Note: If the unfairness is really bad, it may not even get
|
|
|
|
|
// past the sleep.
|
|
|
|
|
//
|
|
|
|
|
// If the scheduling rules change, this may not be enough time
|
|
|
|
|
// to let all goroutines run, but for now we cycle through
|
|
|
|
|
// them rapidly.
|
2016-11-02 16:18:22 -04:00
|
|
|
//
|
|
|
|
|
// OpenBSD's scheduler makes every usleep() take at least
|
|
|
|
|
// 20ms, so we need a long time to ensure all goroutines have
|
|
|
|
|
// run. If they haven't run after 30ms, give it another 1000ms
|
|
|
|
|
// and check again.
|
runtime: fix goroutine priority elevation
Currently it's possible for user code to exploit the high scheduler
priority of the GC worker in conjunction with the runnext optimization
to elevate a user goroutine to high priority so it will always run
even if there are other runnable goroutines.
For example, if a goroutine is in a tight allocation loop, the
following can happen:
1. Goroutine 1 allocates, triggering a GC.
2. G 1 attempts an assist, but fails and blocks.
3. The scheduler runs the GC worker, since it is high priority.
Note that this also starts a new scheduler quantum.
4. The GC worker does enough work to satisfy the assist.
5. The GC worker readies G 1, putting it in runnext.
6. GC finishes and the scheduler runs G 1 from runnext, giving it
the rest of the GC worker's quantum.
7. Go to 1.
Even if there are other goroutines on the run queue, they never get a
chance to run in the above sequence. This requires a confluence of
circumstances that make it unlikely, though not impossible, that it
would happen in "real" code. In the test added by this commit, we
force this confluence by setting GOMAXPROCS to 1 and GOGC to 1 so it's
easy for the test to repeated trigger GC and wake from a blocked
assist.
We fix this by making GC always put user goroutines at the end of the
run queue, instead of in runnext. This makes it so user code can't
piggy-back on the GC's high priority to make a user goroutine act like
it has high priority. The only other situation where GC wakes user
goroutines is waking all blocked assists at the end, but this uses the
global run queue and hence doesn't have this problem.
Fixes #15706.
Change-Id: I1589dee4b7b7d0c9c8575ed3472226084dfce8bc
Reviewed-on: https://go-review.googlesource.com/23172
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-05-17 18:46:03 -04:00
|
|
|
time.Sleep(30 * time.Millisecond)
|
2016-11-02 16:18:22 -04:00
|
|
|
var fail bool
|
runtime: fix goroutine priority elevation
Currently it's possible for user code to exploit the high scheduler
priority of the GC worker in conjunction with the runnext optimization
to elevate a user goroutine to high priority so it will always run
even if there are other runnable goroutines.
For example, if a goroutine is in a tight allocation loop, the
following can happen:
1. Goroutine 1 allocates, triggering a GC.
2. G 1 attempts an assist, but fails and blocks.
3. The scheduler runs the GC worker, since it is high priority.
Note that this also starts a new scheduler quantum.
4. The GC worker does enough work to satisfy the assist.
5. The GC worker readies G 1, putting it in runnext.
6. GC finishes and the scheduler runs G 1 from runnext, giving it
the rest of the GC worker's quantum.
7. Go to 1.
Even if there are other goroutines on the run queue, they never get a
chance to run in the above sequence. This requires a confluence of
circumstances that make it unlikely, though not impossible, that it
would happen in "real" code. In the test added by this commit, we
force this confluence by setting GOMAXPROCS to 1 and GOGC to 1 so it's
easy for the test to repeated trigger GC and wake from a blocked
assist.
We fix this by making GC always put user goroutines at the end of the
run queue, instead of in runnext. This makes it so user code can't
piggy-back on the GC's high priority to make a user goroutine act like
it has high priority. The only other situation where GC wakes user
goroutines is waking all blocked assists at the end, but this uses the
global run queue and hence doesn't have this problem.
Fixes #15706.
Change-Id: I1589dee4b7b7d0c9c8575ed3472226084dfce8bc
Reviewed-on: https://go-review.googlesource.com/23172
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-05-17 18:46:03 -04:00
|
|
|
for i := range count {
|
|
|
|
|
if atomic.LoadInt64(&count[i]) == 0 {
|
2016-11-02 16:18:22 -04:00
|
|
|
fail = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if fail {
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
for i := range count {
|
|
|
|
|
if atomic.LoadInt64(&count[i]) == 0 {
|
|
|
|
|
fmt.Printf("goroutine %d did not run\n", i)
|
|
|
|
|
return
|
|
|
|
|
}
|
runtime: fix goroutine priority elevation
Currently it's possible for user code to exploit the high scheduler
priority of the GC worker in conjunction with the runnext optimization
to elevate a user goroutine to high priority so it will always run
even if there are other runnable goroutines.
For example, if a goroutine is in a tight allocation loop, the
following can happen:
1. Goroutine 1 allocates, triggering a GC.
2. G 1 attempts an assist, but fails and blocks.
3. The scheduler runs the GC worker, since it is high priority.
Note that this also starts a new scheduler quantum.
4. The GC worker does enough work to satisfy the assist.
5. The GC worker readies G 1, putting it in runnext.
6. GC finishes and the scheduler runs G 1 from runnext, giving it
the rest of the GC worker's quantum.
7. Go to 1.
Even if there are other goroutines on the run queue, they never get a
chance to run in the above sequence. This requires a confluence of
circumstances that make it unlikely, though not impossible, that it
would happen in "real" code. In the test added by this commit, we
force this confluence by setting GOMAXPROCS to 1 and GOGC to 1 so it's
easy for the test to repeated trigger GC and wake from a blocked
assist.
We fix this by making GC always put user goroutines at the end of the
run queue, instead of in runnext. This makes it so user code can't
piggy-back on the GC's high priority to make a user goroutine act like
it has high priority. The only other situation where GC wakes user
goroutines is waking all blocked assists at the end, but this uses the
global run queue and hence doesn't have this problem.
Fixes #15706.
Change-Id: I1589dee4b7b7d0c9c8575ed3472226084dfce8bc
Reviewed-on: https://go-review.googlesource.com/23172
Reviewed-by: Rick Hudson <rlh@golang.org>
2016-05-17 18:46:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("OK")
|
|
|
|
|
}
|