Commit d11862a3 authored by Florian Fischer's avatar Florian Fischer
Browse files

move duplicated io_uring code into io_uring.h

parent 0978dafc
#pragma once
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
extern size_t warmup, iterations;
......@@ -4,26 +4,18 @@
#include <stdlib.h>
#include "common.h"
#include "io_uring.h"
#include "stopwatch.h"
struct io_uring ring;
void init(__attribute__((unused)) int fd) {
int res = io_uring_queue_init(16, &ring, IORING_SETUP_SQPOLL);
if (res < 0) {
errno = res;
err(EXIT_FAILURE, "io_uring_setup failed");
}
}
void init(__attribute__((unused)) int fd) { io_uring_init_sqpoll(&ring); }
void do_write(int fd, const void *buf, size_t count) {
start_watch();
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
while (unlikely(!sqe)) {
sqe = io_uring_get_sqe(&ring);
}
struct io_uring_sqe *sqe = sqpoll_ring_get_sqe(&ring);
io_uring_prep_write(sqe, fd, buf, count, 0);
start_watch();
int res = io_uring_submit(&ring);
struct io_uring_cqe *cqe;
......@@ -32,11 +24,7 @@ void do_write(int fd, const void *buf, size_t count) {
stop_watch();
if (res < 0) {
err(EXIT_FAILURE, "io_submit failed");
}
if (res < 0) err(EXIT_FAILURE, "io_submit failed");
if (cqe->res < 0) {
err(EXIT_FAILURE, "write request failed");
}
if (cqe->res < 0) err(EXIT_FAILURE, "write request failed");
}
......@@ -4,29 +4,19 @@
#include <stdlib.h>
#include "common.h"
#include "io_uring.h"
#include "stopwatch.h"
struct io_uring ring;
void init(__attribute__((unused)) int fd) {
int res = io_uring_queue_init(16, &ring, IORING_SETUP_SQPOLL);
if (res < 0) {
errno = res;
err(EXIT_FAILURE, "io_uring_setup failed");
}
}
void init(__attribute__((unused)) int fd) { io_uring_init_sqpoll(&ring); }
void do_write(int fd, const void *buf, size_t count) {
start_watch();
struct io_uring_sqe *sqe = io_uring_get_sqe(&ring);
while (unlikely(!sqe)) {
sqe = io_uring_get_sqe(&ring);
}
struct io_uring_sqe *sqe = sqpoll_ring_get_sqe(&ring);
io_uring_prep_write(sqe, fd, buf, count, 0);
start_watch();
int res = io_uring_submit_and_wait(&ring, 1);
stop_watch();
if (res < 0) {
......@@ -35,11 +25,8 @@ void do_write(int fd, const void *buf, size_t count) {
struct io_uring_cqe *cqe;
res = io_uring_peek_cqe(&ring, &cqe);
if (res < 0) {
err(EXIT_FAILURE, "io_uring_peek_cqe failed");
}
if (cqe->res < 0) {
err(EXIT_FAILURE, "write request failed");
}
if (res < 0) err(EXIT_FAILURE, "io_uring_peek_cqe failed");
if (cqe->res < 0) err(EXIT_FAILURE, "write request failed");
}
#pragma once
#include <err.h>
#include <liburing.h>
#include "common.h"
#define GET_SQE_ATTEMPTS 1000000
struct io_uring_sqe* sqpoll_ring_get_sqe(struct io_uring* ring) {
struct io_uring_sqe* sqe;
size_t attempts = 0;
for (;;) {
sqe = io_uring_get_sqe(ring);
if (likely(sqe)) return sqe;
++attempts;
if (attempts < GET_SQE_ATTEMPTS) continue;
errx(EXIT_FAILURE, "failed to get sqe after %d", GET_SQE_ATTEMPTS);
}
}
#define SQPOLL_RING_ENTRIES 16
void io_uring_init_sqpoll(struct io_uring* ring) {
int res = io_uring_queue_init(SQPOLL_RING_ENTRIES, ring, IORING_SETUP_SQPOLL);
if (res < 0) {
errno = res;
err(EXIT_FAILURE, "io_uring_setup failed");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment