2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-11 06:30:31 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-11 06:30:31 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2018-01-11 06:30:31 +01:00
|
|
|
|
|
|
|
#pragma once
|
2019-01-23 22:20:38 +01:00
|
|
|
#define HAVE_FS_AIO_H
|
2018-01-11 06:30:31 +01:00
|
|
|
#include <linux/aio_abi.h>
|
|
|
|
|
2019-04-30 00:03:33 +02:00
|
|
|
/// Define this to try all read requests with RWF_NOWAIT first and indicate
|
|
|
|
/// EAGAIN failure with a log dwarning. On EAGAIN the request is resubmitted
|
|
|
|
/// without RWF_NOWAIT if the user expected blocking behavior.
|
|
|
|
//#define RB_DEBUG_FS_AIO_READ_BLOCKING
|
|
|
|
|
2019-04-30 02:48:06 +02:00
|
|
|
/// Define to time the io_submit() system call with a syscall_usage_warning.
|
|
|
|
/// This emits a warning when the kernel spent a lot of time in io_submit().
|
|
|
|
/// The resolution is very low at 10ms but it is not expensive to use.
|
|
|
|
//#define RB_DEBUG_FS_AIO_SUBMIT_BLOCKING
|
|
|
|
|
2022-05-26 21:28:28 +02:00
|
|
|
#pragma GCC visibility push(hidden)
|
2018-11-28 03:09:12 +01:00
|
|
|
namespace ircd::fs::aio
|
2018-08-21 07:45:06 +02:00
|
|
|
{
|
2018-12-18 22:40:46 +01:00
|
|
|
size_t write(const fd &, const const_iovec_view &, const write_opts &);
|
2020-05-07 04:14:20 +02:00
|
|
|
size_t read(const vector_view<read_op> &);
|
2018-12-18 22:40:46 +01:00
|
|
|
size_t read(const fd &, const const_iovec_view &, const read_opts &);
|
2020-05-06 22:37:56 +02:00
|
|
|
size_t fsync(const fd &, const sync_opts &);
|
2018-08-21 07:45:06 +02:00
|
|
|
}
|
2022-05-26 21:28:28 +02:00
|
|
|
#pragma GCC visibility pop
|
2018-08-21 07:45:06 +02:00
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
/// AIO context instance from the system. Right now this is a singleton with
|
2018-11-28 03:09:12 +01:00
|
|
|
/// an extern instance pointer at fs::aio::context maintained by fs::aio::init.
|
2022-05-26 21:28:28 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fs::aio::system
|
2018-01-11 06:30:31 +01:00
|
|
|
{
|
2019-03-30 21:11:39 +01:00
|
|
|
struct aio_context;
|
2019-03-27 09:52:37 +01:00
|
|
|
|
2018-12-30 01:32:09 +01:00
|
|
|
static const int eventfd_flags;
|
|
|
|
|
2018-12-27 21:04:48 +01:00
|
|
|
/// io_getevents vector (in)
|
|
|
|
std::vector<io_event> event;
|
|
|
|
uint64_t ecount {0};
|
2018-03-13 00:26:55 +01:00
|
|
|
|
2018-12-27 19:36:27 +01:00
|
|
|
/// io_submit queue (out)
|
|
|
|
std::vector<iocb *> queue;
|
|
|
|
size_t qcount {0};
|
|
|
|
|
2018-12-27 21:04:48 +01:00
|
|
|
/// other state
|
|
|
|
ctx::dock dock;
|
|
|
|
size_t in_flight {0};
|
2019-01-17 21:24:47 +01:00
|
|
|
bool handle_set {false};
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2019-03-27 11:06:46 +01:00
|
|
|
size_t handle_size {0};
|
|
|
|
std::unique_ptr<uint8_t[]> handle_data;
|
|
|
|
static ios::descriptor handle_descriptor;
|
2020-12-18 05:27:44 +01:00
|
|
|
static ios::descriptor chase_descriptor;
|
2019-03-27 11:06:46 +01:00
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
/// An eventfd which will be notified by the system; we integrate this with
|
2018-01-11 06:30:31 +01:00
|
|
|
/// the ircd io_service core epoll() event loop. The EFD_SEMAPHORE flag is
|
2018-12-27 21:04:48 +01:00
|
|
|
/// not used to reduce the number of triggers. The semaphore value is the
|
|
|
|
/// ecount (above) which will reflect a hint for how many AIO's are done.
|
2018-02-06 07:42:40 +01:00
|
|
|
asio::posix::stream_descriptor resfd;
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
/// Handler to the io context we submit requests to the system with
|
2019-03-30 21:11:39 +01:00
|
|
|
const custom_ptr<const aio_context> head;
|
|
|
|
const io_event *ring {nullptr};
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2019-01-13 23:02:03 +01:00
|
|
|
size_t max_events() const;
|
|
|
|
size_t max_submit() const;
|
|
|
|
size_t request_count() const; // qcount + in_flight
|
|
|
|
size_t request_avail() const; // max_events - request_count()
|
|
|
|
|
2018-01-11 06:30:31 +01:00
|
|
|
// Callback stack invoked when the sigfd is notified of completed events.
|
|
|
|
void handle_event(const io_event &) noexcept;
|
|
|
|
void handle_events() noexcept;
|
2018-03-08 18:27:32 +01:00
|
|
|
void handle(const boost::system::error_code &, const size_t) noexcept;
|
2018-01-11 06:30:31 +01:00
|
|
|
void set_handle();
|
|
|
|
|
2019-03-14 19:49:57 +01:00
|
|
|
void dequeue_one(const std::error_code &);
|
|
|
|
void dequeue_all(const std::error_code &);
|
|
|
|
size_t io_submit();
|
2019-04-21 05:34:01 +02:00
|
|
|
size_t submit() noexcept;
|
2018-12-27 19:36:27 +01:00
|
|
|
void chase() noexcept;
|
|
|
|
|
2019-04-22 18:29:09 +02:00
|
|
|
bool submit(request &);
|
2019-04-21 05:34:01 +02:00
|
|
|
bool cancel(request &);
|
2019-04-22 18:29:09 +02:00
|
|
|
bool wait(request &);
|
2018-12-21 19:30:04 +01:00
|
|
|
|
|
|
|
// Control panel
|
2018-08-19 07:58:43 +02:00
|
|
|
bool wait();
|
2018-03-13 00:26:55 +01:00
|
|
|
bool interrupt();
|
|
|
|
|
2019-01-13 23:02:03 +01:00
|
|
|
system(const size_t &max_events,
|
|
|
|
const size_t &max_submit);
|
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
~system() noexcept;
|
2018-01-11 06:30:31 +01:00
|
|
|
};
|
|
|
|
|
2022-05-26 21:28:28 +02:00
|
|
|
struct [[gnu::visibility("internal")]]
|
|
|
|
ircd::fs::aio::system::aio_context
|
2019-03-27 09:52:37 +01:00
|
|
|
{
|
|
|
|
static constexpr uint MAGIC {0xA10A10A1};
|
|
|
|
|
|
|
|
uint id; // kernel internal index number
|
|
|
|
uint nr; // number of io_events
|
|
|
|
uint head;
|
|
|
|
uint tail;
|
|
|
|
uint magic;
|
|
|
|
uint compat_features;
|
|
|
|
uint incompat_features;
|
2019-03-30 21:11:39 +01:00
|
|
|
uint header_length; // size of aio_context
|
2019-03-27 09:52:37 +01:00
|
|
|
struct io_event io_events[0];
|
|
|
|
};
|
|
|
|
// 128 bytes + ring size
|
|
|
|
|
2018-01-11 06:30:31 +01:00
|
|
|
/// Generic request control block.
|
2022-05-26 21:28:28 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fs::aio::request
|
2018-01-11 06:30:31 +01:00
|
|
|
:iocb
|
|
|
|
{
|
|
|
|
struct read;
|
|
|
|
struct write;
|
2018-08-19 07:58:43 +02:00
|
|
|
struct fsync;
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2020-05-07 04:13:14 +02:00
|
|
|
ssize_t retval;
|
|
|
|
ssize_t errcode;
|
|
|
|
const struct opts *opts;
|
|
|
|
ctx::dock *waiter;
|
2020-05-06 22:37:56 +02:00
|
|
|
|
|
|
|
bool wait();
|
2018-01-11 06:30:31 +01:00
|
|
|
|
|
|
|
public:
|
2018-12-02 00:33:30 +01:00
|
|
|
const_iovec_view iovec() const;
|
2019-03-11 03:12:16 +01:00
|
|
|
bool completed() const;
|
2019-04-21 09:20:44 +02:00
|
|
|
bool queued() const;
|
2018-11-28 04:54:24 +01:00
|
|
|
|
2020-05-06 22:37:56 +02:00
|
|
|
size_t complete();
|
|
|
|
void submit();
|
2019-04-21 05:34:01 +02:00
|
|
|
bool cancel();
|
2018-01-11 06:30:31 +01:00
|
|
|
|
2020-05-31 01:33:22 +02:00
|
|
|
request(const int &fd = -1,
|
|
|
|
const struct opts *const & = nullptr,
|
|
|
|
ctx::dock *const & = nullptr);
|
|
|
|
|
|
|
|
request(request &&) = delete;
|
|
|
|
request(const request &) = delete;
|
|
|
|
request &operator=(request &&) = delete;
|
|
|
|
request &operator=(const request &) = delete;
|
2018-01-18 07:03:07 +01:00
|
|
|
~request() noexcept;
|
2018-01-11 06:30:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Read request control block
|
2022-05-26 21:28:28 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fs::aio::request::read
|
2018-01-11 06:30:31 +01:00
|
|
|
:request
|
|
|
|
{
|
2020-05-06 22:37:56 +02:00
|
|
|
read(ctx::dock &, const int &fd, const read_opts &, const const_iovec_view &);
|
|
|
|
read() = default;
|
2018-01-11 06:30:31 +01:00
|
|
|
};
|
2018-02-06 08:28:33 +01:00
|
|
|
|
|
|
|
/// Write request control block
|
2022-05-26 21:28:28 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fs::aio::request::write
|
2018-02-06 08:28:33 +01:00
|
|
|
:request
|
|
|
|
{
|
2020-05-06 22:37:56 +02:00
|
|
|
write(ctx::dock &, const int &fd, const write_opts &, const const_iovec_view &);
|
|
|
|
write() = default;
|
2018-02-06 08:28:33 +01:00
|
|
|
};
|
2018-08-19 07:58:43 +02:00
|
|
|
|
|
|
|
/// fsync request control block
|
2022-05-26 21:28:28 +02:00
|
|
|
struct [[gnu::visibility("hidden")]]
|
|
|
|
ircd::fs::aio::request::fsync
|
2018-08-19 07:58:43 +02:00
|
|
|
:request
|
|
|
|
{
|
2020-05-06 22:37:56 +02:00
|
|
|
fsync(ctx::dock &, const int &fd, const sync_opts &);
|
|
|
|
fsync() = default;
|
2018-08-19 07:58:43 +02:00
|
|
|
};
|