source: libcfa/src/concurrency/io/setup.cfa @ 2cd784a

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 2cd784a was dddb3dd0, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Changed io to use ring per kernel threads.

  • Property mode set to 100644
File size: 11.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#if defined(__CFA_DEBUG__)
20        // #define __CFA_DEBUG_PRINT_IO__
21        // #define __CFA_DEBUG_PRINT_IO_CORE__
22#endif
23
24#include "io/types.hfa"
25#include "kernel.hfa"
26
27#if !defined(CFA_HAVE_LINUX_IO_URING_H)
28        void ?{}(io_context_params & this) {}
29
30        void  ?{}($io_context & this, struct cluster & cl) {}
31        void ^?{}($io_context & this) {}
32
33        $io_arbiter * create(void) { return 0p; }
34        void destroy($io_arbiter *) {}
35
36#else
37        #include <errno.h>
38        #include <stdint.h>
39        #include <string.h>
40        #include <signal.h>
41        #include <unistd.h>
42
43        extern "C" {
44                #include <pthread.h>
45                #include <sys/epoll.h>
46                #include <sys/eventfd.h>
47                #include <sys/mman.h>
48                #include <sys/syscall.h>
49
50                #include <linux/io_uring.h>
51        }
52
53        #include "bitmanip.hfa"
54        #include "kernel_private.hfa"
55        #include "thread.hfa"
56
57        void ?{}(io_context_params & this) {
58                this.num_entries = 256;
59        }
60
61        static void * __io_poller_slow( void * arg );
62
63        // Weirdly, some systems that do support io_uring don't actually define these
64        #ifdef __alpha__
65                /*
66                * alpha is the only exception, all other architectures
67                * have common numbers for new system calls.
68                */
69                #ifndef __NR_io_uring_setup
70                        #define __NR_io_uring_setup           535
71                #endif
72                #ifndef __NR_io_uring_enter
73                        #define __NR_io_uring_enter           536
74                #endif
75                #ifndef __NR_io_uring_register
76                        #define __NR_io_uring_register        537
77                #endif
78        #else /* !__alpha__ */
79                #ifndef __NR_io_uring_setup
80                        #define __NR_io_uring_setup           425
81                #endif
82                #ifndef __NR_io_uring_enter
83                        #define __NR_io_uring_enter           426
84                #endif
85                #ifndef __NR_io_uring_register
86                        #define __NR_io_uring_register        427
87                #endif
88        #endif
89
90//=============================================================================================
91// I/O Context Constrution/Destruction
92//=============================================================================================
93
94
95
96        static void __io_uring_setup ( $io_context & this, const io_context_params & params_in, int procfd );
97        static void __io_uring_teardown( $io_context & this );
98        static void __epoll_register($io_context & ctx);
99        static void __epoll_unregister($io_context & ctx);
100        void __ioarbiter_register( $io_arbiter & mutex, $io_context & ctx );
101        void __ioarbiter_unregister( $io_arbiter & mutex, $io_context & ctx );
102
103        void ?{}($io_context & this, processor * proc, struct cluster & cl) {
104                /* paranoid */ verify( cl.io.arbiter );
105                this.proc = proc;
106                this.arbiter = cl.io.arbiter;
107                this.ext_sq.empty = true;
108                (this.ext_sq.blocked){};
109                __io_uring_setup( this, cl.io.params, proc->idle );
110                __cfadbg_print_safe(io_core, "Kernel I/O : Created ring for io_context %u (%p)\n", this.fd, &this);
111        }
112
113        void ^?{}($io_context & this) {
114                __cfadbg_print_safe(io_core, "Kernel I/O : tearing down io_context %u\n", this.fd);
115
116                __io_uring_teardown( this );
117                __cfadbg_print_safe(io_core, "Kernel I/O : Destroyed ring for io_context %u\n", this.fd);
118        }
119
120        extern void __disable_interrupts_hard();
121        extern void __enable_interrupts_hard();
122
123        static void __io_uring_setup( $io_context & this, const io_context_params & params_in, int procfd ) {
124                // Step 1 : call to setup
125                struct io_uring_params params;
126                memset(&params, 0, sizeof(params));
127                // if( params_in.poll_submit   ) params.flags |= IORING_SETUP_SQPOLL;
128                // if( params_in.poll_complete ) params.flags |= IORING_SETUP_IOPOLL;
129
130                __u32 nentries = params_in.num_entries != 0 ? params_in.num_entries : 256;
131                if( !is_pow2(nentries) ) {
132                        abort("ERROR: I/O setup 'num_entries' must be a power of 2\n");
133                }
134
135                int fd = syscall(__NR_io_uring_setup, nentries, &params );
136                if(fd < 0) {
137                        abort("KERNEL ERROR: IO_URING SETUP - %s\n", strerror(errno));
138                }
139
140                // Step 2 : mmap result
141                struct __sub_ring_t & sq = this.sq;
142                struct __cmp_ring_t & cq = this.cq;
143
144                // calculate the right ring size
145                sq.ring_sz = params.sq_off.array + (params.sq_entries * sizeof(unsigned)           );
146                cq.ring_sz = params.cq_off.cqes  + (params.cq_entries * sizeof(struct io_uring_cqe));
147
148                // Requires features
149                #if defined(IORING_FEAT_SINGLE_MMAP)
150                        // adjust the size according to the parameters
151                        if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
152                                cq.ring_sz = sq.ring_sz = max(cq.ring_sz, sq.ring_sz);
153                        }
154                #endif
155
156                // mmap the Submit Queue into existence
157                sq.ring_ptr = mmap(0, sq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQ_RING);
158                if (sq.ring_ptr == (void*)MAP_FAILED) {
159                        abort("KERNEL ERROR: IO_URING MMAP1 - %s\n", strerror(errno));
160                }
161
162                // Requires features
163                #if defined(IORING_FEAT_SINGLE_MMAP)
164                        // mmap the Completion Queue into existence (may or may not be needed)
165                        if ((params.features & IORING_FEAT_SINGLE_MMAP) != 0) {
166                                cq.ring_ptr = sq.ring_ptr;
167                        }
168                        else
169                #endif
170                {
171                        // We need multiple call to MMAP
172                        cq.ring_ptr = mmap(0, cq.ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_CQ_RING);
173                        if (cq.ring_ptr == (void*)MAP_FAILED) {
174                                munmap(sq.ring_ptr, sq.ring_sz);
175                                abort("KERNEL ERROR: IO_URING MMAP2 - %s\n", strerror(errno));
176                        }
177                }
178
179                // mmap the submit queue entries
180                size_t size = params.sq_entries * sizeof(struct io_uring_sqe);
181                sq.sqes = (struct io_uring_sqe *)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, fd, IORING_OFF_SQES);
182                if (sq.sqes == (struct io_uring_sqe *)MAP_FAILED) {
183                        munmap(sq.ring_ptr, sq.ring_sz);
184                        if (cq.ring_ptr != sq.ring_ptr) munmap(cq.ring_ptr, cq.ring_sz);
185                        abort("KERNEL ERROR: IO_URING MMAP3 - %s\n", strerror(errno));
186                }
187
188                // Step 3 : Initialize the data structure
189                // Get the pointers from the kernel to fill the structure
190                // submit queue
191                sq.kring.head  = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.head);
192                sq.kring.tail  = (volatile __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.tail);
193                sq.kring.array = (         __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.array);
194                sq.mask        = (   const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_mask);
195                sq.num         = (   const __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.ring_entries);
196                sq.flags       = (         __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.flags);
197                sq.dropped     = (         __u32 *)(((intptr_t)sq.ring_ptr) + params.sq_off.dropped);
198
199                sq.kring.released = 0;
200
201                sq.free_ring.head = 0;
202                sq.free_ring.tail = *sq.num;
203                sq.free_ring.array = alloc( *sq.num, 128`align );
204                for(i; (__u32)*sq.num) {
205                        sq.free_ring.array[i] = i;
206                }
207
208                sq.to_submit = 0;
209
210                // completion queue
211                cq.head      = (volatile __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.head);
212                cq.tail      = (volatile __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.tail);
213                cq.mask      = (   const __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_mask);
214                cq.num       = (   const __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.ring_entries);
215                cq.overflow  = (         __u32 *)(((intptr_t)cq.ring_ptr) + params.cq_off.overflow);
216                cq.cqes = (struct io_uring_cqe *)(((intptr_t)cq.ring_ptr) + params.cq_off.cqes);
217
218                // Step 4 : eventfd
219                // io_uring_register is so f*cking slow on some machine that it
220                // will never succeed if preemption isn't hard blocked
221                __cfadbg_print_safe(io_core, "Kernel I/O : registering %d for completion with ring %d\n", procfd, fd);
222
223                __disable_interrupts_hard();
224
225                int ret = syscall( __NR_io_uring_register, fd, IORING_REGISTER_EVENTFD, &procfd, 1);
226                if (ret < 0) {
227                        abort("KERNEL ERROR: IO_URING EVENTFD REGISTER - %s\n", strerror(errno));
228                }
229
230                __enable_interrupts_hard();
231
232                __cfadbg_print_safe(io_core, "Kernel I/O : registered %d for completion with ring %d\n", procfd, fd);
233
234                // some paranoid checks
235                /* 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  );
236                /* paranoid */ verifyf( (*cq.num)  >= nentries, "IO_URING Expected %u entries, got %u", nentries, *cq.num );
237                /* paranoid */ verifyf( (*cq.head) == 0, "IO_URING Expected head to be 0, got %u", *cq.head );
238                /* paranoid */ verifyf( (*cq.tail) == 0, "IO_URING Expected tail to be 0, got %u", *cq.tail );
239
240                /* 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 );
241                /* paranoid */ verifyf( (*sq.num) >= nentries, "IO_URING Expected %u entries, got %u", nentries, *sq.num );
242                /* paranoid */ verifyf( (*sq.kring.head) == 0, "IO_URING Expected head to be 0, got %u", *sq.kring.head );
243                /* paranoid */ verifyf( (*sq.kring.tail) == 0, "IO_URING Expected tail to be 0, got %u", *sq.kring.tail );
244
245                // Update the global ring info
246                this.ring_flags = 0;
247                this.fd         = fd;
248        }
249
250        static void __io_uring_teardown( $io_context & this ) {
251                // Shutdown the io rings
252                struct __sub_ring_t & sq = this.sq;
253                struct __cmp_ring_t & cq = this.cq;
254
255                // unmap the submit queue entries
256                munmap(sq.sqes, (*sq.num) * sizeof(struct io_uring_sqe));
257
258                // unmap the Submit Queue ring
259                munmap(sq.ring_ptr, sq.ring_sz);
260
261                // unmap the Completion Queue ring, if it is different
262                if (cq.ring_ptr != sq.ring_ptr) {
263                        munmap(cq.ring_ptr, cq.ring_sz);
264                }
265
266                // close the file descriptor
267                close(this.fd);
268
269                free( this.sq.free_ring.array ); // Maybe null, doesn't matter
270        }
271
272        void __cfa_io_start( processor * proc ) {
273                proc->io.ctx = alloc();
274                (*proc->io.ctx){proc, *proc->cltr};
275        }
276        void __cfa_io_stop ( processor * proc ) {
277                ^(*proc->io.ctx){};
278                free(proc->io.ctx);
279        }
280
281//=============================================================================================
282// I/O Context Sleep
283//=============================================================================================
284        // static inline void __epoll_ctl($io_context & ctx, int op, const char * error) {
285        //      struct epoll_event ev;
286        //      ev.events = EPOLLIN | EPOLLONESHOT;
287        //      ev.data.u64 = (__u64)&ctx;
288        //      int ret = epoll_ctl(iopoll.epollfd, op, ctx.efd, &ev);
289        //      if (ret < 0) {
290        //              abort( "KERNEL ERROR: EPOLL %s - (%d) %s\n", error, (int)errno, strerror(errno) );
291        //      }
292        // }
293
294        // static void __epoll_register($io_context & ctx) {
295        //      __epoll_ctl(ctx, EPOLL_CTL_ADD, "ADD");
296        // }
297
298        // static void __epoll_unregister($io_context & ctx) {
299        //      // Read the current epoch so we know when to stop
300        //      size_t curr = __atomic_load_n(&iopoll.epoch, __ATOMIC_SEQ_CST);
301
302        //      // Remove the fd from the iopoller
303        //      __epoll_ctl(ctx, EPOLL_CTL_DEL, "REMOVE");
304
305        //      // Notify the io poller thread of the shutdown
306        //      iopoll.run = false;
307        //      sigval val = { 1 };
308        //      pthread_sigqueue( iopoll.thrd, SIGUSR1, val );
309
310        //      // Make sure all this is done
311        //      __atomic_thread_fence(__ATOMIC_SEQ_CST);
312
313        //      // Wait for the next epoch
314        //      while(curr == iopoll.epoch && !iopoll.stopped) Pause();
315        // }
316
317        // void __ioctx_prepare_block($io_context & ctx) {
318        //      __cfadbg_print_safe(io_core, "Kernel I/O - epoll : Re-arming io poller %d (%p)\n", ctx.fd, &ctx);
319        //      __epoll_ctl(ctx, EPOLL_CTL_MOD, "REARM");
320        // }
321
322
323//=============================================================================================
324// I/O Context Misc Setup
325//=============================================================================================
326        void ?{}( $io_arbiter & this ) {
327                this.pending.flag = false;
328        }
329
330        void ^?{}( $io_arbiter & mutex this ) {
331                // /* paranoid */ verify( empty(this.assigned) );
332                // /* paranoid */ verify( empty(this.available) );
333                /* paranoid */ verify( is_empty(this.pending.blocked) );
334        }
335
336        $io_arbiter * create(void) {
337                return new();
338        }
339        void destroy($io_arbiter * arbiter) {
340                delete(arbiter);
341        }
342
343//=============================================================================================
344// I/O Context Misc Setup
345//=============================================================================================
346
347#endif
Note: See TracBrowser for help on using the repository browser.