Skip to content

Commit a6df230

Browse files
authored
Separate io_uring into a separate library. AIO in io_uring mode uses new io_uring lib. (#9462)
1 parent d07a6ac commit a6df230

25 files changed

+726
-228
lines changed

CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ find_package(OpenSSL)
102102
# Check for IO faculties
103103
check_symbol_exists(epoll_create "sys/epoll.h" TS_USE_EPOLL)
104104
check_symbol_exists(kqueue "sys/event.h" TS_USE_KQUEUE)
105+
set(CMAKE_REQUIRED_LIBRARIES uring)
105106
check_symbol_exists(io_uring_queue_init "liburing.h" HAVE_IOURING)
106107
check_symbol_exists(getresuid unistd.h HAVE_GETRESUID)
107108
check_symbol_exists(getresgid unistd.h HAVE_GETRESGID)
@@ -111,9 +112,10 @@ check_symbol_exists(eventfd sys/eventfd.h HAVE_EVENTFD)
111112
check_symbol_exists(SSL_CTX_set_tlsext_ticket_key_cb openssl/ssl.h HAVE_SSL_CTX_SET_TLSEXT_TICKET_KEY_CB)
112113

113114
option(USE_IOURING "Use experimental io_uring (linux only)" 0)
114-
if (HAVE_IOURING AND USE_IOUIRNG)
115-
set(TS_USE_LINUX_IO_URING)
116-
endif(HAVE_IOURING AND USE_IOUIRNG)
115+
if (HAVE_IOURING AND USE_IOURING)
116+
message(Using io_uring)
117+
set(TS_USE_LINUX_IO_URING 1)
118+
endif(HAVE_IOURING AND USE_IOURING)
117119

118120
# Check ssl functionality
119121
list(APPEND CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,7 @@ AC_ARG_ENABLE([experimental-linux-io-uring],
17011701
[enable_linux_io_uring="${enableval}"],
17021702
[enable_linux_io_uring=no]
17031703
)
1704+
AM_CONDITIONAL([ENABLE_IO_URING], [ test "x${enable_linux_io_uring}" = "xyes" ])
17041705

17051706
AS_IF([test "x$enable_linux_io_uring" = "xyes"], [
17061707
URING_LIBS="-luring"
@@ -2331,6 +2332,7 @@ AC_SUBST([default_stack_size], [$with_default_stack_size])
23312332
#
23322333
iocore_include_dirs="\
23332334
-I\$(abs_top_srcdir)/iocore/eventsystem \
2335+
-I\$(abs_top_srcdir)/iocore/io_uring \
23342336
-I\$(abs_top_srcdir)/iocore/net \
23352337
-I\$(abs_top_srcdir)/iocore/net/quic \
23362338
-I\$(abs_top_srcdir)/iocore/aio \
@@ -2382,6 +2384,7 @@ AC_CONFIG_FILES([
23822384
include/tscore/ink_config.h
23832385
iocore/Makefile
23842386
iocore/aio/Makefile
2387+
iocore/io_uring/Makefile
23852388
iocore/cache/Makefile
23862389
iocore/dns/Makefile
23872390
iocore/eventsystem/Makefile

iocore/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818

1919
add_subdirectory(eventsystem)
20+
add_subdirectory(io_uring)
2021
add_subdirectory(net)
2122
add_subdirectory(aio)
2223
add_subdirectory(dns)
@@ -27,6 +28,7 @@ add_subdirectory(cache)
2728
set(IOCORE_INCLUDE_DIRS
2829
${CMAKE_SOURCE_DIR}/iocore/eventsystem
2930
${CMAKE_SOURCE_DIR}/iocore/dns
31+
${CMAKE_SOURCE_DIR}/iocore/io_uring
3032
${CMAKE_SOURCE_DIR}/iocore/aio
3133
${CMAKE_SOURCE_DIR}/iocore/net
3234
${CMAKE_SOURCE_DIR}/iocore/cache

iocore/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@
1717
# limitations under the License.
1818

1919
SUBDIRS = eventsystem net aio dns hostdb utils cache
20+
if ENABLE_IO_URING
21+
SUBDIRS += io_uring
22+
endif

iocore/aio/AIO.cc

Lines changed: 15 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,10 @@ RecInt aio_io_uring_wq_unbounded = 0;
7373
RecRawStatBlock *aio_rsb = nullptr;
7474
Continuation *aio_err_callbck = nullptr;
7575
// AIO Stats
76-
std::atomic<uint64_t> aio_num_read = 0;
77-
std::atomic<uint64_t> aio_bytes_read = 0;
78-
std::atomic<uint64_t> aio_num_write = 0;
79-
std::atomic<uint64_t> aio_bytes_written = 0;
80-
std::atomic<uint64_t> io_uring_submissions = 0;
81-
std::atomic<uint64_t> io_uring_completions = 0;
76+
std::atomic<uint64_t> aio_num_read = 0;
77+
std::atomic<uint64_t> aio_bytes_read = 0;
78+
std::atomic<uint64_t> aio_num_write = 0;
79+
std::atomic<uint64_t> aio_bytes_written = 0;
8280

8381
/*
8482
* Stats
@@ -534,87 +532,12 @@ AIOThreadInfo::aio_thread_main(AIOThreadInfo *thr_info)
534532

535533
#elif AIO_MODE == AIO_MODE_IO_URING
536534

537-
std::atomic<int> aio_main_wq_fd;
538-
539-
DiskHandler::DiskHandler()
540-
{
541-
io_uring_params p{};
542-
543-
if (aio_io_uring_attach_wq > 0) {
544-
int wq_fd = get_main_queue_fd();
545-
if (wq_fd > 0) {
546-
p.flags = IORING_SETUP_ATTACH_WQ;
547-
p.wq_fd = wq_fd;
548-
}
549-
}
550-
551-
if (aio_io_uring_sq_poll_ms > 0) {
552-
p.flags |= IORING_SETUP_SQPOLL;
553-
p.sq_thread_idle = aio_io_uring_sq_poll_ms;
554-
}
555-
556-
int ret = io_uring_queue_init_params(aio_io_uring_queue_entries, &ring, &p);
557-
if (ret < 0) {
558-
throw std::runtime_error(strerror(-ret));
559-
}
560-
561-
/* no sharing for non-fixed either */
562-
if (aio_io_uring_sq_poll_ms && !(p.features & IORING_FEAT_SQPOLL_NONFIXED)) {
563-
throw std::runtime_error("No SQPOLL sharing with nonfixed");
564-
}
565-
566-
// assign this handler to the thread
567-
// TODO(cmcfarlen): maybe a bad place for this!
568-
this_ethread()->diskHandler = this;
569-
}
570-
571-
DiskHandler::~DiskHandler()
572-
{
573-
io_uring_queue_exit(&ring);
574-
}
575-
576-
void
577-
DiskHandler::set_main_queue(DiskHandler *dh)
578-
{
579-
dh->set_wq_max_workers(aio_io_uring_wq_bounded, aio_io_uring_wq_unbounded);
580-
aio_main_wq_fd.store(dh->ring.ring_fd);
581-
}
582-
583-
int
584-
DiskHandler::get_main_queue_fd()
585-
{
586-
return aio_main_wq_fd.load();
587-
}
588-
589-
int
590-
DiskHandler::set_wq_max_workers(unsigned int bounded, unsigned int unbounded)
591-
{
592-
if (bounded == 0 && unbounded == 0) {
593-
return 0;
594-
}
595-
unsigned int args[2] = {bounded, unbounded};
596-
int result = io_uring_register_iowq_max_workers(&ring, args);
597-
return result;
598-
}
599-
600-
std::pair<int, int>
601-
DiskHandler::get_wq_max_workers()
602-
{
603-
unsigned int args[2] = {0, 0};
604-
io_uring_register_iowq_max_workers(&ring, args);
605-
return std::make_pair(args[0], args[1]);
606-
}
607-
608-
void
609-
DiskHandler::submit()
610-
{
611-
io_uring_submissions.fetch_add(io_uring_submit(&ring));
612-
}
535+
#include "I_IO_URING.h"
613536

614537
void
615-
DiskHandler::handle_cqe(io_uring_cqe *cqe)
538+
ink_aiocb::handle_complete(io_uring_cqe *cqe)
616539
{
617-
AIOCallback *op = static_cast<AIOCallback *>(io_uring_cqe_get_data(cqe));
540+
AIOCallback *op = this_op;
618541

619542
op->aio_result = static_cast<int64_t>(cqe->res);
620543
op->link.prev = nullptr;
@@ -643,68 +566,15 @@ DiskHandler::handle_cqe(io_uring_cqe *cqe)
643566
}
644567
}
645568

646-
void
647-
DiskHandler::service()
648-
{
649-
io_uring_cqe *cqe = nullptr;
650-
io_uring_peek_cqe(&ring, &cqe);
651-
while (cqe) {
652-
handle_cqe(cqe);
653-
io_uring_completions.fetch_add(1);
654-
io_uring_cqe_seen(&ring, cqe);
655-
656-
cqe = nullptr;
657-
io_uring_peek_cqe(&ring, &cqe);
658-
}
659-
}
660-
661-
void
662-
DiskHandler::submit_and_wait(int ms)
663-
{
664-
ink_hrtime t = ink_hrtime_from_msec(ms);
665-
timespec ts = ink_hrtime_to_timespec(t);
666-
__kernel_timespec timeout = {ts.tv_sec, ts.tv_nsec};
667-
io_uring_cqe *cqe = nullptr;
668-
669-
io_uring_submit_and_wait_timeout(&ring, &cqe, 1, &timeout, nullptr);
670-
while (cqe) {
671-
handle_cqe(cqe);
672-
io_uring_completions.fetch_add(1);
673-
io_uring_cqe_seen(&ring, cqe);
674-
675-
cqe = nullptr;
676-
io_uring_peek_cqe(&ring, &cqe);
677-
}
678-
}
679-
680-
int
681-
DiskHandler::register_eventfd()
682-
{
683-
int fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
684-
685-
io_uring_register_eventfd(&ring, fd);
686-
687-
return fd;
688-
}
689-
690-
DiskHandler *
691-
DiskHandler::local_context()
692-
{
693-
// TODO(cmcfarlen): load config
694-
thread_local DiskHandler threadContext;
695-
696-
return &threadContext;
697-
}
698-
699569
int
700570
ink_aio_read(AIOCallback *op_in, int /* fromAPI ATS_UNUSED */)
701571
{
702-
EThread *t = this_ethread();
703-
AIOCallback *op = op_in;
572+
IOUringContext *ur = IOUringContext::local_context();
573+
AIOCallback *op = op_in;
704574
while (op) {
705-
io_uring_sqe *sqe = t->diskHandler->next_sqe();
575+
op->aiocb.this_op = op;
576+
io_uring_sqe *sqe = ur->next_sqe(&op->aiocb);
706577
io_uring_prep_read(sqe, op->aiocb.aio_fildes, op->aiocb.aio_buf, op->aiocb.aio_nbytes, op->aiocb.aio_offset);
707-
io_uring_sqe_set_data(sqe, op);
708578
op->aiocb.aio_lio_opcode = LIO_READ;
709579
if (op->then) {
710580
sqe->flags |= IOSQE_IO_LINK;
@@ -720,12 +590,12 @@ ink_aio_read(AIOCallback *op_in, int /* fromAPI ATS_UNUSED */)
720590
int
721591
ink_aio_write(AIOCallback *op_in, int /* fromAPI ATS_UNUSED */)
722592
{
723-
EThread *t = this_ethread();
724-
AIOCallback *op = op_in;
593+
IOUringContext *ur = IOUringContext::local_context();
594+
AIOCallback *op = op_in;
725595
while (op) {
726-
io_uring_sqe *sqe = t->diskHandler->next_sqe();
596+
op->aiocb.this_op = op;
597+
io_uring_sqe *sqe = ur->next_sqe(&op->aiocb);
727598
io_uring_prep_write(sqe, op->aiocb.aio_fildes, op->aiocb.aio_buf, op->aiocb.aio_nbytes, op->aiocb.aio_offset);
728-
io_uring_sqe_set_data(sqe, op);
729599
op->aiocb.aio_lio_opcode = LIO_WRITE;
730600
if (op->then) {
731601
sqe->flags |= IOSQE_IO_LINK;

iocore/aio/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818

1919
add_library(aio)
2020
target_sources(aio PRIVATE AIO.cc Inline.cc)
21-
target_include_directories(aio PRIVATE ${CMAKE_SOURCE_DIR}/iocore/eventsystem)
21+
target_include_directories(aio PRIVATE ${CMAKE_SOURCE_DIR}/iocore/eventsystem ${CMAKE_SOURCE_DIR}/iocore/io_uring)

iocore/aio/I_AIO.h

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,21 @@ typedef struct io_event ink_io_event_t;
6868
#define aio_buf u.c.buf
6969

7070
#elif AIO_MODE == AIO_MODE_IO_URING
71-
#include <liburing.h>
71+
#include "I_IO_URING.h"
7272

7373
struct AIOCallback;
74-
struct ink_aiocb {
74+
struct ink_aiocb : public IOUringCompletionHandler {
7575
int aio_fildes = -1; /* file descriptor or status: AIO_NOT_IN_PROGRESS */
7676
void *aio_buf = nullptr; /* buffer location */
7777
size_t aio_nbytes = 0; /* length of transfer */
7878
off_t aio_offset = 0; /* file offset */
7979

80-
int aio_lio_opcode = 0; /* listio operation */
81-
int aio_state = 0; /* state flag for List I/O */
82-
AIOCallback *aio_op = nullptr;
80+
int aio_lio_opcode = 0; /* listio operation */
81+
int aio_state = 0; /* state flag for List I/O */
82+
AIOCallback *this_op = nullptr;
83+
AIOCallback *aio_op = nullptr;
84+
85+
void handle_complete(io_uring_cqe *) override;
8386
};
8487

8588
#else
@@ -153,41 +156,6 @@ struct DiskHandler : public Continuation {
153156
};
154157
#endif
155158

156-
#if AIO_MODE == AIO_MODE_IO_URING
157-
158-
class DiskHandler
159-
{
160-
public:
161-
DiskHandler();
162-
~DiskHandler();
163-
164-
io_uring_sqe *
165-
next_sqe()
166-
{
167-
return io_uring_get_sqe(&ring);
168-
}
169-
170-
int set_wq_max_workers(unsigned int bounded, unsigned int unbounded);
171-
std::pair<int, int> get_wq_max_workers();
172-
173-
void submit();
174-
void service();
175-
void submit_and_wait(int ms);
176-
177-
int register_eventfd();
178-
179-
static DiskHandler *local_context();
180-
static void set_main_queue(DiskHandler *);
181-
static int get_main_queue_fd();
182-
183-
private:
184-
io_uring ring;
185-
186-
void handle_cqe(io_uring_cqe *);
187-
};
188-
189-
#endif
190-
191159
void ink_aio_init(ts::ModuleVersion version);
192160
int ink_aio_start();
193161
void ink_aio_set_callback(Continuation *error_callback);

iocore/aio/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
AM_CPPFLAGS += \
2020
-I$(abs_top_srcdir)/iocore/eventsystem \
21+
-I$(abs_top_srcdir)/iocore/io_uring \
2122
-I$(abs_top_srcdir)/include \
2223
-I$(abs_top_srcdir)/lib \
2324
@SWOC_INCLUDES@

iocore/cache/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ add_library(inkcache STATIC
3434
)
3535
target_include_directories(inkcache PRIVATE
3636
${CMAKE_SOURCE_DIR}/iocore/eventsystem
37+
${CMAKE_SOURCE_DIR}/iocore/io_uring
3738
${CMAKE_SOURCE_DIR}/iocore/dns
3839
${CMAKE_SOURCE_DIR}/iocore/aio
3940
${CMAKE_SOURCE_DIR}/iocore/net

iocore/dns/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ add_library(inkdns STATIC
2525
target_include_directories(inkdns PRIVATE
2626
${CMAKE_SOURCE_DIR}/iocore/eventsystem
2727
${CMAKE_SOURCE_DIR}/iocore/dns
28+
${CMAKE_SOURCE_DIR}/iocore/io_uring
2829
${CMAKE_SOURCE_DIR}/iocore/aio
2930
${CMAKE_SOURCE_DIR}/iocore/net
3031
${CMAKE_SOURCE_DIR}/iocore/cache

0 commit comments

Comments
 (0)