source: libcfa/src/concurrency/iocall.cfa @ 4b84e35

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

Fixed iocall

  • Property mode set to 100644
File size: 12.4 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// iocall.cfa --
8//
9// Author           : Thierry Delisle
10// Created On       : Wed Jul  1 14:51:00 2020
11// Last Modified By :
12// Last Modified On :
13// Update Count     :
14//
15
16//=============================================================================================
17// I/O uring backend
18//=============================================================================================
19
20#if defined(HAVE_LINUX_IO_URING_H)
21        #include <stdint.h>
22        #include <linux/io_uring.h>
23
24        #include "kernel_private.hfa"
25
26        extern [* struct io_uring_sqe, uint32_t] __submit_alloc( struct __io_data & ring, uint64_t data );
27        extern void __submit( struct __io_data & ring, uint32_t idx );
28
29        static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd) {
30                this.opcode = opcode;
31                #if !defined(IOSQE_ASYNC)
32                        this.flags = 0;
33                #else
34                        this.flags = IOSQE_ASYNC;
35                #endif
36                this.ioprio = 0;
37                this.fd = fd;
38                this.off = 0;
39                this.addr = 0;
40                this.len = 0;
41                this.rw_flags = 0;
42                this.__pad2[0] = this.__pad2[1] = this.__pad2[2] = 0;
43        }
44
45        static inline void ?{}(struct io_uring_sqe & this, uint8_t opcode, int fd, void * addr, uint32_t len, uint64_t off ) {
46                (this){ opcode, fd };
47                this.off = off;
48                this.addr = (uint64_t)(uintptr_t)addr;
49                this.len = len;
50        }
51
52        #define __submit_prelude \
53                __io_user_data_t data = { 0, active_thread() }; \
54                struct __io_data & ring = *data.thrd->curr_cluster->io; \
55                struct io_uring_sqe * sqe; \
56                uint32_t idx; \
57                [sqe, idx] = __submit_alloc( ring, (uint64_t)(uintptr_t)&data );
58
59        #define __submit_wait \
60                /*__cfaabi_bits_print_safe( STDERR_FILENO, "Preparing user data %p for %p\n", &data, data.thrd );*/ \
61                verify( sqe->user_data == (uint64_t)(uintptr_t)&data ); \
62                __submit( ring, idx ); \
63                park( __cfaabi_dbg_ctx ); \
64                return data.result;
65#endif
66
67//=============================================================================================
68// I/O Forwards
69//=============================================================================================
70
71// Some forward declarations
72#include <unistd.h>
73
74extern "C" {
75        #include <sys/types.h>
76        #include <sys/socket.h>
77        #include <sys/syscall.h>
78
79#if defined(HAVE_PREADV2)
80        struct iovec;
81        extern ssize_t preadv2 (int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
82#endif
83#if defined(HAVE_PWRITEV2)
84        struct iovec;
85        extern ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
86#endif
87
88        extern int fsync(int fd);
89        extern int sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags);
90
91        struct msghdr;
92        struct sockaddr;
93        extern ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
94        extern ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
95        extern ssize_t send(int sockfd, const void *buf, size_t len, int flags);
96        extern ssize_t recv(int sockfd, void *buf, size_t len, int flags);
97        extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
98        extern int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
99
100        extern int fallocate(int fd, int mode, uint64_t offset, uint64_t len);
101        extern int posix_fadvise(int fd, uint64_t offset, uint64_t len, int advice);
102        extern int madvise(void *addr, size_t length, int advice);
103
104        extern int openat(int dirfd, const char *pathname, int flags, mode_t mode);
105        extern int close(int fd);
106
107        extern ssize_t read (int fd, void *buf, size_t count);
108}
109
110//=============================================================================================
111// I/O Interface
112//=============================================================================================
113
114//-----------------------------------------------------------------------------
115// Asynchronous operations
116#if defined(HAVE_PREADV2)
117        ssize_t cfa_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) {
118                #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READV)
119                        return preadv2(fd, iov, iovcnt, offset, flags);
120                #else
121                        __submit_prelude
122
123                        (*sqe){ IORING_OP_READV, fd, iov, iovcnt, offset };
124
125                        __submit_wait
126                #endif
127        }
128#endif
129
130#if defined(HAVE_PWRITEV2)
131        ssize_t cfa_pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags) {
132                #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITEV)
133                        return pwritev2(fd, iov, iovcnt, offset, flags);
134                #else
135                        __submit_prelude
136
137                        (*sqe){ IORING_OP_WRITEV, fd, iov, iovcnt, offset };
138
139                        __submit_wait
140                #endif
141        }
142#endif
143
144int cfa_fsync(int fd) {
145        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FSYNC)
146                return fsync(fd);
147        #else
148                __submit_prelude
149
150                (*sqe){ IORING_OP_FSYNC, fd };
151
152                __submit_wait
153        #endif
154}
155
156int cfa_sync_file_range(int fd, int64_t offset, int64_t nbytes, unsigned int flags) {
157        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SYNC_FILE_RANGE)
158                return sync_file_range(fd, offset, nbytes, flags);
159        #else
160                __submit_prelude
161
162                (*sqe){ IORING_OP_SYNC_FILE_RANGE, fd };
163                sqe->off = offset;
164                sqe->len = nbytes;
165                sqe->sync_range_flags = flags;
166
167                __submit_wait
168        #endif
169}
170
171
172ssize_t cfa_sendmsg(int sockfd, const struct msghdr *msg, int flags) {
173        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SENDMSG)
174                return sendmsg(sockfd, msg, flags);
175        #else
176                __submit_prelude
177
178                (*sqe){ IORING_OP_SENDMSG, sockfd, msg, 1, 0 };
179                sqe->msg_flags = flags;
180
181                __submit_wait
182        #endif
183}
184
185ssize_t cfa_recvmsg(int sockfd, struct msghdr *msg, int flags) {
186        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECVMSG)
187                return recvmsg(sockfd, msg, flags);
188        #else
189                __submit_prelude
190
191                (*sqe){ IORING_OP_RECVMSG, sockfd, msg, 1, 0 };
192                sqe->msg_flags = flags;
193
194                __submit_wait
195        #endif
196}
197
198ssize_t cfa_send(int sockfd, const void *buf, size_t len, int flags) {
199        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_SEND)
200                return send( sockfd, buf, len, flags );
201        #else
202                __submit_prelude
203
204                (*sqe){ IORING_OP_SEND, sockfd };
205                sqe->addr = (uint64_t)buf;
206                sqe->len = len;
207                sqe->msg_flags = flags;
208
209                __submit_wait
210        #endif
211}
212
213ssize_t cfa_recv(int sockfd, void *buf, size_t len, int flags) {
214        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_RECV)
215                return recv( sockfd, buf, len, flags );
216        #else
217                __submit_prelude
218
219                (*sqe){ IORING_OP_RECV, sockfd };
220                sqe->addr = (uint64_t)buf;
221                sqe->len = len;
222                sqe->msg_flags = flags;
223
224                __submit_wait
225        #endif
226}
227
228int cfa_accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) {
229        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_ACCEPT)
230                return accept4( sockfd, addr, addrlen, flags );
231        #else
232                __submit_prelude
233
234                (*sqe){ IORING_OP_ACCEPT, sockfd };
235                sqe->addr = addr;
236                sqe->addr2 = addrlen;
237                sqe->accept_flags = flags;
238
239                __submit_wait
240        #endif
241}
242
243int cfa_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
244        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CONNECT)
245                return connect( sockfd, addr, addrlen );
246        #else
247                __submit_prelude
248
249                (*sqe){ IORING_OP_CONNECT, sockfd };
250                sqe->addr = (uint64_t)addr;
251                sqe->off = addrlen;
252
253                __submit_wait
254        #endif
255}
256
257int cfa_fallocate(int fd, int mode, uint64_t offset, uint64_t len) {
258        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FALLOCATE)
259                return fallocate( fd, mode, offset, len );
260        #else
261                __submit_prelude
262
263                (*sqe){ IORING_OP_FALLOCATE, fd };
264                sqe->off = offset;
265                sqe->len = length;
266                sqe->mode = mode;
267
268                __submit_wait
269        #endif
270}
271
272int cfa_fadvise(int fd, uint64_t offset, uint64_t len, int advice) {
273        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_FADVISE)
274                return posix_fadvise( fd, offset, len, advice );
275        #else
276                __submit_prelude
277
278                (*sqe){ IORING_OP_FADVISE, fd };
279                sqe->off = (uint64_t)offset;
280                sqe->len = length;
281                sqe->fadvise_advice = advice;
282
283                __submit_wait
284        #endif
285}
286
287int cfa_madvise(void *addr, size_t length, int advice) {
288        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_MADVISE)
289                return madvise( addr, length, advice );
290        #else
291                __submit_prelude
292
293                (*sqe){ IORING_OP_MADVISE, 0 };
294                sqe->addr = (uint64_t)addr;
295                sqe->len = length;
296                sqe->fadvise_advice = advice;
297
298                __submit_wait
299        #endif
300}
301
302int cfa_openat(int dirfd, const char *pathname, int flags, mode_t mode) {
303        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_OPENAT)
304                return openat( dirfd, pathname, flags, mode );
305        #else
306                __submit_prelude
307
308                (*sqe){ IORING_OP_OPENAT, dirfd };
309                sqe->addr = (uint64_t)pathname;
310                sqe->open_flags = flags;
311                sqe->mode = mode;
312
313                __submit_wait
314        #endif
315}
316
317int cfa_close(int fd) {
318        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_CLOSE)
319                return close( fd );
320        #else
321                __submit_prelude
322
323                (*sqe){ IORING_OP_CLOSE, fd };
324
325                __submit_wait
326        #endif
327}
328
329
330ssize_t cfa_read(int fd, void *buf, size_t count) {
331        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_READ)
332                return read( fd, buf, count );
333        #else
334                __submit_prelude
335
336                (*sqe){ IORING_OP_READ, fd, buf, count, 0 };
337
338                __submit_wait
339        #endif
340}
341
342ssize_t cfa_write(int fd, void *buf, size_t count) {
343        #if !defined(HAVE_LINUX_IO_URING_H) || !defined(IORING_OP_WRITE)
344                return read( fd, buf, count );
345        #else
346                __submit_prelude
347
348                (*sqe){ IORING_OP_WRITE, fd, buf, count, 0 };
349
350                __submit_wait
351        #endif
352}
353
354//-----------------------------------------------------------------------------
355// Check if a function is asynchronous
356
357// Macro magic to reduce the size of the following switch case
358#define IS_DEFINED_APPLY(f, ...) f(__VA_ARGS__)
359#define IS_DEFINED_SECOND(first, second, ...) second
360#define IS_DEFINED_TEST(expansion) _CFA_IO_FEATURE_##expansion
361#define IS_DEFINED(macro) IS_DEFINED_APPLY( IS_DEFINED_SECOND,IS_DEFINED_TEST(macro) false, true)
362
363bool has_user_level_blocking( fptr_t func ) {
364        #if defined(HAVE_LINUX_IO_URING_H)
365                #if defined(HAVE_PREADV2)
366                        if( /*func == (fptr_t)preadv2 || */
367                                func == (fptr_t)cfa_preadv2 )
368                                #define _CFA_IO_FEATURE_IORING_OP_READV ,
369                                return IS_DEFINED(IORING_OP_READV);
370                #endif
371
372                #if defined(HAVE_PWRITEV2)
373                        if( /*func == (fptr_t)pwritev2 || */
374                                func == (fptr_t)cfa_pwritev2 )
375                                #define _CFA_IO_FEATURE_IORING_OP_WRITEV ,
376                                return IS_DEFINED(IORING_OP_WRITEV);
377                #endif
378
379                if( /*func == (fptr_t)fsync || */
380                        func == (fptr_t)cfa_fsync )
381                        #define _CFA_IO_FEATURE_IORING_OP_FSYNC ,
382                        return IS_DEFINED(IORING_OP_FSYNC);
383
384                if( /*func == (fptr_t)ync_file_range || */
385                        func == (fptr_t)cfa_sync_file_range )
386                        #define _CFA_IO_FEATURE_IORING_OP_SYNC_FILE_RANGE ,
387                        return IS_DEFINED(IORING_OP_SYNC_FILE_RANGE);
388
389                if( /*func == (fptr_t)sendmsg || */
390                        func == (fptr_t)cfa_sendmsg )
391                        #define _CFA_IO_FEATURE_IORING_OP_SENDMSG ,
392                        return IS_DEFINED(IORING_OP_SENDMSG);
393
394                if( /*func == (fptr_t)recvmsg || */
395                        func == (fptr_t)cfa_recvmsg )
396                        #define _CFA_IO_FEATURE_IORING_OP_RECVMSG ,
397                        return IS_DEFINED(IORING_OP_RECVMSG);
398
399                if( /*func == (fptr_t)send || */
400                        func == (fptr_t)cfa_send )
401                        #define _CFA_IO_FEATURE_IORING_OP_SEND ,
402                        return IS_DEFINED(IORING_OP_SEND);
403
404                if( /*func == (fptr_t)recv || */
405                        func == (fptr_t)cfa_recv )
406                        #define _CFA_IO_FEATURE_IORING_OP_RECV ,
407                        return IS_DEFINED(IORING_OP_RECV);
408
409                if( /*func == (fptr_t)accept4 || */
410                        func == (fptr_t)cfa_accept4 )
411                        #define _CFA_IO_FEATURE_IORING_OP_ACCEPT ,
412                        return IS_DEFINED(IORING_OP_ACCEPT);
413
414                if( /*func == (fptr_t)connect || */
415                        func == (fptr_t)cfa_connect )
416                        #define _CFA_IO_FEATURE_IORING_OP_CONNECT ,
417                        return IS_DEFINED(IORING_OP_CONNECT);
418
419                if( /*func == (fptr_t)fallocate || */
420                        func == (fptr_t)cfa_fallocate )
421                        #define _CFA_IO_FEATURE_IORING_OP_FALLOCATE ,
422                        return IS_DEFINED(IORING_OP_FALLOCATE);
423
424                if( /*func == (fptr_t)posix_fadvise || */
425                        func == (fptr_t)cfa_fadvise )
426                        #define _CFA_IO_FEATURE_IORING_OP_FADVISE ,
427                        return IS_DEFINED(IORING_OP_FADVISE);
428
429                if( /*func == (fptr_t)madvise || */
430                        func == (fptr_t)cfa_madvise )
431                        #define _CFA_IO_FEATURE_IORING_OP_MADVISE ,
432                        return IS_DEFINED(IORING_OP_MADVISE);
433
434                if( /*func == (fptr_t)openat || */
435                        func == (fptr_t)cfa_openat )
436                        #define _CFA_IO_FEATURE_IORING_OP_OPENAT ,
437                        return IS_DEFINED(IORING_OP_OPENAT);
438
439                if( /*func == (fptr_t)close || */
440                        func == (fptr_t)cfa_close )
441                        #define _CFA_IO_FEATURE_IORING_OP_CLOSE ,
442                        return IS_DEFINED(IORING_OP_CLOSE);
443
444                if( /*func == (fptr_t)read || */
445                        func == (fptr_t)cfa_read )
446                        #define _CFA_IO_FEATURE_IORING_OP_READ ,
447                        return IS_DEFINED(IORING_OP_READ);
448
449                if( /*func == (fptr_t)write || */
450                        func == (fptr_t)cfa_write )
451                        #define _CFA_IO_FEATURE_IORING_OP_WRITE ,
452                        return IS_DEFINED(IORING_OP_WRITE);
453        #endif
454
455        return false;
456}
Note: See TracBrowser for help on using the repository browser.