io_uring-cp.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Simple test program that demonstrates a file copy through io_uring. This
  4. * uses the API exposed by liburing.
  5. *
  6. * Copyright (C) 2018-2019 Jens Axboe
  7. */
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <assert.h>
  14. #include <errno.h>
  15. #include <inttypes.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <sys/ioctl.h>
  19. #include "liburing.h"
  20. #define QD 64
  21. #define BS (32*1024)
  22. static int infd, outfd;
  23. struct io_data {
  24. int read;
  25. off_t first_offset, offset;
  26. size_t first_len;
  27. struct iovec iov;
  28. };
  29. static int setup_context(unsigned entries, struct io_uring *ring)
  30. {
  31. int ret;
  32. ret = io_uring_queue_init(entries, ring, 0);
  33. if (ret < 0) {
  34. fprintf(stderr, "queue_init: %s\n", strerror(-ret));
  35. return -1;
  36. }
  37. return 0;
  38. }
  39. static int get_file_size(int fd, off_t *size)
  40. {
  41. struct stat st;
  42. if (fstat(fd, &st) < 0)
  43. return -1;
  44. if (S_ISREG(st.st_mode)) {
  45. *size = st.st_size;
  46. return 0;
  47. } else if (S_ISBLK(st.st_mode)) {
  48. unsigned long long bytes;
  49. if (ioctl(fd, BLKGETSIZE64, &bytes) != 0)
  50. return -1;
  51. *size = bytes;
  52. return 0;
  53. }
  54. return -1;
  55. }
  56. static void queue_prepped(struct io_uring *ring, struct io_data *data)
  57. {
  58. struct io_uring_sqe *sqe;
  59. sqe = io_uring_get_sqe(ring);
  60. assert(sqe);
  61. if (data->read)
  62. io_uring_prep_readv(sqe, infd, &data->iov, 1, data->offset);
  63. else
  64. io_uring_prep_writev(sqe, outfd, &data->iov, 1, data->offset);
  65. io_uring_sqe_set_data(sqe, data);
  66. }
  67. static int queue_read(struct io_uring *ring, off_t size, off_t offset)
  68. {
  69. struct io_uring_sqe *sqe;
  70. struct io_data *data;
  71. data = malloc(size + sizeof(*data));
  72. if (!data)
  73. return 1;
  74. sqe = io_uring_get_sqe(ring);
  75. if (!sqe) {
  76. free(data);
  77. return 1;
  78. }
  79. data->read = 1;
  80. data->offset = data->first_offset = offset;
  81. data->iov.iov_base = data + 1;
  82. data->iov.iov_len = size;
  83. data->first_len = size;
  84. io_uring_prep_readv(sqe, infd, &data->iov, 1, offset);
  85. io_uring_sqe_set_data(sqe, data);
  86. return 0;
  87. }
  88. static void queue_write(struct io_uring *ring, struct io_data *data)
  89. {
  90. data->read = 0;
  91. data->offset = data->first_offset;
  92. data->iov.iov_base = data + 1;
  93. data->iov.iov_len = data->first_len;
  94. queue_prepped(ring, data);
  95. io_uring_submit(ring);
  96. }
  97. static int copy_file(struct io_uring *ring, off_t insize)
  98. {
  99. unsigned long reads, writes;
  100. struct io_uring_cqe *cqe;
  101. off_t write_left, offset;
  102. int ret;
  103. write_left = insize;
  104. writes = reads = offset = 0;
  105. while (insize || write_left) {
  106. unsigned long had_reads;
  107. int got_comp;
  108. /*
  109. * Queue up as many reads as we can
  110. */
  111. had_reads = reads;
  112. while (insize) {
  113. off_t this_size = insize;
  114. if (reads + writes >= QD)
  115. break;
  116. if (this_size > BS)
  117. this_size = BS;
  118. else if (!this_size)
  119. break;
  120. if (queue_read(ring, this_size, offset))
  121. break;
  122. insize -= this_size;
  123. offset += this_size;
  124. reads++;
  125. }
  126. if (had_reads != reads) {
  127. ret = io_uring_submit(ring);
  128. if (ret < 0) {
  129. fprintf(stderr, "io_uring_submit: %s\n", strerror(-ret));
  130. break;
  131. }
  132. }
  133. /*
  134. * Queue is full at this point. Find at least one completion.
  135. */
  136. got_comp = 0;
  137. while (write_left) {
  138. struct io_data *data;
  139. if (!got_comp) {
  140. ret = io_uring_wait_cqe(ring, &cqe);
  141. got_comp = 1;
  142. } else
  143. ret = io_uring_peek_cqe(ring, &cqe);
  144. if (ret < 0) {
  145. fprintf(stderr, "io_uring_peek_cqe: %s\n",
  146. strerror(-ret));
  147. return 1;
  148. }
  149. if (!cqe)
  150. break;
  151. data = io_uring_cqe_get_data(cqe);
  152. if (cqe->res < 0) {
  153. if (cqe->res == -EAGAIN) {
  154. queue_prepped(ring, data);
  155. io_uring_cqe_seen(ring, cqe);
  156. continue;
  157. }
  158. fprintf(stderr, "cqe failed: %s\n",
  159. strerror(-cqe->res));
  160. return 1;
  161. } else if ((size_t) cqe->res != data->iov.iov_len) {
  162. /* Short read/write, adjust and requeue */
  163. data->iov.iov_base += cqe->res;
  164. data->iov.iov_len -= cqe->res;
  165. data->offset += cqe->res;
  166. queue_prepped(ring, data);
  167. io_uring_cqe_seen(ring, cqe);
  168. continue;
  169. }
  170. /*
  171. * All done. if write, nothing else to do. if read,
  172. * queue up corresponding write.
  173. */
  174. if (data->read) {
  175. queue_write(ring, data);
  176. write_left -= data->first_len;
  177. reads--;
  178. writes++;
  179. } else {
  180. free(data);
  181. writes--;
  182. }
  183. io_uring_cqe_seen(ring, cqe);
  184. }
  185. }
  186. return 0;
  187. }
  188. int main(int argc, char *argv[])
  189. {
  190. struct io_uring ring;
  191. off_t insize;
  192. int ret;
  193. if (argc < 3) {
  194. printf("%s: infile outfile\n", argv[0]);
  195. return 1;
  196. }
  197. infd = open(argv[1], O_RDONLY);
  198. if (infd < 0) {
  199. perror("open infile");
  200. return 1;
  201. }
  202. outfd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644);
  203. if (outfd < 0) {
  204. perror("open outfile");
  205. return 1;
  206. }
  207. if (setup_context(QD, &ring))
  208. return 1;
  209. if (get_file_size(infd, &insize))
  210. return 1;
  211. ret = copy_file(&ring, insize);
  212. close(infd);
  213. close(outfd);
  214. io_uring_queue_exit(&ring);
  215. return ret;
  216. }