polyphony 0.84 → 0.86

Sign up to get free protection for your applications and to get access to all the features.
Files changed (241) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +14 -0
  3. data/Gemfile.lock +1 -1
  4. data/Rakefile +1 -1
  5. data/examples/core/multi_suspend.rb +39 -0
  6. data/examples/core/shutdown_all_children.rb +41 -0
  7. data/examples/io/gzip.rb +8 -0
  8. data/examples/io/splice_echo_server.rb +15 -0
  9. data/ext/polyphony/backend_io_uring.c +57 -31
  10. data/ext/polyphony/io_extensions.c +137 -26
  11. data/lib/polyphony/extensions/fiber.rb +3 -1
  12. data/lib/polyphony/extensions/io.rb +4 -0
  13. data/lib/polyphony/extensions/pipe.rb +4 -0
  14. data/lib/polyphony/extensions/socket.rb +4 -0
  15. data/lib/polyphony/version.rb +1 -1
  16. data/polyphony.gemspec +1 -1
  17. data/test/test_backend.rb +1 -1
  18. data/test/test_fiber.rb +5 -2
  19. data/test/test_signal.rb +3 -3
  20. data/vendor/liburing/.github/pull_request_template.md +86 -0
  21. data/vendor/liburing/.github/workflows/build.yml +85 -0
  22. data/vendor/liburing/.github/workflows/shellcheck.yml +20 -0
  23. data/vendor/liburing/.gitignore +149 -0
  24. data/vendor/liburing/COPYING +502 -0
  25. data/vendor/liburing/COPYING.GPL +339 -0
  26. data/vendor/liburing/LICENSE +7 -0
  27. data/vendor/liburing/Makefile +82 -0
  28. data/vendor/liburing/Makefile.common +5 -0
  29. data/vendor/liburing/Makefile.quiet +11 -0
  30. data/vendor/liburing/README +46 -0
  31. data/vendor/liburing/configure +486 -0
  32. data/vendor/liburing/debian/README.Debian +7 -0
  33. data/vendor/liburing/debian/changelog +27 -0
  34. data/vendor/liburing/debian/compat +1 -0
  35. data/vendor/liburing/debian/control +48 -0
  36. data/vendor/liburing/debian/copyright +49 -0
  37. data/vendor/liburing/debian/liburing-dev.install +4 -0
  38. data/vendor/liburing/debian/liburing-dev.manpages +6 -0
  39. data/vendor/liburing/debian/liburing1-udeb.install +1 -0
  40. data/vendor/liburing/debian/liburing1.install +1 -0
  41. data/vendor/liburing/debian/liburing1.symbols +32 -0
  42. data/vendor/liburing/debian/patches/series +1 -0
  43. data/vendor/liburing/debian/rules +81 -0
  44. data/vendor/liburing/debian/source/format +1 -0
  45. data/vendor/liburing/debian/source/local-options +2 -0
  46. data/vendor/liburing/debian/source/options +1 -0
  47. data/vendor/liburing/debian/watch +3 -0
  48. data/vendor/liburing/examples/Makefile +38 -0
  49. data/vendor/liburing/examples/io_uring-cp.c +282 -0
  50. data/vendor/liburing/examples/io_uring-test.c +112 -0
  51. data/vendor/liburing/examples/link-cp.c +193 -0
  52. data/vendor/liburing/examples/ucontext-cp.c +273 -0
  53. data/vendor/liburing/liburing.pc.in +12 -0
  54. data/vendor/liburing/liburing.spec +66 -0
  55. data/vendor/liburing/make-debs.sh +53 -0
  56. data/vendor/liburing/man/io_uring.7 +754 -0
  57. data/vendor/liburing/man/io_uring_cq_advance.3 +35 -0
  58. data/vendor/liburing/man/io_uring_cq_ready.3 +25 -0
  59. data/vendor/liburing/man/io_uring_cqe_get_data.3 +34 -0
  60. data/vendor/liburing/man/io_uring_cqe_seen.3 +32 -0
  61. data/vendor/liburing/man/io_uring_enter.2 +1483 -0
  62. data/vendor/liburing/man/io_uring_free_probe.3 +24 -0
  63. data/vendor/liburing/man/io_uring_get_probe.3 +29 -0
  64. data/vendor/liburing/man/io_uring_get_sqe.3 +38 -0
  65. data/vendor/liburing/man/io_uring_opcode_supported.3 +29 -0
  66. data/vendor/liburing/man/io_uring_prep_msg_ring.3 +58 -0
  67. data/vendor/liburing/man/io_uring_prep_read.3 +50 -0
  68. data/vendor/liburing/man/io_uring_prep_read_fixed.3 +54 -0
  69. data/vendor/liburing/man/io_uring_prep_readv.3 +51 -0
  70. data/vendor/liburing/man/io_uring_prep_readv2.3 +79 -0
  71. data/vendor/liburing/man/io_uring_prep_write.3 +50 -0
  72. data/vendor/liburing/man/io_uring_prep_write_fixed.3 +54 -0
  73. data/vendor/liburing/man/io_uring_prep_writev.3 +51 -0
  74. data/vendor/liburing/man/io_uring_prep_writev2.3 +78 -0
  75. data/vendor/liburing/man/io_uring_queue_exit.3 +27 -0
  76. data/vendor/liburing/man/io_uring_queue_init.3 +44 -0
  77. data/vendor/liburing/man/io_uring_register.2 +688 -0
  78. data/vendor/liburing/man/io_uring_register_buffers.3 +41 -0
  79. data/vendor/liburing/man/io_uring_register_files.3 +35 -0
  80. data/vendor/liburing/man/io_uring_setup.2 +534 -0
  81. data/vendor/liburing/man/io_uring_sq_ready.3 +25 -0
  82. data/vendor/liburing/man/io_uring_sq_space_left.3 +25 -0
  83. data/vendor/liburing/man/io_uring_sqe_set_data.3 +30 -0
  84. data/vendor/liburing/man/io_uring_sqe_set_flags.3 +60 -0
  85. data/vendor/liburing/man/io_uring_sqring_wait.3 +30 -0
  86. data/vendor/liburing/man/io_uring_submit.3 +29 -0
  87. data/vendor/liburing/man/io_uring_submit_and_wait.3 +34 -0
  88. data/vendor/liburing/man/io_uring_submit_and_wait_timeout.3 +49 -0
  89. data/vendor/liburing/man/io_uring_unregister_buffers.3 +26 -0
  90. data/vendor/liburing/man/io_uring_unregister_files.3 +26 -0
  91. data/vendor/liburing/man/io_uring_wait_cqe.3 +33 -0
  92. data/vendor/liburing/man/io_uring_wait_cqe_nr.3 +36 -0
  93. data/vendor/liburing/man/io_uring_wait_cqe_timeout.3 +39 -0
  94. data/vendor/liburing/man/io_uring_wait_cqes.3 +46 -0
  95. data/vendor/liburing/src/Makefile +89 -0
  96. data/vendor/liburing/src/arch/aarch64/syscall.h +95 -0
  97. data/vendor/liburing/src/arch/generic/lib.h +21 -0
  98. data/vendor/liburing/src/arch/generic/syscall.h +87 -0
  99. data/vendor/liburing/src/arch/syscall-defs.h +67 -0
  100. data/vendor/liburing/src/arch/x86/lib.h +32 -0
  101. data/vendor/liburing/src/arch/x86/syscall.h +160 -0
  102. data/vendor/liburing/src/include/liburing/barrier.h +81 -0
  103. data/vendor/liburing/src/include/liburing/io_uring.h +442 -0
  104. data/vendor/liburing/src/include/liburing.h +921 -0
  105. data/vendor/liburing/src/int_flags.h +8 -0
  106. data/vendor/liburing/src/lib.h +57 -0
  107. data/vendor/liburing/src/liburing.map +53 -0
  108. data/vendor/liburing/src/nolibc.c +48 -0
  109. data/vendor/liburing/src/queue.c +403 -0
  110. data/vendor/liburing/src/register.c +293 -0
  111. data/vendor/liburing/src/setup.c +332 -0
  112. data/vendor/liburing/src/syscall.c +47 -0
  113. data/vendor/liburing/src/syscall.h +103 -0
  114. data/vendor/liburing/test/232c93d07b74-test.c +306 -0
  115. data/vendor/liburing/test/35fa71a030ca-test.c +329 -0
  116. data/vendor/liburing/test/500f9fbadef8-test.c +89 -0
  117. data/vendor/liburing/test/7ad0e4b2f83c-test.c +93 -0
  118. data/vendor/liburing/test/8a9973408177-test.c +106 -0
  119. data/vendor/liburing/test/917257daa0fe-test.c +53 -0
  120. data/vendor/liburing/test/Makefile +244 -0
  121. data/vendor/liburing/test/a0908ae19763-test.c +58 -0
  122. data/vendor/liburing/test/a4c0b3decb33-test.c +180 -0
  123. data/vendor/liburing/test/accept-link.c +254 -0
  124. data/vendor/liburing/test/accept-reuse.c +164 -0
  125. data/vendor/liburing/test/accept-test.c +79 -0
  126. data/vendor/liburing/test/accept.c +477 -0
  127. data/vendor/liburing/test/across-fork.c +283 -0
  128. data/vendor/liburing/test/b19062a56726-test.c +53 -0
  129. data/vendor/liburing/test/b5837bd5311d-test.c +77 -0
  130. data/vendor/liburing/test/ce593a6c480a-test.c +136 -0
  131. data/vendor/liburing/test/close-opath.c +122 -0
  132. data/vendor/liburing/test/config +10 -0
  133. data/vendor/liburing/test/connect.c +398 -0
  134. data/vendor/liburing/test/cq-full.c +96 -0
  135. data/vendor/liburing/test/cq-overflow.c +294 -0
  136. data/vendor/liburing/test/cq-peek-batch.c +102 -0
  137. data/vendor/liburing/test/cq-ready.c +94 -0
  138. data/vendor/liburing/test/cq-size.c +64 -0
  139. data/vendor/liburing/test/d4ae271dfaae-test.c +96 -0
  140. data/vendor/liburing/test/d77a67ed5f27-test.c +65 -0
  141. data/vendor/liburing/test/defer.c +307 -0
  142. data/vendor/liburing/test/double-poll-crash.c +185 -0
  143. data/vendor/liburing/test/drop-submit.c +92 -0
  144. data/vendor/liburing/test/eeed8b54e0df-test.c +114 -0
  145. data/vendor/liburing/test/empty-eownerdead.c +45 -0
  146. data/vendor/liburing/test/eventfd-disable.c +151 -0
  147. data/vendor/liburing/test/eventfd-reg.c +76 -0
  148. data/vendor/liburing/test/eventfd-ring.c +97 -0
  149. data/vendor/liburing/test/eventfd.c +112 -0
  150. data/vendor/liburing/test/exec-target.c +6 -0
  151. data/vendor/liburing/test/exit-no-cleanup.c +117 -0
  152. data/vendor/liburing/test/fadvise.c +202 -0
  153. data/vendor/liburing/test/fallocate.c +249 -0
  154. data/vendor/liburing/test/fc2a85cb02ef-test.c +131 -0
  155. data/vendor/liburing/test/file-register.c +858 -0
  156. data/vendor/liburing/test/file-update.c +173 -0
  157. data/vendor/liburing/test/file-verify.c +629 -0
  158. data/vendor/liburing/test/files-exit-hang-poll.c +128 -0
  159. data/vendor/liburing/test/files-exit-hang-timeout.c +134 -0
  160. data/vendor/liburing/test/fixed-link.c +90 -0
  161. data/vendor/liburing/test/fpos.c +252 -0
  162. data/vendor/liburing/test/fsync.c +224 -0
  163. data/vendor/liburing/test/hardlink.c +136 -0
  164. data/vendor/liburing/test/helpers.c +135 -0
  165. data/vendor/liburing/test/helpers.h +67 -0
  166. data/vendor/liburing/test/io-cancel.c +550 -0
  167. data/vendor/liburing/test/io_uring_enter.c +296 -0
  168. data/vendor/liburing/test/io_uring_register.c +676 -0
  169. data/vendor/liburing/test/io_uring_setup.c +192 -0
  170. data/vendor/liburing/test/iopoll.c +372 -0
  171. data/vendor/liburing/test/lfs-openat-write.c +119 -0
  172. data/vendor/liburing/test/lfs-openat.c +275 -0
  173. data/vendor/liburing/test/link-timeout.c +1107 -0
  174. data/vendor/liburing/test/link.c +496 -0
  175. data/vendor/liburing/test/link_drain.c +229 -0
  176. data/vendor/liburing/test/madvise.c +195 -0
  177. data/vendor/liburing/test/mkdir.c +108 -0
  178. data/vendor/liburing/test/msg-ring.c +234 -0
  179. data/vendor/liburing/test/multicqes_drain.c +387 -0
  180. data/vendor/liburing/test/nop-all-sizes.c +99 -0
  181. data/vendor/liburing/test/nop.c +115 -0
  182. data/vendor/liburing/test/open-close.c +261 -0
  183. data/vendor/liburing/test/openat2.c +308 -0
  184. data/vendor/liburing/test/personality.c +204 -0
  185. data/vendor/liburing/test/pipe-eof.c +83 -0
  186. data/vendor/liburing/test/pipe-reuse.c +105 -0
  187. data/vendor/liburing/test/poll-cancel-ton.c +135 -0
  188. data/vendor/liburing/test/poll-cancel.c +228 -0
  189. data/vendor/liburing/test/poll-link.c +230 -0
  190. data/vendor/liburing/test/poll-many.c +208 -0
  191. data/vendor/liburing/test/poll-mshot-update.c +273 -0
  192. data/vendor/liburing/test/poll-ring.c +48 -0
  193. data/vendor/liburing/test/poll-v-poll.c +353 -0
  194. data/vendor/liburing/test/poll.c +109 -0
  195. data/vendor/liburing/test/pollfree.c +426 -0
  196. data/vendor/liburing/test/probe.c +135 -0
  197. data/vendor/liburing/test/read-write.c +876 -0
  198. data/vendor/liburing/test/register-restrictions.c +633 -0
  199. data/vendor/liburing/test/rename.c +135 -0
  200. data/vendor/liburing/test/ring-leak.c +173 -0
  201. data/vendor/liburing/test/ring-leak2.c +249 -0
  202. data/vendor/liburing/test/rsrc_tags.c +449 -0
  203. data/vendor/liburing/test/runtests-loop.sh +16 -0
  204. data/vendor/liburing/test/runtests.sh +170 -0
  205. data/vendor/liburing/test/rw_merge_test.c +97 -0
  206. data/vendor/liburing/test/self.c +91 -0
  207. data/vendor/liburing/test/send_recv.c +286 -0
  208. data/vendor/liburing/test/send_recvmsg.c +345 -0
  209. data/vendor/liburing/test/sendmsg_fs_cve.c +200 -0
  210. data/vendor/liburing/test/shared-wq.c +84 -0
  211. data/vendor/liburing/test/short-read.c +75 -0
  212. data/vendor/liburing/test/shutdown.c +165 -0
  213. data/vendor/liburing/test/sigfd-deadlock.c +74 -0
  214. data/vendor/liburing/test/skip-cqe.c +429 -0
  215. data/vendor/liburing/test/socket-rw-eagain.c +158 -0
  216. data/vendor/liburing/test/socket-rw-offset.c +157 -0
  217. data/vendor/liburing/test/socket-rw.c +145 -0
  218. data/vendor/liburing/test/splice.c +512 -0
  219. data/vendor/liburing/test/sq-full-cpp.cc +45 -0
  220. data/vendor/liburing/test/sq-full.c +45 -0
  221. data/vendor/liburing/test/sq-poll-dup.c +204 -0
  222. data/vendor/liburing/test/sq-poll-kthread.c +169 -0
  223. data/vendor/liburing/test/sq-poll-share.c +137 -0
  224. data/vendor/liburing/test/sq-space_left.c +159 -0
  225. data/vendor/liburing/test/sqpoll-cancel-hang.c +157 -0
  226. data/vendor/liburing/test/sqpoll-disable-exit.c +196 -0
  227. data/vendor/liburing/test/sqpoll-exit-hang.c +78 -0
  228. data/vendor/liburing/test/sqpoll-sleep.c +69 -0
  229. data/vendor/liburing/test/statx.c +172 -0
  230. data/vendor/liburing/test/stdout.c +232 -0
  231. data/vendor/liburing/test/submit-link-fail.c +154 -0
  232. data/vendor/liburing/test/submit-reuse.c +239 -0
  233. data/vendor/liburing/test/symlink.c +116 -0
  234. data/vendor/liburing/test/teardowns.c +58 -0
  235. data/vendor/liburing/test/thread-exit.c +143 -0
  236. data/vendor/liburing/test/timeout-new.c +252 -0
  237. data/vendor/liburing/test/timeout-overflow.c +204 -0
  238. data/vendor/liburing/test/timeout.c +1523 -0
  239. data/vendor/liburing/test/unlink.c +112 -0
  240. data/vendor/liburing/test/wakeup-hang.c +162 -0
  241. metadata +227 -2
