source: libcfa/src/concurrency/io/setup.cfa@ 85eafc5

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 85eafc5 was f277633e, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Fixed missing constructor when io_uring is not present

  • Property mode set to 100644
File size: 15.9 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/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#include "io/types.hfa"
20#include "kernel.hfa"
21
22#if !defined(CFA_HAVE_LINUX_IO_URING_H)
23 void __kernel_io_startup() {
24 // Nothing to do without io_uring
25 }
26
27 void __kernel_io_shutdown() {
28 // Nothing to do without io_uring
29 }
30
31 void ?{}(io_context_params & this) {}
32
33 void ?{}(io_context & this, struct cluster & cl) {}
34 void ?{}(io_context & this, struct cluster & cl, const io_context_params & params) {}
35
36 void ^?{}(io_context & this) {}
37 void ^?{}(io_context & this, bool cluster_context) {}
38
39#else
40 #include <errno.h>
41 #include <stdint.h>
42 #include <string.h>
43 #include <signal.h>
44 #include <unistd.h>
45
46 extern "C" {
47 #include <pthread.h>
48 #include <sys/epoll.h>
49 #include <sys/mman.h>
50 #include <sys/syscall.h>
51
52 #include <linux/io_uring.h>
53 }
54
55 #include "bitmanip.hfa"
56 #include "kernel_private.hfa"
57 #include "thread.hfa"
58
59 void ?{}(io_context_params & this) {
60 this.num_entries = 256;
61 this.num_ready = 256;
62 this.submit_aff = -1;
63 this.eager_submits = false;
64 this.poller_submits = false;
65 this.poll_submit = false;
66 this.poll_complete = false;
67 }
68
69 static void * __io_poller_slow( void * arg );
70
71 // Weirdly, some systems that do support io_uring don't actually define these
72 #ifdef __alpha__
73 /*
74 * alpha is the only exception, all other architectures
75 * have common numbers for new system calls.
76 */
77 #ifndef __NR_io_uring_setup
78 #define __NR_io_uring_setup 535
79 #endif
80 #ifndef __NR_io_uring_enter
81 #define __NR_io_uring_enter 536
82 #endif
83 #ifndef __NR_io_uring_register
84 #define __NR_io_uring_register 537
85 #endif
86 #else /* !__alpha__ */
87 #ifndef __NR_io_uring_setup
88 #define __NR_io_uring_setup 425
89 #endif
90 #ifndef __NR_io_uring_enter
91 #define __NR_io_uring_enter 426
92 #endif
93 #ifndef __NR_io_uring_register
94 #define __NR_io_uring_register 427
95 #endif
96 #endif
97
98//=============================================================================================
99// I/O Startup / Shutdown logic + Master Poller
100//=============================================================================================
101
102 // IO Master poller loop forward
103 static void * iopoll_loop( __attribute__((unused)) void * args );
104
105 static struct {
106 pthread_t thrd; // pthread handle to io poller thread
107 void * stack; // pthread stack for io poller thread
108 int epollfd; // file descriptor to the epoll instance
109 volatile bool run; // Whether or not to continue
110 } iopoll;
111
112 void __kernel_io_startup(void) {
113 __cfaabi_dbg_print_safe( "Kernel : Creating EPOLL instance\n" );
114
115 iopoll.epollfd = epoll_create1(0);
116 if (iopoll.epollfd == -1) {
117 abort( "internal error, epoll_create1\n");
118 }
119
120 __cfaabi_dbg_print_safe( "Kernel : Starting io poller thread\n" );
121
122 iopoll.run = true;
123 iopoll.stack = __create_pthread( &iopoll.thrd, iopoll_loop, 0p );
124 }
125
126 void __kernel_io_shutdown(void) {
127 // Notify the io poller thread of the shutdown
128 iopoll.run = false;
129 sigval val = { 1 };
130 pthread_sigqueue( iopoll.thrd, SIGUSR1, val );
131
132 // Wait for the io poller thread to finish
133
134 pthread_join( iopoll.thrd, 0p );
135 free( iopoll.stack );
136
137 int ret = close(iopoll.epollfd);
138 if (ret == -1) {
139 abort( "internal error, close epoll\n");
140 }
141
142 // Io polling is now fully stopped
143
144 __cfaabi_dbg_print_safe( "Kernel : IO poller stopped\n" );
145 }
146
147 static void * iopoll_loop( __attribute__((unused)) void * args ) {
148 __processor_id_t id;
149 id.id = doregister(&id);
150 __cfaabi_dbg_print_safe( "Kernel : IO poller thread starting\n" );
151
152 // Block signals to control when they arrive
153 sigset_t mask;
154 sigfillset(&mask);
155 if ( pthread_sigmask( SIG_BLOCK, &mask, 0p ) == -1 ) {
156 abort( "internal error, pthread_sigmask" );
157 }
158
159 sigdelset( &mask, SIGUSR1 );
160
161 // Create sufficient events
162 struct epoll_event events[10];
163 // Main loop
164 while( iopoll.run ) {
165 // Wait for events
166 int nfds = epoll_pwait( iopoll.epollfd, events, 10, -1, &mask );
167
168 // Check if an error occured
169 if (nfds == -1) {
170 if( errno == EINTR ) continue;
171 abort( "internal error, pthread_sigmask" );
172 }
173
174 for(i; nfds) {
175 $io_ctx_thread * io_ctx = ($io_ctx_thread *)(uintptr_t)events[i].data.u64;
176 /* paranoid */ verify( io_ctx );
177 __cfadbg_print_safe(io_core, "Kernel I/O : Unparking io poller %p\n", io_ctx);
178 #if !defined( __CFA_NO_STATISTICS__ )
179 kernelTLS.this_stats = io_ctx->self.curr_cluster->stats;
180 #endif
181 __post( io_ctx->sem, &id );
182 }
183 }
184
185 __cfaabi_dbg_print_safe( "Kernel : IO poller thread stopping\n" );
186 unregister(&id);
187 return 0p;
188 }
189
190//=============================================================================================
191// I/O Context Constrution/Destruction
192//=============================================================================================
193
194 void ?{}($io_ctx_thread & this, struct cluster & cl) { (this.self){ "IO Poller", cl }; }
195 void main( $io_ctx_thread & this );
196 static inline $thread * get_thread( $io_ctx_thread & this ) { return &this.self; }
197 void ^?{}( $io_ctx_thread & mutex this ) {}
198
199 static void __io_create ( __io_data & this, const io_context_params & params_in );
200 static void __io_destroy( __io_data & this );
201
202 void ?{}(io_context & this, struct cluster & cl, const io_context_params & params) {
203 (this.thrd){ cl };
204 this.thrd.ring = malloc();
205 __cfadbg_print_safe(io_core, "Kernel I/O : Creating ring for io_context %p\n", &this);
206 __io_create( *this.thrd.ring, params );
207
208 __cfadbg_print_safe(io_core, "Kernel I/O : Starting poller thread for io_context %p\n", &this);
209 this.thrd.done = false;
210 __thrd_start( this.thrd, main );
211
212 __cfadbg_print_safe(io_core, "Kernel I/O : io_context %p ready\n", &this);
213 }
214
215 void ?{}(io_context & this, struct cluster & cl) {
216 io_context_params params;
217 (this){ cl, params };
218 }
219
220 void ^?{}(io_context & this, bool cluster_context) {
221 __cfadbg_print_safe(io_core, "Kernel I/O : tearing down io_context %p\n", &this);
222
223 // Notify the thread of the shutdown
224 __atomic_store_n(&this.thrd.done, true, __ATOMIC_SEQ_CST);
225
226 // If this is an io_context within a cluster, things get trickier
227 $thread & thrd = this.thrd.self;
228 if( cluster_context ) {
229 cluster & cltr = *thrd.curr_cluster;
230 /* paranoid */ verify( cltr.nprocessors == 0 || &cltr == mainCluster );
231 /* paranoid */ verify( !ready_mutate_islocked() );
232
233 // We need to adjust the clean-up based on where the thread is
234 if( thrd.state == Ready || thrd.preempted != __NO_PREEMPTION ) {
235
236 ready_schedule_lock( (struct __processor_id_t *)active_processor() );
237
238 // This is the tricky case
239 // The thread was preempted and now it is on the ready queue
240 // The thread should be the last on the list
241 /* paranoid */ verify( thrd.link.next != 0p );
242
243 // Remove the thread from the ready queue of this cluster
244 __attribute__((unused)) bool removed = remove_head( &cltr, &thrd );
245 /* paranoid */ verify( removed );
246 thrd.link.next = 0p;
247 thrd.link.prev = 0p;
248 __cfaabi_dbg_debug_do( thrd.unpark_stale = true );
249
250 // Fixup the thread state
251 thrd.state = Blocked;
252 thrd.ticket = 0;
253 thrd.preempted = __NO_PREEMPTION;
254
255 ready_schedule_unlock( (struct __processor_id_t *)active_processor() );
256
257 // Pretend like the thread was blocked all along
258 }
259 // !!! This is not an else if !!!
260 if( thrd.state == Blocked ) {
261
262 // This is the "easy case"
263 // The thread is parked and can easily be moved to active cluster
264 verify( thrd.curr_cluster != active_cluster() || thrd.curr_cluster == mainCluster );
265 thrd.curr_cluster = active_cluster();
266
267 // unpark the fast io_poller
268 unpark( &thrd __cfaabi_dbg_ctx2 );
269 }
270 else {
271
272 // The thread is in a weird state
273 // I don't know what to do here
274 abort("io_context poller thread is in unexpected state, cannot clean-up correctly\n");
275 }
276 } else {
277 unpark( &thrd __cfaabi_dbg_ctx2 );
278 }
279
280 ^(this.thrd){};
281 __cfadbg_print_safe(io_core, "Kernel I/O : Stopped poller thread for io_context %p\n", &this);
282
283 __io_destroy( *this.thrd.ring );
284 __cfadbg_print_safe(io_core, "Kernel I/O : Destroyed ring for io_context %p\n", &this);
285
286 free(this.thrd.ring);
287 }
288
289 void ^?{}(io_context & this) {
290 ^(this){ false };
291 }
292
293 static void __io_create( __io_data & this, const io_context_params & params_in ) {
294 // Step 1 : call to setup
295 struct io_uring_params params;
296 memset(&params, 0, sizeof(params));
297 if( params_in.poll_submit ) params.flags |= IORING_SETUP_SQPOLL;
298 if( params_in.poll_complete ) params.flags |= IORING_SETUP_IOPOLL;
299
300 uint32_t nentries = params_in.num_entries;
301
302 int fd = syscall(__NR_io_uring_setup, nentries, &params );
303 if(fd < 0) {
304 abort("KERNEL ERROR: IO_URING SETUP - %s\n", strerror(errno));
305 }
306
307 // Step 2 : mmap result
308 memset( &this, 0, sizeof(struct __io_data) );
309 struct __submition_data & sq = this.submit_q;
310 struct __completion_data & cq = this.completion_q;
311
312 // calculate the right ring size
313 sq.ring_sz = params.sq_off.array + (params.sq_entries * sizeof(unsigned) );
314 cq.ring_sz = params.cq_off.cqes + (params.cq_entries * sizeof(struct io_uring_cqe));
315
316 // Requires features
317 #if defined(IORING_FEAT_SINGLE_MMAP)
318 // adjust the size according to the parameters
319 if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
320 cq.ring_sz = sq.ring_sz = max(cq.ring_sz, sq.ring_sz);
321 }
322 #endif
323
324 // mmap the Submit Queue into existence
325 sq.ring_ptr = mmap(0, sq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING);
326 if (sq.ring_ptr == (void*)MAP_FAILED) {
327 abort("KERNEL ERROR: IO_URING MMAP1 - %s\n", strerror(errno));
328 }
329
330 // Requires features
331 #if defined(IORING_FEAT_SINGLE_MMAP)
332 // mmap the Completion Queue into existence (may or may not be needed)
333 if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
334 cq.ring_ptr = sq.ring_ptr;
335 }
336 else
337 #endif
338 {
339 // We need multiple call to MMAP
340 cq.ring_ptr = mmap(0, cq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING);
341 if (cq.ring_ptr == (void*)MAP_FAILED) {
342 munmap(sq.ring_ptr, sq.ring_sz);
343 abort("KERNEL ERROR: IO_URING MMAP2 - %s\n", strerror(errno));
344 }
345 }
346
347 // mmap the submit queue entries
348 size_t size = params.sq_entries * sizeof(struct io_uring_sqe);
349 sq.sqes = (struct io_uring_sqe *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES);
350 if (sq.sqes == (struct io_uring_sqe *)MAP_FAILED) {
351 munmap(sq.ring_ptr, sq.ring_sz);
352 if (cq.ring_ptr != sq.ring_ptr) munmap(cq.ring_ptr, cq.ring_sz);
353 abort("KERNEL ERROR: IO_URING MMAP3 - %s\n", strerror(errno));
354 }
355
356 // Get the pointers from the kernel to fill the structure
357 // submit queue
358 sq.head = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.head);
359 sq.tail = (volatile uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail);
360 sq.mask = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask);
361 sq.num = ( const uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries);
362 sq.flags = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags);
363 sq.dropped = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped);
364 sq.array = ( uint32_t *)(((intptr_t)sq.ring_ptr) + params.sq_off.array);
365 sq.prev_head = *sq.head;
366
367 {
368 const uint32_t num = *sq.num;
369 for( i; num ) {
370 sq.sqes[i].user_data = 0ul64;
371 }
372 }
373
374 (sq.lock){};
375 (sq.release_lock){};
376
377 if( params_in.poller_submits || params_in.eager_submits ) {
378 /* paranoid */ verify( is_pow2( params_in.num_ready ) || (params_in.num_ready < 8) );
379 sq.ready_cnt = max( params_in.num_ready, 8 );
380 sq.ready = alloc_align( 64, sq.ready_cnt );
381 for(i; sq.ready_cnt) {
382 sq.ready[i] = -1ul32;
383 }
384 }
385 else {
386 sq.ready_cnt = 0;
387 sq.ready = 0p;
388 }
389
390 // completion queue
391 cq.head = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.head);
392 cq.tail = (volatile uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.tail);
393 cq.mask = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_mask);
394 cq.num = ( const uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_entries);
395 cq.overflow = ( uint32_t *)(((intptr_t)cq.ring_ptr) + params.cq_off.overflow);
396 cq.cqes = (struct io_uring_cqe *)(((intptr_t)cq.ring_ptr) + params.cq_off.cqes);
397
398 // some paranoid checks
399 /* 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 );
400 /* paranoid */ verifyf( (*cq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *cq.num );
401 /* paranoid */ verifyf( (*cq.head) == 0, "IO_URING Expected head to be 0, got %u", *cq.head );
402 /* paranoid */ verifyf( (*cq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *cq.tail );
403
404 /* 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 );
405 /* paranoid */ verifyf( (*sq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *sq.num );
406 /* paranoid */ verifyf( (*sq.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.head );
407 /* paranoid */ verifyf( (*sq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.tail );
408
409 // Update the global ring info
410 this.ring_flags = params.flags;
411 this.fd = fd;
412 this.eager_submits = params_in.eager_submits;
413 this.poller_submits = params_in.poller_submits;
414 }
415
416 static void __io_destroy( __io_data & this ) {
417 // Shutdown the io rings
418 struct __submition_data & sq = this.submit_q;
419 struct __completion_data & cq = this.completion_q;
420
421 // unmap the submit queue entries
422 munmap(sq.sqes, (*sq.num) * sizeof(struct io_uring_sqe));
423
424 // unmap the Submit Queue ring
425 munmap(sq.ring_ptr, sq.ring_sz);
426
427 // unmap the Completion Queue ring, if it is different
428 if (cq.ring_ptr != sq.ring_ptr) {
429 munmap(cq.ring_ptr, cq.ring_sz);
430 }
431
432 // close the file descriptor
433 close(this.fd);
434
435 free( this.submit_q.ready ); // Maybe null, doesn't matter
436 }
437
438//=============================================================================================
439// I/O Context Sleep
440//=============================================================================================
441
442 void __ioctx_register($io_ctx_thread & ctx, struct epoll_event & ev) {
443 ev.events = EPOLLIN | EPOLLONESHOT;
444 ev.data.u64 = (uint64_t)&ctx;
445 int ret = epoll_ctl(iopoll.epollfd, EPOLL_CTL_ADD, ctx.ring->fd, &ev);
446 if (ret < 0) {
447 abort( "KERNEL ERROR: EPOLL ADD - (%d) %s\n", (int)errno, strerror(errno) );
448 }
449 }
450
451 void __ioctx_prepare_block($io_ctx_thread & ctx, struct epoll_event & ev) {
452 int ret = epoll_ctl(iopoll.epollfd, EPOLL_CTL_MOD, ctx.ring->fd, &ev);
453 if (ret < 0) {
454 abort( "KERNEL ERROR: EPOLL REARM - (%d) %s\n", (int)errno, strerror(errno) );
455 }
456 }
457
458//=============================================================================================
459// I/O Context Misc Setup
460//=============================================================================================
461 void register_fixed_files( io_context & ctx, int * files, unsigned count ) {
462 int ret = syscall( __NR_io_uring_register, ctx.thrd.ring->fd, IORING_REGISTER_FILES, files, count );
463 if( ret < 0 ) {
464 abort( "KERNEL ERROR: IO_URING SYSCALL - (%d) %s\n", (int)errno, strerror(errno) );
465 }
466
467 __cfadbg_print_safe( io_core, "Kernel I/O : Performed io_register for %p, returned %d\n", active_thread(), ret );
468 }
469
470 void register_fixed_files( cluster & cltr, int * files, unsigned count ) {
471 for(i; cltr.io.cnt) {
472 register_fixed_files( cltr.io.ctxs[i], files, count );
473 }
474 }
475#endif
Note: See TracBrowser for help on using the repository browser.