1 | /* |
1 | /* |
2 | * libev linux io_uring fd activity backend |
2 | * libev linux io_uring fd activity backend |
3 | * |
3 | * |
4 | * Copyright (c) 2019 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2019-2020 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
44 | * b) best is not necessarily very good. |
44 | * b) best is not necessarily very good. |
45 | * c) it's better than the aio mess, doesn't suffer from the fork problems |
45 | * c) it's better than the aio mess, doesn't suffer from the fork problems |
46 | * of linux aio or epoll and so on and so on. and you could do event stuff |
46 | * of linux aio or epoll and so on and so on. and you could do event stuff |
47 | * without any syscalls. what's not to like? |
47 | * without any syscalls. what's not to like? |
48 | * d) ok, it's vastly more complex, but that's ok, really. |
48 | * d) ok, it's vastly more complex, but that's ok, really. |
49 | * e) why 3 mmaps instead of one? one would be more space-efficient, |
49 | * e) why two mmaps instead of one? one would be more space-efficient, |
50 | * and I can't see what benefit three would have (other than being |
50 | * and I can't see what benefit two would have (other than being |
51 | * somehow resizable/relocatable, but that's apparently not possible). |
51 | * somehow resizable/relocatable, but that's apparently not possible). |
52 | * f) hmm, it's practiclaly undebuggable (gdb can't access the memory, and |
52 | * f) hmm, it's practically undebuggable (gdb can't access the memory, and |
53 | the bizarre way structure offsets are commuinicated makes it hard to |
53 | * the bizarre way structure offsets are communicated makes it hard to |
54 | * just print the ring buffer heads, even *iff* the memory were visible |
54 | * just print the ring buffer heads, even *iff* the memory were visible |
55 | * in gdb. but then, that's also ok, really. |
55 | * in gdb. but then, that's also ok, really. |
56 | * g) well, you cannot specify a timeout when waiting for events. no, |
56 | * g) well, you cannot specify a timeout when waiting for events. no, |
57 | * seriously, the interface doesn't support a timeout. never seen _that_ |
57 | * seriously, the interface doesn't support a timeout. never seen _that_ |
58 | * before. sure, you can use a timerfd, but that's another syscall |
58 | * before. sure, you can use a timerfd, but that's another syscall |
59 | * you could have avoided. overall, this bizarre omission smells |
59 | * you could have avoided. overall, this bizarre omission smells |
60 | * like a µ-optimisation by the io_uring author for his personal |
60 | * like a µ-optimisation by the io_uring author for his personal |
61 | * applications, to the detriment of everybody else who just wants |
61 | * applications, to the detriment of everybody else who just wants |
62 | * an event loop. but, umm, ok, if that's all, it could be worse. |
62 | * an event loop. but, umm, ok, if that's all, it could be worse. |
|
|
63 | * (from what I gather from the author Jens Axboe, it simply didn't |
|
|
64 | * occur to him, and he made good on it by adding an unlimited number |
|
|
65 | * of timeouts later :). |
63 | * h) there is a hardcoded limit of 4096 outstanding events. okay, |
66 | * h) initially there was a hardcoded limit of 4096 outstanding events. |
64 | * at least there is no arbitrary low system-wide limit... |
67 | * later versions not only bump this to 32k, but also can handle |
|
|
68 | * an unlimited amount of events, so this only affects the batch size. |
65 | * i) unlike linux aio, you *can* register more then the limit |
69 | * i) unlike linux aio, you *can* register more then the limit |
66 | * of fd events, and the kernel will "gracefully" signal an |
70 | * of fd events. while early verisons of io_uring signalled an overflow |
67 | * overflow, after which you could destroy and recreate the kernel |
71 | * and you ended up getting wet. 5.5+ does not do this anymore. |
68 | * state, a bit bigger, or fall back to e.g. poll. thats not |
|
|
69 | * totally insane, but kind of questions the point a high |
|
|
70 | * performance I/O framework when it doesn't really work |
|
|
71 | * under stress. |
|
|
72 | * j) but, oh my! is has exactly the same bugs as the linux aio backend, |
72 | * j) but, oh my! it had exactly the same bugs as the linux aio backend, |
73 | * where some undocumented poll combinations just fail. |
73 | * where some undocumented poll combinations just fail. fortunately, |
74 | * so we need epoll AGAIN as a fallback. AGAIN! epoll!! and of course, |
74 | * after finally reaching the author, he was more than willing to fix |
75 | * this is completely undocumented, have I mantioned this already? |
75 | * this probably in 5.6+. |
76 | * k) overall, the *API* itself is, I dare to say, not a total trainwreck. |
76 | * k) overall, the *API* itself is, I dare to say, not a total trainwreck. |
77 | * the big isuess with it are the bugs requiring epoll, which might |
77 | * once the bugs ae fixed (probably in 5.6+), it will be without |
78 | * or might not get fixed (do I hold my breath?). |
78 | * competition. |
79 | */ |
79 | */ |
|
|
80 | |
|
|
81 | /* TODO: use internal TIMEOUT */ |
|
|
82 | /* TODO: take advantage of single mmap, NODROP etc. */ |
|
|
83 | /* TODO: resize cq/sq size independently */ |
80 | |
84 | |
81 | #include <sys/timerfd.h> |
85 | #include <sys/timerfd.h> |
82 | #include <sys/mman.h> |
86 | #include <sys/mman.h> |
83 | #include <poll.h> |
87 | #include <poll.h> |
|
|
88 | #include <stdint.h> |
84 | |
89 | |
85 | #define IOURING_INIT_ENTRIES 32 |
90 | #define IOURING_INIT_ENTRIES 32 |
86 | |
91 | |
87 | /*****************************************************************************/ |
92 | /*****************************************************************************/ |
88 | /* syscall wrapdadoop - this section has the raw api/abi definitions */ |
93 | /* syscall wrapdadoop - this section has the raw api/abi definitions */ |
… | |
… | |
96 | { |
101 | { |
97 | __u8 opcode; |
102 | __u8 opcode; |
98 | __u8 flags; |
103 | __u8 flags; |
99 | __u16 ioprio; |
104 | __u16 ioprio; |
100 | __s32 fd; |
105 | __s32 fd; |
|
|
106 | union { |
101 | __u64 off; |
107 | __u64 off; |
|
|
108 | __u64 addr2; |
|
|
109 | }; |
102 | __u64 addr; |
110 | __u64 addr; |
103 | __u32 len; |
111 | __u32 len; |
104 | union { |
112 | union { |
105 | __kernel_rwf_t rw_flags; |
113 | __kernel_rwf_t rw_flags; |
106 | __u32 fsync_flags; |
114 | __u32 fsync_flags; |
107 | __u16 poll_events; |
115 | __u16 poll_events; |
108 | __u32 sync_range_flags; |
116 | __u32 sync_range_flags; |
109 | __u32 msg_flags; |
117 | __u32 msg_flags; |
|
|
118 | __u32 timeout_flags; |
|
|
119 | __u32 accept_flags; |
|
|
120 | __u32 cancel_flags; |
|
|
121 | __u32 open_flags; |
|
|
122 | __u32 statx_flags; |
|
|
123 | __u32 fadvise_advice; |
110 | }; |
124 | }; |
111 | __u64 user_data; |
125 | __u64 user_data; |
112 | union { |
126 | union { |
113 | __u16 buf_index; |
127 | __u16 buf_index; |
|
|
128 | __u16 personality; |
114 | __u64 __pad2[3]; |
129 | __u64 __pad2[3]; |
115 | }; |
130 | }; |
116 | }; |
131 | }; |
117 | |
132 | |
118 | struct io_uring_cqe |
133 | struct io_uring_cqe |
… | |
… | |
151 | __u32 sq_entries; |
166 | __u32 sq_entries; |
152 | __u32 cq_entries; |
167 | __u32 cq_entries; |
153 | __u32 flags; |
168 | __u32 flags; |
154 | __u32 sq_thread_cpu; |
169 | __u32 sq_thread_cpu; |
155 | __u32 sq_thread_idle; |
170 | __u32 sq_thread_idle; |
|
|
171 | __u32 features; |
156 | __u32 resv[5]; |
172 | __u32 resv[4]; |
157 | struct io_sqring_offsets sq_off; |
173 | struct io_sqring_offsets sq_off; |
158 | struct io_cqring_offsets cq_off; |
174 | struct io_cqring_offsets cq_off; |
159 | }; |
175 | }; |
160 | |
176 | |
|
|
177 | #define IORING_FEAT_SINGLE_MMAP 0x00000001 |
|
|
178 | #define IORING_FEAT_NODROP 0x00000002 |
|
|
179 | #define IORING_FEAT_SUBMIT_STABLE 0x00000004 |
|
|
180 | |
|
|
181 | #define IORING_SETUP_CQSIZE 0x00000008 |
|
|
182 | #define IORING_SETUP_CLAMP 0x00000010 |
|
|
183 | |
161 | #define IORING_OP_POLL_ADD 6 |
184 | #define IORING_OP_POLL_ADD 6 |
162 | #define IORING_OP_POLL_REMOVE 7 |
185 | #define IORING_OP_POLL_REMOVE 7 |
|
|
186 | #define IORING_OP_TIMEOUT 11 |
|
|
187 | #define IORING_OP_TIMEOUT_REMOVE 12 |
|
|
188 | |
|
|
189 | #define IORING_REGISTER_EVENTFD 4 |
|
|
190 | #define IORING_REGISTER_EVENTFD_ASYNC 7 |
|
|
191 | #define IORING_REGISTER_PROBE 8 |
|
|
192 | |
|
|
193 | #define IO_URING_OP_SUPPORTED 1 |
|
|
194 | |
|
|
195 | struct io_uring_probe_op { |
|
|
196 | __u8 op; |
|
|
197 | __u8 resv; |
|
|
198 | __u16 flags; |
|
|
199 | __u32 resv2; |
|
|
200 | }; |
|
|
201 | |
|
|
202 | struct io_uring_probe |
|
|
203 | { |
|
|
204 | __u8 last_op; |
|
|
205 | __u8 ops_len; |
|
|
206 | __u16 resv; |
|
|
207 | __u32 resv2[3]; |
|
|
208 | struct io_uring_probe_op ops[0]; |
|
|
209 | }; |
|
|
210 | |
|
|
211 | /* relative or absolute, reference clock is CLOCK_MONOTONIC */ |
|
|
212 | struct iouring_kernel_timespec |
|
|
213 | { |
|
|
214 | int64_t tv_sec; |
|
|
215 | long long tv_nsec; |
|
|
216 | }; |
|
|
217 | |
|
|
218 | #define IORING_TIMEOUT_ABS 0x00000001 |
163 | |
219 | |
164 | #define IORING_ENTER_GETEVENTS 0x01 |
220 | #define IORING_ENTER_GETEVENTS 0x01 |
165 | |
221 | |
166 | #define IORING_OFF_SQ_RING 0x00000000ULL |
222 | #define IORING_OFF_SQ_RING 0x00000000ULL |
167 | #define IORING_OFF_CQ_RING 0x08000000ULL |
|
|
168 | #define IORING_OFF_SQES 0x10000000ULL |
223 | #define IORING_OFF_SQES 0x10000000ULL |
|
|
224 | |
|
|
225 | #define IORING_FEAT_SINGLE_MMAP 0x00000001 |
|
|
226 | #define IORING_FEAT_NODROP 0x00000002 |
|
|
227 | #define IORING_FEAT_SUBMIT_STABLE 0x00000004 |
169 | |
228 | |
170 | inline_size |
229 | inline_size |
171 | int |
230 | int |
172 | evsys_io_uring_setup (unsigned entries, struct io_uring_params *params) |
231 | evsys_io_uring_setup (unsigned entries, struct io_uring_params *params) |
173 | { |
232 | { |
… | |
… | |
179 | evsys_io_uring_enter (int fd, unsigned to_submit, unsigned min_complete, unsigned flags, const sigset_t *sig, size_t sigsz) |
238 | evsys_io_uring_enter (int fd, unsigned to_submit, unsigned min_complete, unsigned flags, const sigset_t *sig, size_t sigsz) |
180 | { |
239 | { |
181 | return ev_syscall6 (SYS_io_uring_enter, fd, to_submit, min_complete, flags, sig, sigsz); |
240 | return ev_syscall6 (SYS_io_uring_enter, fd, to_submit, min_complete, flags, sig, sigsz); |
182 | } |
241 | } |
183 | |
242 | |
|
|
243 | inline_size |
|
|
244 | int |
|
|
245 | evsys_io_uring_register (unsigned int fd, unsigned int opcode, void *arg, unsigned int nr_args) |
|
|
246 | { |
|
|
247 | return ev_syscall4 (SYS_io_uring_register, fd, opcode, arg, nr_args); |
|
|
248 | } |
|
|
249 | |
184 | /*****************************************************************************/ |
250 | /*****************************************************************************/ |
185 | /* actual backed implementation */ |
251 | /* actual backend implementation */ |
186 | |
252 | |
187 | /* we hope that volatile will make the compiler access this variables only once */ |
253 | /* we hope that volatile will make the compiler access this variables only once */ |
188 | #define EV_SQ_VAR(name) *(volatile unsigned *)((char *)iouring_sq_ring + iouring_sq_ ## name) |
|
|
189 | #define EV_CQ_VAR(name) *(volatile unsigned *)((char *)iouring_cq_ring + iouring_cq_ ## name) |
254 | #define EV_SQ_VAR(name) *(volatile unsigned *)((char *)iouring_ring + iouring_sq_ ## name) |
|
|
255 | #define EV_CQ_VAR(name) *(volatile unsigned *)((char *)iouring_ring + iouring_cq_ ## name) |
190 | |
256 | |
191 | /* the index array */ |
257 | /* the index array */ |
192 | #define EV_SQ_ARRAY ((unsigned *)((char *)iouring_sq_ring + iouring_sq_array)) |
258 | #define EV_SQ_ARRAY ((unsigned *)((char *)iouring_ring + iouring_sq_array)) |
193 | |
259 | |
194 | /* the submit/completion queue entries */ |
260 | /* the submit/completion queue entries */ |
195 | #define EV_SQES ((struct io_uring_sqe *) iouring_sqes) |
261 | #define EV_SQES ((struct io_uring_sqe *) iouring_sqes) |
196 | #define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes)) |
262 | #define EV_CQES ((struct io_uring_cqe *)((char *)iouring_ring + iouring_cq_cqes)) |
|
|
263 | |
|
|
264 | inline_speed |
|
|
265 | int |
|
|
266 | iouring_enter (EV_P_ ev_tstamp timeout) |
|
|
267 | { |
|
|
268 | int res; |
|
|
269 | |
|
|
270 | EV_RELEASE_CB; |
|
|
271 | |
|
|
272 | res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1, |
|
|
273 | timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0); |
|
|
274 | |
|
|
275 | assert (("libev: io_uring_enter did not consume all sqes", (res < 0 || res == iouring_to_submit))); |
|
|
276 | |
|
|
277 | iouring_to_submit = 0; |
|
|
278 | |
|
|
279 | EV_ACQUIRE_CB; |
|
|
280 | |
|
|
281 | return res; |
|
|
282 | } |
|
|
283 | |
|
|
284 | /* TODO: can we move things around so we don't need this forward-reference? */ |
|
|
285 | static void |
|
|
286 | iouring_poll (EV_P_ ev_tstamp timeout); |
197 | |
287 | |
198 | static |
288 | static |
199 | struct io_uring_sqe * |
289 | struct io_uring_sqe * |
200 | iouring_sqe_get (EV_P) |
290 | iouring_sqe_get (EV_P) |
201 | { |
291 | { |
|
|
292 | unsigned tail; |
|
|
293 | |
|
|
294 | for (;;) |
|
|
295 | { |
202 | unsigned tail = EV_SQ_VAR (tail); |
296 | tail = EV_SQ_VAR (tail); |
203 | |
297 | |
204 | if (tail + 1 - EV_SQ_VAR (head) > EV_SQ_VAR (ring_entries)) |
298 | if (ecb_expect_true (tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries))) |
|
|
299 | break; /* whats the problem, we have free sqes */ |
|
|
300 | |
|
|
301 | /* queue full, need to flush and possibly handle some events */ |
|
|
302 | |
|
|
303 | #if EV_FEATURE_CODE |
|
|
304 | /* first we ask the kernel nicely, most often this frees up some sqes */ |
|
|
305 | int res = iouring_enter (EV_A_ EV_TS_CONST (0.)); |
|
|
306 | |
|
|
307 | ECB_MEMORY_FENCE_ACQUIRE; /* better safe than sorry */ |
|
|
308 | |
|
|
309 | if (res >= 0) |
|
|
310 | continue; /* yes, it worked, try again */ |
|
|
311 | #endif |
|
|
312 | |
|
|
313 | /* some problem, possibly EBUSY - do the full poll and let it handle any issues */ |
|
|
314 | |
|
|
315 | iouring_poll (EV_A_ EV_TS_CONST (0.)); |
|
|
316 | /* iouring_poll should have done ECB_MEMORY_FENCE_ACQUIRE for us */ |
205 | { |
317 | } |
206 | /* queue full, flush */ |
|
|
207 | evsys_io_uring_enter (iouring_fd, iouring_to_submit, 0, 0, 0, 0); |
|
|
208 | iouring_to_submit = 0; |
|
|
209 | } |
|
|
210 | |
318 | |
211 | assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries))); |
319 | /*assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)));*/ |
212 | |
320 | |
213 | return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); |
321 | return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); |
214 | } |
322 | } |
215 | |
323 | |
216 | inline_size |
324 | inline_size |
217 | struct io_uring_sqe * |
325 | void |
218 | iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe) |
326 | iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe) |
219 | { |
327 | { |
220 | unsigned idx = sqe - EV_SQES; |
328 | unsigned idx = sqe - EV_SQES; |
221 | |
329 | |
222 | EV_SQ_ARRAY [idx] = idx; |
330 | EV_SQ_ARRAY [idx] = idx; |
… | |
… | |
236 | iouring_tfd_cb (EV_P_ struct ev_io *w, int revents) |
344 | iouring_tfd_cb (EV_P_ struct ev_io *w, int revents) |
237 | { |
345 | { |
238 | iouring_tfd_to = EV_TSTAMP_HUGE; |
346 | iouring_tfd_to = EV_TSTAMP_HUGE; |
239 | } |
347 | } |
240 | |
348 | |
241 | static void |
|
|
242 | iouring_epoll_cb (EV_P_ struct ev_io *w, int revents) |
|
|
243 | { |
|
|
244 | epoll_poll (EV_A_ 0); |
|
|
245 | } |
|
|
246 | |
|
|
247 | /* called for full and partial cleanup */ |
349 | /* called for full and partial cleanup */ |
248 | ecb_cold |
350 | ecb_cold |
249 | static int |
351 | static void |
250 | iouring_internal_destroy (EV_P) |
352 | iouring_internal_destroy (EV_P) |
251 | { |
353 | { |
252 | close (iouring_tfd); |
354 | close (iouring_tfd); |
253 | close (iouring_fd); |
355 | close (iouring_fd); |
254 | |
356 | |
255 | if (iouring_sq_ring != MAP_FAILED) munmap (iouring_sq_ring, iouring_sq_ring_size); |
357 | if (iouring_ring != MAP_FAILED) munmap (iouring_ring, iouring_ring_size); |
256 | if (iouring_cq_ring != MAP_FAILED) munmap (iouring_cq_ring, iouring_cq_ring_size); |
|
|
257 | if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes , iouring_sqes_size ); |
358 | if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes, iouring_sqes_size); |
258 | |
359 | |
259 | if (ev_is_active (&iouring_epoll_w)) ev_ref (EV_A); ev_io_stop (EV_A_ &iouring_epoll_w); |
360 | if (ev_is_active (&iouring_tfd_w)) |
260 | if (ev_is_active (&iouring_tfd_w )) ev_ref (EV_A); ev_io_stop (EV_A_ &iouring_tfd_w ); |
361 | { |
|
|
362 | ev_ref (EV_A); |
|
|
363 | ev_io_stop (EV_A_ &iouring_tfd_w); |
|
|
364 | } |
261 | } |
365 | } |
262 | |
366 | |
263 | ecb_cold |
367 | ecb_cold |
264 | static int |
368 | static int |
265 | iouring_internal_init (EV_P) |
369 | iouring_internal_init (EV_P) |
266 | { |
370 | { |
267 | struct io_uring_params params = { 0 }; |
371 | struct io_uring_params params = { 0 }; |
|
|
372 | uint32_t sq_size, cq_size; |
|
|
373 | |
|
|
374 | params.flags = IORING_SETUP_CLAMP; |
268 | |
375 | |
269 | iouring_to_submit = 0; |
376 | iouring_to_submit = 0; |
270 | |
377 | |
271 | iouring_tfd = -1; |
378 | iouring_tfd = -1; |
272 | iouring_sq_ring = MAP_FAILED; |
379 | iouring_ring = MAP_FAILED; |
273 | iouring_cq_ring = MAP_FAILED; |
|
|
274 | iouring_sqes = MAP_FAILED; |
380 | iouring_sqes = MAP_FAILED; |
275 | |
381 | |
276 | for (;;) |
382 | if (!have_monotonic) /* cannot really happen, but what if11 */ |
277 | { |
383 | return -1; |
|
|
384 | |
278 | iouring_fd = evsys_io_uring_setup (iouring_entries, ¶ms); |
385 | iouring_fd = evsys_io_uring_setup (iouring_entries, ¶ms); |
279 | |
386 | |
280 | if (iouring_fd >= 0) |
387 | if (iouring_fd < 0) |
281 | break; /* yippie */ |
|
|
282 | |
|
|
283 | if (errno != EINVAL) |
|
|
284 | return -1; /* we failed */ |
|
|
285 | |
|
|
286 | /* EINVAL: lots of possible reasons, but maybe |
|
|
287 | * it is because we hit the unqueryable hardcoded size limit |
|
|
288 | */ |
|
|
289 | |
|
|
290 | /* we hit the limit already, give up */ |
|
|
291 | if (iouring_max_entries) |
|
|
292 | return -1; |
388 | return -1; |
293 | |
389 | |
294 | /* first time we hit EINVAL? assume we hit the limit, so go back and retry */ |
390 | if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEAT_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE)) |
295 | iouring_entries >>= 1; |
391 | return -1; /* we require the above features */ |
296 | iouring_max_entries = iouring_entries; |
|
|
297 | } |
|
|
298 | |
392 | |
|
|
393 | /* TODO: remember somehow whether our queue size has been clamped */ |
|
|
394 | |
299 | iouring_sq_ring_size = params.sq_off.array + params.sq_entries * sizeof (unsigned); |
395 | sq_size = params.sq_off.array + params.sq_entries * sizeof (unsigned); |
300 | iouring_cq_ring_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe); |
396 | cq_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe); |
|
|
397 | |
|
|
398 | iouring_ring_size = sq_size > cq_size ? sq_size : cq_size; |
301 | iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe); |
399 | iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe); |
302 | |
400 | |
303 | iouring_sq_ring = mmap (0, iouring_sq_ring_size, PROT_READ | PROT_WRITE, |
401 | iouring_ring = mmap (0, iouring_ring_size, PROT_READ | PROT_WRITE, |
304 | MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING); |
402 | MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING); |
305 | iouring_cq_ring = mmap (0, iouring_cq_ring_size, PROT_READ | PROT_WRITE, |
|
|
306 | MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_CQ_RING); |
|
|
307 | iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE, |
403 | iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE, |
308 | MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES); |
404 | MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES); |
309 | |
405 | |
310 | if (iouring_sq_ring == MAP_FAILED || iouring_cq_ring == MAP_FAILED || iouring_sqes == MAP_FAILED) |
406 | if (iouring_ring == MAP_FAILED || iouring_sqes == MAP_FAILED) |
311 | return -1; |
407 | return -1; |
312 | |
408 | |
313 | iouring_sq_head = params.sq_off.head; |
409 | iouring_sq_head = params.sq_off.head; |
314 | iouring_sq_tail = params.sq_off.tail; |
410 | iouring_sq_tail = params.sq_off.tail; |
315 | iouring_sq_ring_mask = params.sq_off.ring_mask; |
411 | iouring_sq_ring_mask = params.sq_off.ring_mask; |
… | |
… | |
323 | iouring_cq_ring_mask = params.cq_off.ring_mask; |
419 | iouring_cq_ring_mask = params.cq_off.ring_mask; |
324 | iouring_cq_ring_entries = params.cq_off.ring_entries; |
420 | iouring_cq_ring_entries = params.cq_off.ring_entries; |
325 | iouring_cq_overflow = params.cq_off.overflow; |
421 | iouring_cq_overflow = params.cq_off.overflow; |
326 | iouring_cq_cqes = params.cq_off.cqes; |
422 | iouring_cq_cqes = params.cq_off.cqes; |
327 | |
423 | |
|
|
424 | iouring_tfd_to = EV_TSTAMP_HUGE; |
|
|
425 | |
328 | iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC); |
426 | iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC); |
329 | |
427 | |
330 | if (iouring_tfd < 0) |
428 | if (iouring_tfd < 0) |
331 | return iouring_tfd; |
429 | return -1; |
332 | |
|
|
333 | iouring_tfd_to = EV_TSTAMP_HUGE; |
|
|
334 | |
430 | |
335 | return 0; |
431 | return 0; |
336 | } |
432 | } |
337 | |
433 | |
338 | ecb_cold |
434 | ecb_cold |
… | |
… | |
342 | iouring_internal_destroy (EV_A); |
438 | iouring_internal_destroy (EV_A); |
343 | |
439 | |
344 | while (iouring_internal_init (EV_A) < 0) |
440 | while (iouring_internal_init (EV_A) < 0) |
345 | ev_syserr ("(libev) io_uring_setup"); |
441 | ev_syserr ("(libev) io_uring_setup"); |
346 | |
442 | |
347 | /* forking epoll should also effectively unregister all fds from the backend */ |
|
|
348 | epoll_fork (EV_A); |
|
|
349 | /* epoll_fork already did this. hopefully */ |
|
|
350 | /*fd_rearm_all (EV_A);*/ |
443 | fd_rearm_all (EV_A); |
351 | |
|
|
352 | ev_io_stop (EV_A_ &iouring_epoll_w); |
|
|
353 | ev_io_set (EV_A_ &iouring_epoll_w, backend_fd, EV_READ); |
|
|
354 | ev_io_start (EV_A_ &iouring_epoll_w); |
|
|
355 | |
444 | |
356 | ev_io_stop (EV_A_ &iouring_tfd_w); |
445 | ev_io_stop (EV_A_ &iouring_tfd_w); |
357 | ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ); |
446 | ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ); |
358 | ev_io_start (EV_A_ &iouring_tfd_w); |
447 | ev_io_start (EV_A_ &iouring_tfd_w); |
359 | } |
448 | } |
… | |
… | |
361 | /*****************************************************************************/ |
450 | /*****************************************************************************/ |
362 | |
451 | |
363 | static void |
452 | static void |
364 | iouring_modify (EV_P_ int fd, int oev, int nev) |
453 | iouring_modify (EV_P_ int fd, int oev, int nev) |
365 | { |
454 | { |
366 | if (ecb_expect_false (anfds [fd].eflags)) |
|
|
367 | { |
|
|
368 | /* we handed this fd over to epoll, so undo this first */ |
|
|
369 | /* we do it manually because the optimisations on epoll_modify won't do us any good */ |
|
|
370 | epoll_ctl (iouring_fd, EPOLL_CTL_DEL, fd, 0); |
|
|
371 | anfds [fd].eflags = 0; |
|
|
372 | oev = 0; |
|
|
373 | } |
|
|
374 | |
|
|
375 | if (oev) |
455 | if (oev) |
376 | { |
456 | { |
377 | /* we assume the sqe's are all "properly" initialised */ |
457 | /* we assume the sqe's are all "properly" initialised */ |
378 | struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); |
458 | struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); |
379 | sqe->opcode = IORING_OP_POLL_REMOVE; |
459 | sqe->opcode = IORING_OP_POLL_REMOVE; |
380 | sqe->fd = fd; |
460 | sqe->fd = fd; |
|
|
461 | /* Jens Axboe notified me that user_data is not what is documented, but is |
|
|
462 | * some kind of unique ID that has to match, otherwise the request cannot |
|
|
463 | * be removed. Since we don't *really* have that, we pass in the old |
|
|
464 | * generation counter - if that fails, too bad, it will hopefully be removed |
|
|
465 | * at close time and then be ignored. */ |
|
|
466 | sqe->addr = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); |
381 | sqe->user_data = -1; |
467 | sqe->user_data = (uint64_t)-1; |
382 | iouring_sqe_submit (EV_A_ sqe); |
468 | iouring_sqe_submit (EV_A_ sqe); |
383 | |
469 | |
384 | /* increment generation counter to avoid handling old events */ |
470 | /* increment generation counter to avoid handling old events */ |
385 | ++anfds [fd].egen; |
471 | ++anfds [fd].egen; |
386 | } |
472 | } |
… | |
… | |
388 | if (nev) |
474 | if (nev) |
389 | { |
475 | { |
390 | struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); |
476 | struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); |
391 | sqe->opcode = IORING_OP_POLL_ADD; |
477 | sqe->opcode = IORING_OP_POLL_ADD; |
392 | sqe->fd = fd; |
478 | sqe->fd = fd; |
|
|
479 | sqe->addr = 0; |
393 | sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); |
480 | sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); |
394 | sqe->poll_events = |
481 | sqe->poll_events = |
395 | (nev & EV_READ ? POLLIN : 0) |
482 | (nev & EV_READ ? POLLIN : 0) |
396 | | (nev & EV_WRITE ? POLLOUT : 0); |
483 | | (nev & EV_WRITE ? POLLOUT : 0); |
397 | iouring_sqe_submit (EV_A_ sqe); |
484 | iouring_sqe_submit (EV_A_ sqe); |
… | |
… | |
427 | { |
514 | { |
428 | int fd = cqe->user_data & 0xffffffffU; |
515 | int fd = cqe->user_data & 0xffffffffU; |
429 | uint32_t gen = cqe->user_data >> 32; |
516 | uint32_t gen = cqe->user_data >> 32; |
430 | int res = cqe->res; |
517 | int res = cqe->res; |
431 | |
518 | |
432 | /* ignore fd removal events, if there are any. TODO: verify */ |
519 | /* user_data -1 is a remove that we are not atm. interested in */ |
433 | if (cqe->user_data == (__u64)-1) |
520 | if (cqe->user_data == (uint64_t)-1) |
434 | abort ();//D |
521 | return; |
435 | |
522 | |
436 | assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
523 | assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
437 | |
524 | |
438 | /* documentation lies, of course. the result value is NOT like |
525 | /* documentation lies, of course. the result value is NOT like |
439 | * normal syscalls, but like linux raw syscalls, i.e. negative |
526 | * normal syscalls, but like linux raw syscalls, i.e. negative |
440 | * error numbers. fortunate, as otherwise there would be no way |
527 | * error numbers. fortunate, as otherwise there would be no way |
441 | * to get error codes at all. still, why not document this? |
528 | * to get error codes at all. still, why not document this? |
442 | */ |
529 | */ |
443 | |
530 | |
444 | /* ignore event if generation doesn't match */ |
531 | /* ignore event if generation doesn't match */ |
|
|
532 | /* other than skipping removal events, */ |
445 | /* this should actually be very rare */ |
533 | /* this should actually be very rare */ |
446 | if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) |
534 | if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) |
447 | return; |
535 | return; |
448 | |
536 | |
449 | if (ecb_expect_false (res < 0)) |
537 | if (ecb_expect_false (res < 0)) |
450 | { |
538 | { |
451 | if (res == -EINVAL) |
539 | /*TODO: EINVAL handling (was something failed with this fd)*/ |
452 | { |
|
|
453 | /* we assume this error code means the fd/poll combination is buggy |
|
|
454 | * and fall back to epoll. |
|
|
455 | * this error code might also indicate a bug, but the kernel doesn't |
|
|
456 | * distinguish between those two conditions, so... sigh... |
|
|
457 | */ |
|
|
458 | |
540 | |
459 | epoll_modify (EV_A_ fd, 0, anfds [fd].events); |
|
|
460 | } |
|
|
461 | else if (res == -EBADF) |
541 | if (res == -EBADF) |
462 | { |
542 | { |
463 | assert (("libev: event loop rejected bad fd", res != -EBADF)); |
543 | assert (("libev: event loop rejected bad fd", res != -EBADF)); |
464 | fd_kill (EV_A_ fd); |
544 | fd_kill (EV_A_ fd); |
465 | } |
545 | } |
466 | else |
546 | else |
… | |
… | |
492 | iouring_overflow (EV_P) |
572 | iouring_overflow (EV_P) |
493 | { |
573 | { |
494 | /* we have two options, resize the queue (by tearing down |
574 | /* we have two options, resize the queue (by tearing down |
495 | * everything and recreating it, or living with it |
575 | * everything and recreating it, or living with it |
496 | * and polling. |
576 | * and polling. |
497 | * we implement this by resizing tghe queue, and, if that fails, |
577 | * we implement this by resizing the queue, and, if that fails, |
498 | * we just recreate the state on every failure, which |
578 | * we just recreate the state on every failure, which |
499 | * kind of is a very inefficient poll. |
579 | * kind of is a very inefficient poll. |
500 | * one danger is, due to the bios toward lower fds, |
580 | * one danger is, due to the bios toward lower fds, |
501 | * we will only really get events for those, so |
581 | * we will only really get events for those, so |
502 | * maybe we need a poll() fallback, after all. |
582 | * maybe we need a poll() fallback, after all. |
… | |
… | |
514 | else |
594 | else |
515 | { |
595 | { |
516 | /* we hit the kernel limit, we should fall back to something else. |
596 | /* we hit the kernel limit, we should fall back to something else. |
517 | * we can either poll() a few times and hope for the best, |
597 | * we can either poll() a few times and hope for the best, |
518 | * poll always, or switch to epoll. |
598 | * poll always, or switch to epoll. |
519 | * since we use epoll anyways, go epoll. |
599 | * TODO: is this necessary with newer kernels? |
520 | */ |
600 | */ |
521 | |
601 | |
522 | iouring_internal_destroy (EV_A); |
602 | iouring_internal_destroy (EV_A); |
523 | |
603 | |
524 | /* this should make it so that on return, we don'T call any uring functions */ |
604 | /* this should make it so that on return, we don't call any uring functions */ |
525 | iouring_to_submit = 0; |
605 | iouring_to_submit = 0; |
526 | |
606 | |
527 | for (;;) |
607 | for (;;) |
528 | { |
608 | { |
529 | backend = epoll_init (EV_A_ 0); |
609 | backend = epoll_init (EV_A_ 0); |
… | |
… | |
570 | |
650 | |
571 | static void |
651 | static void |
572 | iouring_poll (EV_P_ ev_tstamp timeout) |
652 | iouring_poll (EV_P_ ev_tstamp timeout) |
573 | { |
653 | { |
574 | /* if we have events, no need for extra syscalls, but we might have to queue events */ |
654 | /* if we have events, no need for extra syscalls, but we might have to queue events */ |
|
|
655 | /* we also clar the timeout if there are outstanding fdchanges */ |
|
|
656 | /* the latter should only happen if both the sq and cq are full, most likely */ |
|
|
657 | /* because we have a lot of event sources that immediately complete */ |
|
|
658 | /* TODO: fdchacngecnt is always 0 because fd_reify does not have two buffers yet */ |
575 | if (iouring_handle_cq (EV_A)) |
659 | if (iouring_handle_cq (EV_A) || fdchangecnt) |
576 | timeout = EV_TS_CONST (0.); |
660 | timeout = EV_TS_CONST (0.); |
577 | else |
661 | else |
578 | /* no events, so maybe wait for some */ |
662 | /* no events, so maybe wait for some */ |
579 | iouring_tfd_update (EV_A_ timeout); |
663 | iouring_tfd_update (EV_A_ timeout); |
580 | |
664 | |
581 | /* only enter the kernel if we have somethign to submit, or we need to wait */ |
665 | /* only enter the kernel if we have something to submit, or we need to wait */ |
582 | if (timeout || iouring_to_submit) |
666 | if (timeout || iouring_to_submit) |
583 | { |
667 | { |
584 | int res; |
668 | int res = iouring_enter (EV_A_ timeout); |
585 | |
|
|
586 | EV_RELEASE_CB; |
|
|
587 | |
|
|
588 | res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1, |
|
|
589 | timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0); |
|
|
590 | iouring_to_submit = 0; |
|
|
591 | |
|
|
592 | EV_ACQUIRE_CB; |
|
|
593 | |
669 | |
594 | if (ecb_expect_false (res < 0)) |
670 | if (ecb_expect_false (res < 0)) |
595 | if (errno == EINTR) |
671 | if (errno == EINTR) |
596 | /* ignore */; |
672 | /* ignore */; |
|
|
673 | else if (errno == EBUSY) |
|
|
674 | /* cq full, cannot submit - should be rare because we flush the cq first, so simply ignore */; |
597 | else |
675 | else |
598 | ev_syserr ("(libev) iouring setup"); |
676 | ev_syserr ("(libev) iouring setup"); |
599 | else |
677 | else |
600 | iouring_handle_cq (EV_A); |
678 | iouring_handle_cq (EV_A); |
601 | } |
679 | } |
… | |
… | |
603 | |
681 | |
604 | inline_size |
682 | inline_size |
605 | int |
683 | int |
606 | iouring_init (EV_P_ int flags) |
684 | iouring_init (EV_P_ int flags) |
607 | { |
685 | { |
608 | if (!epoll_init (EV_A_ 0)) |
|
|
609 | return 0; |
|
|
610 | |
|
|
611 | ev_io_init (&iouring_epoll_w, iouring_epoll_cb, backend_fd, EV_READ); |
|
|
612 | ev_set_priority (&iouring_epoll_w, EV_MAXPRI); |
|
|
613 | |
|
|
614 | ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ); |
|
|
615 | ev_set_priority (&iouring_tfd_w, EV_MAXPRI); |
|
|
616 | |
|
|
617 | iouring_entries = IOURING_INIT_ENTRIES; |
686 | iouring_entries = IOURING_INIT_ENTRIES; |
618 | iouring_max_entries = 0; |
687 | iouring_max_entries = 0; |
619 | |
688 | |
620 | if (iouring_internal_init (EV_A) < 0) |
689 | if (iouring_internal_init (EV_A) < 0) |
621 | { |
690 | { |
622 | iouring_internal_destroy (EV_A); |
691 | iouring_internal_destroy (EV_A); |
623 | return 0; |
692 | return 0; |
624 | } |
693 | } |
625 | |
694 | |
626 | ev_io_start (EV_A_ &iouring_epoll_w); |
695 | ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ); |
627 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
696 | ev_set_priority (&iouring_tfd_w, EV_MINPRI); |
628 | |
|
|
629 | ev_io_start (EV_A_ &iouring_tfd_w); |
697 | ev_io_start (EV_A_ &iouring_tfd_w); |
630 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
698 | ev_unref (EV_A); /* watcher should not keep loop alive */ |
631 | |
699 | |
632 | backend_modify = iouring_modify; |
700 | backend_modify = iouring_modify; |
633 | backend_poll = iouring_poll; |
701 | backend_poll = iouring_poll; |
… | |
… | |
638 | inline_size |
706 | inline_size |
639 | void |
707 | void |
640 | iouring_destroy (EV_P) |
708 | iouring_destroy (EV_P) |
641 | { |
709 | { |
642 | iouring_internal_destroy (EV_A); |
710 | iouring_internal_destroy (EV_A); |
643 | epoll_destroy (EV_A); |
|
|
644 | } |
711 | } |
645 | |
712 | |