@@ -0,0 +1,387 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Description: generic tests for io_uring drain io
4
+ *
5
+ * The main idea is to randomly generate different type of sqe to
6
+ * challenge the drain logic. There are some restrictions for the
7
+ * generated sqes, details in io_uring maillist:
8
+ * https://lore.kernel.org/io-uring/39a49b4c-27c2-1035-b250-51daeccaab9b@linux.alibaba.com/
9
+ *
10
+ */
11
+ #include <errno.h>
12
+ #include <stdio.h>
13
+ #include <unistd.h>
14
+ #include <stdlib.h>
15
+ #include <string.h>
16
+ #include <time.h>
17
+ #include <poll.h>
18
+
19
+ #include "liburing.h"
20
+
21
+ enum {
22
+ multi,
23
+ single,
24
+ nop,
25
+ cancel,
26
+ op_last,
27
+ };
28
+
29
+ struct sqe_info {
30
+ __u8 op;
31
+ unsigned flags;
32
+ };
33
+
34
+ #define max_entry 50
35
+
36
+ /*
37
+ * sqe_flags: combination of sqe flags
38
+ * multi_sqes: record the user_data/index of all the multishot sqes
39
+ * cnt: how many entries there are in multi_sqes
40
+ * we can leverage multi_sqes array for cancellation: we randomly pick
41
+ * up an entry in multi_sqes when form a cancellation sqe.
42
+ * multi_cap: limitation of number of multishot sqes
43
+ */
44
+ const unsigned sqe_flags[4] = {0, IOSQE_IO_LINK, IOSQE_IO_DRAIN,
45
+ IOSQE_IO_LINK | IOSQE_IO_DRAIN};
46
+ int multi_sqes[max_entry], cnt = 0;
47
+ int multi_cap = max_entry / 5;
48
+
49
+ int write_pipe(int pipe, char *str)
50
+ {
51
+ int ret;
52
+ do {
53
+ errno = 0;
54
+ ret = write(pipe, str, 3);
55
+ } while (ret == -1 && errno == EINTR);
56
+ return ret;
57
+ }
58
+
59
+ void read_pipe(int pipe)
60
+ {
61
+ char str[4] = {0};
62
+ int ret;
63
+
64
+ ret = read(pipe, &str, 3);
65
+ if (ret < 0)
66
+ perror("read");
67
+ }
68
+
69
+ int trigger_event(int p[])
70
+ {
71
+ int ret;
72
+ if ((ret = write_pipe(p[1], "foo")) != 3) {
73
+ fprintf(stderr, "bad write return %d\n", ret);
74
+ return 1;
75
+ }
76
+ read_pipe(p[0]);
77
+ return 0;
78
+ }
79
+
80
+ void io_uring_sqe_prep(int op, struct io_uring_sqe *sqe, unsigned sqe_flags, int arg)
81
+ {
82
+ switch (op) {
83
+ case multi:
84
+ io_uring_prep_poll_add(sqe, arg, POLLIN);
85
+ sqe->len |= IORING_POLL_ADD_MULTI;
86
+ break;
87
+ case single:
88
+ io_uring_prep_poll_add(sqe, arg, POLLIN);
89
+ break;
90
+ case nop:
91
+ io_uring_prep_nop(sqe);
92
+ break;
93
+ case cancel:
94
+ io_uring_prep_poll_remove(sqe, arg);
95
+ break;
96
+ }
97
+ sqe->flags = sqe_flags;
98
+ }
99
+
100
+ __u8 generate_flags(int sqe_op)
101
+ {
102
+ __u8 flags = 0;
103
+ /*
104
+ * drain sqe must be put after multishot sqes cancelled
105
+ */
106
+ do {
107
+ flags = sqe_flags[rand() % 4];
108
+ } while ((flags & IOSQE_IO_DRAIN) && cnt);
109
+
110
+ /*
111
+ * cancel req cannot have drain or link flag
112
+ */
113
+ if (sqe_op == cancel) {
114
+ flags &= ~(IOSQE_IO_DRAIN | IOSQE_IO_LINK);
115
+ }
116
+ /*
117
+ * avoid below case:
118
+ * sqe0(multishot, link)->sqe1(nop, link)->sqe2(nop)->sqe3(cancel_sqe0)
119
+ * sqe3 may excute before sqe0 so that sqe0 isn't cancelled
120
+ */
121
+ if (sqe_op == multi)
122
+ flags &= ~IOSQE_IO_LINK;
123
+
124
+ return flags;
125
+
126
+ }
127
+
128
+ /*
129
+ * function to generate opcode of a sqe
130
+ * several restrictions here:
131
+ * - cancel all the previous multishot sqes as soon as possible when
132
+ * we reach high watermark.
133
+ * - ensure there is some multishot sqe when generating a cancel sqe
134
+ * - ensure a cancel/multshot sqe is not in a linkchain
135
+ * - ensure number of multishot sqes doesn't exceed multi_cap
136
+ * - don't generate multishot sqes after high watermark
137
+ */
138
+ int generate_opcode(int i, int pre_flags)
139
+ {
140
+ int sqe_op;
141
+ int high_watermark = max_entry - max_entry / 5;
142
+ bool retry0 = false, retry1 = false, retry2 = false;
143
+
144
+ if ((i >= high_watermark) && cnt) {
145
+ sqe_op = cancel;
146
+ } else {
147
+ do {
148
+ sqe_op = rand() % op_last;
149
+ retry0 = (sqe_op == cancel) && (!cnt || (pre_flags & IOSQE_IO_LINK));
150
+ retry1 = (sqe_op == multi) && ((multi_cap - 1 < 0) || i >= high_watermark);
151
+ retry2 = (sqe_op == multi) && (pre_flags & IOSQE_IO_LINK);
152
+ } while (retry0 || retry1 || retry2);
153
+ }
154
+
155
+ if (sqe_op == multi)
156
+ multi_cap--;
157
+ return sqe_op;
158
+ }
159
+
160
+ static inline void add_multishot_sqe(int index)
161
+ {
162
+ multi_sqes[cnt++] = index;
163
+ }
164
+
165
+ int remove_multishot_sqe()
166
+ {
167
+ int ret;
168
+
169
+ int rem_index = rand() % cnt;
170
+ ret = multi_sqes[rem_index];
171
+ multi_sqes[rem_index] = multi_sqes[cnt - 1];
172
+ cnt--;
173
+
174
+ return ret;
175
+ }
176
+
177
+ static int test_generic_drain(struct io_uring *ring)
178
+ {
179
+ struct io_uring_cqe *cqe;
180
+ struct io_uring_sqe *sqe[max_entry];
181
+ struct sqe_info si[max_entry];
182
+ int cqe_data[max_entry << 1], cqe_res[max_entry << 1];
183
+ int i, j, ret, arg = 0;
184
+ int pipes[max_entry][2];
185
+ int pre_flags = 0;
186
+
187
+ for (i = 0; i < max_entry; i++) {
188
+ if (pipe(pipes[i]) != 0) {
189
+ perror("pipe");
190
+ return 1;
191
+ }
192
+ }
193
+
194
+ srand((unsigned)time(NULL));
195
+ for (i = 0; i < max_entry; i++) {
196
+ sqe[i] = io_uring_get_sqe(ring);
197
+ if (!sqe[i]) {
198
+ printf("get sqe failed\n");
199
+ goto err;
200
+ }
201
+
202
+ int sqe_op = generate_opcode(i, pre_flags);
203
+ __u8 flags = generate_flags(sqe_op);
204
+
205
+ if (sqe_op == cancel)
206
+ arg = remove_multishot_sqe();
207
+ if (sqe_op == multi || sqe_op == single)
208
+ arg = pipes[i][0];
209
+ io_uring_sqe_prep(sqe_op, sqe[i], flags, arg);
210
+ sqe[i]->user_data = i;
211
+ si[i].op = sqe_op;
212
+ si[i].flags = flags;
213
+ pre_flags = flags;
214
+ if (sqe_op == multi)
215
+ add_multishot_sqe(i);
216
+ }
217
+
218
+ ret = io_uring_submit(ring);
219
+ if (ret < 0) {
220
+ printf("sqe submit failed\n");
221
+ goto err;
222
+ } else if (ret < max_entry) {
223
+ printf("Submitted only %d\n", ret);
224
+ goto err;
225
+ }
226
+
227
+ sleep(4);
228
+ // TODO: randomize event triggerring order
229
+ for (i = 0; i < max_entry; i++) {
230
+ if (si[i].op != multi && si[i].op != single)
231
+ continue;
232
+
233
+ if (trigger_event(pipes[i]))
234
+ goto err;
235
+ }
236
+ sleep(5);
237
+ i = 0;
238
+ while (!io_uring_peek_cqe(ring, &cqe)) {
239
+ cqe_data[i] = cqe->user_data;
240
+ cqe_res[i++] = cqe->res;
241
+ io_uring_cqe_seen(ring, cqe);
242
+ }
243
+
244
+ /*
245
+ * compl_bits is a bit map to record completions.
246
+ * eg. sqe[0], sqe[1], sqe[2] fully completed
247
+ * then compl_bits is 000...00111b
248
+ *
249
+ */
250
+ unsigned long long compl_bits = 0;
251
+ for (j = 0; j < i; j++) {
252
+ int index = cqe_data[j];
253
+ if ((si[index].flags & IOSQE_IO_DRAIN) && index) {
254
+ if ((~compl_bits) & ((1ULL << index) - 1)) {
255
+ printf("drain failed\n");
256
+ goto err;
257
+ }
258
+ }
259
+ /*
260
+ * for multishot sqes, record them only when it is cancelled
261
+ */
262
+ if ((si[index].op != multi) || (cqe_res[j] == -ECANCELED))
263
+ compl_bits |= (1ULL << index);
264
+ }
265
+
266
+ return 0;
267
+ err:
268
+ return 1;
269
+ }
270
+
271
+ static int test_simple_drain(struct io_uring *ring)
272
+ {
273
+ struct io_uring_cqe *cqe;
274
+ struct io_uring_sqe *sqe[2];
275
+ int i, ret;
276
+ int pipe1[2], pipe2[2];
277
+
278
+ if (pipe(pipe1) != 0 || pipe(pipe2) != 0) {
279
+ perror("pipe");
280
+ return 1;
281
+ }
282
+
283
+ for (i = 0; i < 2; i++) {
284
+ sqe[i] = io_uring_get_sqe(ring);
285
+ if (!sqe[i]) {
286
+ printf("get sqe failed\n");
287
+ goto err;
288
+ }
289
+ }
290
+
291
+ io_uring_prep_poll_multishot(sqe[0], pipe1[0], POLLIN);
292
+ sqe[0]->user_data = 0;
293
+
294
+ io_uring_prep_poll_add(sqe[1], pipe2[0], POLLIN);
295
+ sqe[1]->user_data = 1;
296
+
297
+ ret = io_uring_submit(ring);
298
+ if (ret < 0) {
299
+ printf("sqe submit failed\n");
300
+ goto err;
301
+ } else if (ret < 2) {
302
+ printf("Submitted only %d\n", ret);
303
+ goto err;
304
+ }
305
+
306
+ for (i = 0; i < 2; i++) {
307
+ if (trigger_event(pipe1))
308
+ goto err;
309
+ }
310
+ if (trigger_event(pipe2))
311
+ goto err;
312
+
313
+ for (i = 0; i < 2; i++) {
314
+ sqe[i] = io_uring_get_sqe(ring);
315
+ if (!sqe[i]) {
316
+ printf("get sqe failed\n");
317
+ goto err;
318
+ }
319
+ }
320
+
321
+ io_uring_prep_poll_remove(sqe[0], 0);
322
+ sqe[0]->user_data = 2;
323
+
324
+ io_uring_prep_nop(sqe[1]);
325
+ sqe[1]->flags |= IOSQE_IO_DRAIN;
326
+ sqe[1]->user_data = 3;
327
+
328
+ ret = io_uring_submit(ring);
329
+ if (ret < 0) {
330
+ printf("sqe submit failed\n");
331
+ goto err;
332
+ } else if (ret < 2) {
333
+ printf("Submitted only %d\n", ret);
334
+ goto err;
335
+ }
336
+
337
+ for (i = 0; i < 6; i++) {
338
+ ret = io_uring_wait_cqe(ring, &cqe);
339
+ if (ret < 0) {
340
+ printf("wait completion %d\n", ret);
341
+ goto err;
342
+ }
343
+ if ((i == 5) && (cqe->user_data != 3))
344
+ goto err;
345
+ io_uring_cqe_seen(ring, cqe);
346
+ }
347
+
348
+ close(pipe1[0]);
349
+ close(pipe1[1]);
350
+ close(pipe2[0]);
351
+ close(pipe2[1]);
352
+ return 0;
353
+ err:
354
+ return 1;
355
+ }
356
+
357
+ int main(int argc, char *argv[])
358
+ {
359
+ struct io_uring ring;
360
+ int i, ret;
361
+
362
+ if (argc > 1)
363
+ return 0;
364
+
365
+ ret = io_uring_queue_init(1024, &ring, 0);
366
+ if (ret) {
367
+ printf("ring setup failed\n");
368
+ return 1;
369
+ }
370
+
371
+ for (i = 0; i < 5; i++) {
372
+ ret = test_simple_drain(&ring);
373
+ if (ret) {
374
+ fprintf(stderr, "test_simple_drain failed\n");
375
+ break;
376
+ }
377
+ }
378
+
379
+ for (i = 0; i < 5; i++) {
380
+ ret = test_generic_drain(&ring);
381
+ if (ret) {
382
+ fprintf(stderr, "test_generic_drain failed\n");
383
+ break;
384
+ }
385
+ }
386
+ return ret;
387
+ }
@@ -0,0 +1,99 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Description: exercise full filling of SQ and CQ ring
4
+ *
5
+ */
6
+ #include <errno.h>
7
+ #include <stdio.h>
8
+ #include <unistd.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+ #include <fcntl.h>
12
+
13
+ #include "liburing.h"
14
+
15
+ #define MAX_ENTRIES 32768
16
+
17
+ static int fill_nops(struct io_uring *ring)
18
+ {
19
+ struct io_uring_sqe *sqe;
20
+ int filled = 0;
21
+
22
+ do {
23
+ sqe = io_uring_get_sqe(ring);
24
+ if (!sqe)
25
+ break;
26
+
27
+ io_uring_prep_nop(sqe);
28
+ filled++;
29
+ } while (1);
30
+
31
+ return filled;
32
+ }
33
+
34
+ static int test_nops(struct io_uring *ring)
35
+ {
36
+ struct io_uring_cqe *cqe;
37
+ int ret, nr, total = 0, i;
38
+
39
+ nr = fill_nops(ring);
40
+
41
+ ret = io_uring_submit(ring);
42
+ if (ret != nr) {
43
+ fprintf(stderr, "submit %d, wanted %d\n", ret, nr);
44
+ goto err;
45
+ }
46
+ total += ret;
47
+
48
+ nr = fill_nops(ring);
49
+
50
+ ret = io_uring_submit(ring);
51
+ if (ret != nr) {
52
+ fprintf(stderr, "submit %d, wanted %d\n", ret, nr);
53
+ goto err;
54
+ }
55
+ total += ret;
56
+
57
+ for (i = 0; i < total; i++) {
58
+ ret = io_uring_wait_cqe(ring, &cqe);
59
+ if (ret < 0) {
60
+ fprintf(stderr, "wait completion %d\n", ret);
61
+ goto err;
62
+ }
63
+
64
+ io_uring_cqe_seen(ring, cqe);
65
+ }
66
+ return 0;
67
+ err:
68
+ return 1;
69
+ }
70
+
71
+ int main(int argc, char *argv[])
72
+ {
73
+ struct io_uring ring;
74
+ int ret, depth;
75
+
76
+ if (argc > 1)
77
+ return 0;
78
+
79
+ depth = 1;
80
+ while (depth <= MAX_ENTRIES) {
81
+ ret = io_uring_queue_init(depth, &ring, 0);
82
+ if (ret) {
83
+ if (ret == -ENOMEM)
84
+ break;
85
+ fprintf(stderr, "ring setup failed: %d\n", ret);
86
+ return 1;
87
+ }
88
+
89
+ ret = test_nops(&ring);
90
+ if (ret) {
91
+ fprintf(stderr, "test_single_nop failed\n");
92
+ return ret;
93
+ }
94
+ depth <<= 1;
95
+ io_uring_queue_exit(&ring);
96
+ }
97
+
98
+ return 0;
99
+ }
@@ -0,0 +1,115 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Description: run various nop tests
4
+ *
5
+ */
6
+ #include <errno.h>
7
+ #include <stdio.h>
8
+ #include <unistd.h>
9
+ #include <stdlib.h>
10
+ #include <string.h>
11
+ #include <fcntl.h>
12
+
13
+ #include "liburing.h"
14
+
15
+ static int test_single_nop(struct io_uring *ring)
16
+ {
17
+ struct io_uring_cqe *cqe;
18
+ struct io_uring_sqe *sqe;
19
+ int ret;
20
+
21
+ sqe = io_uring_get_sqe(ring);
22
+ if (!sqe) {
23
+ fprintf(stderr, "get sqe failed\n");
24
+ goto err;
25
+ }
26
+
27
+ io_uring_prep_nop(sqe);
28
+
29
+ ret = io_uring_submit(ring);
30
+ if (ret <= 0) {
31
+ fprintf(stderr, "sqe submit failed: %d\n", ret);
32
+ goto err;
33
+ }
34
+
35
+ ret = io_uring_wait_cqe(ring, &cqe);
36
+ if (ret < 0) {
37
+ fprintf(stderr, "wait completion %d\n", ret);
38
+ goto err;
39
+ }
40
+
41
+ io_uring_cqe_seen(ring, cqe);
42
+ return 0;
43
+ err:
44
+ return 1;
45
+ }
46
+
47
+ static int test_barrier_nop(struct io_uring *ring)
48
+ {
49
+ struct io_uring_cqe *cqe;
50
+ struct io_uring_sqe *sqe;
51
+ int ret, i;
52
+
53
+ for (i = 0; i < 8; i++) {
54
+ sqe = io_uring_get_sqe(ring);
55
+ if (!sqe) {
56
+ fprintf(stderr, "get sqe failed\n");
57
+ goto err;
58
+ }
59
+
60
+ io_uring_prep_nop(sqe);
61
+ if (i == 4)
62
+ sqe->flags = IOSQE_IO_DRAIN;
63
+ }
64
+
65
+ ret = io_uring_submit(ring);
66
+ if (ret < 0) {
67
+ fprintf(stderr, "sqe submit failed: %d\n", ret);
68
+ goto err;
69
+ } else if (ret < 8) {
70
+ fprintf(stderr, "Submitted only %d\n", ret);
71
+ goto err;
72
+ }
73
+
74
+ for (i = 0; i < 8; i++) {
75
+ ret = io_uring_wait_cqe(ring, &cqe);
76
+ if (ret < 0) {
77
+ fprintf(stderr, "wait completion %d\n", ret);
78
+ goto err;
79
+ }
80
+ io_uring_cqe_seen(ring, cqe);
81
+ }
82
+
83
+ return 0;
84
+ err:
85
+ return 1;
86
+ }
87
+
88
+ int main(int argc, char *argv[])
89
+ {
90
+ struct io_uring ring;
91
+ int ret;
92
+
93
+ if (argc > 1)
94
+ return 0;
95
+
96
+ ret = io_uring_queue_init(8, &ring, 0);
97
+ if (ret) {
98
+ fprintf(stderr, "ring setup failed: %d\n", ret);
99
+ return 1;
100
+ }
101
+
102
+ ret = test_single_nop(&ring);
103
+ if (ret) {
104
+ fprintf(stderr, "test_single_nop failed\n");
105
+ return ret;
106
+ }
107
+
108
+ ret = test_barrier_nop(&ring);
109
+ if (ret) {
110
+ fprintf(stderr, "test_barrier_nop failed\n");
111
+ return ret;
112
+ }
113
+
114
+ return 0;
115
+ }