-
Notifications
You must be signed in to change notification settings - Fork 43
/
iouring.go
404 lines (328 loc) · 8.65 KB
/
iouring.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
//go:build linux
// +build linux
package iouring
import (
"errors"
"log"
"os"
"runtime"
"sync"
"syscall"
iouring_syscall "github.com/iceber/iouring-go/syscall"
)
// IOURing contains iouring_syscall submission and completion queue.
// It's safe for concurrent use by multiple goroutines.
type IOURing struct {
params *iouring_syscall.IOURingParams
fd int
eventfd int
cqeSign chan struct{}
sq *SubmissionQueue
cq *CompletionQueue
async bool
drain bool
Flags uint32
Features uint32
submitLock sync.Mutex
userDataLock sync.RWMutex
userDatas map[uint64]*UserData
fileRegister FileRegister
fdclosed bool
closer chan struct{}
closed chan struct{}
}
// New return a IOURing instance by IOURingOptions
func New(entries uint, opts ...IOURingOption) (*IOURing, error) {
iour := &IOURing{
params: &iouring_syscall.IOURingParams{},
userDatas: make(map[uint64]*UserData),
cqeSign: make(chan struct{}, 1),
closer: make(chan struct{}),
closed: make(chan struct{}),
}
for _, opt := range opts {
opt(iour)
}
var err error
iour.fd, err = iouring_syscall.IOURingSetup(entries, iour.params)
if err != nil {
return nil, err
}
if err := mmapIOURing(iour); err != nil {
munmapIOURing(iour)
return nil, err
}
iour.fileRegister = &fileRegister{
iouringFd: iour.fd,
sparseIndexs: make(map[int]int),
}
iour.Flags = iour.params.Flags
iour.Features = iour.params.Features
if err := iour.registerEventfd(); err != nil {
iour.Close()
return nil, err
}
if err := registerIOURing(iour); err != nil {
iour.Close()
return nil, err
}
go iour.run()
return iour, nil
}
// Size iouring submission queue size
func (iour *IOURing) Size() int {
return int(*iour.sq.entries)
}
// Close IOURing
func (iour *IOURing) Close() error {
iour.submitLock.Lock()
defer iour.submitLock.Unlock()
select {
case <-iour.closer:
default:
close(iour.closer)
}
if iour.eventfd > 0 {
if err := removeIOURing(iour); err != nil {
return err
}
syscall.Close(iour.eventfd)
iour.eventfd = -1
}
<-iour.closed
if err := munmapIOURing(iour); err != nil {
return err
}
if !iour.fdclosed {
if err := syscall.Close(iour.fd); err != nil {
return os.NewSyscallError("close", err)
}
iour.fdclosed = true
}
return nil
}
// IsClosed IOURing is closed
func (iour *IOURing) IsClosed() (closed bool) {
select {
case <-iour.closer:
closed = true
default:
}
return
}
func (iour *IOURing) getSQEntry() iouring_syscall.SubmissionQueueEntry {
for {
sqe := iour.sq.getSQEntry()
if sqe != nil {
return sqe
}
runtime.Gosched()
}
}
func (iour *IOURing) doRequest(sqe iouring_syscall.SubmissionQueueEntry, request PrepRequest, ch chan<- Result) (*UserData, error) {
userData := makeUserData(iour, ch)
request(sqe, userData)
userData.setOpcode(sqe.Opcode())
sqe.SetUserData(userData.id)
userData.request.fd = int(sqe.Fd())
if sqe.Fd() >= 0 {
if index, ok := iour.fileRegister.GetFileIndex(int32(sqe.Fd())); ok {
sqe.SetFdIndex(int32(index))
} else if iour.Flags&iouring_syscall.IORING_SETUP_SQPOLL != 0 &&
iour.Features&iouring_syscall.IORING_FEAT_SQPOLL_NONFIXED == 0 {
/*
Before version 5.10 of the Linux kernel, to successful use SQPoll, the application
must register a set of files to be used for IO through iour.RegisterFiles.
Failure to do so will result in submitted IO being errored with EBADF
The presence of this feature can be detected by the IORING_FEAT_SQPOLL_NONFIXED
In Version 5.10 and later, it is no longer necessary to register files to use SQPoll
*/
return nil, ErrUnregisteredFile
}
}
if iour.async {
sqe.SetFlags(iouring_syscall.IOSQE_FLAGS_ASYNC)
}
if iour.drain {
sqe.SetFlags(iouring_syscall.IOSQE_FLAGS_IO_DRAIN)
}
return userData, nil
}
// SubmitRequest by Request function and io result is notified via channel
// return request id, can be used to cancel a request
func (iour *IOURing) SubmitRequest(request PrepRequest, ch chan<- Result) (Request, error) {
iour.submitLock.Lock()
defer iour.submitLock.Unlock()
if iour.IsClosed() {
return nil, ErrIOURingClosed
}
sqe := iour.getSQEntry()
userData, err := iour.doRequest(sqe, request, ch)
if err != nil {
iour.sq.fallback(1)
return nil, err
}
iour.userDataLock.Lock()
iour.userDatas[userData.id] = userData
iour.userDataLock.Unlock()
if _, err = iour.submit(); err != nil {
iour.userDataLock.Lock()
delete(iour.userDatas, userData.id)
iour.userDataLock.Unlock()
return nil, err
}
return userData.request, nil
}
// SubmitRequests by Request functions and io results are notified via channel
func (iour *IOURing) SubmitRequests(requests []PrepRequest, ch chan<- Result) (RequestSet, error) {
// TODO(iceber): no length limit
if len(requests) > int(*iour.sq.entries) {
return nil, errors.New("too many requests")
}
iour.submitLock.Lock()
defer iour.submitLock.Unlock()
if iour.IsClosed() {
return nil, ErrIOURingClosed
}
var sqeN uint32
userDatas := make([]*UserData, 0, len(requests))
for _, request := range requests {
sqe := iour.getSQEntry()
sqeN++
userData, err := iour.doRequest(sqe, request, ch)
if err != nil {
iour.sq.fallback(sqeN)
return nil, err
}
userDatas = append(userDatas, userData)
}
// must be located before the lock operation to
// avoid the compiler's adjustment of the code order.
// issue: https://github.com/Iceber/iouring-go/issues/8
rset := newRequestSet(userDatas)
iour.userDataLock.Lock()
for _, data := range userDatas {
iour.userDatas[data.id] = data
}
iour.userDataLock.Unlock()
if _, err := iour.submit(); err != nil {
iour.userDataLock.Lock()
for _, data := range userDatas {
delete(iour.userDatas, data.id)
}
iour.userDataLock.Unlock()
return nil, err
}
return rset, nil
}
func (iour *IOURing) needEnter(flags *uint32) bool {
if (iour.Flags & iouring_syscall.IORING_SETUP_SQPOLL) == 0 {
return true
}
if iour.sq.needWakeup() {
*flags |= iouring_syscall.IORING_SQ_NEED_WAKEUP
return true
}
return false
}
func (iour *IOURing) submit() (submitted int, err error) {
submitted = iour.sq.flush()
var flags uint32
if !iour.needEnter(&flags) || submitted == 0 {
return
}
if (iour.Flags & iouring_syscall.IORING_SETUP_IOPOLL) != 0 {
flags |= iouring_syscall.IORING_ENTER_FLAGS_GETEVENTS
}
submitted, err = iouring_syscall.IOURingEnter(iour.fd, uint32(submitted), 0, flags, nil)
return
}
/*
func (iour *IOURing) submitAndWait(waitCount uint32) (submitted int, err error) {
submitted = iour.sq.flush()
var flags uint32
if !iour.needEnter(&flags) && waitCount == 0 {
return
}
if waitCount != 0 || (iour.Flags&iouring_syscall.IORING_SETUP_IOPOLL) != 0 {
flags |= iouring_syscall.IORING_ENTER_FLAGS_GETEVENTS
}
submitted, err = iouring_syscall.IOURingEnter(iour.fd, uint32(submitted), waitCount, flags, nil)
return
}
*/
func (iour *IOURing) getCQEvent(wait bool) (cqe iouring_syscall.CompletionQueueEvent, err error) {
var tryPeeks int
for {
if cqe = iour.cq.peek(); cqe != nil {
// Copy CQE.
cqe = cqe.Clone()
iour.cq.advance(1)
return
}
if !wait && !iour.sq.cqOverflow() {
err = syscall.EAGAIN
return
}
if iour.sq.cqOverflow() {
_, err = iouring_syscall.IOURingEnter(iour.fd, 0, 0, iouring_syscall.IORING_ENTER_FLAGS_GETEVENTS, nil)
if err != nil {
return
}
continue
}
if tryPeeks++; tryPeeks < 3 {
runtime.Gosched()
continue
}
select {
case <-iour.cqeSign:
case <-iour.closer:
return nil, ErrIOURingClosed
}
}
}
func (iour *IOURing) run() {
for {
cqe, err := iour.getCQEvent(true)
if cqe == nil || err != nil {
if err == ErrIOURingClosed {
close(iour.closed)
return
}
log.Println("runComplete error: ", err)
continue
}
// log.Println("cqe user data", (cqe.UserData))
iour.userDataLock.Lock()
userData := iour.userDatas[cqe.UserData()]
if userData == nil {
iour.userDataLock.Unlock()
log.Println("runComplete: notfound user data ", uintptr(cqe.UserData()))
continue
}
delete(iour.userDatas, cqe.UserData())
iour.userDataLock.Unlock()
userData.request.complate(cqe)
// ignore link timeout
if userData.opcode == iouring_syscall.IORING_OP_LINK_TIMEOUT {
continue
}
if userData.resulter != nil {
userData.resulter <- userData.request
}
}
}
// Result submit cancel request
func (iour *IOURing) submitCancel(id uint64) (Request, error) {
if iour == nil {
return nil, ErrRequestCompleted
}
return iour.SubmitRequest(cancelRequest(id), nil)
}
func cancelRequest(id uint64) PrepRequest {
return func(sqe iouring_syscall.SubmissionQueueEntry, userData *UserData) {
userData.request.resolver = cancelResolver
sqe.PrepOperation(iouring_syscall.IORING_OP_ASYNC_CANCEL, -1, id, 0, 0)
}
}