-
Notifications
You must be signed in to change notification settings - Fork 33
/
queue.go
296 lines (267 loc) · 8.74 KB
/
queue.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
/*
* Copyright 2023 CloudWeGo 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 shmipc
import (
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"sync/atomic"
"syscall"
"unsafe"
)
const (
queueHeaderLength = 24
)
type queueManager struct {
path string
sendQueue *queue
recvQueue *queue
mem []byte
mmapMapType MemMapType
memFd int
}
//default cap is 16384, which mean that 16384 * 8 = 128 KB memory.
type queue struct {
sync.Mutex
head *int64 // consumer write, producer read
tail *int64 // producer write, consumer read
workingFlag *uint32 //when peer is consuming the queue, the workingFlag is 1, otherwise 0.
cap int64
queueBytesOnMemory []byte // it could be from share memory or process memory.
}
type queueElement struct {
seqID uint32
offsetInShmBuf uint32
status uint32
}
func createQueueManagerWithMemFd(queuePathName string, queueCap uint32) (*queueManager, error) {
memFd, err := MemfdCreate(queuePathName, 0)
if err != nil {
return nil, err
}
memSize := countQueueMemSize(queueCap) * queueCount
if err := syscall.Ftruncate(memFd, int64(memSize)); err != nil {
return nil, fmt.Errorf("createQueueManagerWithMemFd truncate share memory failed,%w", err)
}
mem, err := syscall.Mmap(memFd, 0, memSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
return nil, err
}
for i := 0; i < len(mem); i++ {
mem[i] = 0
}
return &queueManager{
sendQueue: createQueueFromBytes(mem[:memSize/2], queueCap),
recvQueue: createQueueFromBytes(mem[memSize/2:], queueCap),
mem: mem,
path: queuePathName,
mmapMapType: MemMapTypeMemFd,
memFd: memFd,
}, nil
}
func createQueueManager(shmPath string, queueCap uint32) (*queueManager, error) {
//ignore mkdir error
_ = os.MkdirAll(filepath.Dir(shmPath), os.ModePerm)
if pathExists(shmPath) {
return nil, errors.New("queue was existed,path" + shmPath)
}
memSize := countQueueMemSize(queueCap) * queueCount
if !canCreateOnDevShm(uint64(memSize), shmPath) {
return nil, fmt.Errorf("err:%s path:%s, size:%d", ErrShareMemoryHadNotLeftSpace.Error(), shmPath, memSize)
}
f, err := os.OpenFile(shmPath, os.O_CREATE|os.O_RDWR, os.ModePerm)
if err != nil {
return nil, err
}
defer f.Close()
if err := f.Truncate(int64(memSize)); err != nil {
return nil, fmt.Errorf("truncate share memory failed,%s", err.Error())
}
mem, err := syscall.Mmap(int(f.Fd()), 0, memSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
return nil, err
}
for i := 0; i < len(mem); i++ {
mem[i] = 0
}
return &queueManager{
sendQueue: createQueueFromBytes(mem[:memSize/2], queueCap),
recvQueue: createQueueFromBytes(mem[memSize/2:], queueCap),
mem: mem,
path: shmPath,
}, nil
}
func mappingQueueManagerMemfd(queuePathName string, memFd int) (*queueManager, error) {
var fileInfo syscall.Stat_t
if err := syscall.Fstat(memFd, &fileInfo); err != nil {
return nil, err
}
mappingSize := int(fileInfo.Size)
//a queueManager have two queue, a queue's head and tail should align to 8 byte boundary
if isArmArch() && mappingSize%16 != 0 {
return nil, fmt.Errorf("the memory size of queue should be a multiple of 16")
}
mem, err := syscall.Mmap(memFd, 0, mappingSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
return nil, err
}
return &queueManager{
sendQueue: mappingQueueFromBytes(mem[mappingSize/2:]),
recvQueue: mappingQueueFromBytes(mem[:mappingSize/2]),
mem: mem,
path: queuePathName,
memFd: memFd,
mmapMapType: MemMapTypeMemFd,
}, nil
}
func mappingQueueManager(shmPath string) (*queueManager, error) {
f, err := os.OpenFile(shmPath, os.O_RDWR, os.ModePerm)
if err != nil {
return nil, err
}
defer f.Close()
fileInfo, err := f.Stat()
if err != nil {
return nil, err
}
mappingSize := int(fileInfo.Size())
//a queueManager have two queue, a queue's head and tail should align to 8 byte boundary
if isArmArch() && mappingSize%16 != 0 {
return nil, fmt.Errorf("the memory size of queue should be a multiple of 16")
}
mem, err := syscall.Mmap(int(f.Fd()), 0, mappingSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
return nil, err
}
return &queueManager{
sendQueue: mappingQueueFromBytes(mem[mappingSize/2:]),
recvQueue: mappingQueueFromBytes(mem[:mappingSize/2]),
mem: mem,
path: shmPath,
}, nil
}
func countQueueMemSize(queueCap uint32) int {
return queueHeaderLength + queueElementLen*int(queueCap)
}
func createQueueFromBytes(data []byte, cap uint32) *queue {
*(*uint32)(unsafe.Pointer(&data[0])) = cap
q := mappingQueueFromBytes(data)
*q.head = 0
*q.tail = 0
*q.workingFlag = 0
return q
}
func mappingQueueFromBytes(data []byte) *queue {
cap := *(*uint32)(unsafe.Pointer(&data[0]))
queueStartOffset := queueHeaderLength
queueEndOffset := queueHeaderLength + cap*queueElementLen
if isArmArch() {
// align 8 byte boundary for head and tail
return &queue{
cap: int64(cap),
workingFlag: (*uint32)(unsafe.Pointer(&data[4])),
head: (*int64)(unsafe.Pointer(&data[8])),
tail: (*int64)(unsafe.Pointer(&data[16])),
queueBytesOnMemory: data[queueStartOffset:queueEndOffset],
}
}
return &queue{
cap: int64(cap),
head: (*int64)(unsafe.Pointer(&data[4])),
tail: (*int64)(unsafe.Pointer(&data[12])),
workingFlag: (*uint32)(unsafe.Pointer(&data[20])),
queueBytesOnMemory: data[queueStartOffset:queueEndOffset],
}
}
//cap prefer equals 2^n
func createQueue(cap uint32) *queue {
return createQueueFromBytes(make([]byte, queueHeaderLength+int(cap*queueElementLen)), cap)
}
func (q *queueManager) unmap() {
if err := syscall.Munmap(q.mem); err != nil {
internalLogger.warnf("queueManager unmap error:" + err.Error())
}
if q.mmapMapType == MemMapTypeDevShmFile {
if err := os.Remove(q.path); err != nil {
internalLogger.warnf("queueManager remove file:%s failed, error=%s", q.path, err.Error())
} else {
internalLogger.infof("queueManager remove file:%s", q.path)
}
} else {
if err := syscall.Close(q.memFd); err != nil {
internalLogger.warnf("queueManager close queue fd:%d, error:%s", q.memFd, err.Error())
} else {
internalLogger.infof("queueManager close queue fd:%d", q.memFd)
}
}
}
func (q *queue) isFull() bool {
return q.size() == q.cap
}
func (q *queue) isEmpty() bool {
return q.size() == 0
}
func (q *queue) size() int64 {
return atomic.LoadInt64(q.tail) - atomic.LoadInt64(q.head)
}
func (q *queue) pop() (e queueElement, err error) {
//atomic ensure the data that peer write to share memory could be see.
head := atomic.LoadInt64(q.head)
if head >= atomic.LoadInt64(q.tail) {
err = errQueueEmpty
return
}
queueOffset := (head % q.cap) * queueElementLen
e.seqID = *(*uint32)(unsafe.Pointer(&q.queueBytesOnMemory[queueOffset]))
e.offsetInShmBuf = *(*uint32)(unsafe.Pointer(&q.queueBytesOnMemory[queueOffset+4]))
e.status = *(*uint32)(unsafe.Pointer(&q.queueBytesOnMemory[queueOffset+8]))
atomic.AddInt64(q.head, 1)
return
}
func (q *queue) put(e queueElement) error {
//ensure that increasing q.tail and writing queueElement are both atomic.
//because if firstly increase q.tail, the peer will think that the queue have new element and will consume it.
//but at this moment, the new element hadn't finished writing, the peer will get a old element.
q.Lock()
tail := atomic.LoadInt64(q.tail)
if tail-atomic.LoadInt64(q.head) >= q.cap {
q.Unlock()
return ErrQueueFull
}
queueOffset := (tail % q.cap) * queueElementLen
*(*uint32)(unsafe.Pointer(&q.queueBytesOnMemory[queueOffset])) = e.seqID
*(*uint32)(unsafe.Pointer(&q.queueBytesOnMemory[queueOffset+4])) = e.offsetInShmBuf
*(*uint32)(unsafe.Pointer(&q.queueBytesOnMemory[queueOffset+8])) = e.status
atomic.AddInt64(q.tail, 1)
q.Unlock()
return nil
}
func (q *queue) consumerIsWorking() bool {
return (atomic.LoadUint32(q.workingFlag)) > 0
}
func (q *queue) markWorking() bool {
return atomic.CompareAndSwapUint32(q.workingFlag, 0, 1)
}
func (q *queue) markNotWorking() bool {
atomic.StoreUint32(q.workingFlag, 0)
if q.size() == 0 {
return true
}
atomic.StoreUint32(q.workingFlag, 1)
return false
}