source: libcfa/src/concurrency/iocall.cfa @ e1801fc

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

Added support for non-blocking sendfile

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