[ecf6b46] | 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.cfa -- |
---|
| 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Thu Apr 23 17:31:00 2020 |
---|
| 11 | // Last Modified By : |
---|
| 12 | // Last Modified On : |
---|
| 13 | // Update Count : |
---|
| 14 | // |
---|
| 15 | |
---|
[4069faad] | 16 | // #define __CFA_DEBUG_PRINT_IO__ |
---|
[0a805f2] | 17 | // #define __CFA_DEBUG_PRINT_IO_CORE__ |
---|
[4069faad] | 18 | |
---|
[92976d9] | 19 | #include "kernel.hfa" |
---|
[5c581cc] | 20 | #include "bitmanip.hfa" |
---|
[92976d9] | 21 | |
---|
| 22 | #if !defined(HAVE_LINUX_IO_URING_H) |
---|
[dd4e2d7] | 23 | void __kernel_io_startup( cluster &, unsigned, bool ) { |
---|
[92976d9] | 24 | // Nothing to do without io_uring |
---|
| 25 | } |
---|
| 26 | |
---|
[3f7d0b4] | 27 | void __kernel_io_finish_start( cluster & ) { |
---|
[f6660520] | 28 | // Nothing to do without io_uring |
---|
| 29 | } |
---|
| 30 | |
---|
[3f7d0b4] | 31 | void __kernel_io_prepare_stop( cluster & ) { |
---|
[f6660520] | 32 | // Nothing to do without io_uring |
---|
| 33 | } |
---|
| 34 | |
---|
[3f7d0b4] | 35 | void __kernel_io_shutdown( cluster &, bool ) { |
---|
[92976d9] | 36 | // Nothing to do without io_uring |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | #else |
---|
| 40 | extern "C" { |
---|
| 41 | #define _GNU_SOURCE /* See feature_test_macros(7) */ |
---|
| 42 | #include <errno.h> |
---|
| 43 | #include <stdint.h> |
---|
| 44 | #include <string.h> |
---|
| 45 | #include <unistd.h> |
---|
| 46 | #include <sys/mman.h> |
---|
| 47 | #include <sys/syscall.h> |
---|
| 48 | |
---|
| 49 | #include <linux/io_uring.h> |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | #include "bits/signal.hfa" |
---|
| 53 | #include "kernel_private.hfa" |
---|
| 54 | #include "thread.hfa" |
---|
| 55 | |
---|
| 56 | uint32_t entries_per_cluster() { |
---|
| 57 | return 256; |
---|
| 58 | } |
---|
| 59 | |
---|
[f6660520] | 60 | static void * __io_poller_slow( void * arg ); |
---|
| 61 | |
---|
| 62 | // Weirdly, some systems that do support io_uring don't actually define these |
---|
| 63 | #ifdef __alpha__ |
---|
| 64 | /* |
---|
| 65 | * alpha is the only exception, all other architectures |
---|
| 66 | * have common numbers for new system calls. |
---|
| 67 | */ |
---|
| 68 | #ifndef __NR_io_uring_setup |
---|
| 69 | #define __NR_io_uring_setup 535 |
---|
| 70 | #endif |
---|
| 71 | #ifndef __NR_io_uring_enter |
---|
| 72 | #define __NR_io_uring_enter 536 |
---|
| 73 | #endif |
---|
| 74 | #ifndef __NR_io_uring_register |
---|
| 75 | #define __NR_io_uring_register 537 |
---|
| 76 | #endif |
---|
| 77 | #else /* !__alpha__ */ |
---|
| 78 | #ifndef __NR_io_uring_setup |
---|
| 79 | #define __NR_io_uring_setup 425 |
---|
| 80 | #endif |
---|
| 81 | #ifndef __NR_io_uring_enter |
---|
| 82 | #define __NR_io_uring_enter 426 |
---|
| 83 | #endif |
---|
| 84 | #ifndef __NR_io_uring_register |
---|
| 85 | #define __NR_io_uring_register 427 |
---|
| 86 | #endif |
---|
| 87 | #endif |
---|
| 88 | |
---|
[61dd73d] | 89 | // Fast poller user-thread |
---|
| 90 | // Not using the "thread" keyword because we want to control |
---|
| 91 | // more carefully when to start/stop it |
---|
| 92 | struct __io_poller_fast { |
---|
| 93 | struct __io_data * ring; |
---|
| 94 | $thread thrd; |
---|
| 95 | }; |
---|
| 96 | |
---|
| 97 | void ?{}( __io_poller_fast & this, struct cluster & cltr ) { |
---|
| 98 | this.ring = cltr.io; |
---|
| 99 | (this.thrd){ "Fast I/O Poller", cltr }; |
---|
| 100 | } |
---|
| 101 | void ^?{}( __io_poller_fast & mutex this ); |
---|
| 102 | void main( __io_poller_fast & this ); |
---|
| 103 | static inline $thread * get_thread( __io_poller_fast & this ) { return &this.thrd; } |
---|
| 104 | void ^?{}( __io_poller_fast & mutex this ) {} |
---|
| 105 | |
---|
| 106 | struct __submition_data { |
---|
| 107 | // Head and tail of the ring (associated with array) |
---|
| 108 | volatile uint32_t * head; |
---|
| 109 | volatile uint32_t * tail; |
---|
| 110 | |
---|
| 111 | // The actual kernel ring which uses head/tail |
---|
| 112 | // indexes into the sqes arrays |
---|
| 113 | uint32_t * array; |
---|
| 114 | |
---|
| 115 | // number of entries and mask to go with it |
---|
| 116 | const uint32_t * num; |
---|
| 117 | const uint32_t * mask; |
---|
| 118 | |
---|
| 119 | // Submission flags (Not sure what for) |
---|
| 120 | uint32_t * flags; |
---|
| 121 | |
---|
| 122 | // number of sqes not submitted (whatever that means) |
---|
| 123 | uint32_t * dropped; |
---|
| 124 | |
---|
| 125 | // Like head/tail but not seen by the kernel |
---|
[5dadc9b] | 126 | volatile uint32_t * ready; |
---|
| 127 | uint32_t ready_cnt; |
---|
[61dd73d] | 128 | |
---|
| 129 | __spinlock_t lock; |
---|
| 130 | |
---|
| 131 | // A buffer of sqes (not the actual ring) |
---|
| 132 | struct io_uring_sqe * sqes; |
---|
| 133 | |
---|
| 134 | // The location and size of the mmaped area |
---|
| 135 | void * ring_ptr; |
---|
| 136 | size_t ring_sz; |
---|
| 137 | |
---|
| 138 | // Statistics |
---|
| 139 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 140 | struct { |
---|
| 141 | struct { |
---|
[6f121b8] | 142 | volatile unsigned long long int rdy; |
---|
| 143 | volatile unsigned long long int csm; |
---|
| 144 | volatile unsigned long long int avl; |
---|
[05cfa4d] | 145 | volatile unsigned long long int cnt; |
---|
[61dd73d] | 146 | } submit_avg; |
---|
[5dadc9b] | 147 | struct { |
---|
| 148 | volatile unsigned long long int val; |
---|
| 149 | volatile unsigned long long int cnt; |
---|
| 150 | volatile unsigned long long int block; |
---|
| 151 | } look_avg; |
---|
[6f121b8] | 152 | struct { |
---|
| 153 | volatile unsigned long long int val; |
---|
| 154 | volatile unsigned long long int cnt; |
---|
| 155 | volatile unsigned long long int block; |
---|
| 156 | } alloc_avg; |
---|
[61dd73d] | 157 | } stats; |
---|
| 158 | #endif |
---|
| 159 | }; |
---|
| 160 | |
---|
| 161 | struct __completion_data { |
---|
| 162 | // Head and tail of the ring |
---|
| 163 | volatile uint32_t * head; |
---|
| 164 | volatile uint32_t * tail; |
---|
| 165 | |
---|
| 166 | // number of entries and mask to go with it |
---|
| 167 | const uint32_t * mask; |
---|
| 168 | const uint32_t * num; |
---|
| 169 | |
---|
| 170 | // number of cqes not submitted (whatever that means) |
---|
| 171 | uint32_t * overflow; |
---|
| 172 | |
---|
| 173 | // the kernel ring |
---|
| 174 | struct io_uring_cqe * cqes; |
---|
| 175 | |
---|
| 176 | // The location and size of the mmaped area |
---|
| 177 | void * ring_ptr; |
---|
| 178 | size_t ring_sz; |
---|
| 179 | |
---|
| 180 | // Statistics |
---|
| 181 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 182 | struct { |
---|
| 183 | struct { |
---|
| 184 | unsigned long long int val; |
---|
| 185 | unsigned long long int slow_cnt; |
---|
| 186 | unsigned long long int fast_cnt; |
---|
| 187 | } completed_avg; |
---|
| 188 | } stats; |
---|
| 189 | #endif |
---|
| 190 | }; |
---|
| 191 | |
---|
| 192 | struct __io_data { |
---|
| 193 | struct __submition_data submit_q; |
---|
| 194 | struct __completion_data completion_q; |
---|
[b6f2b213] | 195 | uint32_t ring_flags; |
---|
| 196 | int cltr_flags; |
---|
[61dd73d] | 197 | int fd; |
---|
| 198 | semaphore submit; |
---|
| 199 | volatile bool done; |
---|
| 200 | struct { |
---|
| 201 | struct { |
---|
| 202 | void * stack; |
---|
| 203 | pthread_t kthrd; |
---|
[5c581cc] | 204 | volatile bool blocked; |
---|
[61dd73d] | 205 | } slow; |
---|
| 206 | __io_poller_fast fast; |
---|
| 207 | __bin_sem_t sem; |
---|
| 208 | } poller; |
---|
| 209 | }; |
---|
[185efe6] | 210 | |
---|
[92976d9] | 211 | //============================================================================================= |
---|
| 212 | // I/O Startup / Shutdown logic |
---|
| 213 | //============================================================================================= |
---|
[dd4e2d7] | 214 | void __kernel_io_startup( cluster & this, unsigned io_flags, bool main_cluster ) { |
---|
[61dd73d] | 215 | this.io = malloc(); |
---|
| 216 | |
---|
[92976d9] | 217 | // Step 1 : call to setup |
---|
| 218 | struct io_uring_params params; |
---|
| 219 | memset(¶ms, 0, sizeof(params)); |
---|
| 220 | |
---|
[2d8f7b0] | 221 | uint32_t nentries = entries_per_cluster(); |
---|
| 222 | |
---|
| 223 | int fd = syscall(__NR_io_uring_setup, nentries, ¶ms ); |
---|
[92976d9] | 224 | if(fd < 0) { |
---|
| 225 | abort("KERNEL ERROR: IO_URING SETUP - %s\n", strerror(errno)); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | // Step 2 : mmap result |
---|
[61dd73d] | 229 | memset( this.io, 0, sizeof(struct __io_data) ); |
---|
| 230 | struct __submition_data & sq = this.io->submit_q; |
---|
| 231 | struct __completion_data & cq = this.io->completion_q; |
---|
[92976d9] | 232 | |
---|
| 233 | // calculate the right ring size |
---|
[2d8f7b0] | 234 | sq.ring_sz = params.sq_off.array + (params.sq_entries * sizeof(unsigned) ); |
---|
| 235 | cq.ring_sz = params.cq_off.cqes + (params.cq_entries * sizeof(struct io_uring_cqe)); |
---|
[92976d9] | 236 | |
---|
| 237 | // Requires features |
---|
[d384787] | 238 | #if defined(IORING_FEAT_SINGLE_MMAP) |
---|
| 239 | // adjust the size according to the parameters |
---|
| 240 | if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) { |
---|
| 241 | cq->ring_sz = sq->ring_sz = max(cq->ring_sz, sq->ring_sz); |
---|
| 242 | } |
---|
| 243 | #endif |
---|
[92976d9] | 244 | |
---|
| 245 | // mmap the Submit Queue into existence |
---|
[2d8f7b0] | 246 | sq.ring_ptr = mmap(0, sq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING); |
---|
| 247 | if (sq.ring_ptr == (void*)MAP_FAILED) { |
---|
[92976d9] | 248 | abort("KERNEL ERROR: IO_URING MMAP1 - %s\n", strerror(errno)); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | // Requires features |
---|
[d384787] | 252 | #if defined(IORING_FEAT_SINGLE_MMAP) |
---|
| 253 | // mmap the Completion Queue into existence (may or may not be needed) |
---|
| 254 | if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) { |
---|
| 255 | cq->ring_ptr = sq->ring_ptr; |
---|
| 256 | } |
---|
| 257 | else |
---|
| 258 | #endif |
---|
| 259 | { |
---|
[92976d9] | 260 | // We need multiple call to MMAP |
---|
[2d8f7b0] | 261 | cq.ring_ptr = mmap(0, cq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING); |
---|
| 262 | if (cq.ring_ptr == (void*)MAP_FAILED) { |
---|
| 263 | munmap(sq.ring_ptr, sq.ring_sz); |
---|
[92976d9] | 264 | abort("KERNEL ERROR: IO_URING MMAP2 - %s\n", strerror(errno)); |
---|
| 265 | } |
---|
[d384787] | 266 | } |
---|
[92976d9] | 267 | |
---|
| 268 | // mmap the submit queue entries |
---|
| 269 | size_t size = params.sq_entries * sizeof(struct io_uring_sqe); |
---|
[2d8f7b0] | 270 | sq.sqes = (struct io_uring_sqe *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES); |
---|
| 271 | if (sq.sqes == (struct io_uring_sqe *)MAP_FAILED) { |
---|
| 272 | munmap(sq.ring_ptr, sq.ring_sz); |
---|
| 273 | if (cq.ring_ptr != sq.ring_ptr) munmap(cq.ring_ptr, cq.ring_sz); |
---|
[92976d9] | 274 | abort("KERNEL ERROR: IO_URING MMAP3 - %s\n", strerror(errno)); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | // Get the pointers from the kernel to fill the structure |
---|
| 278 | // submit queue |
---|
[2d8f7b0] | 279 | sq.head = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.head); |
---|
| 280 | sq.tail = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail); |
---|
| 281 | sq.mask = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask); |
---|
| 282 | sq.num = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries); |
---|
| 283 | sq.flags = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags); |
---|
| 284 | sq.dropped = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped); |
---|
| 285 | sq.array = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.array); |
---|
[6f121b8] | 286 | |
---|
| 287 | { |
---|
| 288 | const uint32_t num = *sq.num; |
---|
| 289 | for( i; num ) { |
---|
| 290 | sq.sqes[i].user_data = 0ul64; |
---|
| 291 | } |
---|
| 292 | } |
---|
[5dadc9b] | 293 | |
---|
| 294 | if( io_flags & CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS ) { |
---|
[5c581cc] | 295 | /* paranoid */ verify( is_pow2( io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET ) || ((io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET) < 8) ); |
---|
[dd4e2d7] | 296 | sq.ready_cnt = max(io_flags >> CFA_CLUSTER_IO_BUFFLEN_OFFSET, 8); |
---|
[0335620] | 297 | sq.ready = alloc_align( 64, sq.ready_cnt ); |
---|
[5dadc9b] | 298 | for(i; sq.ready_cnt) { |
---|
| 299 | sq.ready[i] = -1ul32; |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | else { |
---|
| 303 | sq.ready_cnt = 0; |
---|
| 304 | sq.ready = 0p; |
---|
| 305 | } |
---|
[92976d9] | 306 | |
---|
| 307 | // completion queue |
---|
[2d8f7b0] | 308 | cq.head = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.head); |
---|
| 309 | cq.tail = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.tail); |
---|
| 310 | cq.mask = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_mask); |
---|
| 311 | cq.num = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_entries); |
---|
| 312 | cq.overflow = ( uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.overflow); |
---|
| 313 | cq.cqes = (struct io_uring_cqe *)(((intptr_t)cq.ring_ptr) + params.cq_off.cqes); |
---|
| 314 | |
---|
| 315 | // some paranoid checks |
---|
| 316 | /* 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 ); |
---|
| 317 | /* paranoid */ verifyf( (*cq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *cq.num ); |
---|
| 318 | /* paranoid */ verifyf( (*cq.head) == 0, "IO_URING Expected head to be 0, got %u", *cq.head ); |
---|
| 319 | /* paranoid */ verifyf( (*cq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *cq.tail ); |
---|
| 320 | |
---|
| 321 | /* 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 ); |
---|
| 322 | /* paranoid */ verifyf( (*sq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *sq.num ); |
---|
| 323 | /* paranoid */ verifyf( (*sq.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.head ); |
---|
| 324 | /* paranoid */ verifyf( (*sq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.tail ); |
---|
[92976d9] | 325 | |
---|
| 326 | // Update the global ring info |
---|
[b6f2b213] | 327 | this.io->ring_flags = params.flags; |
---|
| 328 | this.io->cltr_flags = io_flags; |
---|
| 329 | this.io->fd = fd; |
---|
| 330 | this.io->done = false; |
---|
[61dd73d] | 331 | (this.io->submit){ min(*sq.num, *cq.num) }; |
---|
[92976d9] | 332 | |
---|
[d384787] | 333 | // Initialize statistics |
---|
[038be32] | 334 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[6f121b8] | 335 | this.io->submit_q.stats.submit_avg.rdy = 0; |
---|
| 336 | this.io->submit_q.stats.submit_avg.csm = 0; |
---|
| 337 | this.io->submit_q.stats.submit_avg.avl = 0; |
---|
| 338 | this.io->submit_q.stats.submit_avg.cnt = 0; |
---|
[5dadc9b] | 339 | this.io->submit_q.stats.look_avg.val = 0; |
---|
| 340 | this.io->submit_q.stats.look_avg.cnt = 0; |
---|
| 341 | this.io->submit_q.stats.look_avg.block = 0; |
---|
[6f121b8] | 342 | this.io->submit_q.stats.alloc_avg.val = 0; |
---|
| 343 | this.io->submit_q.stats.alloc_avg.cnt = 0; |
---|
| 344 | this.io->submit_q.stats.alloc_avg.block = 0; |
---|
[61dd73d] | 345 | this.io->completion_q.stats.completed_avg.val = 0; |
---|
| 346 | this.io->completion_q.stats.completed_avg.slow_cnt = 0; |
---|
| 347 | this.io->completion_q.stats.completed_avg.fast_cnt = 0; |
---|
[038be32] | 348 | #endif |
---|
[d384787] | 349 | |
---|
[f6660520] | 350 | if(!main_cluster) { |
---|
| 351 | __kernel_io_finish_start( this ); |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | |
---|
| 355 | void __kernel_io_finish_start( cluster & this ) { |
---|
[b6f2b213] | 356 | if( this.io->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ) { |
---|
| 357 | __cfadbg_print_safe(io_core, "Kernel I/O : Creating fast poller for cluter %p\n", &this); |
---|
| 358 | (this.io->poller.fast){ this }; |
---|
| 359 | __thrd_start( this.io->poller.fast, main ); |
---|
| 360 | } |
---|
[f6660520] | 361 | |
---|
[92976d9] | 362 | // Create the poller thread |
---|
[0a805f2] | 363 | __cfadbg_print_safe(io_core, "Kernel I/O : Creating slow poller for cluter %p\n", &this); |
---|
[5c581cc] | 364 | this.io->poller.slow.blocked = false; |
---|
[61dd73d] | 365 | this.io->poller.slow.stack = __create_pthread( &this.io->poller.slow.kthrd, __io_poller_slow, &this ); |
---|
[92976d9] | 366 | } |
---|
| 367 | |
---|
[f6660520] | 368 | void __kernel_io_prepare_stop( cluster & this ) { |
---|
[0a805f2] | 369 | __cfadbg_print_safe(io_core, "Kernel I/O : Stopping pollers for cluster\n", &this); |
---|
[92976d9] | 370 | // Notify the poller thread of the shutdown |
---|
[61dd73d] | 371 | __atomic_store_n(&this.io->done, true, __ATOMIC_SEQ_CST); |
---|
[f6660520] | 372 | |
---|
| 373 | // Stop the IO Poller |
---|
[92976d9] | 374 | sigval val = { 1 }; |
---|
[61dd73d] | 375 | pthread_sigqueue( this.io->poller.slow.kthrd, SIGUSR1, val ); |
---|
| 376 | post( this.io->poller.sem ); |
---|
[92976d9] | 377 | |
---|
| 378 | // Wait for the poller thread to finish |
---|
[61dd73d] | 379 | pthread_join( this.io->poller.slow.kthrd, 0p ); |
---|
| 380 | free( this.io->poller.slow.stack ); |
---|
[f6660520] | 381 | |
---|
[0a805f2] | 382 | __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller stopped for cluster\n", &this); |
---|
[4069faad] | 383 | |
---|
[b6f2b213] | 384 | if( this.io->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ) { |
---|
[05cfa4d] | 385 | with( this.io->poller.fast ) { |
---|
| 386 | /* paranoid */ verify( this.procs.head == 0p || &this == mainCluster ); |
---|
| 387 | /* paranoid */ verify( this.idles.head == 0p || &this == mainCluster ); |
---|
| 388 | |
---|
| 389 | // We need to adjust the clean-up based on where the thread is |
---|
[5dadc9b] | 390 | if( thrd.state == Ready || thrd.preempted != __NO_PREEMPTION ) { |
---|
[05cfa4d] | 391 | |
---|
| 392 | // This is the tricky case |
---|
| 393 | // The thread was preempted and now it is on the ready queue |
---|
| 394 | /* paranoid */ verify( thrd.next == 1p ); // The thread should be the last on the list |
---|
| 395 | /* paranoid */ verify( this.ready_queue.head == &thrd ); // The thread should be the only thing on the list |
---|
| 396 | |
---|
| 397 | // Remove the thread from the ready queue of this cluster |
---|
| 398 | this.ready_queue.head = 1p; |
---|
| 399 | thrd.next = 0p; |
---|
[6f121b8] | 400 | __cfaabi_dbg_debug_do( thrd.unpark_stale = true ); |
---|
[05cfa4d] | 401 | |
---|
| 402 | // Fixup the thread state |
---|
| 403 | thrd.state = Blocked; |
---|
| 404 | thrd.preempted = __NO_PREEMPTION; |
---|
| 405 | |
---|
| 406 | // Pretend like the thread was blocked all along |
---|
| 407 | } |
---|
| 408 | // !!! This is not an else if !!! |
---|
| 409 | if( thrd.state == Blocked ) { |
---|
[6502a2b] | 410 | |
---|
[05cfa4d] | 411 | // This is the "easy case" |
---|
| 412 | // The thread is parked and can easily be moved to active cluster |
---|
| 413 | verify( thrd.curr_cluster != active_cluster() || thrd.curr_cluster == mainCluster ); |
---|
| 414 | thrd.curr_cluster = active_cluster(); |
---|
[6502a2b] | 415 | |
---|
[f6660520] | 416 | // unpark the fast io_poller |
---|
[05cfa4d] | 417 | unpark( &thrd __cfaabi_dbg_ctx2 ); |
---|
| 418 | } |
---|
| 419 | else { |
---|
| 420 | |
---|
| 421 | // The thread is in a weird state |
---|
| 422 | // I don't know what to do here |
---|
| 423 | abort("Fast poller thread is in unexpected state, cannot clean-up correctly\n"); |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | } |
---|
[f6660520] | 427 | |
---|
[61dd73d] | 428 | ^(this.io->poller.fast){}; |
---|
[4069faad] | 429 | |
---|
[0a805f2] | 430 | __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller stopped for cluster\n", &this); |
---|
[b6f2b213] | 431 | } |
---|
[f6660520] | 432 | } |
---|
| 433 | |
---|
| 434 | void __kernel_io_shutdown( cluster & this, bool main_cluster ) { |
---|
| 435 | if(!main_cluster) { |
---|
| 436 | __kernel_io_prepare_stop( this ); |
---|
| 437 | } |
---|
[92976d9] | 438 | |
---|
[d384787] | 439 | // print statistics |
---|
[038be32] | 440 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 441 | if(this.print_stats) { |
---|
[61dd73d] | 442 | with(this.io->submit_q.stats, this.io->completion_q.stats) { |
---|
[6f121b8] | 443 | double avgrdy = ((double)submit_avg.rdy) / submit_avg.cnt; |
---|
| 444 | double avgcsm = ((double)submit_avg.csm) / submit_avg.cnt; |
---|
| 445 | double avgavl = ((double)submit_avg.avl) / submit_avg.cnt; |
---|
| 446 | |
---|
[5dadc9b] | 447 | double lavgv = 0; |
---|
| 448 | double lavgb = 0; |
---|
| 449 | if(look_avg.cnt != 0) { |
---|
| 450 | lavgv = ((double)look_avg.val ) / look_avg.cnt; |
---|
| 451 | lavgb = ((double)look_avg.block) / look_avg.cnt; |
---|
| 452 | } |
---|
| 453 | |
---|
[6f121b8] | 454 | double aavgv = 0; |
---|
| 455 | double aavgb = 0; |
---|
| 456 | if(alloc_avg.cnt != 0) { |
---|
| 457 | aavgv = ((double)alloc_avg.val ) / alloc_avg.cnt; |
---|
| 458 | aavgb = ((double)alloc_avg.block) / alloc_avg.cnt; |
---|
| 459 | } |
---|
| 460 | |
---|
[068a202] | 461 | __cfaabi_bits_print_safe( STDOUT_FILENO, |
---|
[61dd73d] | 462 | "----- I/O uRing Stats -----\n" |
---|
[5dadc9b] | 463 | "- total submit calls : %'15llu\n" |
---|
[6f121b8] | 464 | "- avg ready entries : %'18.2lf\n" |
---|
| 465 | "- avg submitted entries : %'18.2lf\n" |
---|
| 466 | "- avg available entries : %'18.2lf\n" |
---|
[5dadc9b] | 467 | "- total ready search : %'15llu\n" |
---|
| 468 | "- avg ready search len : %'18.2lf\n" |
---|
| 469 | "- avg ready search block : %'18.2lf\n" |
---|
[6f121b8] | 470 | "- total alloc search : %'15llu\n" |
---|
| 471 | "- avg alloc search len : %'18.2lf\n" |
---|
| 472 | "- avg alloc search block : %'18.2lf\n" |
---|
[5dadc9b] | 473 | "- total wait calls : %'15llu (%'llu slow, %'llu fast)\n" |
---|
| 474 | "- avg completion/wait : %'18.2lf\n", |
---|
[61dd73d] | 475 | submit_avg.cnt, |
---|
[6f121b8] | 476 | avgrdy, |
---|
| 477 | avgcsm, |
---|
| 478 | avgavl, |
---|
[5dadc9b] | 479 | look_avg.cnt, |
---|
| 480 | lavgv, |
---|
| 481 | lavgb, |
---|
[6f121b8] | 482 | alloc_avg.cnt, |
---|
| 483 | aavgv, |
---|
| 484 | aavgb, |
---|
[61dd73d] | 485 | completed_avg.slow_cnt + completed_avg.fast_cnt, |
---|
| 486 | completed_avg.slow_cnt, completed_avg.fast_cnt, |
---|
| 487 | ((double)completed_avg.val) / (completed_avg.slow_cnt + completed_avg.fast_cnt) |
---|
| 488 | ); |
---|
| 489 | } |
---|
[038be32] | 490 | } |
---|
| 491 | #endif |
---|
[d384787] | 492 | |
---|
[92976d9] | 493 | // Shutdown the io rings |
---|
[61dd73d] | 494 | struct __submition_data & sq = this.io->submit_q; |
---|
| 495 | struct __completion_data & cq = this.io->completion_q; |
---|
[92976d9] | 496 | |
---|
| 497 | // unmap the submit queue entries |
---|
[2d8f7b0] | 498 | munmap(sq.sqes, (*sq.num) * sizeof(struct io_uring_sqe)); |
---|
[92976d9] | 499 | |
---|
| 500 | // unmap the Submit Queue ring |
---|
| 501 | munmap(sq.ring_ptr, sq.ring_sz); |
---|
| 502 | |
---|
| 503 | // unmap the Completion Queue ring, if it is different |
---|
| 504 | if (cq.ring_ptr != sq.ring_ptr) { |
---|
| 505 | munmap(cq.ring_ptr, cq.ring_sz); |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | // close the file descriptor |
---|
[61dd73d] | 509 | close(this.io->fd); |
---|
| 510 | |
---|
[5dadc9b] | 511 | free( this.io->submit_q.ready ); // Maybe null, doesn't matter |
---|
[61dd73d] | 512 | free( this.io ); |
---|
[92976d9] | 513 | } |
---|
| 514 | |
---|
| 515 | //============================================================================================= |
---|
| 516 | // I/O Polling |
---|
| 517 | //============================================================================================= |
---|
| 518 | struct io_user_data { |
---|
| 519 | int32_t result; |
---|
| 520 | $thread * thrd; |
---|
| 521 | }; |
---|
| 522 | |
---|
| 523 | // Process a single completion message from the io_uring |
---|
| 524 | // This is NOT thread-safe |
---|
[5dadc9b] | 525 | static [int, bool] __drain_io( & struct __io_data ring, * sigset_t mask, int waitcnt, bool in_kernel ) { |
---|
| 526 | unsigned to_submit = 0; |
---|
| 527 | if( ring.cltr_flags & CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS ) { |
---|
| 528 | |
---|
| 529 | // If the poller thread also submits, then we need to aggregate the submissions which are ready |
---|
[6f121b8] | 530 | uint32_t tail = *ring.submit_q.tail; |
---|
[5dadc9b] | 531 | const uint32_t mask = *ring.submit_q.mask; |
---|
| 532 | |
---|
| 533 | // Go through the list of ready submissions |
---|
| 534 | for( i; ring.submit_q.ready_cnt ) { |
---|
| 535 | // replace any submission with the sentinel, to consume it. |
---|
| 536 | uint32_t idx = __atomic_exchange_n( &ring.submit_q.ready[i], -1ul32, __ATOMIC_RELAXED); |
---|
| 537 | |
---|
| 538 | // If it was already the sentinel, then we are done |
---|
| 539 | if( idx == -1ul32 ) continue; |
---|
| 540 | |
---|
| 541 | // If we got a real submission, append it to the list |
---|
[6f121b8] | 542 | ring.submit_q.array[ (tail + to_submit) & mask ] = idx & mask; |
---|
[5dadc9b] | 543 | to_submit++; |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | // Increment the tail based on how many we are ready to submit |
---|
[6f121b8] | 547 | __atomic_fetch_add(ring.submit_q.tail, to_submit, __ATOMIC_SEQ_CST); |
---|
[5dadc9b] | 548 | } |
---|
| 549 | |
---|
[6f121b8] | 550 | const uint32_t smask = *ring.submit_q.mask; |
---|
| 551 | uint32_t shead = *ring.submit_q.head; |
---|
[5dadc9b] | 552 | int ret = syscall( __NR_io_uring_enter, ring.fd, to_submit, waitcnt, IORING_ENTER_GETEVENTS, mask, _NSIG / 8); |
---|
[d384787] | 553 | if( ret < 0 ) { |
---|
| 554 | switch((int)errno) { |
---|
| 555 | case EAGAIN: |
---|
| 556 | case EINTR: |
---|
| 557 | return -EAGAIN; |
---|
| 558 | default: |
---|
| 559 | abort( "KERNEL ERROR: IO_URING WAIT - %s\n", strerror(errno) ); |
---|
| 560 | } |
---|
| 561 | } |
---|
| 562 | |
---|
[6f121b8] | 563 | verify( (shead + ret) == *ring.submit_q.head ); |
---|
| 564 | |
---|
| 565 | // Release the consumed SQEs |
---|
| 566 | for( i; ret ) { |
---|
| 567 | uint32_t idx = ring.submit_q.array[ (i + shead) & smask ]; |
---|
| 568 | ring.submit_q.sqes[ idx ].user_data = 0; |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | uint32_t avail = 0; |
---|
| 572 | uint32_t sqe_num = *ring.submit_q.num; |
---|
| 573 | for(i; sqe_num) { |
---|
| 574 | if( ring.submit_q.sqes[ i ].user_data == 0 ) avail++; |
---|
| 575 | } |
---|
| 576 | |
---|
| 577 | // update statistics |
---|
| 578 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 579 | ring.submit_q.stats.submit_avg.rdy += to_submit; |
---|
| 580 | ring.submit_q.stats.submit_avg.csm += ret; |
---|
| 581 | ring.submit_q.stats.submit_avg.avl += avail; |
---|
| 582 | ring.submit_q.stats.submit_avg.cnt += 1; |
---|
| 583 | #endif |
---|
| 584 | |
---|
[d384787] | 585 | // Drain the queue |
---|
[92976d9] | 586 | unsigned head = *ring.completion_q.head; |
---|
[6f121b8] | 587 | unsigned tail = *ring.completion_q.tail; |
---|
| 588 | const uint32_t mask = *ring.completion_q.mask; |
---|
| 589 | |
---|
| 590 | // Memory barrier |
---|
| 591 | __atomic_thread_fence( __ATOMIC_SEQ_CST ); |
---|
[92976d9] | 592 | |
---|
[d384787] | 593 | // Nothing was new return 0 |
---|
| 594 | if (head == tail) { |
---|
| 595 | return 0; |
---|
| 596 | } |
---|
[92976d9] | 597 | |
---|
[d384787] | 598 | uint32_t count = tail - head; |
---|
| 599 | for(i; count) { |
---|
[6f121b8] | 600 | unsigned idx = (head + i) & mask; |
---|
[d384787] | 601 | struct io_uring_cqe & cqe = ring.completion_q.cqes[idx]; |
---|
[92976d9] | 602 | |
---|
[d384787] | 603 | /* paranoid */ verify(&cqe); |
---|
[92976d9] | 604 | |
---|
[d384787] | 605 | struct io_user_data * data = (struct io_user_data *)cqe.user_data; |
---|
[4069faad] | 606 | __cfadbg_print_safe( io, "Kernel I/O : Performed reading io cqe %p, result %d for %p\n", data, cqe.res, data->thrd ); |
---|
[2d8f7b0] | 607 | |
---|
[d384787] | 608 | data->result = cqe.res; |
---|
[f6660520] | 609 | if(!in_kernel) { unpark( data->thrd __cfaabi_dbg_ctx2 ); } |
---|
| 610 | else { __unpark( data->thrd __cfaabi_dbg_ctx2 ); } |
---|
[d384787] | 611 | } |
---|
[2d8f7b0] | 612 | |
---|
| 613 | // Allow new submissions to happen |
---|
[6f121b8] | 614 | // V(ring.submit, count); |
---|
[92976d9] | 615 | |
---|
| 616 | // Mark to the kernel that the cqe has been seen |
---|
| 617 | // Ensure that the kernel only sees the new value of the head index after the CQEs have been read. |
---|
[6f121b8] | 618 | __atomic_thread_fence( __ATOMIC_SEQ_CST ); |
---|
[d384787] | 619 | __atomic_fetch_add( ring.completion_q.head, count, __ATOMIC_RELAXED ); |
---|
[92976d9] | 620 | |
---|
[5dadc9b] | 621 | return [count, count > 0 || to_submit > 0]; |
---|
[92976d9] | 622 | } |
---|
| 623 | |
---|
[f6660520] | 624 | static void * __io_poller_slow( void * arg ) { |
---|
[92976d9] | 625 | cluster * cltr = (cluster *)arg; |
---|
[61dd73d] | 626 | struct __io_data & ring = *cltr->io; |
---|
[92976d9] | 627 | |
---|
| 628 | sigset_t mask; |
---|
| 629 | sigfillset(&mask); |
---|
| 630 | if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) { |
---|
| 631 | abort( "KERNEL ERROR: IO_URING - pthread_sigmask" ); |
---|
| 632 | } |
---|
| 633 | |
---|
| 634 | sigdelset( &mask, SIGUSR1 ); |
---|
| 635 | |
---|
| 636 | verify( (*ring.submit_q.head) == (*ring.submit_q.tail) ); |
---|
| 637 | verify( (*ring.completion_q.head) == (*ring.completion_q.tail) ); |
---|
| 638 | |
---|
[1539bbd] | 639 | __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller for ring %p ready\n", &ring); |
---|
| 640 | |
---|
[b6f2b213] | 641 | if( ring.cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ) { |
---|
| 642 | while(!__atomic_load_n(&ring.done, __ATOMIC_SEQ_CST)) { |
---|
[5dadc9b] | 643 | |
---|
[5c581cc] | 644 | __atomic_store_n( &ring.poller.slow.blocked, true, __ATOMIC_SEQ_CST ); |
---|
| 645 | |
---|
[f6660520] | 646 | // In the user-thread approach drain and if anything was drained, |
---|
| 647 | // batton pass to the user-thread |
---|
[5dadc9b] | 648 | int count; |
---|
| 649 | bool again; |
---|
[5c581cc] | 650 | [count, again] = __drain_io( ring, &mask, 1, true ); |
---|
| 651 | |
---|
| 652 | __atomic_store_n( &ring.poller.slow.blocked, false, __ATOMIC_SEQ_CST ); |
---|
[3c039b0] | 653 | |
---|
| 654 | // Update statistics |
---|
| 655 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 656 | ring.completion_q.stats.completed_avg.val += count; |
---|
| 657 | ring.completion_q.stats.completed_avg.slow_cnt += 1; |
---|
| 658 | #endif |
---|
| 659 | |
---|
[5dadc9b] | 660 | if(again) { |
---|
[0a805f2] | 661 | __cfadbg_print_safe(io_core, "Kernel I/O : Moving to ring %p to fast poller\n", &ring); |
---|
[f6660520] | 662 | __unpark( &ring.poller.fast.thrd __cfaabi_dbg_ctx2 ); |
---|
| 663 | wait( ring.poller.sem ); |
---|
| 664 | } |
---|
[b6f2b213] | 665 | } |
---|
| 666 | } |
---|
| 667 | else { |
---|
| 668 | while(!__atomic_load_n(&ring.done, __ATOMIC_SEQ_CST)) { |
---|
[f6660520] | 669 | //In the naive approach, just poll the io completion queue directly |
---|
[5dadc9b] | 670 | int count; |
---|
| 671 | bool again; |
---|
| 672 | [count, again] = __drain_io( ring, &mask, 1, true ); |
---|
[3c039b0] | 673 | |
---|
| 674 | // Update statistics |
---|
| 675 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 676 | ring.completion_q.stats.completed_avg.val += count; |
---|
| 677 | ring.completion_q.stats.completed_avg.slow_cnt += 1; |
---|
| 678 | #endif |
---|
[b6f2b213] | 679 | } |
---|
[92976d9] | 680 | } |
---|
| 681 | |
---|
[1539bbd] | 682 | __cfadbg_print_safe(io_core, "Kernel I/O : Slow poller for ring %p stopping\n", &ring); |
---|
| 683 | |
---|
[92976d9] | 684 | return 0p; |
---|
| 685 | } |
---|
| 686 | |
---|
[61dd73d] | 687 | void main( __io_poller_fast & this ) { |
---|
[b6f2b213] | 688 | verify( this.ring->cltr_flags & CFA_CLUSTER_IO_POLLER_USER_THREAD ); |
---|
| 689 | |
---|
[61dd73d] | 690 | // Start parked |
---|
| 691 | park( __cfaabi_dbg_ctx ); |
---|
[f6660520] | 692 | |
---|
[61dd73d] | 693 | __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller for ring %p ready\n", &this.ring); |
---|
[1539bbd] | 694 | |
---|
[4e74466] | 695 | int reset = 0; |
---|
| 696 | |
---|
[61dd73d] | 697 | // Then loop until we need to start |
---|
| 698 | while(!__atomic_load_n(&this.ring->done, __ATOMIC_SEQ_CST)) { |
---|
[5dadc9b] | 699 | |
---|
[61dd73d] | 700 | // Drain the io |
---|
[5dadc9b] | 701 | int count; |
---|
| 702 | bool again; |
---|
| 703 | [count, again] = __drain_io( *this.ring, 0p, 0, false ); |
---|
| 704 | |
---|
| 705 | if(!again) reset++; |
---|
[3c039b0] | 706 | |
---|
[61dd73d] | 707 | // Update statistics |
---|
| 708 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 709 | this.ring->completion_q.stats.completed_avg.val += count; |
---|
| 710 | this.ring->completion_q.stats.completed_avg.fast_cnt += 1; |
---|
| 711 | #endif |
---|
[3c039b0] | 712 | |
---|
[5dadc9b] | 713 | // If we got something, just yield and check again |
---|
[4e74466] | 714 | if(reset < 5) { |
---|
[61dd73d] | 715 | yield(); |
---|
| 716 | } |
---|
[5dadc9b] | 717 | // We didn't get anything baton pass to the slow poller |
---|
[61dd73d] | 718 | else { |
---|
| 719 | __cfadbg_print_safe(io_core, "Kernel I/O : Moving to ring %p to slow poller\n", &this.ring); |
---|
[5dadc9b] | 720 | reset = 0; |
---|
| 721 | |
---|
| 722 | // wake up the slow poller |
---|
[61dd73d] | 723 | post( this.ring->poller.sem ); |
---|
[5dadc9b] | 724 | |
---|
| 725 | // park this thread |
---|
[61dd73d] | 726 | park( __cfaabi_dbg_ctx ); |
---|
[f6660520] | 727 | } |
---|
| 728 | } |
---|
[61dd73d] | 729 | |
---|
| 730 | __cfadbg_print_safe(io_core, "Kernel I/O : Fast poller for ring %p stopping\n", &this.ring); |
---|
| 731 | } |
---|
[f6660520] | 732 | |
---|
[0335620] | 733 | static inline void __wake_poller( struct __io_data & ring ) __attribute__((artificial)); |
---|
[5dadc9b] | 734 | static inline void __wake_poller( struct __io_data & ring ) { |
---|
[5c581cc] | 735 | if(!__atomic_load_n( &ring.poller.slow.blocked, __ATOMIC_SEQ_CST)) return; |
---|
| 736 | |
---|
| 737 | sigval val = { 1 }; |
---|
| 738 | pthread_sigqueue( ring.poller.slow.kthrd, SIGUSR1, val ); |
---|
[5dadc9b] | 739 | } |
---|
| 740 | |
---|
[92976d9] | 741 | //============================================================================================= |
---|
| 742 | // I/O Submissions |
---|
| 743 | //============================================================================================= |
---|
| 744 | |
---|
[2d8f7b0] | 745 | // Submition steps : |
---|
| 746 | // 1 - We need to make sure we don't overflow any of the buffer, P(ring.submit) to make sure |
---|
| 747 | // entries are available. The semaphore make sure that there is no more operations in |
---|
| 748 | // progress then the number of entries in the buffer. This probably limits concurrency |
---|
| 749 | // more than necessary since submitted but not completed operations don't need any |
---|
| 750 | // entries in user space. However, I don't know what happens if we overflow the buffers |
---|
| 751 | // because too many requests completed at once. This is a safe approach in all cases. |
---|
| 752 | // Furthermore, with hundreds of entries, this may be okay. |
---|
| 753 | // |
---|
| 754 | // 2 - Allocate a queue entry. The ring already has memory for all entries but only the ones |
---|
| 755 | // listed in sq.array are visible by the kernel. For those not listed, the kernel does not |
---|
| 756 | // offer any assurance that an entry is not being filled by multiple flags. Therefore, we |
---|
| 757 | // need to write an allocator that allows allocating concurrently. |
---|
| 758 | // |
---|
| 759 | // 3 - Actually fill the submit entry, this is the only simple and straightforward step. |
---|
| 760 | // |
---|
| 761 | // 4 - Append the entry index to the array and adjust the tail accordingly. This operation |
---|
| 762 | // needs to arrive to two concensus at the same time: |
---|
| 763 | // A - The order in which entries are listed in the array: no two threads must pick the |
---|
| 764 | // same index for their entries |
---|
| 765 | // B - When can the tail be update for the kernel. EVERY entries in the array between |
---|
| 766 | // head and tail must be fully filled and shouldn't ever be touched again. |
---|
| 767 | // |
---|
| 768 | |
---|
[6f121b8] | 769 | static inline [* struct io_uring_sqe, uint32_t] __submit_alloc( struct __io_data & ring, uint64_t data ) { |
---|
| 770 | verify( data != 0 ); |
---|
| 771 | |
---|
| 772 | // Prepare the data we need |
---|
| 773 | __attribute((unused)) int len = 0; |
---|
| 774 | __attribute((unused)) int block = 0; |
---|
| 775 | uint32_t cnt = *ring.submit_q.num; |
---|
| 776 | uint32_t mask = *ring.submit_q.mask; |
---|
| 777 | uint32_t off = __tls_rand(); |
---|
| 778 | |
---|
| 779 | // Loop around looking for an available spot |
---|
| 780 | LOOKING: for() { |
---|
| 781 | // Look through the list starting at some offset |
---|
| 782 | for(i; cnt) { |
---|
| 783 | uint64_t expected = 0; |
---|
| 784 | uint32_t idx = (i + off) & mask; |
---|
| 785 | struct io_uring_sqe * sqe = &ring.submit_q.sqes[idx]; |
---|
| 786 | volatile uint64_t * udata = &sqe->user_data; |
---|
| 787 | |
---|
| 788 | if( *udata == expected && |
---|
| 789 | __atomic_compare_exchange_n( udata, &expected, data, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) |
---|
| 790 | { |
---|
| 791 | // update statistics |
---|
| 792 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 793 | __atomic_fetch_add( &ring.submit_q.stats.alloc_avg.val, len, __ATOMIC_RELAXED ); |
---|
| 794 | __atomic_fetch_add( &ring.submit_q.stats.alloc_avg.block, block, __ATOMIC_RELAXED ); |
---|
| 795 | __atomic_fetch_add( &ring.submit_q.stats.alloc_avg.cnt, 1, __ATOMIC_RELAXED ); |
---|
| 796 | #endif |
---|
| 797 | |
---|
| 798 | // Success return the data |
---|
| 799 | return [sqe, idx]; |
---|
| 800 | } |
---|
| 801 | verify(expected != data); |
---|
[2489d31] | 802 | |
---|
[6f121b8] | 803 | len ++; |
---|
| 804 | } |
---|
[2489d31] | 805 | |
---|
[6f121b8] | 806 | block++; |
---|
| 807 | yield(); |
---|
| 808 | } |
---|
[2489d31] | 809 | } |
---|
| 810 | |
---|
[61dd73d] | 811 | static inline void __submit( struct __io_data & ring, uint32_t idx ) { |
---|
[5dadc9b] | 812 | // Get now the data we definetely need |
---|
| 813 | uint32_t * const tail = ring.submit_q.tail; |
---|
[2489d31] | 814 | const uint32_t mask = *ring.submit_q.mask; |
---|
| 815 | |
---|
[5dadc9b] | 816 | // There are 2 submission schemes, check which one we are using |
---|
| 817 | if( ring.cltr_flags & CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS ) { |
---|
| 818 | // If the poller thread submits, then we just need to add this to the ready array |
---|
[2489d31] | 819 | |
---|
[5dadc9b] | 820 | /* paranoid */ verify( idx <= mask ); |
---|
| 821 | /* paranoid */ verify( idx != -1ul32 ); |
---|
| 822 | |
---|
| 823 | // We need to find a spot in the ready array |
---|
| 824 | __attribute((unused)) int len = 0; |
---|
| 825 | __attribute((unused)) int block = 0; |
---|
[5c581cc] | 826 | uint32_t ready_mask = ring.submit_q.ready_cnt - 1; |
---|
| 827 | uint32_t off = __tls_rand(); |
---|
| 828 | LOOKING: for() { |
---|
[5dadc9b] | 829 | for(i; ring.submit_q.ready_cnt) { |
---|
[5c581cc] | 830 | uint32_t ii = (i + off) & ready_mask; |
---|
[6f121b8] | 831 | uint32_t expected = -1ul32; |
---|
[5c581cc] | 832 | if( __atomic_compare_exchange_n( &ring.submit_q.ready[ii], &expected, idx, true, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED ) ) { |
---|
[5dadc9b] | 833 | break LOOKING; |
---|
| 834 | } |
---|
[6f121b8] | 835 | verify(expected != idx); |
---|
[5dadc9b] | 836 | |
---|
| 837 | len ++; |
---|
| 838 | } |
---|
| 839 | |
---|
| 840 | block++; |
---|
| 841 | yield(); |
---|
[2489d31] | 842 | } |
---|
[5dadc9b] | 843 | |
---|
| 844 | __wake_poller( ring ); |
---|
| 845 | |
---|
| 846 | // update statistics |
---|
| 847 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 848 | __atomic_fetch_add( &ring.submit_q.stats.look_avg.val, len, __ATOMIC_RELAXED ); |
---|
| 849 | __atomic_fetch_add( &ring.submit_q.stats.look_avg.block, block, __ATOMIC_RELAXED ); |
---|
| 850 | __atomic_fetch_add( &ring.submit_q.stats.look_avg.cnt, 1, __ATOMIC_RELAXED ); |
---|
| 851 | #endif |
---|
[dd4e2d7] | 852 | |
---|
| 853 | __cfadbg_print_safe( io, "Kernel I/O : Added %u to ready for %p\n", idx, active_thread() ); |
---|
[2d8f7b0] | 854 | } |
---|
[5dadc9b] | 855 | else { |
---|
| 856 | // get mutual exclusion |
---|
| 857 | lock(ring.submit_q.lock __cfaabi_dbg_ctx2); |
---|
[2489d31] | 858 | |
---|
[5dadc9b] | 859 | // Append to the list of ready entries |
---|
| 860 | |
---|
| 861 | /* paranoid */ verify( idx <= mask ); |
---|
| 862 | |
---|
| 863 | ring.submit_q.array[ (*tail) & mask ] = idx & mask; |
---|
| 864 | __atomic_fetch_add(tail, 1ul32, __ATOMIC_SEQ_CST); |
---|
[d384787] | 865 | |
---|
[5dadc9b] | 866 | // Submit however, many entries need to be submitted |
---|
| 867 | int ret = syscall( __NR_io_uring_enter, ring.fd, 1, 0, 0, 0p, 0); |
---|
| 868 | if( ret < 0 ) { |
---|
| 869 | switch((int)errno) { |
---|
| 870 | default: |
---|
| 871 | abort( "KERNEL ERROR: IO_URING SUBMIT - %s\n", strerror(errno) ); |
---|
| 872 | } |
---|
| 873 | } |
---|
| 874 | |
---|
| 875 | // update statistics |
---|
| 876 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[6f121b8] | 877 | ring.submit_q.stats.submit_avg.csm += 1; |
---|
[5dadc9b] | 878 | ring.submit_q.stats.submit_avg.cnt += 1; |
---|
| 879 | #endif |
---|
| 880 | |
---|
| 881 | unlock(ring.submit_q.lock); |
---|
[dd4e2d7] | 882 | |
---|
| 883 | __cfadbg_print_safe( io, "Kernel I/O : Performed io_submit for %p, returned %d\n", active_thread(), ret ); |
---|
[5dadc9b] | 884 | } |
---|
[2489d31] | 885 | } |
---|
| 886 | |
---|
| 887 | static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd) { |
---|
| 888 | this.opcode = opcode; |
---|
| 889 | #if !defined(IOSQE_ASYNC) |
---|
| 890 | this.flags = 0; |
---|
| 891 | #else |
---|
| 892 | this.flags = IOSQE_ASYNC; |
---|
| 893 | #endif |
---|
| 894 | this.ioprio = 0; |
---|
| 895 | this.fd = fd; |
---|
| 896 | this.off = 0; |
---|
| 897 | this.addr = 0; |
---|
| 898 | this.len = 0; |
---|
| 899 | this.rw_flags = 0; |
---|
| 900 | this.__pad2[0] = this.__pad2[1] = this.__pad2[2] = 0; |
---|
[2d8f7b0] | 901 | } |
---|
| 902 | |
---|
[2489d31] | 903 | static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd, void * addr, uint32_t len, uint64_t off ) { |
---|
| 904 | (this){ opcode, fd }; |
---|
| 905 | this.off = off; |
---|
| 906 | this.addr = (uint64_t)addr; |
---|
| 907 | this.len = len; |
---|
| 908 | } |
---|
[f6660520] | 909 | |
---|
[92976d9] | 910 | |
---|
| 911 | //============================================================================================= |
---|
| 912 | // I/O Interface |
---|
| 913 | //============================================================================================= |
---|
[f6660520] | 914 | |
---|
[2d8f7b0] | 915 | #define __submit_prelude \ |
---|
[6f121b8] | 916 | io_user_data data = { 0, active_thread() }; \ |
---|
| 917 | struct __io_data & ring = *data.thrd->curr_cluster->io; \ |
---|
[2d8f7b0] | 918 | struct io_uring_sqe * sqe; \ |
---|
| 919 | uint32_t idx; \ |
---|
[6f121b8] | 920 | [sqe, idx] = __submit_alloc( ring, (uint64_t)&data ); |
---|
[2d8f7b0] | 921 | |
---|
| 922 | #define __submit_wait \ |
---|
[185efe6] | 923 | /*__cfaabi_bits_print_safe( STDERR_FILENO, "Preparing user data %p for %p\n", &data, data.thrd );*/ \ |
---|
[6f121b8] | 924 | verify( sqe->user_data == (uint64_t)&data ); \ |
---|
[2d8f7b0] | 925 | __submit( ring, idx ); \ |
---|
| 926 | park( __cfaabi_dbg_ctx ); \ |
---|
| 927 | return data.result; |
---|
[ecf6b46] | 928 | #endif |
---|
[2d8f7b0] | 929 | |
---|
[0ea6c5a] | 930 | // Some forward declarations |
---|
| 931 | extern "C" { |
---|
[1268ad8] | 932 | #include <unistd.h> |
---|
[0ea6c5a] | 933 | #include <sys/types.h> |
---|
[93f7c001] | 934 | #include <sys/socket.h> |
---|
[6136ecc] | 935 | #include <sys/syscall.h> |
---|
[08a994e] | 936 | |
---|
| 937 | #if defined(HAVE_PREADV2) |
---|
[0ea6c5a] | 938 | struct iovec; |
---|
| 939 | extern ssize_t preadv2 (int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags); |
---|
[08a994e] | 940 | #endif |
---|
| 941 | #if defined(HAVE_PWRITEV2) |
---|
| 942 | struct iovec; |
---|
[0ea6c5a] | 943 | extern ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags); |
---|
[08a994e] | 944 | #endif |
---|
[0ea6c5a] | 945 | |
---|
| 946 | extern int fsync(int fd); |
---|
| 947 | extern int sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags); |
---|
| 948 | |
---|
| 949 | struct msghdr; |
---|
| 950 | struct sockaddr; |
---|
| 951 | extern ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags); |
---|
| 952 | extern ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); |
---|
| 953 | extern ssize_t send(int sockfd, const void *buf, size_t len, int flags); |
---|
| 954 | extern ssize_t recv(int sockfd, void *buf, size_t len, int flags); |
---|
| 955 | extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); |
---|
| 956 | extern int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
---|
| 957 | |
---|
| 958 | extern int fallocate(int fd, int mode, uint64_t offset, uint64_t len); |
---|
| 959 | extern int posix_fadvise(int fd, uint64_t offset, uint64_t len, int advice); |
---|
| 960 | extern int madvise(void *addr, size_t length, int advice); |
---|
| 961 | |
---|
| 962 | extern int openat(int dirfd, const char *pathname, int flags, mode_t mode); |
---|
| 963 | extern int close(int fd); |
---|
| 964 | |
---|
| 965 | extern ssize_t read (int fd, void *buf, size_t count); |
---|
| 966 | } |
---|
| 967 | |
---|
[2d8f7b0] | 968 | //----------------------------------------------------------------------------- |
---|
| 969 | // Asynchronous operations |
---|
[08a994e] | 970 | #if defined(HAVE_PREADV2) |
---|
| 971 | ssize_t cfa_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) { |
---|
| 972 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READV) |
---|
| 973 | return preadv2(fd, iov, iovcnt, offset, flags); |
---|
| 974 | #else |
---|
| 975 | __submit_prelude |
---|
[ecf6b46] | 976 | |
---|
[08a994e] | 977 | (*sqe){ IORING_OP_READV, fd, iov, iovcnt, offset }; |
---|
[ecf6b46] | 978 | |
---|
[08a994e] | 979 | __submit_wait |
---|
| 980 | #endif |
---|
| 981 | } |
---|
| 982 | #endif |
---|
[ecf6b46] | 983 | |
---|
[08a994e] | 984 | #if defined(HAVE_PWRITEV2) |
---|
| 985 | ssize_t cfa_pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) { |
---|
| 986 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITEV) |
---|
| 987 | return pwritev2(fd, iov, iovcnt, offset, flags); |
---|
| 988 | #else |
---|
| 989 | __submit_prelude |
---|
[ecf6b46] | 990 | |
---|
[08a994e] | 991 | (*sqe){ IORING_OP_WRITEV, fd, iov, iovcnt, offset }; |
---|
[ecf6b46] | 992 | |
---|
[08a994e] | 993 | __submit_wait |
---|
| 994 | #endif |
---|
| 995 | } |
---|
| 996 | #endif |
---|
[ecf6b46] | 997 | |
---|
| 998 | int cfa_fsync(int fd) { |
---|
| 999 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FSYNC) |
---|
| 1000 | return fsync(fd); |
---|
| 1001 | #else |
---|
| 1002 | __submit_prelude |
---|
| 1003 | |
---|
| 1004 | (*sqe){ IORING_OP_FSYNC, fd }; |
---|
| 1005 | |
---|
| 1006 | __submit_wait |
---|
| 1007 | #endif |
---|
| 1008 | } |
---|
| 1009 | |
---|
| 1010 | int cfa_sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags) { |
---|
| 1011 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SYNC_FILE_RANGE) |
---|
| 1012 | return sync_file_range(fd, offset, nbytes, flags); |
---|
| 1013 | #else |
---|
| 1014 | __submit_prelude |
---|
| 1015 | |
---|
| 1016 | (*sqe){ IORING_OP_SYNC_FILE_RANGE, fd }; |
---|
| 1017 | sqe->off = offset; |
---|
| 1018 | sqe->len = nbytes; |
---|
| 1019 | sqe->sync_range_flags = flags; |
---|
| 1020 | |
---|
| 1021 | __submit_wait |
---|
| 1022 | #endif |
---|
| 1023 | } |
---|
| 1024 | |
---|
| 1025 | |
---|
| 1026 | ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags) { |
---|
| 1027 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SENDMSG) |
---|
[2292067] | 1028 | return sendmsg(sockfd, msg, flags); |
---|
[ecf6b46] | 1029 | #else |
---|
| 1030 | __submit_prelude |
---|
| 1031 | |
---|
| 1032 | (*sqe){ IORING_OP_SENDMSG, sockfd, msg, 1, 0 }; |
---|
| 1033 | sqe->msg_flags = flags; |
---|
| 1034 | |
---|
| 1035 | __submit_wait |
---|
| 1036 | #endif |
---|
| 1037 | } |
---|
| 1038 | |
---|
| 1039 | ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags) { |
---|
| 1040 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECVMSG) |
---|
[2292067] | 1041 | return recvmsg(sockfd, msg, flags); |
---|
[ecf6b46] | 1042 | #else |
---|
| 1043 | __submit_prelude |
---|
| 1044 | |
---|
| 1045 | (*sqe){ IORING_OP_RECVMSG, sockfd, msg, 1, 0 }; |
---|
| 1046 | sqe->msg_flags = flags; |
---|
| 1047 | |
---|
| 1048 | __submit_wait |
---|
| 1049 | #endif |
---|
| 1050 | } |
---|
| 1051 | |
---|
| 1052 | ssize_t cfa_send(int sockfd, const void *buf, size_t len, int flags) { |
---|
| 1053 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SEND) |
---|
| 1054 | return send( sockfd, buf, len, flags ); |
---|
| 1055 | #else |
---|
| 1056 | __submit_prelude |
---|
| 1057 | |
---|
| 1058 | (*sqe){ IORING_OP_SEND, sockfd }; |
---|
| 1059 | sqe->addr = (uint64_t)buf; |
---|
| 1060 | sqe->len = len; |
---|
| 1061 | sqe->msg_flags = flags; |
---|
| 1062 | |
---|
| 1063 | __submit_wait |
---|
| 1064 | #endif |
---|
| 1065 | } |
---|
| 1066 | |
---|
| 1067 | ssize_t cfa_recv(int sockfd, void *buf, size_t len, int flags) { |
---|
| 1068 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECV) |
---|
| 1069 | return recv( sockfd, buf, len, flags ); |
---|
| 1070 | #else |
---|
| 1071 | __submit_prelude |
---|
| 1072 | |
---|
| 1073 | (*sqe){ IORING_OP_RECV, sockfd }; |
---|
| 1074 | sqe->addr = (uint64_t)buf; |
---|
| 1075 | sqe->len = len; |
---|
| 1076 | sqe->msg_flags = flags; |
---|
| 1077 | |
---|
| 1078 | __submit_wait |
---|
| 1079 | #endif |
---|
| 1080 | } |
---|
| 1081 | |
---|
| 1082 | int cfa_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) { |
---|
| 1083 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_ACCEPT) |
---|
[0ea6c5a] | 1084 | return accept4( sockfd, addr, addrlen, flags ); |
---|
[ecf6b46] | 1085 | #else |
---|
| 1086 | __submit_prelude |
---|
| 1087 | |
---|
| 1088 | (*sqe){ IORING_OP_ACCEPT, sockfd }; |
---|
| 1089 | sqe->addr = addr; |
---|
| 1090 | sqe->addr2 = addrlen; |
---|
| 1091 | sqe->accept_flags = flags; |
---|
| 1092 | |
---|
| 1093 | __submit_wait |
---|
| 1094 | #endif |
---|
| 1095 | } |
---|
| 1096 | |
---|
| 1097 | int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { |
---|
| 1098 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CONNECT) |
---|
[0ea6c5a] | 1099 | return connect( sockfd, addr, addrlen ); |
---|
[ecf6b46] | 1100 | #else |
---|
| 1101 | __submit_prelude |
---|
| 1102 | |
---|
| 1103 | (*sqe){ IORING_OP_CONNECT, sockfd }; |
---|
| 1104 | sqe->addr = (uint64_t)addr; |
---|
| 1105 | sqe->off = addrlen; |
---|
| 1106 | |
---|
| 1107 | __submit_wait |
---|
| 1108 | #endif |
---|
| 1109 | } |
---|
| 1110 | |
---|
| 1111 | int cfa_fallocate(int fd, int mode, uint64_t offset, uint64_t len) { |
---|
| 1112 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FALLOCATE) |
---|
| 1113 | return fallocate( fd, mode, offset, len ); |
---|
| 1114 | #else |
---|
| 1115 | __submit_prelude |
---|
| 1116 | |
---|
| 1117 | (*sqe){ IORING_OP_FALLOCATE, fd }; |
---|
| 1118 | sqe->off = offset; |
---|
| 1119 | sqe->len = length; |
---|
| 1120 | sqe->mode = mode; |
---|
| 1121 | |
---|
| 1122 | __submit_wait |
---|
| 1123 | #endif |
---|
| 1124 | } |
---|
| 1125 | |
---|
| 1126 | int cfa_fadvise(int fd, uint64_t offset, uint64_t len, int advice) { |
---|
| 1127 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FADVISE) |
---|
| 1128 | return posix_fadvise( fd, offset, len, advice ); |
---|
| 1129 | #else |
---|
| 1130 | __submit_prelude |
---|
| 1131 | |
---|
| 1132 | (*sqe){ IORING_OP_FADVISE, fd }; |
---|
| 1133 | sqe->off = (uint64_t)offset; |
---|
| 1134 | sqe->len = length; |
---|
| 1135 | sqe->fadvise_advice = advice; |
---|
| 1136 | |
---|
| 1137 | __submit_wait |
---|
| 1138 | #endif |
---|
| 1139 | } |
---|
| 1140 | |
---|
| 1141 | int cfa_madvise(void *addr, size_t length, int advice) { |
---|
| 1142 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_MADVISE) |
---|
| 1143 | return madvise( addr, length, advice ); |
---|
| 1144 | #else |
---|
| 1145 | __submit_prelude |
---|
| 1146 | |
---|
| 1147 | (*sqe){ IORING_OP_MADVISE, 0 }; |
---|
| 1148 | sqe->addr = (uint64_t)addr; |
---|
| 1149 | sqe->len = length; |
---|
| 1150 | sqe->fadvise_advice = advice; |
---|
| 1151 | |
---|
| 1152 | __submit_wait |
---|
| 1153 | #endif |
---|
| 1154 | } |
---|
| 1155 | |
---|
| 1156 | int cfa_openat(int dirfd, const char *pathname, int flags, mode_t mode) { |
---|
| 1157 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_OPENAT) |
---|
| 1158 | return openat( dirfd, pathname, flags, mode ); |
---|
| 1159 | #else |
---|
| 1160 | __submit_prelude |
---|
| 1161 | |
---|
| 1162 | (*sqe){ IORING_OP_OPENAT, dirfd }; |
---|
| 1163 | sqe->addr = (uint64_t)pathname; |
---|
| 1164 | sqe->open_flags = flags; |
---|
| 1165 | sqe->mode = mode; |
---|
| 1166 | |
---|
| 1167 | __submit_wait |
---|
| 1168 | #endif |
---|
| 1169 | } |
---|
| 1170 | |
---|
| 1171 | int cfa_close(int fd) { |
---|
| 1172 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CLOSE) |
---|
| 1173 | return close( fd ); |
---|
| 1174 | #else |
---|
| 1175 | __submit_prelude |
---|
| 1176 | |
---|
| 1177 | (*sqe){ IORING_OP_CLOSE, fd }; |
---|
| 1178 | |
---|
| 1179 | __submit_wait |
---|
| 1180 | #endif |
---|
| 1181 | } |
---|
| 1182 | |
---|
| 1183 | |
---|
| 1184 | ssize_t cfa_read(int fd, void *buf, size_t count) { |
---|
| 1185 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READ) |
---|
| 1186 | return read( fd, buf, count ); |
---|
| 1187 | #else |
---|
| 1188 | __submit_prelude |
---|
| 1189 | |
---|
| 1190 | (*sqe){ IORING_OP_READ, fd, buf, count, 0 }; |
---|
| 1191 | |
---|
| 1192 | __submit_wait |
---|
| 1193 | #endif |
---|
| 1194 | } |
---|
| 1195 | |
---|
| 1196 | ssize_t cfa_write(int fd, void *buf, size_t count) { |
---|
| 1197 | #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITE) |
---|
| 1198 | return read( fd, buf, count ); |
---|
| 1199 | #else |
---|
| 1200 | __submit_prelude |
---|
| 1201 | |
---|
| 1202 | (*sqe){ IORING_OP_WRITE, fd, buf, count, 0 }; |
---|
| 1203 | |
---|
| 1204 | __submit_wait |
---|
| 1205 | #endif |
---|
| 1206 | } |
---|
[2d8f7b0] | 1207 | |
---|
| 1208 | //----------------------------------------------------------------------------- |
---|
| 1209 | // Check if a function is asynchronous |
---|
| 1210 | |
---|
| 1211 | // Macro magic to reduce the size of the following switch case |
---|
[ecf6b46] | 1212 | #define IS_DEFINED_APPLY(f, ...) f(__VA_ARGS__) |
---|
| 1213 | #define IS_DEFINED_SECOND(first, second, ...) second |
---|
| 1214 | #define IS_DEFINED_TEST(expansion) _CFA_IO_FEATURE_##expansion |
---|
| 1215 | #define IS_DEFINED(macro) IS_DEFINED_APPLY( IS_DEFINED_SECOND,IS_DEFINED_TEST(macro) false, true) |
---|
[2d8f7b0] | 1216 | |
---|
[ecf6b46] | 1217 | bool has_user_level_blocking( fptr_t func ) { |
---|
| 1218 | #if defined(HAVE_LINUX_IO_URING_H) |
---|
[08a994e] | 1219 | #if defined(HAVE_PREADV2) |
---|
[171ca0d] | 1220 | if( /*func == (fptr_t)preadv2 || */ |
---|
| 1221 | func == (fptr_t)cfa_preadv2 ) |
---|
| 1222 | #define _CFA_IO_FEATURE_IORING_OP_READV , |
---|
| 1223 | return IS_DEFINED(IORING_OP_READV); |
---|
| 1224 | #endif |
---|
| 1225 | |
---|
| 1226 | #if defined(HAVE_PWRITEV2) |
---|
[08a994e] | 1227 | if( /*func == (fptr_t)pwritev2 || */ |
---|
| 1228 | func == (fptr_t)cfa_pwritev2 ) |
---|
| 1229 | #define _CFA_IO_FEATURE_IORING_OP_WRITEV , |
---|
| 1230 | return IS_DEFINED(IORING_OP_WRITEV); |
---|
| 1231 | #endif |
---|
[2d8f7b0] | 1232 | |
---|
[171ca0d] | 1233 | if( /*func == (fptr_t)fsync || */ |
---|
| 1234 | func == (fptr_t)cfa_fsync ) |
---|
| 1235 | #define _CFA_IO_FEATURE_IORING_OP_FSYNC , |
---|
| 1236 | return IS_DEFINED(IORING_OP_FSYNC); |
---|
[2d8f7b0] | 1237 | |
---|
| 1238 | if( /*func == (fptr_t)ync_file_range || */ |
---|
[ecf6b46] | 1239 | func == (fptr_t)cfa_sync_file_range ) |
---|
[2d8f7b0] | 1240 | #define _CFA_IO_FEATURE_IORING_OP_SYNC_FILE_RANGE , |
---|
| 1241 | return IS_DEFINED(IORING_OP_SYNC_FILE_RANGE); |
---|
| 1242 | |
---|
| 1243 | if( /*func == (fptr_t)sendmsg || */ |
---|
[ecf6b46] | 1244 | func == (fptr_t)cfa_sendmsg ) |
---|
[2d8f7b0] | 1245 | #define _CFA_IO_FEATURE_IORING_OP_SENDMSG , |
---|
| 1246 | return IS_DEFINED(IORING_OP_SENDMSG); |
---|
| 1247 | |
---|
| 1248 | if( /*func == (fptr_t)recvmsg || */ |
---|
[ecf6b46] | 1249 | func == (fptr_t)cfa_recvmsg ) |
---|
[2d8f7b0] | 1250 | #define _CFA_IO_FEATURE_IORING_OP_RECVMSG , |
---|
| 1251 | return IS_DEFINED(IORING_OP_RECVMSG); |
---|
| 1252 | |
---|
| 1253 | if( /*func == (fptr_t)send || */ |
---|
[2489d31] | 1254 | func == (fptr_t)cfa_send ) |
---|
[2d8f7b0] | 1255 | #define _CFA_IO_FEATURE_IORING_OP_SEND , |
---|
| 1256 | return IS_DEFINED(IORING_OP_SEND); |
---|
| 1257 | |
---|
| 1258 | if( /*func == (fptr_t)recv || */ |
---|
[2489d31] | 1259 | func == (fptr_t)cfa_recv ) |
---|
[2d8f7b0] | 1260 | #define _CFA_IO_FEATURE_IORING_OP_RECV , |
---|
| 1261 | return IS_DEFINED(IORING_OP_RECV); |
---|
| 1262 | |
---|
| 1263 | if( /*func == (fptr_t)accept4 || */ |
---|
[2489d31] | 1264 | func == (fptr_t)cfa_accept4 ) |
---|
[2d8f7b0] | 1265 | #define _CFA_IO_FEATURE_IORING_OP_ACCEPT , |
---|
| 1266 | return IS_DEFINED(IORING_OP_ACCEPT); |
---|
| 1267 | |
---|
| 1268 | if( /*func == (fptr_t)connect || */ |
---|
[2489d31] | 1269 | func == (fptr_t)cfa_connect ) |
---|
[2d8f7b0] | 1270 | #define _CFA_IO_FEATURE_IORING_OP_CONNECT , |
---|
| 1271 | return IS_DEFINED(IORING_OP_CONNECT); |
---|
| 1272 | |
---|
| 1273 | if( /*func == (fptr_t)fallocate || */ |
---|
[2489d31] | 1274 | func == (fptr_t)cfa_fallocate ) |
---|
[2d8f7b0] | 1275 | #define _CFA_IO_FEATURE_IORING_OP_FALLOCATE , |
---|
| 1276 | return IS_DEFINED(IORING_OP_FALLOCATE); |
---|
| 1277 | |
---|
[0ea6c5a] | 1278 | if( /*func == (fptr_t)posix_fadvise || */ |
---|
[2489d31] | 1279 | func == (fptr_t)cfa_fadvise ) |
---|
[2d8f7b0] | 1280 | #define _CFA_IO_FEATURE_IORING_OP_FADVISE , |
---|
| 1281 | return IS_DEFINED(IORING_OP_FADVISE); |
---|
| 1282 | |
---|
| 1283 | if( /*func == (fptr_t)madvise || */ |
---|
[2489d31] | 1284 | func == (fptr_t)cfa_madvise ) |
---|
[2d8f7b0] | 1285 | #define _CFA_IO_FEATURE_IORING_OP_MADVISE , |
---|
| 1286 | return IS_DEFINED(IORING_OP_MADVISE); |
---|
| 1287 | |
---|
| 1288 | if( /*func == (fptr_t)openat || */ |
---|
[2489d31] | 1289 | func == (fptr_t)cfa_openat ) |
---|
[2d8f7b0] | 1290 | #define _CFA_IO_FEATURE_IORING_OP_OPENAT , |
---|
| 1291 | return IS_DEFINED(IORING_OP_OPENAT); |
---|
| 1292 | |
---|
| 1293 | if( /*func == (fptr_t)close || */ |
---|
[2489d31] | 1294 | func == (fptr_t)cfa_close ) |
---|
[2d8f7b0] | 1295 | #define _CFA_IO_FEATURE_IORING_OP_CLOSE , |
---|
| 1296 | return IS_DEFINED(IORING_OP_CLOSE); |
---|
| 1297 | |
---|
| 1298 | if( /*func == (fptr_t)read || */ |
---|
[ecf6b46] | 1299 | func == (fptr_t)cfa_read ) |
---|
[2d8f7b0] | 1300 | #define _CFA_IO_FEATURE_IORING_OP_READ , |
---|
| 1301 | return IS_DEFINED(IORING_OP_READ); |
---|
| 1302 | |
---|
| 1303 | if( /*func == (fptr_t)write || */ |
---|
[ecf6b46] | 1304 | func == (fptr_t)cfa_write ) |
---|
[2d8f7b0] | 1305 | #define _CFA_IO_FEATURE_IORING_OP_WRITE , |
---|
| 1306 | return IS_DEFINED(IORING_OP_WRITE); |
---|
[ecf6b46] | 1307 | #endif |
---|
[2d8f7b0] | 1308 | |
---|
[ecf6b46] | 1309 | return false; |
---|
| 1310 | } |
---|