|
| 1 | +#include <fcntl.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | +#include <sys/stat.h> |
| 5 | +#include <sys/ioctl.h> |
| 6 | +#include <liburing.h> |
| 7 | +#include <stdlib.h> |
| 8 | + |
| 9 | +#define QUEUE_DEPTH 1 |
| 10 | +#define BLOCK_SZ 1024 |
| 11 | + |
| 12 | +struct file_info { |
| 13 | + off_t file_sz; |
| 14 | + struct iovec iovecs[]; /* Referred by readv/writev */ |
| 15 | +}; |
| 16 | + |
| 17 | +/* |
| 18 | +* Returns the size of the file whose open file descriptor is passed in. |
| 19 | +* Properly handles regular file and block devices as well. Pretty. |
| 20 | +* */ |
| 21 | + |
| 22 | +off_t get_file_size(int fd) { |
| 23 | + struct stat st; |
| 24 | + |
| 25 | + if(fstat(fd, &st) < 0) { |
| 26 | + perror("fstat"); |
| 27 | + return -1; |
| 28 | + } |
| 29 | + if (S_ISBLK(st.st_mode)) { |
| 30 | + unsigned long long bytes; |
| 31 | + if (ioctl(fd, BLKGETSIZE64, &bytes) != 0) { |
| 32 | + perror("ioctl"); |
| 33 | + return -1; |
| 34 | + } |
| 35 | + return bytes; |
| 36 | + } else if (S_ISREG(st.st_mode)) |
| 37 | + return st.st_size; |
| 38 | + |
| 39 | + return -1; |
| 40 | +} |
| 41 | + |
| 42 | +/* |
| 43 | + * Output a string of characters of len length to stdout. |
| 44 | + * We use buffered output here to be efficient, |
| 45 | + * since we need to output character-by-character. |
| 46 | + * */ |
| 47 | +void output_to_console(char *buf, int len) { |
| 48 | + while (len--) { |
| 49 | + fputc(*buf++, stdout); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/* |
| 54 | + * Wait for a completion to be available, fetch the data from |
| 55 | + * the readv operation and print it to the console. |
| 56 | + * */ |
| 57 | + |
| 58 | +int get_completion_and_print(struct io_uring *ring) { |
| 59 | + struct io_uring_cqe *cqe; |
| 60 | + int ret = io_uring_wait_cqe(ring, &cqe); |
| 61 | + if (ret < 0) { |
| 62 | + perror("io_uring_wait_cqe"); |
| 63 | + return 1; |
| 64 | + } |
| 65 | + if (cqe->res < 0) { |
| 66 | + fprintf(stderr, "Async readv failed.\n"); |
| 67 | + return 1; |
| 68 | + } |
| 69 | + struct file_info *fi = io_uring_cqe_get_data(cqe); |
| 70 | + int blocks = (int) fi->file_sz / BLOCK_SZ; |
| 71 | + if (fi->file_sz % BLOCK_SZ) blocks++; |
| 72 | + for (int i = 0; i < blocks; i ++) |
| 73 | + output_to_console(fi->iovecs[i].iov_base, fi->iovecs[i].iov_len); |
| 74 | + |
| 75 | + io_uring_cqe_seen(ring, cqe); |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +/* |
| 80 | + * Submit the readv request via liburing |
| 81 | + * */ |
| 82 | + |
| 83 | +int submit_read_request(char *file_path, struct io_uring *ring) { |
| 84 | + int file_fd = open(file_path, O_RDONLY); |
| 85 | + if (file_fd < 0) { |
| 86 | + perror("open"); |
| 87 | + return 1; |
| 88 | + } |
| 89 | + off_t file_sz = get_file_size(file_fd); |
| 90 | + off_t bytes_remaining = file_sz; |
| 91 | + off_t offset = 0; |
| 92 | + int current_block = 0; |
| 93 | + int blocks = (int) file_sz / BLOCK_SZ; |
| 94 | + if (file_sz % BLOCK_SZ) blocks++; |
| 95 | + struct file_info *fi = malloc(sizeof(*fi) + |
| 96 | + (sizeof(struct iovec) * blocks)); |
| 97 | + |
| 98 | + /* |
| 99 | + * For each block of the file we need to read, we allocate an iovec struct |
| 100 | + * which is indexed into the iovecs array. This array is passed in as part |
| 101 | + * of the submission. If you don't understand this, then you need to look |
| 102 | + * up how the readv() and writev() system calls work. |
| 103 | + * */ |
| 104 | + while (bytes_remaining) { |
| 105 | + off_t bytes_to_read = bytes_remaining; |
| 106 | + if (bytes_to_read > BLOCK_SZ) |
| 107 | + bytes_to_read = BLOCK_SZ; |
| 108 | + |
| 109 | + offset += bytes_to_read; |
| 110 | + fi->iovecs[current_block].iov_len = bytes_to_read; |
| 111 | + |
| 112 | + void *buf; |
| 113 | + if( posix_memalign(&buf, BLOCK_SZ, BLOCK_SZ)) { |
| 114 | + perror("posix_memalign"); |
| 115 | + return 1; |
| 116 | + } |
| 117 | + fi->iovecs[current_block].iov_base = buf; |
| 118 | + |
| 119 | + current_block++; |
| 120 | + bytes_remaining -= bytes_to_read; |
| 121 | + } |
| 122 | + fi->file_sz = file_sz; |
| 123 | + |
| 124 | + /* Get an SQE */ |
| 125 | + struct io_uring_sqe *sqe = io_uring_get_sqe(ring); |
| 126 | + /* Setup a readv operation */ |
| 127 | + io_uring_prep_readv(sqe, file_fd, fi->iovecs, blocks, 0); |
| 128 | + /* Set user data */ |
| 129 | + io_uring_sqe_set_data(sqe, fi); |
| 130 | + /* Finally, submit the request */ |
| 131 | + io_uring_submit(ring); |
| 132 | + |
| 133 | + return 0; |
| 134 | +} |
| 135 | + |
| 136 | +int main(int argc, char *argv[]) { |
| 137 | + struct io_uring ring; |
| 138 | + |
| 139 | + if (argc < 2) { |
| 140 | + fprintf(stderr, "Usage: %s [file name] <[file name] ...>\n", |
| 141 | + argv[0]); |
| 142 | + return 1; |
| 143 | + } |
| 144 | + |
| 145 | + /* Initialize io_uring */ |
| 146 | + io_uring_queue_init(QUEUE_DEPTH, &ring, 0); |
| 147 | + |
| 148 | + for (int i = 1; i < argc; i++) { |
| 149 | + int ret = submit_read_request(argv[i], &ring); |
| 150 | + if (ret) { |
| 151 | + fprintf(stderr, "Error reading file: %s\n", argv[i]); |
| 152 | + return 1; |
| 153 | + } |
| 154 | + get_completion_and_print(&ring); |
| 155 | + } |
| 156 | + |
| 157 | + /* Call the clean-up function. */ |
| 158 | + io_uring_queue_exit(&ring); |
| 159 | + return 0; |
| 160 | +} |
0 commit comments