1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // io/setup.cfa -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Fri Jul 31 16:25:51 2020 |
---|
11 | // Last Modified By : |
---|
12 | // Last Modified On : |
---|
13 | // Update Count : |
---|
14 | // |
---|
15 | |
---|
16 | #define __cforall_thread__ |
---|
17 | #define _GNU_SOURCE /* See feature_test_macros(7) */ |
---|
18 | |
---|
19 | #if defined(__CFA_DEBUG__) |
---|
20 | // #define __CFA_DEBUG_PRINT_IO__ |
---|
21 | // #define __CFA_DEBUG_PRINT_IO_CORE__ |
---|
22 | #endif |
---|
23 | |
---|
24 | #include "io/types.hfa" |
---|
25 | #include "kernel.hfa" |
---|
26 | |
---|
27 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
28 | void __kernel_io_startup() { |
---|
29 | // Nothing to do without io_uring |
---|
30 | } |
---|
31 | |
---|
32 | void __kernel_io_shutdown() { |
---|
33 | // Nothing to do without io_uring |
---|
34 | } |
---|
35 | |
---|
36 | void ?{}(io_context_params & this) {} |
---|
37 | |
---|
38 | void ?{}(io_context & this, struct cluster & cl) {} |
---|
39 | void ?{}(io_context & this, struct cluster & cl, const io_context_params & params) {} |
---|
40 | |
---|
41 | void ^?{}(io_context & this) {} |
---|
42 | void ^?{}(io_context & this, bool cluster_context) {} |
---|
43 | |
---|
44 | void register_fixed_files( io_context &, int *, unsigned ) {} |
---|
45 | void register_fixed_files( cluster &, int *, unsigned ) {} |
---|
46 | |
---|
47 | #else |
---|
48 | #include <errno.h> |
---|
49 | #include <stdint.h> |
---|
50 | #include <string.h> |
---|
51 | #include <signal.h> |
---|
52 | #include <unistd.h> |
---|
53 | |
---|
54 | extern "C" { |
---|
55 | #include <pthread.h> |
---|
56 | #include <sys/epoll.h> |
---|
57 | #include <sys/eventfd.h> |
---|
58 | #include <sys/mman.h> |
---|
59 | #include <sys/syscall.h> |
---|
60 | |
---|
61 | #include <linux/io_uring.h> |
---|
62 | } |
---|
63 | |
---|
64 | #include "bitmanip.hfa" |
---|
65 | #include "kernel_private.hfa" |
---|
66 | #include "thread.hfa" |
---|
67 | |
---|
68 | void ?{}(io_context_params & this) { |
---|
69 | this.num_entries = 256; |
---|
70 | this.num_ready = 256; |
---|
71 | this.submit_aff = -1; |
---|
72 | this.eager_submits = false; |
---|
73 | this.poller_submits = false; |
---|
74 | this.poll_submit = false; |
---|
75 | this.poll_complete = false; |
---|
76 | } |
---|
77 | |
---|
78 | static void * __io_poller_slow( void * arg ); |
---|
79 | |
---|
80 | // Weirdly, some systems that do support io_uring don't actually define these |
---|
81 | #ifdef __alpha__ |
---|
82 | /* |
---|
83 | * alpha is the only exception, all other architectures |
---|
84 | * have common numbers for new system calls. |
---|
85 | */ |
---|
86 | #ifndef __NR_io_uring_setup |
---|
87 | #define __NR_io_uring_setup 535 |
---|
88 | #endif |
---|
89 | #ifndef __NR_io_uring_enter |
---|
90 | #define __NR_io_uring_enter 536 |
---|
91 | #endif |
---|
92 | #ifndef __NR_io_uring_register |
---|
93 | #define __NR_io_uring_register 537 |
---|
94 | #endif |
---|
95 | #else /* !__alpha__ */ |
---|
96 | #ifndef __NR_io_uring_setup |
---|
97 | #define __NR_io_uring_setup 425 |
---|
98 | #endif |
---|
99 | #ifndef __NR_io_uring_enter |
---|
100 | #define __NR_io_uring_enter 426 |
---|
101 | #endif |
---|
102 | #ifndef __NR_io_uring_register |
---|
103 | #define __NR_io_uring_register 427 |
---|
104 | #endif |
---|
105 | #endif |
---|
106 | |
---|
107 | //============================================================================================= |
---|
108 | // I/O Startup / Shutdown logic + Master Poller |
---|
109 | //============================================================================================= |
---|
110 | |
---|
111 | // IO Master poller loop forward |
---|
112 | static void * iopoll_loop( __attribute__((unused)) void * args ); |
---|
113 | |
---|
114 | static struct { |
---|
115 | pthread_t thrd; // pthread handle to io poller thread |
---|
116 | void * stack; // pthread stack for io poller thread |
---|
117 | int epollfd; // file descriptor to the epoll instance |
---|
118 | volatile bool run; // Whether or not to continue |
---|
119 | volatile size_t epoch; // Epoch used for memory reclamation |
---|
120 | } iopoll; |
---|
121 | |
---|
122 | void __kernel_io_startup(void) { |
---|
123 | __cfadbg_print_safe(io_core, "Kernel : Creating EPOLL instance\n" ); |
---|
124 | |
---|
125 | iopoll.epollfd = epoll_create1(0); |
---|
126 | if (iopoll.epollfd == -1) { |
---|
127 | abort( "internal error, epoll_create1\n"); |
---|
128 | } |
---|
129 | |
---|
130 | __cfadbg_print_safe(io_core, "Kernel : Starting io poller thread\n" ); |
---|
131 | |
---|
132 | iopoll.run = true; |
---|
133 | iopoll.stack = __create_pthread( &iopoll.thrd, iopoll_loop, 0p ); |
---|
134 | iopoll.epoch = 0; |
---|
135 | } |
---|
136 | |
---|
137 | void __kernel_io_shutdown(void) { |
---|
138 | // Notify the io poller thread of the shutdown |
---|
139 | iopoll.run = false; |
---|
140 | sigval val = { 1 }; |
---|
141 | pthread_sigqueue( iopoll.thrd, SIGUSR1, val ); |
---|
142 | |
---|
143 | // Wait for the io poller thread to finish |
---|
144 | |
---|
145 | __destroy_pthread( iopoll.thrd, iopoll.stack, 0p ); |
---|
146 | |
---|
147 | int ret = close(iopoll.epollfd); |
---|
148 | if (ret == -1) { |
---|
149 | abort( "internal error, close epoll\n"); |
---|
150 | } |
---|
151 | |
---|
152 | // Io polling is now fully stopped |
---|
153 | |
---|
154 | __cfadbg_print_safe(io_core, "Kernel : IO poller stopped\n" ); |
---|
155 | } |
---|
156 | |
---|
157 | static void * iopoll_loop( __attribute__((unused)) void * args ) { |
---|
158 | __processor_id_t id; |
---|
159 | id.full_proc = false; |
---|
160 | id.id = doregister(&id); |
---|
161 | __cfaabi_tls.this_proc_id = &id; |
---|
162 | __cfadbg_print_safe(io_core, "Kernel : IO poller thread starting\n" ); |
---|
163 | |
---|
164 | // Block signals to control when they arrive |
---|
165 | sigset_t mask; |
---|
166 | sigfillset(&mask); |
---|
167 | if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) { |
---|
168 | abort( "internal error, pthread_sigmask" ); |
---|
169 | } |
---|
170 | |
---|
171 | sigdelset( &mask, SIGUSR1 ); |
---|
172 | |
---|
173 | // Create sufficient events |
---|
174 | struct epoll_event events[10]; |
---|
175 | // Main loop |
---|
176 | while( iopoll.run ) { |
---|
177 | __cfadbg_print_safe(io_core, "Kernel I/O - epoll : waiting on io_uring contexts\n"); |
---|
178 | |
---|
179 | // increment the epoch to notify any deleters we are starting a new cycle |
---|
180 | __atomic_fetch_add(&iopoll.epoch, 1, __ATOMIC_SEQ_CST); |
---|
181 | |
---|
182 | // Wait for events |
---|
183 | int nfds = epoll_pwait( iopoll.epollfd, events, 10, -1, &mask ); |
---|
184 | |
---|
185 | __cfadbg_print_safe(io_core, "Kernel I/O - epoll : %d io contexts events, waking up\n", nfds); |
---|
186 | |
---|
187 | // Check if an error occured |
---|
188 | if (nfds == -1) { |
---|
189 | if( errno == EINTR ) continue; |
---|
190 | abort( "internal error, pthread_sigmask" ); |
---|
191 | } |
---|
192 | |
---|
193 | for(i; nfds) { |
---|
194 | $io_ctx_thread * io_ctx = ($io_ctx_thread *)(uintptr_t)events[i].data.u64; |
---|
195 | /* paranoid */ verify( io_ctx ); |
---|
196 | __cfadbg_print_safe(io_core, "Kernel I/O - epoll : Unparking io poller %d (%p)\n", io_ctx->ring->fd, io_ctx); |
---|
197 | #if !defined( __CFA_NO_STATISTICS__ ) |
---|
198 | __cfaabi_tls.this_stats = io_ctx->self.curr_cluster->stats; |
---|
199 | #endif |
---|
200 | |
---|
201 | eventfd_t v; |
---|
202 | eventfd_read(io_ctx->ring->efd, &v); |
---|
203 | |
---|
204 | post( io_ctx->sem ); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | __cfadbg_print_safe(io_core, "Kernel : IO poller thread stopping\n" ); |
---|
209 | unregister(&id); |
---|
210 | return 0p; |
---|
211 | } |
---|
212 | |
---|
213 | //============================================================================================= |
---|
214 | // I/O Context Constrution/Destruction |
---|
215 | //============================================================================================= |
---|
216 | |
---|
217 | void ?{}($io_ctx_thread & this, struct cluster & cl) { (this.self){ "IO Poller", cl }; } |
---|
218 | void main( $io_ctx_thread & this ); |
---|
219 | static inline $thread * get_thread( $io_ctx_thread & this ) { return &this.self; } |
---|
220 | void ^?{}( $io_ctx_thread & mutex this ) {} |
---|
221 | |
---|
222 | static void __io_create ( __io_data & this, const io_context_params & params_in ); |
---|
223 | static void __io_destroy( __io_data & this ); |
---|
224 | |
---|
225 | void ?{}(io_context & this, struct cluster & cl, const io_context_params & params) { |
---|
226 | (this.thrd){ cl }; |
---|
227 | this.thrd.ring = malloc(); |
---|
228 | __cfadbg_print_safe(io_core, "Kernel I/O : Creating ring for io_context %p\n", &this); |
---|
229 | __io_create( *this.thrd.ring, params ); |
---|
230 | |
---|
231 | __cfadbg_print_safe(io_core, "Kernel I/O : Starting poller thread for io_context %p\n", &this); |
---|
232 | this.thrd.done = false; |
---|
233 | __thrd_start( this.thrd, main ); |
---|
234 | |
---|
235 | __cfadbg_print_safe(io_core, "Kernel I/O : io_context %p ready\n", &this); |
---|
236 | } |
---|
237 | |
---|
238 | void ?{}(io_context & this, struct cluster & cl) { |
---|
239 | io_context_params params; |
---|
240 | (this){ cl, params }; |
---|
241 | } |
---|
242 | |
---|
243 | void ^?{}(io_context & this, bool cluster_context) { |
---|
244 | __cfadbg_print_safe(io_core, "Kernel I/O : tearing down io_context %p\n", &this); |
---|
245 | |
---|
246 | // Notify the thread of the shutdown |
---|
247 | __atomic_store_n(&this.thrd.done, true, __ATOMIC_SEQ_CST); |
---|
248 | |
---|
249 | // If this is an io_context within a cluster, things get trickier |
---|
250 | $thread & thrd = this.thrd.self; |
---|
251 | if( cluster_context ) { |
---|
252 | // We are about to do weird things with the threads |
---|
253 | // we don't need interrupts to complicate everything |
---|
254 | disable_interrupts(); |
---|
255 | |
---|
256 | // Get cluster info |
---|
257 | cluster & cltr = *thrd.curr_cluster; |
---|
258 | /* paranoid */ verify( cltr.idles.total == 0 || &cltr == mainCluster ); |
---|
259 | /* paranoid */ verify( !ready_mutate_islocked() ); |
---|
260 | |
---|
261 | // We need to adjust the clean-up based on where the thread is |
---|
262 | if( thrd.state == Ready || thrd.preempted != __NO_PREEMPTION ) { |
---|
263 | // This is the tricky case |
---|
264 | // The thread was preempted or ready to run and now it is on the ready queue |
---|
265 | // but the cluster is shutting down, so there aren't any processors to run the ready queue |
---|
266 | // the solution is to steal the thread from the ready-queue and pretend it was blocked all along |
---|
267 | |
---|
268 | ready_schedule_lock(); |
---|
269 | // The thread should on the list |
---|
270 | /* paranoid */ verify( thrd.link.next != 0p ); |
---|
271 | |
---|
272 | // Remove the thread from the ready queue of this cluster |
---|
273 | // The thread should be the last on the list |
---|
274 | __attribute__((unused)) bool removed = remove_head( &cltr, &thrd ); |
---|
275 | /* paranoid */ verify( removed ); |
---|
276 | thrd.link.next = 0p; |
---|
277 | thrd.link.prev = 0p; |
---|
278 | |
---|
279 | // Fixup the thread state |
---|
280 | thrd.state = Blocked; |
---|
281 | thrd.ticket = TICKET_BLOCKED; |
---|
282 | thrd.preempted = __NO_PREEMPTION; |
---|
283 | |
---|
284 | ready_schedule_unlock(); |
---|
285 | |
---|
286 | // Pretend like the thread was blocked all along |
---|
287 | } |
---|
288 | // !!! This is not an else if !!! |
---|
289 | // Ok, now the thread is blocked (whether we cheated to get here or not) |
---|
290 | if( thrd.state == Blocked ) { |
---|
291 | // This is the "easy case" |
---|
292 | // The thread is parked and can easily be moved to active cluster |
---|
293 | verify( thrd.curr_cluster != active_cluster() || thrd.curr_cluster == mainCluster ); |
---|
294 | thrd.curr_cluster = active_cluster(); |
---|
295 | |
---|
296 | // unpark the fast io_poller |
---|
297 | unpark( &thrd ); |
---|
298 | } |
---|
299 | else { |
---|
300 | // The thread is in a weird state |
---|
301 | // I don't know what to do here |
---|
302 | abort("io_context poller thread is in unexpected state, cannot clean-up correctly\n"); |
---|
303 | } |
---|
304 | |
---|
305 | // The weird thread kidnapping stuff is over, restore interrupts. |
---|
306 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
307 | } else { |
---|
308 | post( this.thrd.sem ); |
---|
309 | } |
---|
310 | |
---|
311 | ^(this.thrd){}; |
---|
312 | __cfadbg_print_safe(io_core, "Kernel I/O : Stopped poller thread for io_context %p\n", &this); |
---|
313 | |
---|
314 | __io_destroy( *this.thrd.ring ); |
---|
315 | __cfadbg_print_safe(io_core, "Kernel I/O : Destroyed ring for io_context %p\n", &this); |
---|
316 | |
---|
317 | free(this.thrd.ring); |
---|
318 | } |
---|
319 | |
---|
320 | void ^?{}(io_context & this) { |
---|
321 | ^(this){ false }; |
---|
322 | } |
---|
323 | |
---|
324 | extern void __disable_interrupts_hard(); |
---|
325 | extern void __enable_interrupts_hard(); |
---|
326 | |
---|
327 | static void __io_create( __io_data & this, const io_context_params & params_in ) { |
---|
328 | // Step 1 : call to setup |
---|
329 | struct io_uring_params params; |
---|
330 | memset(¶ms, 0, sizeof(params)); |
---|
331 | if( params_in.poll_submit ) params.flags |= IORING_SETUP_SQPOLL; |
---|
332 | if( params_in.poll_complete ) params.flags |= IORING_SETUP_IOPOLL; |
---|
333 | |
---|
334 | __u32 nentries = params_in.num_entries != 0 ? params_in.num_entries : 256; |
---|
335 | if( !is_pow2(nentries) ) { |
---|
336 | abort("ERROR: I/O setup 'num_entries' must be a power of 2\n"); |
---|
337 | } |
---|
338 | if( params_in.poller_submits && params_in.eager_submits ) { |
---|
339 | abort("ERROR: I/O setup 'poller_submits' and 'eager_submits' cannot be used together\n"); |
---|
340 | } |
---|
341 | |
---|
342 | int fd = syscall(__NR_io_uring_setup, nentries, ¶ms ); |
---|
343 | if(fd < 0) { |
---|
344 | abort("KERNEL ERROR: IO_URING SETUP - %s\n", strerror(errno)); |
---|
345 | } |
---|
346 | |
---|
347 | // Step 2 : mmap result |
---|
348 | memset( &this, 0, sizeof(struct __io_data) ); |
---|
349 | struct __submition_data & sq = this.submit_q; |
---|
350 | struct __completion_data & cq = this.completion_q; |
---|
351 | |
---|
352 | // calculate the right ring size |
---|
353 | sq.ring_sz = params.sq_off.array + (params.sq_entries * sizeof(unsigned) ); |
---|
354 | cq.ring_sz = params.cq_off.cqes + (params.cq_entries * sizeof(struct io_uring_cqe)); |
---|
355 | |
---|
356 | // Requires features |
---|
357 | #if defined(IORING_FEAT_SINGLE_MMAP) |
---|
358 | // adjust the size according to the parameters |
---|
359 | if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) { |
---|
360 | cq.ring_sz = sq.ring_sz = max(cq.ring_sz, sq.ring_sz); |
---|
361 | } |
---|
362 | #endif |
---|
363 | |
---|
364 | // mmap the Submit Queue into existence |
---|
365 | sq.ring_ptr = mmap(0, sq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING); |
---|
366 | if (sq.ring_ptr == (void*)MAP_FAILED) { |
---|
367 | abort("KERNEL ERROR: IO_URING MMAP1 - %s\n", strerror(errno)); |
---|
368 | } |
---|
369 | |
---|
370 | // Requires features |
---|
371 | #if defined(IORING_FEAT_SINGLE_MMAP) |
---|
372 | // mmap the Completion Queue into existence (may or may not be needed) |
---|
373 | if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) { |
---|
374 | cq.ring_ptr = sq.ring_ptr; |
---|
375 | } |
---|
376 | else |
---|
377 | #endif |
---|
378 | { |
---|
379 | // We need multiple call to MMAP |
---|
380 | cq.ring_ptr = mmap(0, cq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING); |
---|
381 | if (cq.ring_ptr == (void*)MAP_FAILED) { |
---|
382 | munmap(sq.ring_ptr, sq.ring_sz); |
---|
383 | abort("KERNEL ERROR: IO_URING MMAP2 - %s\n", strerror(errno)); |
---|
384 | } |
---|
385 | } |
---|
386 | |
---|
387 | // mmap the submit queue entries |
---|
388 | size_t size = params.sq_entries * sizeof(struct io_uring_sqe); |
---|
389 | sq.sqes = (struct io_uring_sqe *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES); |
---|
390 | if (sq.sqes == (struct io_uring_sqe *)MAP_FAILED) { |
---|
391 | munmap(sq.ring_ptr, sq.ring_sz); |
---|
392 | if (cq.ring_ptr != sq.ring_ptr) munmap(cq.ring_ptr, cq.ring_sz); |
---|
393 | abort("KERNEL ERROR: IO_URING MMAP3 - %s\n", strerror(errno)); |
---|
394 | } |
---|
395 | |
---|
396 | // Step 3 : Initialize the data structure |
---|
397 | // Get the pointers from the kernel to fill the structure |
---|
398 | // submit queue |
---|
399 | sq.head = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.head); |
---|
400 | sq.tail = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail); |
---|
401 | sq.mask = ( const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask); |
---|
402 | sq.num = ( const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries); |
---|
403 | sq.flags = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags); |
---|
404 | sq.dropped = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped); |
---|
405 | sq.array = ( __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.array); |
---|
406 | sq.prev_head = *sq.head; |
---|
407 | |
---|
408 | { |
---|
409 | const __u32 num = *sq.num; |
---|
410 | for( i; num ) { |
---|
411 | __sqe_clean( &sq.sqes[i] ); |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | (sq.submit_lock){}; |
---|
416 | (sq.release_lock){}; |
---|
417 | |
---|
418 | if( params_in.poller_submits || params_in.eager_submits ) { |
---|
419 | /* paranoid */ verify( is_pow2( params_in.num_ready ) || (params_in.num_ready < 8) ); |
---|
420 | sq.ready_cnt = max( params_in.num_ready, 8 ); |
---|
421 | sq.ready = alloc( sq.ready_cnt, 64`align ); |
---|
422 | for(i; sq.ready_cnt) { |
---|
423 | sq.ready[i] = -1ul32; |
---|
424 | } |
---|
425 | sq.prev_ready = 0; |
---|
426 | } |
---|
427 | else { |
---|
428 | sq.ready_cnt = 0; |
---|
429 | sq.ready = 0p; |
---|
430 | sq.prev_ready = 0; |
---|
431 | } |
---|
432 | |
---|
433 | // completion queue |
---|
434 | cq.head = (volatile __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.head); |
---|
435 | cq.tail = (volatile __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.tail); |
---|
436 | cq.mask = ( const __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_mask); |
---|
437 | cq.num = ( const __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_entries); |
---|
438 | cq.overflow = ( __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.overflow); |
---|
439 | cq.cqes = (struct io_uring_cqe *)(((intptr_t)cq.ring_ptr) + params.cq_off.cqes); |
---|
440 | |
---|
441 | // Step 4 : eventfd |
---|
442 | // io_uring_register is so f*cking slow on some machine that it |
---|
443 | // will never succeed if preemption isn't hard blocked |
---|
444 | __disable_interrupts_hard(); |
---|
445 | |
---|
446 | int efd = eventfd(0, 0); |
---|
447 | if (efd < 0) { |
---|
448 | abort("KERNEL ERROR: IO_URING EVENTFD - %s\n", strerror(errno)); |
---|
449 | } |
---|
450 | |
---|
451 | int ret = syscall( __NR_io_uring_register, fd, IORING_REGISTER_EVENTFD, &efd, 1); |
---|
452 | if (ret < 0) { |
---|
453 | abort("KERNEL ERROR: IO_URING EVENTFD REGISTER - %s\n", strerror(errno)); |
---|
454 | } |
---|
455 | |
---|
456 | __enable_interrupts_hard(); |
---|
457 | |
---|
458 | // some paranoid checks |
---|
459 | /* paranoid */ verifyf( (*cq.mask) == ((*cq.num) - 1ul32), "IO_URING Expected mask to be %u (%u entries), was %u", (*cq.num) - 1ul32, *cq.num, *cq.mask ); |
---|
460 | /* paranoid */ verifyf( (*cq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *cq.num ); |
---|
461 | /* paranoid */ verifyf( (*cq.head) == 0, "IO_URING Expected head to be 0, got %u", *cq.head ); |
---|
462 | /* paranoid */ verifyf( (*cq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *cq.tail ); |
---|
463 | |
---|
464 | /* paranoid */ verifyf( (*sq.mask) == ((*sq.num) - 1ul32), "IO_URING Expected mask to be %u (%u entries), was %u", (*sq.num) - 1ul32, *sq.num, *sq.mask ); |
---|
465 | /* paranoid */ verifyf( (*sq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *sq.num ); |
---|
466 | /* paranoid */ verifyf( (*sq.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.head ); |
---|
467 | /* paranoid */ verifyf( (*sq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.tail ); |
---|
468 | |
---|
469 | // Update the global ring info |
---|
470 | this.ring_flags = params.flags; |
---|
471 | this.fd = fd; |
---|
472 | this.efd = efd; |
---|
473 | this.eager_submits = params_in.eager_submits; |
---|
474 | this.poller_submits = params_in.poller_submits; |
---|
475 | } |
---|
476 | |
---|
477 | static void __io_destroy( __io_data & this ) { |
---|
478 | // Shutdown the io rings |
---|
479 | struct __submition_data & sq = this.submit_q; |
---|
480 | struct __completion_data & cq = this.completion_q; |
---|
481 | |
---|
482 | // unmap the submit queue entries |
---|
483 | munmap(sq.sqes, (*sq.num) * sizeof(struct io_uring_sqe)); |
---|
484 | |
---|
485 | // unmap the Submit Queue ring |
---|
486 | munmap(sq.ring_ptr, sq.ring_sz); |
---|
487 | |
---|
488 | // unmap the Completion Queue ring, if it is different |
---|
489 | if (cq.ring_ptr != sq.ring_ptr) { |
---|
490 | munmap(cq.ring_ptr, cq.ring_sz); |
---|
491 | } |
---|
492 | |
---|
493 | // close the file descriptor |
---|
494 | close(this.fd); |
---|
495 | close(this.efd); |
---|
496 | |
---|
497 | free( this.submit_q.ready ); // Maybe null, doesn't matter |
---|
498 | } |
---|
499 | |
---|
500 | //============================================================================================= |
---|
501 | // I/O Context Sleep |
---|
502 | //============================================================================================= |
---|
503 | static inline void __ioctx_epoll_ctl($io_ctx_thread & ctx, int op, const char * error) { |
---|
504 | struct epoll_event ev; |
---|
505 | ev.events = EPOLLIN | EPOLLONESHOT; |
---|
506 | ev.data.u64 = (__u64)&ctx; |
---|
507 | int ret = epoll_ctl(iopoll.epollfd, op, ctx.ring->efd, &ev); |
---|
508 | if (ret < 0) { |
---|
509 | abort( "KERNEL ERROR: EPOLL %s - (%d) %s\n", error, (int)errno, strerror(errno) ); |
---|
510 | } |
---|
511 | } |
---|
512 | |
---|
513 | void __ioctx_register($io_ctx_thread & ctx) { |
---|
514 | __ioctx_epoll_ctl(ctx, EPOLL_CTL_ADD, "ADD"); |
---|
515 | } |
---|
516 | |
---|
517 | void __ioctx_prepare_block($io_ctx_thread & ctx) { |
---|
518 | __cfadbg_print_safe(io_core, "Kernel I/O - epoll : Re-arming io poller %d (%p)\n", ctx.ring->fd, &ctx); |
---|
519 | __ioctx_epoll_ctl(ctx, EPOLL_CTL_MOD, "REARM"); |
---|
520 | } |
---|
521 | |
---|
522 | void __ioctx_unregister($io_ctx_thread & ctx) { |
---|
523 | // Read the current epoch so we know when to stop |
---|
524 | size_t curr = __atomic_load_n(&iopoll.epoch, __ATOMIC_SEQ_CST); |
---|
525 | |
---|
526 | // Remove the fd from the iopoller |
---|
527 | __ioctx_epoll_ctl(ctx, EPOLL_CTL_DEL, "REMOVE"); |
---|
528 | |
---|
529 | // Notify the io poller thread of the shutdown |
---|
530 | iopoll.run = false; |
---|
531 | sigval val = { 1 }; |
---|
532 | pthread_sigqueue( iopoll.thrd, SIGUSR1, val ); |
---|
533 | |
---|
534 | // Make sure all this is done |
---|
535 | __atomic_thread_fence(__ATOMIC_SEQ_CST); |
---|
536 | |
---|
537 | // Wait for the next epoch |
---|
538 | while(curr == __atomic_load_n(&iopoll.epoch, __ATOMIC_RELAXED)) yield(); |
---|
539 | } |
---|
540 | |
---|
541 | //============================================================================================= |
---|
542 | // I/O Context Misc Setup |
---|
543 | //============================================================================================= |
---|
544 | void register_fixed_files( io_context & ctx, int * files, unsigned count ) { |
---|
545 | int ret = syscall( __NR_io_uring_register, ctx.thrd.ring->fd, IORING_REGISTER_FILES, files, count ); |
---|
546 | if( ret < 0 ) { |
---|
547 | abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) ); |
---|
548 | } |
---|
549 | |
---|
550 | __cfadbg_print_safe( io_core, "Kernel I/O : Performed io_register for %p, returned %d\n", active_thread(), ret ); |
---|
551 | } |
---|
552 | |
---|
553 | void register_fixed_files( cluster & cltr, int * files, unsigned count ) { |
---|
554 | for(i; cltr.io.cnt) { |
---|
555 | register_fixed_files( cltr.io.ctxs[i], files, count ); |
---|
556 | } |
---|
557 | } |
---|
558 | #endif |
---|