-
Notifications
You must be signed in to change notification settings - Fork 43
/
types.go
326 lines (270 loc) · 7.28 KB
/
types.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
//go:build linux
// +build linux
package iouring
import (
"reflect"
"sync/atomic"
"unsafe"
iouring_syscall "github.com/iceber/iouring-go/syscall"
)
// iouring operations
const (
OpNop uint8 = iota
OpReadv
OpWritev
OpFsync
OpReadFixed
OpWriteFixed
OpPollAdd
OpPollRemove
OpSyncFileRange
OpSendmsg
OpRecvmsg
OpTimeout
OpTimeoutRemove
OpAccept
OpAsyncCancel
OpLinkTimeout
OpConnect
OpFallocate
OpOpenat
OpClose
OpFilesUpdate
OpStatx
OpRead
OpWrite
OpFadvise
OpMadvise
OpSend
OpRecv
OpOpenat2
OpEpollCtl
OpSplice
OpProvideBuffers
OpRemoveBuffers
OpTee
OpShutdown
OpRenameat
OpUnlinkat
OpMkdirat
OpSymlinkat
OpLinkat
)
// cancel operation return value
const (
RequestCanceledSuccessfully = 0
RequestMaybeCanceled = 1
)
// timeout operation return value
const (
TimeoutExpiration = 0
CountCompletion = 1
)
var _zero uintptr
type SubmissionQueueRing interface {
isActive() bool
entrySz() uint32
ringSz() uint32
assignQueue(ptr uintptr, len int)
mappedPtr() uintptr
index(index uint32) iouring_syscall.SubmissionQueueEntry
}
func makeSubmissionQueueRing(flags uint32) SubmissionQueueRing {
if flags&iouring_syscall.IORING_SETUP_SQE128 == 0 {
return new(SubmissionQueueRing64)
} else {
return new(SubmissionQueueRing128)
}
}
type SubmissionQueueRing64 struct {
queue []iouring_syscall.SubmissionQueueEntry64
}
func (ring *SubmissionQueueRing64) isActive() bool {
return ring.queue != nil && len(ring.queue) > 0
}
func (ring *SubmissionQueueRing64) entrySz() uint32 {
return uint32(unsafe.Sizeof(iouring_syscall.SubmissionQueueEntry64{}))
}
func (ring *SubmissionQueueRing64) ringSz() uint32 {
return uint32(len(ring.queue)) * ring.entrySz()
}
func (ring *SubmissionQueueRing64) assignQueue(ptr uintptr, len int) {
ring.queue = *(*[]iouring_syscall.SubmissionQueueEntry64)(
unsafe.Pointer(&reflect.SliceHeader{
Data: ptr,
Len: len,
Cap: len,
}))
}
func (ring *SubmissionQueueRing64) mappedPtr() uintptr {
return uintptr(unsafe.Pointer(&ring.queue[0]))
}
func (ring *SubmissionQueueRing64) index(index uint32) iouring_syscall.SubmissionQueueEntry {
return &ring.queue[index]
}
type SubmissionQueueRing128 struct {
queue []iouring_syscall.SubmissionQueueEntry128
}
func (ring *SubmissionQueueRing128) isActive() bool {
return ring.queue != nil && len(ring.queue) > 0
}
func (ring *SubmissionQueueRing128) entrySz() uint32 {
return uint32(unsafe.Sizeof(iouring_syscall.SubmissionQueueEntry128{}))
}
func (ring *SubmissionQueueRing128) ringSz() uint32 {
return uint32(len(ring.queue)) * ring.entrySz()
}
func (ring *SubmissionQueueRing128) assignQueue(ptr uintptr, len int) {
ring.queue = *(*[]iouring_syscall.SubmissionQueueEntry128)(
unsafe.Pointer(&reflect.SliceHeader{
Data: ptr,
Len: len,
Cap: len,
}))
}
func (ring *SubmissionQueueRing128) mappedPtr() uintptr {
return uintptr(unsafe.Pointer(&ring.queue[0]))
}
func (ring *SubmissionQueueRing128) index(index uint32) iouring_syscall.SubmissionQueueEntry {
return &ring.queue[index]
}
type SubmissionQueue struct {
ptr uintptr
size uint32
head *uint32
tail *uint32
mask *uint32
entries *uint32 // specifies the number of submission queue ring entries
flags *uint32 // used by the kernel to communicate stat information to the application
dropped *uint32 // incrementd for each invalid submission queue entry encountered in the ring buffer
array []uint32
sqes SubmissionQueueRing // submission queue ring
sqeHead uint32
sqeTail uint32
}
func (queue *SubmissionQueue) getSQEntry() iouring_syscall.SubmissionQueueEntry {
head := atomic.LoadUint32(queue.head)
next := queue.sqeTail + 1
if (next - head) <= *queue.entries {
sqe := queue.sqes.index(queue.sqeTail & *queue.mask)
queue.sqeTail = next
sqe.Reset()
return sqe
}
return nil
}
func (queue *SubmissionQueue) fallback(i uint32) {
queue.sqeTail -= i
}
func (queue *SubmissionQueue) cqOverflow() bool {
return (atomic.LoadUint32(queue.flags) & iouring_syscall.IORING_SQ_CQ_OVERFLOW) != 0
}
func (queue *SubmissionQueue) needWakeup() bool {
return (atomic.LoadUint32(queue.flags) & iouring_syscall.IORING_SQ_NEED_WAKEUP) != 0
}
// sync internal status with kernel ring state on the SQ side
// return the number of pending items in the SQ ring, for the shared ring.
func (queue *SubmissionQueue) flush() int {
if queue.sqeHead == queue.sqeTail {
return int(*queue.tail - *queue.head)
}
tail := *queue.tail
for toSubmit := queue.sqeTail - queue.sqeHead; toSubmit > 0; toSubmit-- {
queue.array[tail&*queue.mask] = queue.sqeHead & *queue.mask
tail++
queue.sqeHead++
}
atomic.StoreUint32(queue.tail, tail)
return int(tail - *queue.head)
}
type CompletionQueueRing interface {
isActive() bool
entrySz() uint32
ringSz() uint32
assignQueue(ptr uintptr, len int)
mappedPtr() uintptr
index(index uint32) iouring_syscall.CompletionQueueEvent
}
func makeCompletionQueueRing(flags uint32) CompletionQueueRing {
if flags&iouring_syscall.IORING_SETUP_CQE32 == 0 {
return new(CompletionQueueRing16)
} else {
return new(CompletionQueueRing32)
}
}
type CompletionQueueRing16 struct {
queue []iouring_syscall.CompletionQueueEvent16
}
func (ring *CompletionQueueRing16) isActive() bool {
return ring.queue != nil && len(ring.queue) > 0
}
func (ring *CompletionQueueRing16) entrySz() uint32 {
return uint32(unsafe.Sizeof(iouring_syscall.CompletionQueueEvent16{}))
}
func (ring *CompletionQueueRing16) ringSz() uint32 {
return uint32(len(ring.queue)) * ring.entrySz()
}
func (ring *CompletionQueueRing16) assignQueue(ptr uintptr, len int) {
ring.queue = *(*[]iouring_syscall.CompletionQueueEvent16)(
unsafe.Pointer(&reflect.SliceHeader{
Data: ptr,
Len: len,
Cap: len,
}))
}
func (ring *CompletionQueueRing16) mappedPtr() uintptr {
return uintptr(unsafe.Pointer(&ring.queue[0]))
}
func (ring *CompletionQueueRing16) index(index uint32) iouring_syscall.CompletionQueueEvent {
return &ring.queue[index]
}
type CompletionQueueRing32 struct {
queue []iouring_syscall.CompletionQueueEvent32
}
func (ring *CompletionQueueRing32) isActive() bool {
return ring.queue != nil && len(ring.queue) > 0
}
func (ring *CompletionQueueRing32) entrySz() uint32 {
return uint32(unsafe.Sizeof(iouring_syscall.CompletionQueueEvent32{}))
}
func (ring *CompletionQueueRing32) ringSz() uint32 {
return uint32(len(ring.queue)) * ring.entrySz()
}
func (ring *CompletionQueueRing32) assignQueue(ptr uintptr, len int) {
ring.queue = *(*[]iouring_syscall.CompletionQueueEvent32)(
unsafe.Pointer(&reflect.SliceHeader{
Data: ptr,
Len: len,
Cap: len,
}))
}
func (ring *CompletionQueueRing32) mappedPtr() uintptr {
return uintptr(unsafe.Pointer(&ring.queue[0]))
}
func (ring *CompletionQueueRing32) index(index uint32) iouring_syscall.CompletionQueueEvent {
return &ring.queue[index]
}
type CompletionQueue struct {
ptr uintptr
size uint32
head *uint32
tail *uint32
mask *uint32
entries *uint32
flags *uint32
overflow *uint32
cqes CompletionQueueRing
}
func (queue *CompletionQueue) peek() (cqe iouring_syscall.CompletionQueueEvent) {
head := *queue.head
if head != atomic.LoadUint32(queue.tail) {
// if head < atomic.LoadUint32(queue.tail) {
cqe = queue.cqes.index(head & *queue.mask)
}
return
}
func (queue *CompletionQueue) advance(num uint32) {
if num != 0 {
atomic.AddUint32(queue.head, num)
}
}