summaryrefslogtreecommitdiffhomepage
path: root/pkg/syncevent/waiter_test.go
blob: 3c8cbcdd8aec0e763c296cbe6457091434460f2a (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
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
// 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 (
	"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(&notifiedEvent, 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(&notifiedEvent) == 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, 0)
	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, 0)

	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, 0)
		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, 0)
		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], i)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		ws[0].Assert()
		if id, _ := s.Fetch(true); id != 0 {
			b.Fatalf("Fetch: got %d, wanted 0", id)
		}
	}
}

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")
		}
	}
}

// BenchmarkXxxNotifyAsyncWaitAck measures how long it takes to wait for an
// event while another goroutine signals the event. This assumes that a new
// goroutine doesn't run immediately (i.e. the creator of a new goroutine is
// allowed to go to sleep before the new goroutine has a chance to run).

func BenchmarkWaiterNotifyAsyncWaitAck(b *testing.B) {
	var w Waiter
	w.Init()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		go func() {
			w.Notify(1)
		}()
		w.Wait()
		w.Ack(evBench)
	}
}

func BenchmarkSleeperNotifyAsyncWaitAck(b *testing.B) {
	var s sleep.Sleeper
	var w sleep.Waker
	s.AddWaker(&w, 0)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		go func() {
			w.Assert()
		}()
		s.Fetch(true)
	}
}

func BenchmarkChannelNotifyAsyncWaitAck(b *testing.B) {
	ch := make(chan struct{}, 1)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		go func() {
			select {
			case ch <- struct{}{}:
			default:
			}
		}()
		<-ch
	}
}

// BenchmarkXxxNotifyAsyncWaitMultiAck is equivalent to NotifyAsyncWaitAck, but
// allows for multiple event sources.

func BenchmarkWaiterNotifyAsyncWaitMultiAck(b *testing.B) {
	var w Waiter
	w.Init()

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		go func() {
			w.Notify(evBench)
		}()
		if e := w.Wait(); e != evBench {
			b.Fatalf("Wait: got %#x, wanted %#x", e, evBench)
		}
		w.Ack(evBench)
	}
}

func BenchmarkSleeperNotifyAsyncWaitMultiAck(b *testing.B) {
	var s sleep.Sleeper
	var ws [3]sleep.Waker
	for i := range ws {
		s.AddWaker(&ws[i], i)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		go func() {
			ws[0].Assert()
		}()
		if id, _ := s.Fetch(true); id != 0 {
			b.Fatalf("Fetch: got %d, expected 0", id)
		}
	}
}

func BenchmarkChannelNotifyAsyncWaitMultiAck(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++ {
		go func() {
			select {
			case ch0 <- struct{}{}:
			default:
			}
		}()

		select {
		case <-ch0:
			// ok
		case <-ch1:
			b.Fatalf("received from ch1")
		case <-ch2:
			b.Fatalf("received from ch2")
		}
	}
}