Jens Axboe | e502435 | 2020-02-11 20:34:12 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Description: test massive amounts of poll with cancel |
| 4 | * |
| 5 | */ |
| 6 | #include <errno.h> |
| 7 | #include <stdio.h> |
| 8 | #include <unistd.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <inttypes.h> |
| 12 | #include <sys/poll.h> |
| 13 | #include <sys/wait.h> |
| 14 | #include <sys/signal.h> |
| 15 | |
| 16 | #include "liburing.h" |
| 17 | |
Jens Axboe | 84b89ca | 2019-12-04 20:53:01 -0700 | [diff] [blame] | 18 | #define POLL_COUNT 30000 |
| 19 | |
| 20 | static void *sqe_index[POLL_COUNT]; |
| 21 | |
Jens Axboe | 2454d63 | 2020-02-13 09:25:44 -0700 | [diff] [blame] | 22 | static int reap_events(struct io_uring *ring, unsigned nr_events, int nowait) |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 23 | { |
| 24 | struct io_uring_cqe *cqe; |
| 25 | int i, ret = 0; |
| 26 | |
| 27 | for (i = 0; i < nr_events; i++) { |
Jens Axboe | 2454d63 | 2020-02-13 09:25:44 -0700 | [diff] [blame] | 28 | if (!i && !nowait) |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 29 | ret = io_uring_wait_cqe(ring, &cqe); |
| 30 | else |
| 31 | ret = io_uring_peek_cqe(ring, &cqe); |
| 32 | if (ret) { |
| 33 | if (ret != -EAGAIN) |
| 34 | fprintf(stderr, "cqe peek failed: %d\n", ret); |
| 35 | break; |
| 36 | } |
| 37 | io_uring_cqe_seen(ring, cqe); |
| 38 | } |
| 39 | |
| 40 | return i ? i : ret; |
| 41 | } |
| 42 | |
| 43 | static int del_polls(struct io_uring *ring, int fd, int nr) |
| 44 | { |
Jens Axboe | 8f24d3c | 2019-12-02 08:33:07 -0700 | [diff] [blame] | 45 | int batch, i, ret; |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 46 | struct io_uring_sqe *sqe; |
| 47 | |
| 48 | while (nr) { |
| 49 | batch = 1024; |
| 50 | if (batch > nr) |
| 51 | batch = nr; |
| 52 | |
| 53 | for (i = 0; i < batch; i++) { |
Jens Axboe | 84b89ca | 2019-12-04 20:53:01 -0700 | [diff] [blame] | 54 | void *data; |
| 55 | |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 56 | sqe = io_uring_get_sqe(ring); |
Jens Axboe | 84b89ca | 2019-12-04 20:53:01 -0700 | [diff] [blame] | 57 | data = sqe_index[lrand48() % nr]; |
| 58 | io_uring_prep_poll_remove(sqe, data); |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | ret = io_uring_submit(ring); |
| 62 | if (ret != batch) { |
| 63 | fprintf(stderr, "%s: failed submit, %d\n", __FUNCTION__, ret); |
| 64 | return 1; |
| 65 | } |
| 66 | nr -= batch; |
Jens Axboe | 2454d63 | 2020-02-13 09:25:44 -0700 | [diff] [blame] | 67 | ret = reap_events(ring, 2 * batch, 0); |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 68 | } |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int add_polls(struct io_uring *ring, int fd, int nr) |
| 73 | { |
| 74 | int pending, batch, i, count, ret; |
| 75 | struct io_uring_sqe *sqe; |
| 76 | |
| 77 | pending = count = 0; |
| 78 | while (nr) { |
| 79 | batch = 1024; |
| 80 | if (batch > nr) |
| 81 | batch = nr; |
| 82 | |
| 83 | for (i = 0; i < batch; i++) { |
| 84 | sqe = io_uring_get_sqe(ring); |
| 85 | io_uring_prep_poll_add(sqe, fd, POLLIN); |
Jens Axboe | 84b89ca | 2019-12-04 20:53:01 -0700 | [diff] [blame] | 86 | sqe_index[count++] = sqe; |
| 87 | sqe->user_data = (unsigned long) sqe; |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | ret = io_uring_submit(ring); |
| 91 | if (ret != batch) { |
| 92 | fprintf(stderr, "%s: failed submit, %d\n", __FUNCTION__, ret); |
| 93 | return 1; |
| 94 | } |
| 95 | nr -= batch; |
| 96 | pending += batch; |
Jens Axboe | 2454d63 | 2020-02-13 09:25:44 -0700 | [diff] [blame] | 97 | reap_events(ring, batch, 1); |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | int main(int argc, char *argv[]) |
| 103 | { |
| 104 | struct io_uring ring; |
Jens Axboe | ccfe96e | 2021-02-07 09:19:14 -0700 | [diff] [blame] | 105 | struct io_uring_params p = { }; |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 106 | int pipe1[2]; |
| 107 | int ret; |
| 108 | |
Jens Axboe | a2141fc | 2020-05-19 17:36:19 -0600 | [diff] [blame] | 109 | if (argc > 1) |
| 110 | return 0; |
| 111 | |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 112 | if (pipe(pipe1) != 0) { |
Jens Axboe | a8dc497 | 2020-02-01 09:58:07 -0700 | [diff] [blame] | 113 | perror("pipe"); |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 114 | return 1; |
| 115 | } |
| 116 | |
Jens Axboe | ccfe96e | 2021-02-07 09:19:14 -0700 | [diff] [blame] | 117 | p.flags = IORING_SETUP_CQSIZE; |
| 118 | p.cq_entries = 16384; |
| 119 | ret = io_uring_queue_init_params(1024, &ring, &p); |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 120 | if (ret) { |
Jens Axboe | ccfe96e | 2021-02-07 09:19:14 -0700 | [diff] [blame] | 121 | if (ret == -EINVAL) { |
| 122 | fprintf(stdout, "No CQSIZE, trying without\n"); |
| 123 | ret = io_uring_queue_init(1024, &ring, 0); |
| 124 | if (ret) { |
| 125 | fprintf(stderr, "ring setup failed: %d\n", ret); |
| 126 | return 1; |
| 127 | } |
| 128 | } |
Jens Axboe | da869dc | 2019-11-14 17:24:19 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | add_polls(&ring, pipe1[0], 30000); |
| 132 | #if 0 |
| 133 | usleep(1000); |
| 134 | #endif |
| 135 | del_polls(&ring, pipe1[0], 30000); |
| 136 | |
| 137 | io_uring_queue_exit(&ring); |
| 138 | return 0; |
| 139 | } |