Forked from PeterCorless/newstack-01-file-descriptors.c
Created
May 26, 2020 10:44
-
-
Save NobodyXu/f73d990d2fe72f02c57e6dd8cba323f8 to your computer and use it in GitHub Desktop.
newstack-io_uring-ebpf-examples
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
ssize_t read(int fd, void *buf, size_t count); | |
ssize_t write(int fd, const void *buf, size_t count); |
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
/* Describes what we need from a read */ | |
struct read_descriptor { | |
int fd; | |
char *buf; | |
unsigned long long pos; | |
unsigned long long size; | |
int result; | |
}; | |
/* | |
* given an array of struct read_descriptors, dispatch them in the | |
* io_uring | |
*/ | |
int | |
dispatch_reads(struct io_uring *ring, struct read_descriptor *descv, int nr_desc) | |
{ | |
int i; | |
for (i = 0; i < nr_desc; i++) { | |
struct io_uring_sqe *sqe; | |
struct read_descriptor *desc = &descv[i]; | |
sqe = io_uring_get_sqe(ring); | |
/* Each operation will have a special prep function */ | |
io_uring_prep_read(sqe, desc->fd, desc->buf, desc->size, desc->pos); | |
/* | |
* Whatever value we put here will be reflected when it is | |
* ready. This is how we know which read we are talking about | |
*/ | |
io_uring_sqe_set_data(sqe, desc); | |
} | |
/* For all of the reads above, there will be only one system call! */ | |
return io_uring_submit(ring); | |
} |
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
/* | |
* Consume reads that are available and returns how many were consumed. | |
* System calls issued: ZERO! | |
*/ | |
unsigned | |
consume_reads(struct io_uring *ring) | |
{ | |
unsigned completed; | |
unsigned head; | |
struct io_uring_cqe *cqe; | |
io_uring_for_each_cqe(ring, head, cqe) { | |
completed++; | |
/* Remember what we passed in io_uring_sqe_set_data?. It's here */ | |
struct read_descriptor *desc = (struct read_descriptor*)cqe->user_data; | |
desc->result = cqe->res; | |
} | |
io_uring_cq_advance(ring, completed); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment