blob: 147ce94b58cb88718335903cf46c56d1c8266e05 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cgotest
/*
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
extern void BackgroundSleep(int);
void twoSleep(int);
*/
import "C"
import (
"runtime"
"testing"
"time"
)
var sleepDone = make(chan bool)
func parallelSleep(n int) {
C.twoSleep(C.int(n))
<-sleepDone
}
//export BackgroundSleep
func BackgroundSleep(n int32) {
go func() {
C.sleep(C.uint(n))
sleepDone <- true
}()
}
// wasteCPU starts a background goroutine to waste CPU
// to cause the power management to raise the CPU frequency.
// On ARM this has the side effect of making sleep more accurate.
func wasteCPU() chan struct{} {
done := make(chan struct{})
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) {
if runtime.GOARCH == "arm" {
// on ARM, the 1.3s deadline is frequently missed,
// and burning cpu seems to help
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2))
defer close(wasteCPU())
}
sleepSec := 1
start := time.Now()
parallelSleep(sleepSec)
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.
if dt >= time.Duration(sleepSec)*1300*time.Millisecond {
t.Fatalf("parallel %d-second sleeps slept for %f seconds", sleepSec, dt.Seconds())
}
}
|