1 | #!python3 |
---|
2 | # |
---|
3 | # Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo |
---|
4 | # |
---|
5 | # The contents of this file are covered under the licence agreement in the |
---|
6 | # file "LICENCE" distributed with Cforall. |
---|
7 | # |
---|
8 | # call.cfa.in -- Python script to generate io/call.cfa |
---|
9 | # |
---|
10 | # Author : Thierry Delisle |
---|
11 | # Created On : Fri Sep 11 12:41:16 2020 |
---|
12 | # Last Modified By : |
---|
13 | # Last Modified On : |
---|
14 | # Update Count : |
---|
15 | # |
---|
16 | |
---|
17 | Header = """// |
---|
18 | // Cforall Version 1.0.0 Copyright (C) 2020 University of Waterloo |
---|
19 | // |
---|
20 | // The contents of this file are covered under the licence agreement in the |
---|
21 | // file "LICENCE" distributed with Cforall. |
---|
22 | // |
---|
23 | // call.cfa -- Api for cforall |
---|
24 | // |
---|
25 | // Author : Generated from call.cfa.in |
---|
26 | // Created On : {} |
---|
27 | // |
---|
28 | |
---|
29 | """ |
---|
30 | |
---|
31 | Prelude = """#define __cforall_thread__ |
---|
32 | |
---|
33 | #include <sys/socket.h> // first include because of anonymous types __SOCKADDR_ARG, __CONST_SOCKADDR_ARG |
---|
34 | #include <unistd.h> |
---|
35 | #include <errno.h> |
---|
36 | #include <time.hfa> |
---|
37 | |
---|
38 | #include "bits/defs.hfa" |
---|
39 | #include "kernel.hfa" |
---|
40 | #include "io/types.hfa" |
---|
41 | #include "stats.hfa" |
---|
42 | |
---|
43 | //============================================================================================= |
---|
44 | // I/O uring backend |
---|
45 | //============================================================================================= |
---|
46 | |
---|
47 | #if defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
48 | #include <assert.h> |
---|
49 | #include <stdint.h> |
---|
50 | #include <linux/io_uring.h> |
---|
51 | #include "kernel/fwd.hfa" |
---|
52 | |
---|
53 | static const __u8 REGULAR_FLAGS = 0 |
---|
54 | #if defined(CFA_HAVE_IOSQE_FIXED_FILE) |
---|
55 | | IOSQE_FIXED_FILE |
---|
56 | #endif |
---|
57 | #if defined(CFA_HAVE_IOSQE_IO_DRAIN) |
---|
58 | | IOSQE_IO_DRAIN |
---|
59 | #endif |
---|
60 | #if defined(CFA_HAVE_IOSQE_IO_LINK) |
---|
61 | | IOSQE_IO_LINK |
---|
62 | #endif |
---|
63 | #if defined(CFA_HAVE_IOSQE_IO_HARDLINK) |
---|
64 | | IOSQE_IO_HARDLINK |
---|
65 | #endif |
---|
66 | #if defined(CFA_HAVE_IOSQE_ASYNC) |
---|
67 | | IOSQE_ASYNC |
---|
68 | #endif |
---|
69 | #if defined(CFA_HAVE_IOSQE_BUFFER_SELECTED) |
---|
70 | | IOSQE_BUFFER_SELECTED |
---|
71 | #endif |
---|
72 | ; |
---|
73 | |
---|
74 | static const __u32 SPLICE_FLAGS = 0 |
---|
75 | #if defined(CFA_HAVE_SPLICE_F_FD_IN_FIXED) |
---|
76 | | SPLICE_F_FD_IN_FIXED |
---|
77 | #endif |
---|
78 | ; |
---|
79 | |
---|
80 | extern struct io_context$ * cfa_io_allocate(struct io_uring_sqe * out_sqes[], __u32 out_idxs[], __u32 want) __attribute__((nonnull (1,2))); |
---|
81 | extern void cfa_io_submit( struct io_context$ * in_ctx, __u32 in_idxs[], __u32 have, bool lazy ) __attribute__((nonnull (1,2))); |
---|
82 | #endif |
---|
83 | |
---|
84 | //============================================================================================= |
---|
85 | // I/O Forwards |
---|
86 | //============================================================================================= |
---|
87 | |
---|
88 | extern "C" { |
---|
89 | #if defined(CFA_HAVE_PREADV2) |
---|
90 | struct iovec; |
---|
91 | extern ssize_t preadv2 (int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags); |
---|
92 | #endif |
---|
93 | #if defined(CFA_HAVE_PWRITEV2) |
---|
94 | struct iovec; |
---|
95 | extern ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags); |
---|
96 | #endif |
---|
97 | |
---|
98 | extern int fsync(int fd); |
---|
99 | |
---|
100 | #if __OFF_T_MATCHES_OFF64_T |
---|
101 | typedef __off64_t off_t; |
---|
102 | #else |
---|
103 | typedef __off_t off_t; |
---|
104 | #endif |
---|
105 | typedef __off64_t off64_t; |
---|
106 | extern int sync_file_range(int fd, off64_t offset, off64_t nbytes, unsigned int flags); |
---|
107 | |
---|
108 | struct msghdr; |
---|
109 | struct sockaddr; |
---|
110 | extern ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags); |
---|
111 | extern ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); |
---|
112 | extern ssize_t send(int sockfd, const void *buf, size_t len, int flags); |
---|
113 | extern ssize_t recv(int sockfd, void *buf, size_t len, int flags); |
---|
114 | |
---|
115 | extern int fallocate(int fd, int mode, off_t offset, off_t len); |
---|
116 | extern int posix_fadvise(int fd, off_t offset, off_t len, int advice); |
---|
117 | extern int madvise(void *addr, size_t length, int advice); |
---|
118 | |
---|
119 | extern int openat(int dirfd, const char *pathname, int flags, mode_t mode); |
---|
120 | extern int close(int fd); |
---|
121 | |
---|
122 | extern ssize_t read (int fd, void *buf, size_t count); |
---|
123 | |
---|
124 | struct epoll_event; |
---|
125 | extern int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event); |
---|
126 | |
---|
127 | extern ssize_t splice(int fd_in, __off64_t *off_in, int fd_out, __off64_t *off_out, size_t len, unsigned int flags); |
---|
128 | extern ssize_t tee(int fd_in, int fd_out, size_t len, unsigned int flags); |
---|
129 | } |
---|
130 | |
---|
131 | //============================================================================================= |
---|
132 | // I/O Interface |
---|
133 | //============================================================================================= |
---|
134 | #pragma GCC visibility push(default) |
---|
135 | """ |
---|
136 | |
---|
137 | print(Header.format("A Date")) |
---|
138 | print(Prelude) |
---|
139 | |
---|
140 | import re |
---|
141 | import sys |
---|
142 | class Call: |
---|
143 | def __init__(self, op, signature, body, define=None): |
---|
144 | sig = re.search("(.*) (.*)\((.*)\)", signature) |
---|
145 | if not sig: |
---|
146 | print("OP '{}' has invalid signature {}".format(op, signature), file=sys.stderr) |
---|
147 | sys.exit(1) |
---|
148 | |
---|
149 | self.op = op |
---|
150 | self.ret = sig.group(1) |
---|
151 | self.name = sig.group(2) |
---|
152 | self.params = sig.group(3) |
---|
153 | self.define = define |
---|
154 | self.body = "" |
---|
155 | |
---|
156 | accepted_keys = [ 'ioprio', 'fd', 'off', 'addr2','addr', 'splice_off_in','len', |
---|
157 | 'rw_flags', 'fsync_flags', 'poll_events', 'poll32_events', |
---|
158 | 'sync_range_flags', 'msg_flags', 'timeout_flags', 'accept_flags', |
---|
159 | 'cancel_flags', 'open_flags', 'statx_flags', 'fadvise_advice', |
---|
160 | 'splice_flags', 'buf_index' ,'buf_group' 'personality', |
---|
161 | 'splice_fd_in' ] |
---|
162 | |
---|
163 | for k, v in body.items(): |
---|
164 | if not k in accepted_keys: |
---|
165 | print("OP '{}' has invalid body kew {}".format(op, k), file=sys.stderr) |
---|
166 | sys.exit(1) |
---|
167 | |
---|
168 | self.body += "\n sqe->{key} = {value};".format(key=k, value=v) |
---|
169 | |
---|
170 | |
---|
171 | def args(self): |
---|
172 | param_a = self.params.split(',') |
---|
173 | args_a = [p.replace('*', ' ').split()[-1] for p in param_a] |
---|
174 | for a in args_a: |
---|
175 | if '*' in a: |
---|
176 | print("OP '{}' has invalid * in argument {}".format(self.op, a), file=sys.stderr) |
---|
177 | sys.exit(1) |
---|
178 | |
---|
179 | return ', '.join(args_a) |
---|
180 | |
---|
181 | AsyncTemplate = """inline void async_{name}(io_future_t & future, {params}, __u64 submit_flags) {{ |
---|
182 | #if !defined(CFA_HAVE_LINUX_IO_URING_H) || !defined(CFA_HAVE_IORING_OP_{op}) |
---|
183 | ssize_t res = {name}({args}); |
---|
184 | if (res >= 0) {{ |
---|
185 | fulfil(future, res); |
---|
186 | }} |
---|
187 | else {{ |
---|
188 | fulfil(future, -errno); |
---|
189 | }} |
---|
190 | #else |
---|
191 | __u8 sflags = REGULAR_FLAGS & submit_flags; |
---|
192 | __u32 idx; |
---|
193 | struct io_uring_sqe * sqe; |
---|
194 | struct io_context$ * ctx = cfa_io_allocate( &sqe, &idx, 1 ); |
---|
195 | |
---|
196 | memset(sqe, 0, sizeof(*sqe)); |
---|
197 | sqe->opcode = IORING_OP_{op}; |
---|
198 | sqe->flags = sflags; |
---|
199 | sqe->user_data = (uintptr_t)&future; |
---|
200 | {body} |
---|
201 | |
---|
202 | asm volatile("": : :"memory"); |
---|
203 | |
---|
204 | verify( sqe->user_data == (uintptr_t)&future ); |
---|
205 | cfa_io_submit( ctx, &idx, 1, 0 != (submit_flags & CFA_IO_LAZY) ); |
---|
206 | #endif |
---|
207 | }}""" |
---|
208 | |
---|
209 | SyncTemplate = """{ret} cfa_{name}({params}, __u64 submit_flags) {{ |
---|
210 | io_future_t future; |
---|
211 | |
---|
212 | async_{name}( future, {args}, submit_flags ); |
---|
213 | |
---|
214 | __attribute__((unused)) bool parked; |
---|
215 | parked = wait( future ); |
---|
216 | __STATS__(false, if(!parked) io.submit.nblk += 1; ) |
---|
217 | if( future.result < 0 ) {{ |
---|
218 | errno = -future.result; |
---|
219 | return -1; |
---|
220 | }} |
---|
221 | return future.result; |
---|
222 | }}""" |
---|
223 | |
---|
224 | calls = [ |
---|
225 | # CFA_HAVE_IORING_OP_READV |
---|
226 | Call('READV', 'ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags)', { |
---|
227 | 'fd' : 'fd', |
---|
228 | 'off' : 'offset', |
---|
229 | 'addr': '(uintptr_t)iov', |
---|
230 | 'len' : 'iovcnt', |
---|
231 | }, define = 'CFA_HAVE_PREADV2'), |
---|
232 | # CFA_HAVE_IORING_OP_WRITEV |
---|
233 | Call('WRITEV', 'ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags)', { |
---|
234 | 'fd' : 'fd', |
---|
235 | 'off' : 'offset', |
---|
236 | 'addr': '(uintptr_t)iov', |
---|
237 | 'len' : 'iovcnt' |
---|
238 | }, define = 'CFA_HAVE_PWRITEV2'), |
---|
239 | # CFA_HAVE_IORING_OP_FSYNC |
---|
240 | Call('FSYNC', 'int fsync(int fd)', { |
---|
241 | 'fd': 'fd' |
---|
242 | }), |
---|
243 | # CFA_HAVE_IORING_OP_EPOLL_CTL |
---|
244 | Call('EPOLL_CTL', 'int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)', { |
---|
245 | 'fd': 'epfd', |
---|
246 | 'addr': 'fd', |
---|
247 | 'len': 'op', |
---|
248 | 'off': '(uintptr_t)event' |
---|
249 | }), |
---|
250 | # CFA_HAVE_IORING_OP_SYNC_FILE_RANGE |
---|
251 | Call('SYNC_FILE_RANGE', 'int sync_file_range(int fd, off64_t offset, off64_t nbytes, unsigned int flags)', { |
---|
252 | 'fd': 'fd', |
---|
253 | 'off': 'offset', |
---|
254 | 'len': 'nbytes', |
---|
255 | 'sync_range_flags': 'flags' |
---|
256 | }), |
---|
257 | # CFA_HAVE_IORING_OP_SENDMSG |
---|
258 | Call('SENDMSG', 'ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags)', { |
---|
259 | 'fd': 'sockfd', |
---|
260 | 'addr': '(uintptr_t)(struct msghdr *)msg', |
---|
261 | 'len': '1', |
---|
262 | 'msg_flags': 'flags' |
---|
263 | }), |
---|
264 | # CFA_HAVE_IORING_OP_RECVMSG |
---|
265 | Call('RECVMSG', 'ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags)', { |
---|
266 | 'fd': 'sockfd', |
---|
267 | 'addr': '(uintptr_t)(struct msghdr *)msg', |
---|
268 | 'len': '1', |
---|
269 | 'msg_flags': 'flags' |
---|
270 | }), |
---|
271 | # CFA_HAVE_IORING_OP_SEND |
---|
272 | Call('SEND', 'ssize_t send(int sockfd, const void *buf, size_t len, int flags)', { |
---|
273 | 'fd': 'sockfd', |
---|
274 | 'addr': '(uintptr_t)buf', |
---|
275 | 'len': 'len', |
---|
276 | 'msg_flags': 'flags' |
---|
277 | }), |
---|
278 | # CFA_HAVE_IORING_OP_RECV |
---|
279 | Call('RECV', 'ssize_t recv(int sockfd, void *buf, size_t len, int flags)', { |
---|
280 | 'fd': 'sockfd', |
---|
281 | 'addr': '(uintptr_t)buf', |
---|
282 | 'len': 'len', |
---|
283 | 'msg_flags': 'flags' |
---|
284 | }), |
---|
285 | # CFA_HAVE_IORING_OP_ACCEPT |
---|
286 | Call('ACCEPT', 'int accept4(int sockfd, __SOCKADDR_ARG addr, socklen_t * restrict addrlen, int flags)', { |
---|
287 | 'fd': 'sockfd', |
---|
288 | 'addr': '(uintptr_t)&addr', |
---|
289 | 'addr2': '(uintptr_t)addrlen', |
---|
290 | 'accept_flags': 'flags' |
---|
291 | }), |
---|
292 | # CFA_HAVE_IORING_OP_CONNECT |
---|
293 | Call('CONNECT', 'int connect(int sockfd, __CONST_SOCKADDR_ARG addr, socklen_t addrlen)', { |
---|
294 | 'fd': 'sockfd', |
---|
295 | 'addr': '(uintptr_t)&addr', |
---|
296 | 'off': 'addrlen' |
---|
297 | }), |
---|
298 | # CFA_HAVE_IORING_OP_FALLOCATE |
---|
299 | Call('FALLOCATE', 'int fallocate(int fd, int mode, off_t offset, off_t len)', { |
---|
300 | 'fd': 'fd', |
---|
301 | 'addr': '(uintptr_t)len', |
---|
302 | 'len': 'mode', |
---|
303 | 'off': 'offset' |
---|
304 | }), |
---|
305 | # CFA_HAVE_IORING_OP_FADVISE |
---|
306 | Call('FADVISE', 'int posix_fadvise(int fd, off_t offset, off_t len, int advice)', { |
---|
307 | 'fd': 'fd', |
---|
308 | 'off': 'offset', |
---|
309 | 'len': 'len', |
---|
310 | 'fadvise_advice': 'advice' |
---|
311 | }), |
---|
312 | # CFA_HAVE_IORING_OP_MADVISE |
---|
313 | Call('MADVISE', 'int madvise(void *addr, size_t length, int advice)', { |
---|
314 | 'addr': '(uintptr_t)addr', |
---|
315 | 'len': 'length', |
---|
316 | 'fadvise_advice': 'advice' |
---|
317 | }), |
---|
318 | # CFA_HAVE_IORING_OP_OPENAT |
---|
319 | Call('OPENAT', 'int openat(int dirfd, const char *pathname, int flags, mode_t mode)', { |
---|
320 | 'fd': 'dirfd', |
---|
321 | 'addr': '(uintptr_t)pathname', |
---|
322 | 'len': 'mode', |
---|
323 | 'open_flags': 'flags;' |
---|
324 | }), |
---|
325 | # CFA_HAVE_IORING_OP_OPENAT2 |
---|
326 | Call('OPENAT2', 'int openat2(int dirfd, const char *pathname, struct open_how * how, size_t size)', { |
---|
327 | 'fd': 'dirfd', |
---|
328 | 'addr': 'pathname', |
---|
329 | 'len': 'sizeof(*how)', |
---|
330 | 'off': '(uintptr_t)how', |
---|
331 | }, define = 'CFA_HAVE_OPENAT2'), |
---|
332 | # CFA_HAVE_IORING_OP_CLOSE |
---|
333 | Call('CLOSE', 'int close(int fd)', { |
---|
334 | 'fd': 'fd' |
---|
335 | }), |
---|
336 | # CFA_HAVE_IORING_OP_STATX |
---|
337 | Call('STATX', 'int statx(int dirfd, const char *pathname, int flags, unsigned int mask, struct statx *statxbuf)', { |
---|
338 | 'fd': 'dirfd', |
---|
339 | 'off': '(uintptr_t)statxbuf', |
---|
340 | 'addr': 'pathname', |
---|
341 | 'len': 'mask', |
---|
342 | 'statx_flags': 'flags' |
---|
343 | }, define = 'CFA_HAVE_STATX'), |
---|
344 | # CFA_HAVE_IORING_OP_READ |
---|
345 | Call('READ', 'ssize_t read(int fd, void * buf, size_t count)', { |
---|
346 | 'fd': 'fd', |
---|
347 | 'addr': '(uintptr_t)buf', |
---|
348 | 'len': 'count' |
---|
349 | }), |
---|
350 | # CFA_HAVE_IORING_OP_WRITE |
---|
351 | Call('WRITE', 'ssize_t write(int fd, void * buf, size_t count)', { |
---|
352 | 'fd': 'fd', |
---|
353 | 'addr': '(uintptr_t)buf', |
---|
354 | 'len': 'count' |
---|
355 | }), |
---|
356 | # CFA_HAVE_IORING_OP_SPLICE |
---|
357 | Call('SPLICE', 'ssize_t splice(int fd_in, __off64_t *off_in, int fd_out, __off64_t *off_out, size_t len, unsigned int flags)', { |
---|
358 | 'splice_fd_in': 'fd_in', |
---|
359 | 'splice_off_in': 'off_in ? (__u64)*off_in : (__u64)-1', |
---|
360 | 'fd': 'fd_out', |
---|
361 | 'off': 'off_out ? (__u64)*off_out : (__u64)-1', |
---|
362 | 'len': 'len', |
---|
363 | 'splice_flags': 'flags' |
---|
364 | }), |
---|
365 | # CFA_HAVE_IORING_OP_TEE |
---|
366 | Call('TEE', 'ssize_t tee(int fd_in, int fd_out, size_t len, unsigned int flags)', { |
---|
367 | 'splice_fd_in': 'fd_in', |
---|
368 | 'fd': 'fd_out', |
---|
369 | 'len': 'len', |
---|
370 | 'splice_flags': 'flags' |
---|
371 | }) |
---|
372 | ] |
---|
373 | |
---|
374 | print("//----------") |
---|
375 | print("// synchronous calls") |
---|
376 | for c in calls: |
---|
377 | if c.define: |
---|
378 | print("""#if defined({define}) |
---|
379 | {ret} cfa_{name}({params}, __u64 submit_flags); |
---|
380 | #endif""".format(define=c.define,ret=c.ret, name=c.name, params=c.params)) |
---|
381 | else: |
---|
382 | print("{ret} cfa_{name}({params}, __u64 submit_flags);" |
---|
383 | .format(ret=c.ret, name=c.name, params=c.params)) |
---|
384 | |
---|
385 | print("\n//----------") |
---|
386 | print("// asynchronous calls") |
---|
387 | for c in calls: |
---|
388 | if c.define: |
---|
389 | print("""#if defined({define}) |
---|
390 | void async_{name}(io_future_t & future, {params}, __u64 submit_flags); |
---|
391 | #endif""".format(define=c.define,name=c.name, params=c.params)) |
---|
392 | else: |
---|
393 | print("void async_{name}(io_future_t & future, {params}, __u64 submit_flags);" |
---|
394 | .format(name=c.name, params=c.params)) |
---|
395 | print("\n") |
---|
396 | |
---|
397 | for c in calls: |
---|
398 | print("//-----------------------------------------------------------------------------") |
---|
399 | print("// {}".format(c.name)) |
---|
400 | Async = AsyncTemplate.format( |
---|
401 | name = c.name, |
---|
402 | ret = c.ret, |
---|
403 | params = c.params, |
---|
404 | args = c.args(), |
---|
405 | op = c.op, |
---|
406 | body = c.body |
---|
407 | |
---|
408 | ) |
---|
409 | Sync = SyncTemplate.format( |
---|
410 | name = c.name, |
---|
411 | ret = c.ret, |
---|
412 | params = c.params, |
---|
413 | args = c.args() |
---|
414 | ) |
---|
415 | |
---|
416 | if c.define: |
---|
417 | print("""#if defined({}) |
---|
418 | //---------- |
---|
419 | // asynchronous call |
---|
420 | {} |
---|
421 | |
---|
422 | //---------- |
---|
423 | // synchronous call |
---|
424 | {} |
---|
425 | #endif |
---|
426 | """.format(c.define, "\n\t".join( Async.splitlines() ), "\n\t".join( Sync.splitlines() ))) |
---|
427 | else : |
---|
428 | print("""//---------- |
---|
429 | // asynchronous call |
---|
430 | {} |
---|
431 | |
---|
432 | //---------- |
---|
433 | // synchronous call |
---|
434 | {} |
---|
435 | """.format(Async, Sync)) |
---|
436 | |
---|
437 | print(""" |
---|
438 | //----------------------------------------------------------------------------- |
---|
439 | // Check if a function is has asynchronous |
---|
440 | bool has_user_level_blocking( fptr_t func ) { |
---|
441 | #if defined(CFA_HAVE_LINUX_IO_URING_H)""") |
---|
442 | |
---|
443 | for c in calls: |
---|
444 | if c.define: |
---|
445 | print(""" #if defined({define}) |
---|
446 | if( /*func == (fptr_t)preadv2 || */ |
---|
447 | func == (fptr_t)cfa_{name} || |
---|
448 | func == (fptr_t)async_{name} ) {{ |
---|
449 | #if defined(CFA_HAVE_IORING_OP_{op}) |
---|
450 | return true; |
---|
451 | #else |
---|
452 | return false; |
---|
453 | #endif |
---|
454 | }} |
---|
455 | #endif""".format(define=c.define, name=c.name, op=c.op)) |
---|
456 | else: |
---|
457 | print(""" if( /*func == (fptr_t)preadv2 || */ |
---|
458 | func == (fptr_t)cfa_{name} || |
---|
459 | func == (fptr_t)async_{name} ) {{ |
---|
460 | #if defined(CFA_HAVE_IORING_OP_{op}) |
---|
461 | return true; |
---|
462 | #else |
---|
463 | return false; |
---|
464 | #endif |
---|
465 | }}""".format(name=c.name, op=c.op)) |
---|
466 | |
---|
467 | print(""" #endif |
---|
468 | |
---|
469 | return false; |
---|
470 | }""") |
---|