Created
September 19, 2020 06:45
-
-
Save omegacoleman/9a4ae1a79383d723297c0f05f5f0d67e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/uio.h> | |
#include <sys/eventfd.h> | |
#include <unistd.h> | |
#include <liburing.h> | |
#include <iostream> | |
void exit_perror(const char *s) | |
{ | |
perror(s); | |
std::terminate(); | |
} | |
void read_from_evfd(io_uring *ring, int fd) | |
{ | |
int64_t *buf = new int64_t; | |
auto *iov = new iovec; | |
iov->iov_base = static_cast<void *>(buf); | |
iov->iov_len = sizeof(int64_t); | |
auto *sqe = io_uring_get_sqe(ring); | |
io_uring_prep_readv(sqe, fd, iov, 1, 0); | |
} | |
void sync_write_evfd(int fd) | |
{ | |
int64_t buf = 1; | |
if (write(fd, &buf, sizeof(int64_t)) < 0) | |
exit_perror("write"); | |
} | |
int main(void) | |
{ | |
int evfd1 = eventfd(0, 0); | |
int evfd2 = eventfd(0, 0); | |
io_uring ring; | |
io_uring_queue_init(1024, &ring, 0); | |
for (;;) | |
{ | |
read_from_evfd(&ring, evfd1); // first submit lotta blocking workload on one fd | |
read_from_evfd(&ring, evfd1); | |
read_from_evfd(&ring, evfd1); | |
read_from_evfd(&ring, evfd1); | |
read_from_evfd(&ring, evfd1); | |
read_from_evfd(&ring, evfd1); | |
read_from_evfd(&ring, evfd2); // then another workload that completes immediatly | |
sync_write_evfd(evfd2); | |
io_uring_submit_and_wait(&ring, 1); // never returns with 5.4.x, returns on 5.8 & linux-next | |
io_uring_cqe *cqe; | |
if (io_uring_peek_cqe(&ring, &cqe) < 0) | |
exit_perror("io_uring_peek_cqe"); | |
if (cqe->res < 0) | |
exit_perror("cqe->res"); | |
io_uring_cqe_seen(&ring, cqe); | |
std::cout << "#"; | |
std::cout.flush(); | |
} | |
io_uring_queue_exit(&ring); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment