forked from CN-TU/go-flows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multibuffer.go
260 lines (227 loc) · 6.72 KB
/
multibuffer.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
package packet
import (
"sync"
"sync/atomic"
"github.com/chtisgit/go-flows/flows"
)
/*
Internal handling of batches of buffers with following features:
- Packets can have a preallocated buffer and/or dynamically resized
- Unused Packets go back into the pool
- Packets are kept and exchanged in batches
- Single packets can be recycled from a full batch - no need to recycled everything at once
This is implemented with the following components:
packetBuffer: buffer holding one packet (data + decoded); see buffer.go
multiPacketBuffer: source of empty packets. Holds every allocated packet and keeps track of used empty packets with ref counting in packetBuffer. Uses a lockless algorithm in the fast path (enough packets free) for returning and reserving packets.
shallowMultiPacketBuffer: Holds a batch of packets for processing.
shallowMultiPacketBufferRing: Helper for moving shallowMultiPacketBuffer from one go routine to another using channels and a bit of tracking which packets are used and which can be recycled.
WARNING! The lockless algorithm necessitates, that ONLY the user of multiPacketBuffer.Pop is allowed to write to modify packet contents.
*/
type bufferUsage struct {
buffers int
packets int
}
type multiPacketBuffer struct {
numFree int32
allocSize int32
prealloc int
buffers []*packetBuffer
cond *sync.Cond
resize bool
}
func newMultiPacketBuffer(buffers int32, prealloc int, resize bool) *multiPacketBuffer {
return &multiPacketBuffer{
numFree: 0,
allocSize: buffers,
prealloc: prealloc,
resize: resize,
cond: sync.NewCond(&sync.Mutex{}),
}
}
func (mpb *multiPacketBuffer) replenish() {
new := make([]*packetBuffer, mpb.allocSize)
for j := range new {
new[j] = &packetBuffer{buffer: make([]byte, mpb.prealloc), owner: mpb, resize: mpb.resize}
}
mpb.buffers = append(mpb.buffers, new...)
atomic.AddInt32(&mpb.numFree, mpb.allocSize)
}
// release releases allocSize buffers. NEVER call concurrently with Pop!
func (mpb *multiPacketBuffer) release() {
newlist := make([]*packetBuffer, len(mpb.buffers)-int(mpb.allocSize))
i := 0
freed := int32(0)
for _, b := range mpb.buffers {
if atomic.LoadInt32(&b.inUse) != 0 {
newlist[i] = b
i++
} else if freed == mpb.allocSize {
newlist[i] = b
i++
} else {
freed++
}
}
mpb.buffers = newlist
atomic.AddInt32(&mpb.numFree, -int32(freed))
}
func (mpb *multiPacketBuffer) free(num int32) {
if atomic.AddInt32(&mpb.numFree, num) > batchSize {
mpb.cond.Signal()
}
}
func (mpb *multiPacketBuffer) Pop(buffer *shallowMultiPacketBuffer, low func(int, int), high func(int, int)) {
var num int32
buffer.reset()
if atomic.LoadInt32(&mpb.numFree) < batchSize {
mpb.cond.L.Lock()
for atomic.LoadInt32(&mpb.numFree) < batchSize {
low(int(atomic.LoadInt32(&mpb.numFree)), len(mpb.buffers))
if atomic.LoadInt32(&mpb.numFree) < batchSize {
mpb.cond.Wait()
}
}
mpb.cond.L.Unlock()
}
for _, b := range mpb.buffers {
if atomic.LoadInt32(&b.inUse) == 0 {
if !buffer.push(b) {
break
}
atomic.StoreInt32(&b.inUse, 1)
num++
}
}
atomic.AddInt32(&mpb.numFree, -num)
high(int(atomic.LoadInt32(&mpb.numFree)), len(mpb.buffers))
}
type shallowMultiPacketBuffer struct {
buffers []*packetBuffer
owner *shallowMultiPacketBufferRing
rindex int
windex int
timestamp flows.DateTimeNanoseconds
expire bool
}
func newShallowMultiPacketBuffer(size int, owner *shallowMultiPacketBufferRing) *shallowMultiPacketBuffer {
return &shallowMultiPacketBuffer{
buffers: make([]*packetBuffer, size),
owner: owner,
}
}
func (smpb *shallowMultiPacketBuffer) empty() bool {
return smpb.windex == 0
}
func (smpb *shallowMultiPacketBuffer) full() bool {
return smpb.windex != 0 && smpb.rindex == smpb.windex
}
func (smpb *shallowMultiPacketBuffer) reset() {
smpb.rindex = 0
smpb.windex = 0
}
func (smpb *shallowMultiPacketBuffer) push(buffer *packetBuffer) bool {
if smpb.windex >= len(smpb.buffers) || smpb.windex < 0 {
return false
}
smpb.buffers[smpb.windex] = buffer
smpb.windex++
return true
}
func (smpb *shallowMultiPacketBuffer) read() (ret *packetBuffer) {
if smpb.rindex >= len(smpb.buffers) || smpb.rindex >= smpb.windex || smpb.rindex < 0 {
return nil
}
ret = smpb.buffers[smpb.rindex]
smpb.rindex++
return
}
func (smpb *shallowMultiPacketBuffer) finalize() {
smpb.rindex = 0
if smpb.owner != nil {
atomic.AddInt32(&smpb.owner.currentBuffers, 1)
atomic.AddInt32(&smpb.owner.currentPackets, int32(smpb.windex))
smpb.owner.full <- smpb
}
}
func (smpb *shallowMultiPacketBuffer) finalizeWritten() {
rec := smpb.buffers[smpb.rindex:smpb.windex]
for _, buf := range rec {
buf.Recycle()
}
smpb.windex = smpb.rindex
smpb.finalize()
}
func (smpb *shallowMultiPacketBuffer) recycleEmpty() {
smpb.reset()
if smpb.owner != nil {
smpb.owner.empty <- smpb
}
}
func (smpb *shallowMultiPacketBuffer) recycle() {
if !smpb.empty() {
var num int32
mpb := smpb.buffers[0].owner
buf := smpb.buffers[:smpb.windex]
for i, b := range buf {
if b.canRecycle() {
atomic.StoreInt32(&buf[i].inUse, 0)
num++
}
}
mpb.free(num)
}
smpb.reset()
if smpb.owner != nil {
smpb.owner.empty <- smpb
}
}
func (smpb *shallowMultiPacketBuffer) setTimestamp(t flows.DateTimeNanoseconds) {
smpb.timestamp = t
}
func (smpb *shallowMultiPacketBuffer) Timestamp() flows.DateTimeNanoseconds {
return smpb.timestamp
}
func (smpb *shallowMultiPacketBuffer) Copy(other *shallowMultiPacketBuffer) {
src := smpb.buffers[:smpb.windex]
target := other.buffers[:len(src)]
copy(target, src)
other.rindex = 0
other.windex = smpb.windex
}
type shallowMultiPacketBufferRing struct {
currentPackets int32
currentBuffers int32
empty chan *shallowMultiPacketBuffer
full chan *shallowMultiPacketBuffer
}
func newShallowMultiPacketBufferRing(buffers, batch int) (ret *shallowMultiPacketBufferRing) {
ret = &shallowMultiPacketBufferRing{
empty: make(chan *shallowMultiPacketBuffer, buffers),
full: make(chan *shallowMultiPacketBuffer, buffers),
}
for i := 0; i < buffers; i++ {
ret.empty <- newShallowMultiPacketBuffer(batch, ret)
}
return
}
func (smpbr *shallowMultiPacketBufferRing) usage() bufferUsage {
return bufferUsage{
buffers: int(atomic.LoadInt32(&smpbr.currentBuffers)),
packets: int(atomic.LoadInt32(&smpbr.currentPackets)),
}
}
func (smpbr *shallowMultiPacketBufferRing) popEmpty() (ret *shallowMultiPacketBuffer, ok bool) {
ret, ok = <-smpbr.empty
return
}
func (smpbr *shallowMultiPacketBufferRing) popFull() (ret *shallowMultiPacketBuffer, ok bool) {
ret, ok = <-smpbr.full
if ok {
atomic.AddInt32(&smpbr.currentBuffers, -1)
atomic.AddInt32(&smpbr.currentPackets, -int32(ret.windex))
}
return
}
func (smpbr *shallowMultiPacketBufferRing) close() {
close(smpbr.full)
}