source: libcfa/src/concurrency/iocall.cfa@ 53e4562

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 53e4562 was efc171d1, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Fixed several concurrency warnings

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