mirror of https://github.com/golang/go.git
cgo/misc/test: burn CPU to improve sleep accuracy
Fixes #4008. Run a background goroutine that wastes CPU to trick the power management into raising the CPU frequency which, by side effect, makes sleep more accurate on arm. === RUN TestParallelSleep --- PASS: TestParallelSleep (1.30 seconds) _cgo_gotypes.go:772: sleep(1) slept for 1.000458s R=minux.ma, r CC=golang-dev https://golang.org/cl/6498060
This commit is contained in:
parent
11d75fb62e
commit
d073677569
|
|
@ -35,16 +35,35 @@ func BackgroundSleep(n int) {
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func testParallelSleep(t *testing.T) {
|
// wasteCPU starts a background goroutine to waste CPU
|
||||||
sleepSec := 1
|
// to cause the power management to raise the CPU frequency.
|
||||||
if runtime.GOARCH == "arm" {
|
// On ARM this has the side effect of making sleep more accurate.
|
||||||
// on ARM, the 1.3s deadline is frequently missed,
|
func wasteCPU() chan struct{} {
|
||||||
// so increase sleep time to 2s
|
done := make(chan struct{})
|
||||||
sleepSec = 2
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
default:
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
// pause for a short amount of time to allow the
|
||||||
|
// power management to recognise load has risen.
|
||||||
|
<-time.After(300 * time.Millisecond)
|
||||||
|
return done
|
||||||
|
}
|
||||||
|
|
||||||
|
func testParallelSleep(t *testing.T) {
|
||||||
|
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2))
|
||||||
|
defer close(wasteCPU())
|
||||||
|
|
||||||
|
sleepSec := 1
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
parallelSleep(sleepSec)
|
parallelSleep(sleepSec)
|
||||||
dt := time.Now().Sub(start)
|
dt := time.Since(start)
|
||||||
|
t.Logf("sleep(%d) slept for %v", sleepSec, dt)
|
||||||
// bug used to run sleeps in serial, producing a 2*sleepSec-second delay.
|
// bug used to run sleeps in serial, producing a 2*sleepSec-second delay.
|
||||||
if dt >= time.Duration(sleepSec)*1300*time.Millisecond {
|
if dt >= time.Duration(sleepSec)*1300*time.Millisecond {
|
||||||
t.Fatalf("parallel %d-second sleeps slept for %f seconds", sleepSec, dt.Seconds())
|
t.Fatalf("parallel %d-second sleeps slept for %f seconds", sleepSec, dt.Seconds())
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue