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
|
// 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"
"math/rand"
"testing"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/waiter"
)
func TestBroadcasterFilter(t *testing.T) {
const numReceivers = 2 * MaxEvents
var br Broadcaster
ws := make([]Waiter, numReceivers)
for i := range ws {
ws[i].Init()
br.SubscribeEvents(ws[i].Receiver(), 1<<(i%MaxEvents))
}
for ev := 0; ev < MaxEvents; ev++ {
br.Broadcast(1 << ev)
for i := range ws {
want := NoEvents
if i%MaxEvents == ev {
want = 1 << ev
}
if got := ws[i].Receiver().PendingAndAckAll(); got != want {
t.Errorf("after Broadcast of event %d: waiter %d has pending event set %#x, wanted %#x", ev, i, got, want)
}
}
}
}
// TestBroadcasterManySubscriptions tests that subscriptions are not lost by
// table expansion/compaction.
func TestBroadcasterManySubscriptions(t *testing.T) {
const numReceivers = 5000 // arbitrary
var br Broadcaster
ws := make([]Waiter, numReceivers)
for i := range ws {
ws[i].Init()
}
ids := make([]SubscriptionID, numReceivers)
for i := 0; i < numReceivers; i++ {
// Subscribe receiver i.
ids[i] = br.SubscribeEvents(ws[i].Receiver(), 1)
// Check that receivers [0, i] are subscribed.
br.Broadcast(1)
for j := 0; j <= i; j++ {
if ws[j].Pending() != 1 {
t.Errorf("receiver %d did not receive an event after subscription of receiver %d", j, i)
}
ws[j].Ack(1)
}
}
// Generate a random order for unsubscriptions.
unsub := rand.Perm(numReceivers)
for i := 0; i < numReceivers; i++ {
// Unsubscribe receiver unsub[i].
br.UnsubscribeEvents(ids[unsub[i]])
// Check that receivers [unsub[0], unsub[i]] are not subscribed, and that
// receivers (unsub[i], unsub[numReceivers]) are still subscribed.
br.Broadcast(1)
for j := 0; j <= i; j++ {
if ws[unsub[j]].Pending() != 0 {
t.Errorf("unsub iteration %d: receiver %d received an event after unsubscription of receiver %d", i, unsub[j], unsub[i])
}
}
for j := i + 1; j < numReceivers; j++ {
if ws[unsub[j]].Pending() != 1 {
t.Errorf("unsub iteration %d: receiver %d did not receive an event after unsubscription of receiver %d", i, unsub[j], unsub[i])
}
ws[unsub[j]].Ack(1)
}
}
}
var (
receiverCountsNonZero = []int{1, 4, 16, 64}
receiverCountsIncludingZero = append([]int{0}, receiverCountsNonZero...)
)
// BenchmarkBroadcasterX, BenchmarkMapX, and BenchmarkQueueX benchmark usage
// pattern X (described in terms of Broadcaster) with Broadcaster, a
// Mutex-protected map[*Receiver]Set, and waiter.Queue respectively.
// BenchmarkXxxSubscribeUnsubscribe measures the cost of a Subscribe/Unsubscribe
// cycle.
func BenchmarkBroadcasterSubscribeUnsubscribe(b *testing.B) {
var br Broadcaster
var w Waiter
w.Init()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id := br.SubscribeEvents(w.Receiver(), 1)
br.UnsubscribeEvents(id)
}
}
func BenchmarkMapSubscribeUnsubscribe(b *testing.B) {
var mu sync.Mutex
m := make(map[*Receiver]Set)
var w Waiter
w.Init()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mu.Lock()
m[w.Receiver()] = Set(1)
mu.Unlock()
mu.Lock()
delete(m, w.Receiver())
mu.Unlock()
}
}
func BenchmarkQueueSubscribeUnsubscribe(b *testing.B) {
var q waiter.Queue
e, _ := waiter.NewChannelEntry(nil)
b.ResetTimer()
for i := 0; i < b.N; i++ {
q.EventRegister(&e, 1)
q.EventUnregister(&e)
}
}
// BenchmarkXxxSubscribeUnsubscribeBatch is similar to
// BenchmarkXxxSubscribeUnsubscribe, but subscribes and unsubscribes a large
// number of Receivers at a time in order to measure the amortized overhead of
// table expansion/compaction. (Since waiter.Queue is implemented using a
// linked list, BenchmarkQueueSubscribeUnsubscribe and
// BenchmarkQueueSubscribeUnsubscribeBatch should produce nearly the same
// result.)
const numBatchReceivers = 1000
func BenchmarkBroadcasterSubscribeUnsubscribeBatch(b *testing.B) {
var br Broadcaster
ws := make([]Waiter, numBatchReceivers)
for i := range ws {
ws[i].Init()
}
ids := make([]SubscriptionID, numBatchReceivers)
// Generate a random order for unsubscriptions.
unsub := rand.Perm(numBatchReceivers)
b.ResetTimer()
for i := 0; i < b.N/numBatchReceivers; i++ {
for j := 0; j < numBatchReceivers; j++ {
ids[j] = br.SubscribeEvents(ws[j].Receiver(), 1)
}
for j := 0; j < numBatchReceivers; j++ {
br.UnsubscribeEvents(ids[unsub[j]])
}
}
}
func BenchmarkMapSubscribeUnsubscribeBatch(b *testing.B) {
var mu sync.Mutex
m := make(map[*Receiver]Set)
ws := make([]Waiter, numBatchReceivers)
for i := range ws {
ws[i].Init()
}
// Generate a random order for unsubscriptions.
unsub := rand.Perm(numBatchReceivers)
b.ResetTimer()
for i := 0; i < b.N/numBatchReceivers; i++ {
for j := 0; j < numBatchReceivers; j++ {
mu.Lock()
m[ws[j].Receiver()] = Set(1)
mu.Unlock()
}
for j := 0; j < numBatchReceivers; j++ {
mu.Lock()
delete(m, ws[unsub[j]].Receiver())
mu.Unlock()
}
}
}
func BenchmarkQueueSubscribeUnsubscribeBatch(b *testing.B) {
var q waiter.Queue
es := make([]waiter.Entry, numBatchReceivers)
for i := range es {
es[i], _ = waiter.NewChannelEntry(nil)
}
// Generate a random order for unsubscriptions.
unsub := rand.Perm(numBatchReceivers)
b.ResetTimer()
for i := 0; i < b.N/numBatchReceivers; i++ {
for j := 0; j < numBatchReceivers; j++ {
q.EventRegister(&es[j], 1)
}
for j := 0; j < numBatchReceivers; j++ {
q.EventUnregister(&es[unsub[j]])
}
}
}
// BenchmarkXxxBroadcastRedundant measures how long it takes to Broadcast
// already-pending events to multiple Receivers.
func BenchmarkBroadcasterBroadcastRedundant(b *testing.B) {
for _, n := range receiverCountsIncludingZero {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
var br Broadcaster
ws := make([]Waiter, n)
for i := range ws {
ws[i].Init()
br.SubscribeEvents(ws[i].Receiver(), 1)
}
br.Broadcast(1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
br.Broadcast(1)
}
})
}
}
func BenchmarkMapBroadcastRedundant(b *testing.B) {
for _, n := range receiverCountsIncludingZero {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
var mu sync.Mutex
m := make(map[*Receiver]Set)
ws := make([]Waiter, n)
for i := range ws {
ws[i].Init()
m[ws[i].Receiver()] = Set(1)
}
mu.Lock()
for r := range m {
r.Notify(1)
}
mu.Unlock()
b.ResetTimer()
for i := 0; i < b.N; i++ {
mu.Lock()
for r := range m {
r.Notify(1)
}
mu.Unlock()
}
})
}
}
func BenchmarkQueueBroadcastRedundant(b *testing.B) {
for _, n := range receiverCountsIncludingZero {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
var q waiter.Queue
for i := 0; i < n; i++ {
e, _ := waiter.NewChannelEntry(nil)
q.EventRegister(&e, 1)
}
q.Notify(1)
b.ResetTimer()
for i := 0; i < b.N; i++ {
q.Notify(1)
}
})
}
}
// BenchmarkXxxBroadcastAck measures how long it takes to Broadcast events to
// multiple Receivers, check that all Receivers have received the event, and
// clear the event from all Receivers.
func BenchmarkBroadcasterBroadcastAck(b *testing.B) {
for _, n := range receiverCountsNonZero {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
var br Broadcaster
ws := make([]Waiter, n)
for i := range ws {
ws[i].Init()
br.SubscribeEvents(ws[i].Receiver(), 1)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
br.Broadcast(1)
for j := range ws {
if got, want := ws[j].Pending(), Set(1); got != want {
b.Fatalf("Receiver.Pending(): got %#x, wanted %#x", got, want)
}
ws[j].Ack(1)
}
}
})
}
}
func BenchmarkMapBroadcastAck(b *testing.B) {
for _, n := range receiverCountsNonZero {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
var mu sync.Mutex
m := make(map[*Receiver]Set)
ws := make([]Waiter, n)
for i := range ws {
ws[i].Init()
m[ws[i].Receiver()] = Set(1)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mu.Lock()
for r := range m {
r.Notify(1)
}
mu.Unlock()
for j := range ws {
if got, want := ws[j].Pending(), Set(1); got != want {
b.Fatalf("Receiver.Pending(): got %#x, wanted %#x", got, want)
}
ws[j].Ack(1)
}
}
})
}
}
func BenchmarkQueueBroadcastAck(b *testing.B) {
for _, n := range receiverCountsNonZero {
b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
var q waiter.Queue
chs := make([]chan struct{}, n)
for i := range chs {
e, ch := waiter.NewChannelEntry(nil)
q.EventRegister(&e, 1)
chs[i] = ch
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
q.Notify(1)
for _, ch := range chs {
select {
case <-ch:
default:
b.Fatalf("channel did not receive event")
}
}
}
})
}
}
|