-
Notifications
You must be signed in to change notification settings - Fork 10
/
types.go
267 lines (238 loc) · 6.08 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
// +build linux
package iouring
import (
"runtime"
"sync"
"sync/atomic"
"github.com/pkg/errors"
)
const (
// CqSeenFlag is a nonstandard flag for handling concurrent readers
// from the CompletionQueue.
CqSeenFlag = 1
)
var (
// ErrEntryNotFound is returned when a CQE is not found.
ErrEntryNotFound = errors.New("Completion entry not found")
errCQEMissing = errors.New("cqe missing")
cqePool = sync.Pool{
New: func() interface{} {
return &CompletionEntry{}
},
}
)
type completionRequest struct {
id uint64
res int32
flags uint32
done chan struct{}
}
// Params are used to configured a io uring.
type Params struct {
SqEntries uint32
CqEntries uint32
Flags uint32
SqThreadCPU uint32
SqThreadIdle uint32
Features uint32
WqFD uint32
Resv [3]uint32
SqOffset SQRingOffset
CqOffset CQRingOffset
}
// SQRingOffset describes the various submit queue offsets.
type SQRingOffset struct {
Head uint32
Tail uint32
RingMask uint32
Entries uint32
Flags uint32
Dropped uint32
Array uint32
Resv1 uint32
Resv2 uint64
}
// CQRingOffset describes the various completion queue offsets.
type CQRingOffset struct {
Head uint32
Tail uint32
RingMask uint32
Entries uint32
Overflow uint32
Cqes uint32
Flags uint32
Resv [2]uint64
}
// SubmitEntry is an IO submission data structure (Submission Queue Entry).
type SubmitEntry struct {
Opcode Opcode /* type of operation for this sqe */
Flags uint8 /* IOSQE_ flags */
Ioprio uint16 /* ioprio for the request */
Fd int32 /* file descriptor to do IO on */
Offset uint64 /* offset into file */
Addr uint64 /* pointer to buffer or iovecs */
Len uint32 /* buffer size or number of iovecs */
UFlags int32
UserData uint64
Anon0 [24]byte /* extra padding */
}
// Reset is used to reset an SubmitEntry.
func (e *SubmitEntry) Reset() {
e.Opcode = Nop
e.Flags = 0
e.Ioprio = 0
e.Fd = -1
e.Offset = 0
e.Addr = 0
e.Len = 0
e.UFlags = 0
e.UserData = 0
}
// SubmitQueue represents the submit queue ring buffer.
type SubmitQueue struct {
Size uint32
Head *uint32
Tail *uint32
Mask *uint32
Flags *uint32
Dropped *uint32
// Array holds entries to be submitted; it must never be resized it is mmap'd.
Array []uint32
// Entries must never be resized, it is mmap'd.
Entries []SubmitEntry
// ptr is pointer to the start of the mmap.
ptr uintptr
// entered is when the ring is being entered.
entered *uint32
// writes is used to keep track of the number of concurrent writers to
// the ring.
writes *uint32
}
// Reset is used to reset all entries.
func (s *SubmitQueue) Reset() {
for _, entry := range s.Entries {
entry.Reset()
}
}
// NeedWakeup is used to determine whether the submit queue needs awoken.
func (s *SubmitQueue) NeedWakeup() bool {
return atomic.LoadUint32(s.Flags)&SqNeedWakeup != 0
}
func (s *SubmitQueue) enterLock() {
for {
if atomic.LoadUint32(s.writes) != 0 && atomic.LoadUint32(s.entered) == 1 {
runtime.Gosched()
continue
}
if atomic.CompareAndSwapUint32(s.entered, 0, 1) {
break
}
}
}
func (s *SubmitQueue) enterUnlock() {
atomic.StoreUint32(s.entered, 0)
}
// completeWrite is used to signal that an entry in the map has been fully
// written.
func (s *SubmitQueue) completeWrite() {
for {
writes := atomic.LoadUint32(s.writes)
if writes == 0 {
panic("invalid number of sq write completions")
}
if atomic.CompareAndSwapUint32(s.writes, writes, writes-1) {
return
}
runtime.Gosched()
}
}
// CompletionEntry IO completion data structure (Completion Queue Entry).
type CompletionEntry struct {
UserData uint64 /* sqe->data submission data passed back */
Res int32 /* result code for this event */
Flags uint32
}
// IsZero returns if the CQE is zero valued.
func (c *CompletionEntry) IsZero() bool {
return c.UserData == 0 && c.Res == 0 && c.Flags == 0
}
// CompletionQueue represents the completion queue ring buffer.
type CompletionQueue struct {
Size uint32
Head *uint32
Tail *uint32
Mask *uint32
Overflow *uint32
Flags *uint32
// Entries must never be resized, it is mmap'd.
Entries []CompletionEntry
ptr uintptr
}
// Advance is used to advance the completion queue by a count.
func (c *CompletionQueue) Advance(count int) {
atomic.AddUint32(c.Head, uint32(count))
}
// EntryBy (DEPRECATED) returns a CompletionEntry by comparing the user data,
// this should be called after the ring has been entered.
func (c *CompletionQueue) EntryBy(userData uint64) (*CompletionEntry, error) {
head := atomic.LoadUint32(c.Head)
tail := atomic.LoadUint32(c.Tail)
mask := atomic.LoadUint32(c.Mask)
if head&mask == tail&mask {
return nil, ErrEntryNotFound
}
// seenIdx is used for indicating the largest consecutive seen CQEs,
// which is then used for setting the new head position. This is done
// by setting the CqSeenFlag bit on a CQE UserData once a CQE has been
// read. The head is then set to the largest consecutive seen index.
seenIdx := head & mask
seen := false
seenEnd := false
for i := seenIdx; i <= uint32(len(c.Entries)-1); i++ {
cqe := c.Entries[i]
if cqe.Flags&CqSeenFlag == CqSeenFlag || cqe.IsZero() {
seen = true
} else if !seenEnd {
seen = false
seenEnd = true
}
if seen == true && !seenEnd {
seenIdx = i + 1
}
if cqe.UserData == userData {
c.Entries[i].Flags |= CqSeenFlag
if seenIdx == c.Size {
seenIdx = 0
}
atomic.StoreUint32(c.Head, seenIdx)
return &c.Entries[i], nil
}
}
// Handle wrapping.
seenIdx = uint32(0)
seen = false
seenEnd = false
tail = atomic.LoadUint32(c.Tail)
mask = atomic.LoadUint32(c.Mask)
for i := uint32(0); i <= tail&mask; i++ {
cqe := c.Entries[i]
if cqe.Flags&CqSeenFlag == CqSeenFlag || cqe.IsZero() {
seen = true
} else if !seenEnd {
seen = false
seenEnd = true
}
if seen == true && !seenEnd {
seenIdx = i + 1
}
if cqe.UserData == userData {
c.Entries[i].Flags |= CqSeenFlag
if seenIdx == c.Size {
seenIdx = 0
}
atomic.StoreUint32(c.Head, seenIdx)
return &c.Entries[i], nil
}
}
return nil, ErrEntryNotFound
}