-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlfring_test.go
437 lines (413 loc) · 11.3 KB
/
lfring_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
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
/*
* MIT License
*
* Copyright (c) 2017 Milad (Mike) Taghavi <mitghi[at]me/gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package lfring
import (
"fmt"
"sync"
"testing"
"time"
"unsafe"
)
// - MARK: Test-structs section.
type tstnode struct {
uid string
value int
}
// - MARK: Test section.
func TestRingConcurrent(t *testing.T) {
const size uint64 = 1024
var (
r *Ring = NewRing(size - 7)
wg *sync.WaitGroup = &sync.WaitGroup{}
startCh chan struct{}
)
if r.size != size {
t.Fatalf("assertion failed, r.size(%d)!=size(%d).", r.size, size)
}
startCh = make(chan struct{})
for i := 0; i < 2; i++ {
wg.Add(1)
go func(wg *sync.WaitGroup, start <-chan struct{}, index int) {
tmp := make([]interface{}, 0)
t := 0
time.Sleep(time.Millisecond * 190)
<-start
L:
for i := 0; i < 200; i++ {
if i == 50 {
time.Sleep(time.Millisecond * 5)
}
if t == 2 {
goto DONE
}
if r.Push(&tstnode{value: i * index}) {
tmp = append(tmp, i)
}
// runtime.Gosched()
}
t++
goto L
DONE:
fmt.Printf("PUSH[GOROUTINE %2.2d,\tLEN %d]:\n%v\n\n ", index, len(tmp), tmp)
wg.Done()
}(wg, startCh, i+1)
}
for i := 0; i < 2; i++ {
wg.Add(1)
go func(wg *sync.WaitGroup, start <-chan struct{}, index int) {
tmp := make([]interface{}, 0)
time.Sleep(time.Millisecond * 200)
<-start
for i := 0; i < 100; i++ {
if i == 20 {
time.Sleep(time.Millisecond * 5)
}
if a, b := r.Pop(); b {
if x, ok := (a).(*tstnode); !ok || a == nil {
continue
} else {
tmp = append(tmp, x)
}
}
// runtime.Gosched()
}
fmt.Printf("POP[GOROUTINE %2.2d,\tLEN %d]:\n%v\n\n", index, len(tmp), tmp)
wg.Done()
}(wg, startCh, i)
}
for i := 0; i < 4; i++ {
startCh <- struct{}{}
}
wg.Wait()
fmt.Printf("RING-NODES:\n%v\n", r.nodes)
}
func TestRingSerial(t *testing.T) {
const rcap = 8
var (
lfq *Ring = NewRing(rcap - 3)
)
// lfq.size rounds to nearest pow2
// therefor `lfq.size==rcap`
if lfq.size != rcap {
t.Fatal("assertion failed, expected equal.")
}
for i := 0; i < rcap; i++ {
uid := fmt.Sprintf("user_%d", i)
if !lfq.Push(&tstnode{uid: uid}) {
t.Fatal("inconsistent state, unable to push.")
}
}
fmt.Printf("(push)RING: %+v\n", lfq)
// pushing into full ring returns false
if lfq.Push(&tstnode{uid: "invalid"}) {
t.Fatal("inconsistent state.")
}
if (lfq.count != lfq.maxrdi) && (lfq.wri != 0 && lfq.count == 8) {
t.Fatal("asesrtion failed.")
}
for i := 0; i < rcap; i++ {
uid := fmt.Sprintf("user_%d", i)
val, ok := lfq.Pop()
if !ok {
t.Fatal("inconsistent state, unable to pop item.")
}
item, ok := val.(*tstnode)
if !ok || item == nil {
t.Fatal("inconsistent state, invalid value returned.")
}
if item.uid != uid {
t.Fatal("assertion failed, order violation.")
}
}
for i := 0; i < rcap; i++ {
if lfq.nodes[i] != nil {
t.Fatal("assertion failed, expected all slots to be nil.")
}
}
// popping from empty ring returns nil, false
if _, ok := lfq.Pop(); ok {
t.Fatal("inconsistent state, returned value from empty ring.")
}
if (lfq.wri != lfq.rdi) && lfq.count != 0 {
t.Fatal("assertion failed.")
}
fmt.Printf("(pop)RING: %+v\n", lfq)
}
type Sample struct {
value int
}
type SliceIFTest struct {
nodes []interface{}
sid string
}
type SliceTest struct {
nodes []unsafe.Pointer
sid string
rdi uint64
}
type ArrayTest struct {
nodes [16]unsafe.Pointer
sid string
}
type tststore struct {
counter uint64
nodes *SliceTest
}
type ststaligned struct {
nodes []unsafe.Pointer // storage with capacity `size`, pow2
wri, rdi, maxrdi, size uint64 // write, read, max-read and size (mask) indexes
count uint64 // occupancy counter
addrdata uintptr
}
// - MARK: Test-structs section.
type tstsample struct {
value int
}
type tstslice struct {
nodes []unsafe.Pointer
sid string
}
func init() {
const _archfmt = "========================\n%-6s%8d\n%-6s%12d\n%-6s%11d\n========================\n"
fmt.Printf(_archfmt,
"Address size", cArchADDRSIZE,
"Tag size", cArchMAXTAG,
"Word size", cArchWORDSIZE)
}
func TestTag(t *testing.T) {
const sval = 8
var (
s *Sample = &Sample{value: sval}
sptr unsafe.Pointer
tag uint
err error
)
sptr, err = TaggedPointer(unsafe.Pointer(s), 1)
if err != nil {
t.Fatal("assertion failed, expected==nil.")
}
s = (*Sample)(sptr)
tag = GetTag(sptr)
fmt.Println("Tag: ", tag)
sptr = Untag(unsafe.Pointer(s))
s = (*Sample)(sptr)
if s == nil || s.value != sval {
t.Fatal("assertion failed, expected s.value==sval.")
}
tag = GetTag(sptr)
if tag != 0 {
t.Fatal("assertion failed, expected tag==0.")
}
}
func TestAtomicSliceWithInterface(t *testing.T) {
var (
s *Sample = &Sample{value: 8}
a *SliceIFTest = &SliceIFTest{nodes: make([]interface{}, 16)}
sptr unsafe.Pointer
sval *Sample
ok bool
)
if !SetSliceSlotI(unsafe.Pointer(&a.nodes), 0, sizeINTERFACE, unsafe.Pointer(s)) {
t.Fatal("inconsistent state, cannot set slot.")
}
if a.nodes[0] == nil {
t.Fatal("inconsistent state, value is not inserted.")
} else {
fmt.Println(a.nodes[2], " is the head value", a.nodes[1])
}
fmt.Println("nodes(iface):", a.nodes)
sptr, ok = PopSliceSlot(unsafe.Pointer(&a.nodes), 0, sizeINTERFACE)
if !ok {
t.Fatal("inconsistent state, cannot pop slot.")
}
sval = *(**Sample)(sptr)
if sval == nil {
t.Fatal("inconsistent state.")
} else if sval.value != 8 {
t.Fatal("assertion failed.")
}
fmt.Println("Pointer(S) has Value(", sval.value, ")")
}
func TestAtomicSlice(t *testing.T) {
var (
s *Sample = &Sample{value: 8}
a *SliceTest = &SliceTest{nodes: make([]unsafe.Pointer, 16)}
sptr unsafe.Pointer
sval *Sample
ok bool
)
if !SetSliceSlot(unsafe.Pointer(&a.nodes), 0, ArchPTRSIZE, unsafe.Pointer(s)) {
t.Fatal("inconsistent state, cannot set slot.")
}
if a.nodes[0] == nil {
t.Fatal("inconsistent state, value is not inserted.")
}
fmt.Printf("nodes: %#x\n", a.nodes)
sptr, ok = PopSliceSlot(unsafe.Pointer(&a.nodes), 0, ArchPTRSIZE)
if !ok {
t.Fatal("inconsistent state, cannot pop slot.")
}
fmt.Printf("nodes: %#x\n", a.nodes)
sval = (*Sample)(sptr)
if sval == nil {
t.Fatal("inconsistent state.")
} else if sval.value != 8 {
t.Fatal("assertion failed.")
}
fmt.Println("Pointer(S) has Value(", sval.value, ")")
}
func TestCASTag(t *testing.T) {
var (
s *Sample = &Sample{value: 8}
sptr unsafe.Pointer
ok bool
err error
)
sptr, err = TaggedPointer(unsafe.Pointer(s), 1)
if err != nil {
t.Fatal("inconsistent state, expected err==nil.")
}
fmt.Println("sptr: ", sptr)
if sptr, ok = CompareAndSwapPointerTag(sptr, 1, 2); sptr == nil || !ok {
t.Fatal("inconsistent state, expected !nil, true.", sptr, ok)
}
fmt.Println("sptr: swap1->2 ", sptr)
if tag := GetTag(sptr); tag != 2 {
t.Fatal("assertion failed, expected 2. got ", tag)
}
}
func TestRDCSS(t *testing.T) {
var (
expected = &tststore{
counter: 16,
nodes: &SliceTest{make([]unsafe.Pointer, 0), "replaced", 0},
}
c = &tststore{
counter: 16,
nodes: &SliceTest{make([]unsafe.Pointer, 0), "original", 0},
}
crdiptr unsafe.Pointer
)
crdiptr = unsafe.Pointer(&c.counter)
if !RDCSS(
(*unsafe.Pointer)(crdiptr),
(unsafe.Pointer)(unsafe.Pointer(uintptr(expected.counter))),
(*unsafe.Pointer)(unsafe.Pointer(&c.nodes)),
unsafe.Pointer(c.nodes),
unsafe.Pointer(expected.nodes),
) {
t.Fatal("Expected RDCSS to succeed")
}
if c.nodes == nil {
t.Fatal("inconsistent state.")
}
if c.nodes.sid != "replaced" {
t.Fatal("inconsistent state.", c.nodes)
}
}
func TestRDCSS2(t *testing.T) {
var (
item = &Sample{16}
sl [8]unsafe.Pointer
slptr *[8]unsafe.Pointer
n *Sample
)
slptr = &sl
if !SetArraySlot(unsafe.Pointer(slptr), 4, unsafe.Sizeof(slptr), unsafe.Pointer(item)) {
t.Fatal("inconsistent state.")
}
nsptr, ok := PopArraySlot(unsafe.Pointer(slptr), 4, unsafe.Sizeof(slptr))
if !ok {
t.Fatal("inconsistent state.")
}
n = (*Sample)(nsptr)
if n == nil {
t.Fatal("inconsistent state.")
} else if n.value != 16 {
t.Fatal("inconsistent state.")
}
}
func TestRDCSS2x(t *testing.T) {
var (
s *tstsample = &tstsample{value: 64}
n *tstsample = &tstsample{value: 128}
ring *ststaligned = &ststaligned{nodes: make([]unsafe.Pointer, 8)}
slotptr unsafe.Pointer = unsafe.Pointer(OffsetSliceSlot(unsafe.Pointer(&ring.nodes), 1, ArchPTRSIZE))
rdiPtr unsafe.Pointer = unsafe.Pointer(&ring.rdi)
vptr **tstsample // value pointer
)
if !SetSliceSlot(unsafe.Pointer(&ring.nodes), 1, ArchPTRSIZE, unsafe.Pointer(&s)) {
t.Fatal("inconsistent state, can't write to slice/slot.")
}
if ring.nodes[1] == nil {
t.Fatal("assertion failed, expected non-nil.")
}
vptr = (**tstsample)(ring.nodes[1])
if vptr == nil {
t.Fatal("assertion failed, expected non-nil.")
} else if *vptr == nil {
t.Fatal("assertion failed, expected non-nil.")
}
if (*vptr) != s {
t.Fatal("assertion failed, expected equal as 's' is written to slot 1.")
}
if (((*vptr).value != s.value) && s.value == 64) || s.value != 64 {
t.Fatal("assertion failed, invalid pointers.")
}
fmt.Printf("nodes: %#x\n", ring.nodes)
if !RDCSS(
(*unsafe.Pointer)(unsafe.Pointer(&rdiPtr)),
(unsafe.Pointer)(unsafe.Pointer(rdiPtr)),
(*unsafe.Pointer)(unsafe.Pointer(slotptr)),
(unsafe.Pointer)(unsafe.Pointer(&s)),
(unsafe.Pointer)(unsafe.Pointer(n)),
) {
t.Fatal("inconsistent state, RDCSS failure.")
}
if s == nil {
t.Fatal("assertion failed, fatal condition from incorrect pointer mutation.")
} else if s.value != 64 {
t.Fatal("assertion failed, invalid pointer.")
}
fmt.Printf("nodes: %#x\n", ring.nodes)
if ring.nodes[1] == nil {
t.Fatal("assertion failed, expected non-nil for occupied slot.")
}
if s == nil {
t.Fatal("assertion failed, fatal condition from incorrect pointer mutation.")
}
vptr = (**tstsample)(slotptr)
if vptr == nil {
t.Fatal("assertion failed, expected non-nil pointer.")
} else if *vptr == nil {
t.Fatal("assertion failed, expected non-nil pointer.")
}
if (*vptr) != n {
t.Fatal("assertion failed, expected equal as 's' is swapped with 'n'.")
}
if (((*vptr).value != n.value) && n.value == 128) || n.value != 128 {
t.Fatal("assertion failed, invalid pointers.")
}
fmt.Println("slotptr:", *(**tstsample)(slotptr), "addr:", slotptr, &n, ring.nodes, ring.nodes[1])
}