forked from cespare/percpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpercpu_test.go
187 lines (180 loc) · 3.9 KB
/
percpu_test.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package percpu
import (
"runtime"
"sync"
"sync/atomic"
"testing"
)
func TestValues(t *testing.T) {
numProcs := runtime.GOMAXPROCS(0)
if numProcs > runtime.NumCPU() {
t.Skip("unreliable with high GOMAXPROCS")
}
// shard -> #goroutines for which the shard is the most frequently seen
freqCounts := make(map[*int]int)
// The short timing makes this a little flaky (a goroutine can just
// finish all its work before another one has started, and reuse the
// same shard). But we should get a successful attempt pretty quickly.
attemptLoop:
for attempt := 0; attempt < 10; attempt++ {
for k := range freqCounts {
delete(freqCounts, k)
}
start := make(chan struct{})
var wg sync.WaitGroup
var mu sync.Mutex
var vs Values[int]
for i := 0; i < numProcs; i++ {
wg.Add(1)
go func() {
defer wg.Done()
seen := make(map[*int]int)
<-start
for i := 0; i < 1e6*(attempt+1); i++ {
p := vs.Get()
seen[p]++
}
var pmax *int
max := 0
for p, n := range seen {
if n > max {
pmax = p
max = n
}
}
mu.Lock()
freqCounts[pmax]++
mu.Unlock()
}()
}
close(start)
wg.Wait()
for _, n := range freqCounts {
if n != 1 {
continue attemptLoop
}
}
return
}
t.Fatalf("shards were not handed out evenly to goroutines: %v", freqCounts)
}
// Confirm that getProcID runs and returns the full set of values
// in [0, GOMAXPROCS) as a quick sanity check.
func TestGetProcID(t *testing.T) {
numProcs := runtime.GOMAXPROCS(0)
if numProcs > runtime.NumCPU() {
t.Skip("unreliable with high GOMAXPROCS")
}
seen := make([]int64, numProcs)
start := make(chan struct{})
var wg sync.WaitGroup
for range seen {
wg.Add(1)
go func() {
defer wg.Done()
<-start
for i := 0; i < 1e6; i++ {
atomic.AddInt64(&seen[getProcID()], 1)
}
}()
}
close(start)
wg.Wait()
for i, n := range seen {
if n == 0 {
t.Fatalf("did not see proc id %d (got: %v)", i, seen)
}
}
}
func TestGrowShards(t *testing.T) {
numProcs := runtime.GOMAXPROCS(0)
if numProcs > runtime.NumCPU() {
t.Skip("unreliable with high GOMAXPROCS")
}
if numProcs < 2 {
t.Skip("need initial GOMAXPROCS as least 2")
}
// shard -> #goroutines for which the shard is the most frequently seen
freqCounts := make(map[*int]int)
// The short timing makes this a little flaky (a goroutine can just
// finish all its work before another one has started, and reuse the
// same shard). But we should get a successful attempt pretty quickly.
attemptLoop:
for attempt := 0; attempt < 10; attempt++ {
for k := range freqCounts {
delete(freqCounts, k)
}
var wg sync.WaitGroup
var mu sync.Mutex
runtime.GOMAXPROCS(numProcs / 2)
var vs Values[int]
start := make(chan struct{})
for i := 0; i < numProcs/2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
seen := make(map[*int]int)
<-start
for i := 0; i < 1e6*(attempt+1); i++ {
p := vs.Get()
seen[p]++
}
var pmax *int
max := 0
for p, n := range seen {
if n > max {
pmax = p
max = n
}
}
mu.Lock()
freqCounts[pmax]++
mu.Unlock()
}()
}
close(start)
wg.Wait()
runtime.GOMAXPROCS(numProcs)
for _, n := range freqCounts {
if n != 1 {
continue attemptLoop
}
}
for k := range freqCounts {
delete(freqCounts, k)
}
start = make(chan struct{})
for i := 0; i < numProcs; i++ {
wg.Add(1)
go func() {
defer wg.Done()
seen := make(map[*int]int)
<-start
for i := 0; i < 1e6*(attempt+1); i++ {
p := vs.Get()
seen[p]++
}
var pmax *int
max := 0
for p, n := range seen {
if n > max {
pmax = p
max = n
}
}
mu.Lock()
freqCounts[pmax]++
mu.Unlock()
}()
}
close(start)
wg.Wait()
for _, n := range freqCounts {
if n != 1 {
continue attemptLoop
}
}
return
}
t.Fatalf("shards were not handed out evenly to goroutines: %v", freqCounts)
}