source: libcfa/src/concurrency/io.cfa@ 77adaee

ADT ast-experimental enum pthread-emulation qualifiedEnum
Last change on this file since 77adaee was 729df21, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 21.3 KB
Line 
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
16#define __cforall_thread__
17#define _GNU_SOURCE
18
19#if defined(__CFA_DEBUG__)
20 // #define __CFA_DEBUG_PRINT_IO__
21 // #define __CFA_DEBUG_PRINT_IO_CORE__
22#endif
23
24
25#if defined(CFA_HAVE_LINUX_IO_URING_H)
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 extern "C" {
33 #include <sys/syscall.h>
34 #include <sys/eventfd.h>
35 #include <sys/uio.h>
36
37 #include <linux/io_uring.h>
38 }
39
40 #include "stats.hfa"
41 #include "kernel.hfa"
42 #include "kernel/fwd.hfa"
43 #include "kernel/private.hfa"
44 #include "kernel/cluster.hfa"
45 #include "io/types.hfa"
46
47 __attribute__((unused)) static const char * opcodes[] = {
48 "OP_NOP",
49 "OP_READV",
50 "OP_WRITEV",
51 "OP_FSYNC",
52 "OP_READ_FIXED",
53 "OP_WRITE_FIXED",
54 "OP_POLL_ADD",
55 "OP_POLL_REMOVE",
56 "OP_SYNC_FILE_RANGE",
57 "OP_SENDMSG",
58 "OP_RECVMSG",
59 "OP_TIMEOUT",
60 "OP_TIMEOUT_REMOVE",
61 "OP_ACCEPT",
62 "OP_ASYNC_CANCEL",
63 "OP_LINK_TIMEOUT",
64 "OP_CONNECT",
65 "OP_FALLOCATE",
66 "OP_OPENAT",
67 "OP_CLOSE",
68 "OP_FILES_UPDATE",
69 "OP_STATX",
70 "OP_READ",
71 "OP_WRITE",
72 "OP_FADVISE",
73 "OP_MADVISE",
74 "OP_SEND",
75 "OP_RECV",
76 "OP_OPENAT2",
77 "OP_EPOLL_CTL",
78 "OP_SPLICE",
79 "OP_PROVIDE_BUFFERS",
80 "OP_REMOVE_BUFFERS",
81 "OP_TEE",
82 "INVALID_OP"
83 };
84
85 static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want );
86 static void __ioarbiter_submit( $io_context * , __u32 idxs[], __u32 have, bool lazy );
87 static void __ioarbiter_flush ( $io_context & );
88 static inline void __ioarbiter_notify( $io_context & ctx );
89//=============================================================================================
90// I/O Polling
91//=============================================================================================
92 static inline unsigned __flush( struct $io_context & );
93 static inline __u32 __release_sqes( struct $io_context & );
94 extern void __kernel_unpark( thread$ * thrd, unpark_hint );
95
96 static void ioring_syscsll( struct $io_context & ctx, unsigned int min_comp, unsigned int flags ) {
97 __STATS__( true, io.calls.flush++; )
98 int ret = syscall( __NR_io_uring_enter, ctx.fd, ctx.sq.to_submit, min_comp, flags, (sigset_t *)0p, _NSIG / 8);
99 if( ret < 0 ) {
100 switch((int)errno) {
101 case EAGAIN:
102 case EINTR:
103 case EBUSY:
104 // Update statistics
105 __STATS__( false, io.calls.errors.busy ++; )
106 return false;
107 default:
108 abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) );
109 }
110 }
111
112 __cfadbg_print_safe(io, "Kernel I/O : %u submitted to io_uring %d\n", ret, ctx.fd);
113 __STATS__( true, io.calls.submitted += ret; )
114 /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num );
115 /* paranoid */ verify( ctx.sq.to_submit >= ret );
116
117 ctx.sq.to_submit -= ret;
118
119 /* paranoid */ verify( ctx.sq.to_submit <= *ctx.sq.num );
120
121 // Release the consumed SQEs
122 __release_sqes( ctx );
123
124 /* paranoid */ verify( ! __preemption_enabled() );
125
126 __atomic_store_n(&ctx.proc->io.pending, false, __ATOMIC_RELAXED);
127 }
128
129 static bool try_acquire( $io_context * ctx ) __attribute__((nonnull(1))) {
130 /* paranoid */ verify( ! __preemption_enabled() );
131 /* paranoid */ verify( ready_schedule_islocked() );
132
133
134 {
135 const __u32 head = *ctx->cq.head;
136 const __u32 tail = *ctx->cq.tail;
137
138 if(head == tail) return false;
139 }
140
141 // Drain the queue
142 if(!__atomic_try_acquire(&ctx->cq.lock)) {
143 __STATS__( false, io.calls.locked++; )
144 return false;
145 }
146
147 return true;
148 }
149
150 static bool __cfa_do_drain( $io_context * ctx, cluster * cltr ) __attribute__((nonnull(1, 2))) {
151 /* paranoid */ verify( ! __preemption_enabled() );
152 /* paranoid */ verify( ready_schedule_islocked() );
153 /* paranoid */ verify( ctx->cq.lock == true );
154
155 const __u32 mask = *ctx->cq.mask;
156 unsigned long long ts_prev = ctx->cq.ts;
157
158 // re-read the head and tail in case it already changed.
159 const __u32 head = *ctx->cq.head;
160 const __u32 tail = *ctx->cq.tail;
161 const __u32 count = tail - head;
162 __STATS__( false, io.calls.drain++; io.calls.completed += count; )
163
164 for(i; count) {
165 unsigned idx = (head + i) & mask;
166 volatile struct io_uring_cqe & cqe = ctx->cq.cqes[idx];
167
168 /* paranoid */ verify(&cqe);
169
170 struct io_future_t * future = (struct io_future_t *)(uintptr_t)cqe.user_data;
171 // __cfadbg_print_safe( io, "Kernel I/O : Syscall completed : cqe %p, result %d for %p\n", &cqe, cqe.res, future );
172
173 __kernel_unpark( fulfil( *future, cqe.res, false ), UNPARK_LOCAL );
174 }
175
176 unsigned long long ts_next = ctx->cq.ts = rdtscl();
177
178 // Mark to the kernel that the cqe has been seen
179 // Ensure that the kernel only sees the new value of the head index after the CQEs have been read.
180 __atomic_store_n( ctx->cq.head, head + count, __ATOMIC_SEQ_CST );
181 ctx->proc->idle_wctx.drain_time = ts_next;
182
183 __cfadbg_print_safe(io, "Kernel I/O : %u completed age %llu\n", count, ts_next);
184 /* paranoid */ verify( ready_schedule_islocked() );
185 /* paranoid */ verify( ! __preemption_enabled() );
186
187 __atomic_unlock(&ctx->cq.lock);
188
189 touch_tsc( cltr->sched.io.tscs, ctx->cq.id, ts_prev, ts_next );
190
191 return true;
192 }
193
194 bool __cfa_io_drain( processor * proc ) {
195 bool local = false;
196 bool remote = false;
197
198 ready_schedule_lock();
199
200 cluster * const cltr = proc->cltr;
201 $io_context * const ctx = proc->io.ctx;
202 /* paranoid */ verify( cltr );
203 /* paranoid */ verify( ctx );
204
205 with(cltr->sched) {
206 const size_t ctxs_count = io.count;
207
208 /* paranoid */ verify( ready_schedule_islocked() );
209 /* paranoid */ verify( ! __preemption_enabled() );
210 /* paranoid */ verify( active_processor() == proc );
211 /* paranoid */ verify( __shard_factor.io > 0 );
212 /* paranoid */ verify( ctxs_count > 0 );
213 /* paranoid */ verify( ctx->cq.id < ctxs_count );
214
215 const unsigned this_cache = cache_id(cltr, ctx->cq.id / __shard_factor.io);
216 const unsigned long long ctsc = rdtscl();
217
218 if(proc->io.target == MAX) {
219 uint64_t chaos = __tls_rand();
220 unsigned ext = chaos & 0xff;
221 unsigned other = (chaos >> 8) % (ctxs_count);
222
223 if(ext < 3 || __atomic_load_n(&caches[other / __shard_factor.io].id, __ATOMIC_RELAXED) == this_cache) {
224 proc->io.target = other;
225 }
226 }
227 else {
228 const unsigned target = proc->io.target;
229 /* paranoid */ verify( io.tscs[target].tv != MAX );
230 HELP: if(target < ctxs_count) {
231 const unsigned long long cutoff = calc_cutoff(ctsc, ctx->cq.id, ctxs_count, io.data, io.tscs, __shard_factor.io);
232 const unsigned long long age = moving_average(ctsc, io.tscs[target].tv, io.tscs[target].ma);
233 __cfadbg_print_safe(io, "Kernel I/O: Help attempt on %u from %u, age %'llu vs cutoff %'llu, %s\n", target, ctx->cq.id, age, cutoff, age > cutoff ? "yes" : "no");
234 if(age <= cutoff) break HELP;
235
236 if(!try_acquire(io.data[target])) break HELP;
237
238 if(!__cfa_do_drain( io.data[target], cltr )) break HELP;
239
240 remote = true;
241 __STATS__( false, io.calls.helped++; )
242 }
243 proc->io.target = MAX;
244 }
245 }
246
247
248 // Drain the local queue
249 if(try_acquire( proc->io.ctx )) {
250 local = __cfa_do_drain( proc->io.ctx, cltr );
251 }
252
253 /* paranoid */ verify( ready_schedule_islocked() );
254 /* paranoid */ verify( ! __preemption_enabled() );
255 /* paranoid */ verify( active_processor() == proc );
256
257 ready_schedule_unlock();
258 return local || remote;
259 }
260
261 bool __cfa_io_flush( processor * proc ) {
262 /* paranoid */ verify( ! __preemption_enabled() );
263 /* paranoid */ verify( proc );
264 /* paranoid */ verify( proc->io.ctx );
265
266 $io_context & ctx = *proc->io.ctx;
267
268 __ioarbiter_flush( ctx );
269
270 if(ctx.sq.to_submit != 0) {
271 ioring_syscsll(ctx, 0, 0);
272
273 }
274
275 return __cfa_io_drain( proc );
276 }
277
278//=============================================================================================
279// I/O Submissions
280//=============================================================================================
281
282// Submition steps :
283// 1 - Allocate a queue entry. The ring already has memory for all entries but only the ones
284// listed in sq.array are visible by the kernel. For those not listed, the kernel does not
285// offer any assurance that an entry is not being filled by multiple flags. Therefore, we
286// need to write an allocator that allows allocating concurrently.
287//
288// 2 - Actually fill the submit entry, this is the only simple and straightforward step.
289//
290// 3 - Append the entry index to the array and adjust the tail accordingly. This operation
291// needs to arrive to two concensus at the same time:
292// A - The order in which entries are listed in the array: no two threads must pick the
293// same index for their entries
294// B - When can the tail be update for the kernel. EVERY entries in the array between
295// head and tail must be fully filled and shouldn't ever be touched again.
296//
297 //=============================================================================================
298 // Allocation
299 // for user's convenience fill the sqes from the indexes
300 static inline void __fill(struct io_uring_sqe * out_sqes[], __u32 want, __u32 idxs[], struct $io_context * ctx) {
301 struct io_uring_sqe * sqes = ctx->sq.sqes;
302 for(i; want) {
303 // __cfadbg_print_safe(io, "Kernel I/O : filling loop\n");
304 out_sqes[i] = &sqes[idxs[i]];
305 }
306 }
307
308 // Try to directly allocate from the a given context
309 // Not thread-safe
310 static inline bool __alloc(struct $io_context * ctx, __u32 idxs[], __u32 want) {
311 __sub_ring_t & sq = ctx->sq;
312 const __u32 mask = *sq.mask;
313 __u32 fhead = sq.free_ring.head; // get the current head of the queue
314 __u32 ftail = sq.free_ring.tail; // get the current tail of the queue
315
316 // If we don't have enough sqes, fail
317 if((ftail - fhead) < want) { return false; }
318
319 // copy all the indexes we want from the available list
320 for(i; want) {
321 // __cfadbg_print_safe(io, "Kernel I/O : allocating loop\n");
322 idxs[i] = sq.free_ring.array[(fhead + i) & mask];
323 }
324
325 // Advance the head to mark the indexes as consumed
326 __atomic_store_n(&sq.free_ring.head, fhead + want, __ATOMIC_RELEASE);
327
328 // return success
329 return true;
330 }
331
332 // Allocate an submit queue entry.
333 // The kernel cannot see these entries until they are submitted, but other threads must be
334 // able to see which entries can be used and which are already un used by an other thread
335 // for convenience, return both the index and the pointer to the sqe
336 // sqe == &sqes[idx]
337 struct $io_context * cfa_io_allocate(struct io_uring_sqe * sqes[], __u32 idxs[], __u32 want) {
338 // __cfadbg_print_safe(io, "Kernel I/O : attempting to allocate %u\n", want);
339
340 disable_interrupts();
341 processor * proc = __cfaabi_tls.this_processor;
342 $io_context * ctx = proc->io.ctx;
343 /* paranoid */ verify( __cfaabi_tls.this_processor );
344 /* paranoid */ verify( ctx );
345
346 // __cfadbg_print_safe(io, "Kernel I/O : attempting to fast allocation\n");
347
348 // We can proceed to the fast path
349 if( __alloc(ctx, idxs, want) ) {
350 // Allocation was successful
351 __STATS__( true, io.alloc.fast += 1; )
352 enable_interrupts();
353
354 // __cfadbg_print_safe(io, "Kernel I/O : fast allocation successful from ring %d\n", ctx->fd);
355
356 __fill( sqes, want, idxs, ctx );
357 return ctx;
358 }
359 // The fast path failed, fallback
360 __STATS__( true, io.alloc.fail += 1; )
361
362 // Fast path failed, fallback on arbitration
363 __STATS__( true, io.alloc.slow += 1; )
364 enable_interrupts();
365
366 $io_arbiter * ioarb = proc->cltr->io.arbiter;
367 /* paranoid */ verify( ioarb );
368
369 // __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for allocation\n");
370
371 struct $io_context * ret = __ioarbiter_allocate(*ioarb, idxs, want);
372
373 // __cfadbg_print_safe(io, "Kernel I/O : slow allocation completed from ring %d\n", ret->fd);
374
375 __fill( sqes, want, idxs,ret );
376 return ret;
377 }
378
379 //=============================================================================================
380 // submission
381 static inline void __submit_only( struct $io_context * ctx, __u32 idxs[], __u32 have) {
382 // We can proceed to the fast path
383 // Get the right objects
384 __sub_ring_t & sq = ctx->sq;
385 const __u32 mask = *sq.mask;
386 __u32 tail = *sq.kring.tail;
387
388 // Add the sqes to the array
389 for( i; have ) {
390 // __cfadbg_print_safe(io, "Kernel I/O : __submit loop\n");
391 sq.kring.array[ (tail + i) & mask ] = idxs[i];
392 }
393
394 // Make the sqes visible to the submitter
395 __atomic_store_n(sq.kring.tail, tail + have, __ATOMIC_RELEASE);
396 sq.to_submit += have;
397
398 __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_RELAXED);
399 __atomic_store_n(&ctx->proc->io.dirty , true, __ATOMIC_RELAXED);
400 }
401
402 static inline void __submit( struct $io_context * ctx, __u32 idxs[], __u32 have, bool lazy) {
403 __sub_ring_t & sq = ctx->sq;
404 __submit_only(ctx, idxs, have);
405
406 if(sq.to_submit > 30) {
407 __tls_stats()->io.flush.full++;
408 __cfa_io_flush( ctx->proc );
409 }
410 if(!lazy) {
411 __tls_stats()->io.flush.eager++;
412 __cfa_io_flush( ctx->proc );
413 }
414 }
415
416 void cfa_io_submit( struct $io_context * inctx, __u32 idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1))) {
417 // __cfadbg_print_safe(io, "Kernel I/O : attempting to submit %u (%s)\n", have, lazy ? "lazy" : "eager");
418
419 disable_interrupts();
420 processor * proc = __cfaabi_tls.this_processor;
421 $io_context * ctx = proc->io.ctx;
422 /* paranoid */ verify( __cfaabi_tls.this_processor );
423 /* paranoid */ verify( ctx );
424
425 // Can we proceed to the fast path
426 if( ctx == inctx ) // We have the right instance?
427 {
428 __submit(ctx, idxs, have, lazy);
429
430 // Mark the instance as no longer in-use, re-enable interrupts and return
431 __STATS__( true, io.submit.fast += 1; )
432 enable_interrupts();
433
434 // __cfadbg_print_safe(io, "Kernel I/O : submitted on fast path\n");
435 return;
436 }
437
438 // Fast path failed, fallback on arbitration
439 __STATS__( true, io.submit.slow += 1; )
440 enable_interrupts();
441
442 // __cfadbg_print_safe(io, "Kernel I/O : falling back on arbiter for submission\n");
443
444 __ioarbiter_submit(inctx, idxs, have, lazy);
445 }
446
447 //=============================================================================================
448 // Flushing
449 // Go through the ring's submit queue and release everything that has already been consumed
450 // by io_uring
451 // This cannot be done by multiple threads
452 static __u32 __release_sqes( struct $io_context & ctx ) {
453 const __u32 mask = *ctx.sq.mask;
454
455 __attribute__((unused))
456 __u32 ctail = *ctx.sq.kring.tail; // get the current tail of the queue
457 __u32 chead = *ctx.sq.kring.head; // get the current head of the queue
458 __u32 phead = ctx.sq.kring.released; // get the head the last time we were here
459
460 __u32 ftail = ctx.sq.free_ring.tail; // get the current tail of the queue
461
462 // the 3 fields are organized like this diagram
463 // except it's are ring
464 // ---+--------+--------+----
465 // ---+--------+--------+----
466 // ^ ^ ^
467 // phead chead ctail
468
469 // make sure ctail doesn't wrap around and reach phead
470 /* paranoid */ verify(
471 (ctail >= chead && chead >= phead)
472 || (chead >= phead && phead >= ctail)
473 || (phead >= ctail && ctail >= chead)
474 );
475
476 // find the range we need to clear
477 __u32 count = chead - phead;
478
479 if(count == 0) {
480 return 0;
481 }
482
483 // We acquired an previous-head/current-head range
484 // go through the range and release the sqes
485 for( i; count ) {
486 // __cfadbg_print_safe(io, "Kernel I/O : release loop\n");
487 __u32 idx = ctx.sq.kring.array[ (phead + i) & mask ];
488 ctx.sq.free_ring.array[ (ftail + i) & mask ] = idx;
489 }
490
491 ctx.sq.kring.released = chead; // note up to were we processed
492 __atomic_store_n(&ctx.sq.free_ring.tail, ftail + count, __ATOMIC_SEQ_CST);
493
494 __ioarbiter_notify(ctx);
495
496 return count;
497 }
498
499//=============================================================================================
500// I/O Arbiter
501//=============================================================================================
502 static inline bool enqueue(__outstanding_io_queue & queue, __outstanding_io & item) {
503 bool was_empty;
504
505 // Lock the list, it's not thread safe
506 lock( queue.lock __cfaabi_dbg_ctx2 );
507 {
508 was_empty = empty(queue.queue);
509
510 // Add our request to the list
511 add( queue.queue, item );
512
513 // Mark as pending
514 __atomic_store_n( &queue.empty, false, __ATOMIC_SEQ_CST );
515 }
516 unlock( queue.lock );
517
518 return was_empty;
519 }
520
521 static inline bool empty(__outstanding_io_queue & queue ) {
522 return __atomic_load_n( &queue.empty, __ATOMIC_SEQ_CST);
523 }
524
525 static $io_context * __ioarbiter_allocate( $io_arbiter & this, __u32 idxs[], __u32 want ) {
526 // __cfadbg_print_safe(io, "Kernel I/O : arbiter allocating\n");
527
528 __STATS__( false, io.alloc.block += 1; )
529
530 // No one has any resources left, wait for something to finish
531 // We need to add ourself to a list of pending allocs and wait for an answer
532 __pending_alloc pa;
533 pa.idxs = idxs;
534 pa.want = want;
535
536 enqueue(this.pending, (__outstanding_io&)pa);
537
538 wait( pa.sem );
539
540 return pa.ctx;
541
542 }
543
544 static void __ioarbiter_notify( $io_arbiter & this, $io_context * ctx ) {
545 /* paranoid */ verify( !empty(this.pending.queue) );
546
547 lock( this.pending.lock __cfaabi_dbg_ctx2 );
548 {
549 while( !empty(this.pending.queue) ) {
550 __cfadbg_print_safe(io, "Kernel I/O : notifying\n");
551 __u32 have = ctx->sq.free_ring.tail - ctx->sq.free_ring.head;
552 __pending_alloc & pa = (__pending_alloc&)head( this.pending.queue );
553
554 if( have > pa.want ) goto DONE;
555 drop( this.pending.queue );
556
557 /* paranoid */__attribute__((unused)) bool ret =
558
559 __alloc(ctx, pa.idxs, pa.want);
560
561 /* paranoid */ verify( ret );
562
563 pa.ctx = ctx;
564
565 post( pa.sem );
566 }
567
568 this.pending.empty = true;
569 DONE:;
570 }
571 unlock( this.pending.lock );
572 }
573
574 static void __ioarbiter_notify( $io_context & ctx ) {
575 if(!empty( ctx.arbiter->pending )) {
576 __ioarbiter_notify( *ctx.arbiter, &ctx );
577 }
578 }
579
580 // Simply append to the pending
581 static void __ioarbiter_submit( $io_context * ctx, __u32 idxs[], __u32 have, bool lazy ) {
582 __cfadbg_print_safe(io, "Kernel I/O : submitting %u from the arbiter to context %u\n", have, ctx->fd);
583
584 __cfadbg_print_safe(io, "Kernel I/O : waiting to submit %u\n", have);
585
586 __external_io ei;
587 ei.idxs = idxs;
588 ei.have = have;
589 ei.lazy = lazy;
590
591 bool we = enqueue(ctx->ext_sq, (__outstanding_io&)ei);
592
593 __atomic_store_n(&ctx->proc->io.pending, true, __ATOMIC_SEQ_CST);
594
595 if( we ) {
596 sigval_t value = { PREEMPT_IO };
597 pthread_sigqueue(ctx->proc->kernel_thread, SIGUSR1, value);
598 }
599
600 wait( ei.sem );
601
602 __cfadbg_print_safe(io, "Kernel I/O : %u submitted from arbiter\n", have);
603 }
604
605 static void __ioarbiter_flush( $io_context & ctx ) {
606 if(!empty( ctx.ext_sq )) {
607 __STATS__( false, io.flush.external += 1; )
608
609 __cfadbg_print_safe(io, "Kernel I/O : arbiter flushing\n");
610
611 lock( ctx.ext_sq.lock __cfaabi_dbg_ctx2 );
612 {
613 while( !empty(ctx.ext_sq.queue) ) {
614 __external_io & ei = (__external_io&)drop( ctx.ext_sq.queue );
615
616 __submit_only(&ctx, ei.idxs, ei.have);
617
618 post( ei.sem );
619 }
620
621 ctx.ext_sq.empty = true;
622 }
623 unlock(ctx.ext_sq.lock );
624 }
625 }
626
627 #if defined(CFA_WITH_IO_URING_IDLE)
628 bool __kernel_read(processor * proc, io_future_t & future, iovec & iov, int fd) {
629 $io_context * ctx = proc->io.ctx;
630 /* paranoid */ verify( ! __preemption_enabled() );
631 /* paranoid */ verify( proc == __cfaabi_tls.this_processor );
632 /* paranoid */ verify( ctx );
633
634 __u32 idx;
635 struct io_uring_sqe * sqe;
636
637 // We can proceed to the fast path
638 if( !__alloc(ctx, &idx, 1) ) {
639 /* paranoid */ verify( false ); // for now check if this happens, next time just abort the sleep.
640 return false;
641 }
642
643 // Allocation was successful
644 __fill( &sqe, 1, &idx, ctx );
645
646 sqe->user_data = (uintptr_t)&future;
647 sqe->flags = 0;
648 sqe->fd = fd;
649 sqe->off = 0;
650 sqe->ioprio = 0;
651 sqe->fsync_flags = 0;
652 sqe->__pad2[0] = 0;
653 sqe->__pad2[1] = 0;
654 sqe->__pad2[2] = 0;
655
656 #if defined(CFA_HAVE_IORING_OP_READ)
657 sqe->opcode = IORING_OP_READ;
658 sqe->addr = (uint64_t)iov.iov_base;
659 sqe->len = iov.iov_len;
660 #elif defined(CFA_HAVE_READV) && defined(CFA_HAVE_IORING_OP_READV)
661 sqe->opcode = IORING_OP_READV;
662 sqe->addr = (uintptr_t)&iov;
663 sqe->len = 1;
664 #else
665 #error CFA_WITH_IO_URING_IDLE but none of CFA_HAVE_READV, CFA_HAVE_IORING_OP_READV or CFA_HAVE_IORING_OP_READ defined
666 #endif
667
668 asm volatile("": : :"memory");
669
670 /* paranoid */ verify( sqe->user_data == (uintptr_t)&future );
671 __submit_only( ctx, &idx, 1 );
672
673 /* paranoid */ verify( proc == __cfaabi_tls.this_processor );
674 /* paranoid */ verify( ! __preemption_enabled() );
675
676 return true;
677 }
678
679 void __cfa_io_idle( processor * proc ) {
680 iovec iov;
681 __atomic_acquire( &proc->io.ctx->cq.lock );
682
683 __attribute__((used)) volatile bool was_reset = false;
684
685 with( proc->idle_wctx) {
686
687 // Do we already have a pending read
688 if(available(*ftr)) {
689 // There is no pending read, we need to add one
690 reset(*ftr);
691
692 iov.iov_base = rdbuf;
693 iov.iov_len = sizeof(eventfd_t);
694 __kernel_read(proc, *ftr, iov, evfd );
695 ftr->result = 0xDEADDEAD;
696 *((eventfd_t *)rdbuf) = 0xDEADDEADDEADDEAD;
697 was_reset = true;
698 }
699 }
700
701 if( !__atomic_load_n( &proc->do_terminate, __ATOMIC_SEQ_CST ) ) {
702 __ioarbiter_flush( *proc->io.ctx );
703 proc->idle_wctx.sleep_time = rdtscl();
704 ioring_syscsll( *proc->io.ctx, 1, IORING_ENTER_GETEVENTS);
705 }
706
707 ready_schedule_lock();
708 __cfa_do_drain( proc->io.ctx, proc->cltr );
709 ready_schedule_unlock();
710
711 asm volatile ("" :: "m" (was_reset));
712 }
713 #endif
714#endif
Note: See TracBrowser for help on using the repository browser.