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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package syncevent
import (
"fmt"
"sync/atomic"
"testing"
"time"
"gvisor.dev/gvisor/pkg/sleep"
"gvisor.dev/gvisor/pkg/sync"
)
func TestWaiterAlreadyPending(t *testing.T) {
var w Waiter
w.Init()
want := Set(1)
w.Notify(want)
if got := w.Wait(); got != want {
t.Errorf("Waiter.Wait: got %#x, wanted %#x", got, want)
}
}
func TestWaiterAsyncNotify(t *testing.T) {
var w Waiter
w.Init()
want := Set(1)
go func() {
time.Sleep(100 * time.Millisecond)
w.Notify(want)
}()
if got := w.Wait(); got != want {
t.Errorf("Waiter.Wait: got %#x, wanted %#x", got, want)
}
}
func TestWaiterWaitFor(t *testing.T) {
var w Waiter
w.Init()
evWaited := Set(1)
evOther := Set(2)
w.Notify(evOther)
notifiedEvent := uint32(0)
go func() {
time.Sleep(100 * time.Millisecond)
atomic.StoreUint32(¬ifiedEvent, 1)
w.Notify(evWaited)
}()
if got, want := w.WaitFor(evWaited), evWaited|evOther; got != want {
t.Errorf("Waiter.WaitFor: got %#x, wanted %#x", got, want)
}
if atomic.LoadUint32(¬ifiedEvent) == 0 {
t.Errorf("Waiter.WaitFor returned before goroutine notified waited-for event")
}
}
func TestWaiterWaitAndAckAll(t *testing.T) {
var w Waiter
w.Init()
w.Notify(AllEvents)
if got := w.WaitAndAckAll(); got != AllEvents {
t.Errorf("Waiter.WaitAndAckAll: got %#x, wanted %#x", got, AllEvents)
}
if got := w.Pending(); got != NoEvents {
t.Errorf("Waiter.WaitAndAckAll did not ack all events: got %#x, wanted 0", got)
}
}
// BenchmarkWaiterX, BenchmarkSleeperX, and BenchmarkChannelX benchmark usage
// pattern X (described in terms of Waiter) with Waiter, sleep.Sleeper, and
// buffered chan struct{} respectively. When the maximum number of event
// sources is relevant, we use 3 event sources because this is representative
// of the kernel.Task.block() use case: an interrupt source, a timeout source,
// and the actual event source being waited on.
// Event set used by most benchmarks.
const evBench Set = 1
// BenchmarkXxxNotifyRedundant measures how long it takes to notify a Waiter of
// an event that is already pending.
func BenchmarkWaiterNotifyRedundant(b *testing.B) {
var w Waiter
w.Init()
w.Notify(evBench)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.Notify(evBench)
}
}
func BenchmarkSleeperNotifyRedundant(b *testing.B) {
var s sleep.Sleeper
var w sleep.Waker
s.AddWaker(&w)
w.Assert()
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.Assert()
}
}
func BenchmarkChannelNotifyRedundant(b *testing.B) {
ch := make(chan struct{}, 1)
ch <- struct{}{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
select {
case ch <- struct{}{}:
default:
}
}
}
// BenchmarkXxxNotifyWaitAck measures how long it takes to notify a Waiter an
// event, return that event using a blocking check, and then unset the event as
// pending.
func BenchmarkWaiterNotifyWaitAck(b *testing.B) {
var w Waiter
w.Init()
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.Notify(evBench)
w.Wait()
w.Ack(evBench)
}
}
func BenchmarkSleeperNotifyWaitAck(b *testing.B) {
var s sleep.Sleeper
var w sleep.Waker
s.AddWaker(&w)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.Assert()
s.Fetch(true)
}
}
func BenchmarkChannelNotifyWaitAck(b *testing.B) {
ch := make(chan struct{}, 1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// notify
select {
case ch <- struct{}{}:
default:
}
// wait + ack
<-ch
}
}
// BenchmarkSleeperMultiNotifyWaitAck is equivalent to
// BenchmarkSleeperNotifyWaitAck, but also includes allocation of a
// temporary sleep.Waker. This is necessary when multiple goroutines may wait
// for the same event, since each sleep.Waker can wake only a single
// sleep.Sleeper.
//
// The syncevent package does not require a distinct object for each
// waiter-waker relationship, so BenchmarkWaiterNotifyWaitAck and
// BenchmarkWaiterMultiNotifyWaitAck would be identical. The analogous state
// for channels, runtime.sudog, is inescapably runtime-allocated, so
// BenchmarkChannelNotifyWaitAck and BenchmarkChannelMultiNotifyWaitAck would
// also be identical.
func BenchmarkSleeperMultiNotifyWaitAck(b *testing.B) {
var s sleep.Sleeper
// The sleep package doesn't provide sync.Pool allocation of Wakers;
// we do for a fairer comparison.
wakerPool := sync.Pool{
New: func() interface{} {
return &sleep.Waker{}
},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := wakerPool.Get().(*sleep.Waker)
s.AddWaker(w)
w.Assert()
s.Fetch(true)
s.Done()
wakerPool.Put(w)
}
}
// BenchmarkXxxTempNotifyWaitAck is equivalent to NotifyWaitAck, but also
// includes allocation of a temporary Waiter. This models the case where a
// goroutine not already associated with a Waiter needs one in order to block.
//
// The analogous state for channels is built into runtime.g, so
// BenchmarkChannelNotifyWaitAck and BenchmarkChannelTempNotifyWaitAck would be
// identical.
func BenchmarkWaiterTempNotifyWaitAck(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := GetWaiter()
w.Notify(evBench)
w.Wait()
w.Ack(evBench)
PutWaiter(w)
}
}
func BenchmarkSleeperTempNotifyWaitAck(b *testing.B) {
// The sleep package doesn't provide sync.Pool allocation of Sleepers;
// we do for a fairer comparison.
sleeperPool := sync.Pool{
New: func() interface{} {
return &sleep.Sleeper{}
},
}
var w sleep.Waker
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := sleeperPool.Get().(*sleep.Sleeper)
s.AddWaker(&w)
w.Assert()
s.Fetch(true)
s.Done()
sleeperPool.Put(s)
}
}
// BenchmarkXxxNotifyWaitMultiAck is equivalent to NotifyWaitAck, but allows
// for multiple event sources.
func BenchmarkWaiterNotifyWaitMultiAck(b *testing.B) {
var w Waiter
w.Init()
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.Notify(evBench)
if e := w.Wait(); e != evBench {
b.Fatalf("Wait: got %#x, wanted %#x", e, evBench)
}
w.Ack(evBench)
}
}
func BenchmarkSleeperNotifyWaitMultiAck(b *testing.B) {
var s sleep.Sleeper
var ws [3]sleep.Waker
for i := range ws {
s.AddWaker(&ws[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
ws[0].Assert()
if v := s.Fetch(true); v != &ws[0] {
b.Fatalf("Fetch: got %v, wanted %v", v, &ws[0])
}
}
}
func BenchmarkChannelNotifyWaitMultiAck(b *testing.B) {
ch0 := make(chan struct{}, 1)
ch1 := make(chan struct{}, 1)
ch2 := make(chan struct{}, 1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// notify
select {
case ch0 <- struct{}{}:
default:
}
// wait + clear
select {
case <-ch0:
// ok
case <-ch1:
b.Fatalf("received from ch1")
case <-ch2:
b.Fatalf("received from ch2")
}
}
}
// BenchmarkXxxPingPong exchanges control between two goroutines.
func BenchmarkWaiterPingPong(b *testing.B) {
var w1, w2 Waiter
w1.Init()
w2.Init()
var wg sync.WaitGroup
defer wg.Wait()
w1.Notify(evBench)
b.ResetTimer()
go func() {
for i := 0; i < b.N; i++ {
w1.Wait()
w1.Ack(evBench)
w2.Notify(evBench)
}
}()
for i := 0; i < b.N; i++ {
w2.Wait()
w2.Ack(evBench)
w1.Notify(evBench)
}
}
func BenchmarkSleeperPingPong(b *testing.B) {
var (
s1 sleep.Sleeper
w1 sleep.Waker
s2 sleep.Sleeper
w2 sleep.Waker
)
s1.AddWaker(&w1)
s2.AddWaker(&w2)
var wg sync.WaitGroup
defer wg.Wait()
w1.Assert()
wg.Add(1)
b.ResetTimer()
go func() {
defer wg.Done()
for i := 0; i < b.N; i++ {
s1.Fetch(true)
w2.Assert()
}
}()
for i := 0; i < b.N; i++ {
s2.Fetch(true)
w1.Assert()
}
}
func BenchmarkChannelPingPong(b *testing.B) {
ch1 := make(chan struct{}, 1)
ch2 := make(chan struct{}, 1)
var wg sync.WaitGroup
defer wg.Wait()
ch1 <- struct{}{}
wg.Add(1)
b.ResetTimer()
go func() {
defer wg.Done()
for i := 0; i < b.N; i++ {
<-ch1
ch2 <- struct{}{}
}
}()
for i := 0; i < b.N; i++ {
<-ch2
ch1 <- struct{}{}
}
}
// BenchmarkXxxPingPongMulti is equivalent to PingPong, but allows each
// goroutine to receive from multiple event sources (although only one is ever
// signaled).
func BenchmarkWaiterPingPongMulti(b *testing.B) {
var w1, w2 Waiter
w1.Init()
w2.Init()
var wg sync.WaitGroup
defer wg.Wait()
w1.Notify(evBench)
wg.Add(1)
b.ResetTimer()
go func() {
defer wg.Done()
for i := 0; i < b.N; i++ {
if e := w1.Wait(); e != evBench {
// b.Fatalf() can only be called from the main goroutine.
panic(fmt.Sprintf("Wait: got %#x, wanted %#x", e, evBench))
}
w1.Ack(evBench)
w2.Notify(evBench)
}
}()
for i := 0; i < b.N; i++ {
if e := w2.Wait(); e != evBench {
b.Fatalf("Wait: got %#x, wanted %#x", e, evBench)
}
w2.Ack(evBench)
w1.Notify(evBench)
}
}
func BenchmarkSleeperPingPongMulti(b *testing.B) {
var (
s1 sleep.Sleeper
w1, w1a, w1b sleep.Waker
s2 sleep.Sleeper
w2, w2a, w2b sleep.Waker
)
s1.AddWaker(&w1)
s1.AddWaker(&w1a)
s1.AddWaker(&w1b)
s2.AddWaker(&w2)
s2.AddWaker(&w2a)
s2.AddWaker(&w2b)
var wg sync.WaitGroup
defer wg.Wait()
w1.Assert()
wg.Add(1)
b.ResetTimer()
go func() {
defer wg.Done()
for i := 0; i < b.N; i++ {
if w := s1.Fetch(true); w != &w1 {
// b.Fatalf() can only be called from the main goroutine.
panic(fmt.Sprintf("Fetch: got %p, wanted %p", w, &w1))
}
w2.Assert()
}
}()
for i := 0; i < b.N; i++ {
if w := s2.Fetch(true); w != &w2 {
b.Fatalf("Fetch: got %p, wanted %p", w, &w2)
}
w1.Assert()
}
}
func BenchmarkChannelPingPongMulti(b *testing.B) {
ch1 := make(chan struct{}, 1)
ch1a := make(chan struct{}, 1)
ch1b := make(chan struct{}, 1)
ch2 := make(chan struct{}, 1)
ch2a := make(chan struct{}, 1)
ch2b := make(chan struct{}, 1)
var wg sync.WaitGroup
defer wg.Wait()
ch1 <- struct{}{}
wg.Add(1)
b.ResetTimer()
go func() {
defer wg.Done()
for i := 0; i < b.N; i++ {
select {
case <-ch1:
case <-ch1a:
panic("received from ch1a")
case <-ch1b:
panic("received from ch1a")
}
ch2 <- struct{}{}
}
}()
for i := 0; i < b.N; i++ {
select {
case <-ch2:
case <-ch2a:
panic("received from ch2a")
case <-ch2b:
panic("received from ch2a")
}
ch1 <- struct{}{}
}
}
|