2018-02-06 07:42:40 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/eventfd.h>
|
2018-08-29 04:52:53 +02:00
|
|
|
#include <ircd/asio.h>
|
2018-02-06 07:42:40 +01:00
|
|
|
#include "aio.h"
|
|
|
|
|
2018-11-28 03:09:12 +01:00
|
|
|
namespace ircd::fs::aio
|
2018-11-01 01:00:17 +01:00
|
|
|
{
|
2018-11-02 04:15:12 +01:00
|
|
|
static int reqprio(int);
|
2018-11-01 01:00:17 +01:00
|
|
|
}
|
|
|
|
|
2018-12-21 21:58:53 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// fs/aio.h
|
|
|
|
//
|
|
|
|
// The contents of this section override weak symbols in ircd/fs.cc when this
|
|
|
|
// unit is conditionally compiled and linked on AIO-supporting platforms. On
|
|
|
|
// non-supporting platforms, or for items not listed here, the definitions in
|
|
|
|
// ircd/fs.cc are the default.
|
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
decltype(ircd::fs::aio::support)
|
|
|
|
ircd::fs::aio::support
|
2018-12-21 21:58:53 +01:00
|
|
|
{
|
|
|
|
true
|
|
|
|
};
|
|
|
|
|
|
|
|
/// True if IOCB_CMD_FSYNC is supported by AIO. If this is false then
|
|
|
|
/// fs::fsync_opts::async=true flag is ignored.
|
2018-12-29 00:02:02 +01:00
|
|
|
decltype(ircd::fs::aio::support_fsync)
|
|
|
|
ircd::fs::aio::support_fsync
|
2018-12-21 21:58:53 +01:00
|
|
|
{
|
|
|
|
false //TODO: get this info
|
|
|
|
};
|
|
|
|
|
|
|
|
/// True if IOCB_CMD_FDSYNC is supported by AIO. If this is false then
|
|
|
|
/// fs::fsync_opts::async=true flag is ignored.
|
2018-12-29 00:02:02 +01:00
|
|
|
decltype(ircd::fs::aio::support_fdsync)
|
|
|
|
ircd::fs::aio::support_fdsync
|
2018-12-21 21:58:53 +01:00
|
|
|
{
|
|
|
|
false //TODO: get this info
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::fs::aio::MAX_EVENTS)
|
|
|
|
ircd::fs::aio::MAX_EVENTS
|
|
|
|
{
|
2018-12-29 00:02:02 +01:00
|
|
|
128L //TODO: get this info
|
2018-12-21 21:58:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::fs::aio::MAX_REQPRIO)
|
|
|
|
ircd::fs::aio::MAX_REQPRIO
|
|
|
|
{
|
|
|
|
info::aio_reqprio_max
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::aio::init::init()
|
|
|
|
{
|
2018-12-29 00:02:02 +01:00
|
|
|
assert(!system);
|
2018-12-21 21:58:53 +01:00
|
|
|
if(!bool(aio::enable))
|
|
|
|
return;
|
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
system = new struct system;
|
2018-12-21 21:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::aio::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-12-29 00:02:02 +01:00
|
|
|
delete system;
|
|
|
|
system = nullptr;
|
2018-12-21 21:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ircd/aio.h
|
|
|
|
//
|
|
|
|
|
2018-08-19 07:58:43 +02:00
|
|
|
//
|
|
|
|
// request::fsync
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::aio::request::fsync::fsync(const int &fd,
|
2018-12-14 01:54:09 +01:00
|
|
|
const sync_opts &opts)
|
2018-12-30 05:02:22 +01:00
|
|
|
:request{fd, &opts}
|
2018-08-19 07:58:43 +02:00
|
|
|
{
|
2018-11-02 04:15:12 +01:00
|
|
|
aio_reqprio = reqprio(opts.priority);
|
2018-08-19 07:58:43 +02:00
|
|
|
aio_lio_opcode = IOCB_CMD_FSYNC;
|
|
|
|
|
|
|
|
aio_buf = 0;
|
|
|
|
aio_nbytes = 0;
|
|
|
|
aio_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-28 03:09:12 +01:00
|
|
|
ircd::fs::aio::fsync(const fd &fd,
|
2018-12-14 01:54:09 +01:00
|
|
|
const sync_opts &opts)
|
2018-08-19 07:58:43 +02:00
|
|
|
{
|
|
|
|
aio::request::fsync request
|
|
|
|
{
|
|
|
|
fd, opts
|
|
|
|
};
|
|
|
|
|
|
|
|
request();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// request::fdsync
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::aio::request::fdsync::fdsync(const int &fd,
|
2018-12-14 01:54:09 +01:00
|
|
|
const sync_opts &opts)
|
2018-12-30 05:02:22 +01:00
|
|
|
:request{fd, &opts}
|
2018-08-19 07:58:43 +02:00
|
|
|
{
|
2018-11-02 04:15:12 +01:00
|
|
|
aio_reqprio = reqprio(opts.priority);
|
2018-08-19 07:58:43 +02:00
|
|
|
aio_lio_opcode = IOCB_CMD_FDSYNC;
|
|
|
|
|
|
|
|
aio_buf = 0;
|
|
|
|
aio_nbytes = 0;
|
|
|
|
aio_offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-28 03:09:12 +01:00
|
|
|
ircd::fs::aio::fdsync(const fd &fd,
|
2018-12-14 01:54:09 +01:00
|
|
|
const sync_opts &opts)
|
2018-08-19 07:58:43 +02:00
|
|
|
{
|
|
|
|
aio::request::fdsync request
|
|
|
|
{
|
|
|
|
fd, opts
|
|
|
|
};
|
|
|
|
|
|
|
|
request();
|
|
|
|
}
|
|
|
|
|
2018-08-19 07:51:56 +02:00
|
|
|
//
|
|
|
|
// request::read
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::aio::request::read::read(const int &fd,
|
2018-12-02 00:33:30 +01:00
|
|
|
const const_iovec_view &iov,
|
2018-08-19 07:51:56 +02:00
|
|
|
const read_opts &opts)
|
2018-12-30 05:02:22 +01:00
|
|
|
:request{fd, &opts}
|
2018-08-19 07:51:56 +02:00
|
|
|
{
|
2018-11-02 04:15:12 +01:00
|
|
|
aio_reqprio = reqprio(opts.priority);
|
2018-11-01 00:29:20 +01:00
|
|
|
aio_lio_opcode = IOCB_CMD_PREADV;
|
2018-08-19 07:51:56 +02:00
|
|
|
|
2018-11-01 00:29:20 +01:00
|
|
|
aio_buf = uintptr_t(iov.data());
|
|
|
|
aio_nbytes = iov.size();
|
2018-08-19 07:51:56 +02:00
|
|
|
aio_offset = opts.offset;
|
|
|
|
}
|
|
|
|
|
2018-11-29 01:53:04 +01:00
|
|
|
size_t
|
2018-11-28 03:09:12 +01:00
|
|
|
ircd::fs::aio::read(const fd &fd,
|
2018-12-18 22:40:46 +01:00
|
|
|
const const_iovec_view &bufs,
|
2018-08-19 07:51:56 +02:00
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
aio::request::read request
|
|
|
|
{
|
2018-12-18 22:40:46 +01:00
|
|
|
fd, bufs, opts
|
2018-08-19 07:51:56 +02:00
|
|
|
};
|
|
|
|
|
2018-11-29 20:04:10 +01:00
|
|
|
stats.cur_reads++;
|
|
|
|
stats.max_reads = std::max(stats.max_reads, stats.cur_reads);
|
|
|
|
const unwind dec{[]
|
|
|
|
{
|
|
|
|
stats.cur_reads--;
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Make request; blocks ircd::ctx until completed or throw.
|
2018-08-19 07:51:56 +02:00
|
|
|
const size_t bytes
|
|
|
|
{
|
|
|
|
request()
|
|
|
|
};
|
|
|
|
|
2018-11-29 20:04:10 +01:00
|
|
|
stats.bytes_read += bytes;
|
|
|
|
stats.reads++;
|
2018-11-29 01:53:04 +01:00
|
|
|
return bytes;
|
2018-08-19 07:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// request::write
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::fs::aio::request::write::write(const int &fd,
|
2018-12-02 00:33:30 +01:00
|
|
|
const const_iovec_view &iov,
|
2018-08-19 07:51:56 +02:00
|
|
|
const write_opts &opts)
|
2018-12-30 05:02:22 +01:00
|
|
|
:request{fd, &opts}
|
2018-08-19 07:51:56 +02:00
|
|
|
{
|
2018-11-02 04:15:12 +01:00
|
|
|
aio_reqprio = reqprio(opts.priority);
|
2018-11-01 00:29:20 +01:00
|
|
|
aio_lio_opcode = IOCB_CMD_PWRITEV;
|
2018-08-19 07:51:56 +02:00
|
|
|
|
2018-11-01 00:29:20 +01:00
|
|
|
aio_buf = uintptr_t(iov.data());
|
|
|
|
aio_nbytes = iov.size();
|
2018-08-19 07:51:56 +02:00
|
|
|
aio_offset = opts.offset;
|
|
|
|
}
|
|
|
|
|
2018-11-29 01:53:04 +01:00
|
|
|
size_t
|
2018-11-28 03:09:12 +01:00
|
|
|
ircd::fs::aio::write(const fd &fd,
|
2018-12-18 22:40:46 +01:00
|
|
|
const const_iovec_view &bufs,
|
2018-08-19 07:51:56 +02:00
|
|
|
const write_opts &opts)
|
|
|
|
{
|
|
|
|
aio::request::write request
|
|
|
|
{
|
2018-12-18 22:40:46 +01:00
|
|
|
fd, bufs, opts
|
2018-08-19 07:51:56 +02:00
|
|
|
};
|
|
|
|
|
2018-11-29 01:53:04 +01:00
|
|
|
#ifndef _NDEBUG
|
|
|
|
const size_t req_bytes
|
2018-08-19 07:51:56 +02:00
|
|
|
{
|
2018-11-29 01:53:04 +01:00
|
|
|
fs::bytes(request.iovec())
|
2018-08-19 07:51:56 +02:00
|
|
|
};
|
2018-11-29 01:53:04 +01:00
|
|
|
#endif
|
2018-08-19 07:51:56 +02:00
|
|
|
|
2018-11-29 20:04:10 +01:00
|
|
|
stats.cur_bytes_write += req_bytes;
|
|
|
|
stats.cur_writes++;
|
|
|
|
stats.max_writes = std::max(stats.max_writes, stats.cur_writes);
|
|
|
|
const unwind dec{[&req_bytes]
|
|
|
|
{
|
|
|
|
stats.cur_bytes_write -= req_bytes;
|
|
|
|
stats.cur_writes--;
|
|
|
|
}};
|
|
|
|
|
|
|
|
// Make the request; ircd::ctx blocks here. Throws on error
|
2018-11-29 01:53:04 +01:00
|
|
|
const size_t bytes
|
2018-08-19 07:51:56 +02:00
|
|
|
{
|
2018-11-29 01:53:04 +01:00
|
|
|
request()
|
2018-08-19 07:51:56 +02:00
|
|
|
};
|
|
|
|
|
2018-11-28 04:53:32 +01:00
|
|
|
// Does linux ever not complete all bytes for an AIO?
|
2018-11-29 01:53:04 +01:00
|
|
|
assert(bytes == req_bytes);
|
|
|
|
|
2018-11-29 20:04:10 +01:00
|
|
|
stats.bytes_write += bytes;
|
|
|
|
stats.writes++;
|
2018-11-29 01:53:04 +01:00
|
|
|
return bytes;
|
2018-08-19 07:51:56 +02:00
|
|
|
}
|
|
|
|
|
2018-08-19 08:08:55 +02:00
|
|
|
//
|
|
|
|
// request::prefetch
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2018-11-28 03:09:12 +01:00
|
|
|
ircd::fs::aio::prefetch(const fd &fd,
|
2018-08-19 08:08:55 +02:00
|
|
|
const size_t &size,
|
|
|
|
const read_opts &opts)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-01 01:00:17 +01:00
|
|
|
//
|
2018-12-20 22:40:30 +01:00
|
|
|
// request
|
2018-11-01 01:00:17 +01:00
|
|
|
//
|
|
|
|
|
2018-12-30 05:02:22 +01:00
|
|
|
ircd::fs::aio::request::request(const int &fd,
|
|
|
|
const struct opts *const &opts)
|
2018-12-20 22:40:30 +01:00
|
|
|
:iocb{0}
|
2018-12-30 05:02:22 +01:00
|
|
|
,opts{opts}
|
2018-11-02 04:15:12 +01:00
|
|
|
{
|
2018-12-29 00:02:02 +01:00
|
|
|
assert(system);
|
2018-12-20 22:40:30 +01:00
|
|
|
assert(ctx::current);
|
2018-11-02 04:15:12 +01:00
|
|
|
|
2018-12-20 22:40:30 +01:00
|
|
|
aio_flags = IOCB_FLAG_RESFD;
|
2018-12-29 00:02:02 +01:00
|
|
|
aio_resfd = system->resfd.native_handle();
|
2018-12-20 22:40:30 +01:00
|
|
|
aio_fildes = fd;
|
|
|
|
aio_data = uintptr_t(this);
|
|
|
|
}
|
2018-11-02 04:15:12 +01:00
|
|
|
|
2018-12-20 22:40:30 +01:00
|
|
|
ircd::fs::aio::request::~request()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Cancel a request. The handler callstack is invoked directly from here
|
|
|
|
/// which means any callback will be invoked or ctx will be notified if
|
|
|
|
/// appropriate.
|
|
|
|
void
|
|
|
|
ircd::fs::aio::request::cancel()
|
|
|
|
{
|
2018-12-29 00:02:02 +01:00
|
|
|
assert(system);
|
|
|
|
system->cancel(*this);
|
2018-12-20 22:40:30 +01:00
|
|
|
stats.bytes_cancel += bytes(iovec());
|
|
|
|
stats.cancel++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Submit a request and properly yield the ircd::ctx. When this returns the
|
|
|
|
/// result will be available or an exception will be thrown.
|
|
|
|
size_t
|
|
|
|
ircd::fs::aio::request::operator()()
|
|
|
|
{
|
2018-12-29 00:02:02 +01:00
|
|
|
assert(system);
|
2018-12-20 22:40:30 +01:00
|
|
|
assert(ctx::current);
|
|
|
|
assert(waiter == ctx::current);
|
|
|
|
|
|
|
|
const size_t submitted_bytes
|
|
|
|
{
|
|
|
|
bytes(iovec())
|
|
|
|
};
|
|
|
|
|
2018-12-27 21:04:48 +01:00
|
|
|
// Wait here until there's room to submit a request
|
2018-12-29 00:02:02 +01:00
|
|
|
system->dock.wait([]
|
2018-12-27 21:04:48 +01:00
|
|
|
{
|
2018-12-29 00:02:02 +01:00
|
|
|
const size_t count(system->qcount + system->in_flight);
|
2018-12-27 21:04:48 +01:00
|
|
|
return count < size_t(max_events);
|
|
|
|
});
|
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
// Submit to system
|
|
|
|
system->submit(*this);
|
2018-12-20 22:40:30 +01:00
|
|
|
|
|
|
|
// Update stats for submission phase
|
|
|
|
stats.bytes_requests += submitted_bytes;
|
|
|
|
stats.requests++;
|
|
|
|
|
|
|
|
const auto &curcnt(stats.requests - stats.complete);
|
|
|
|
stats.max_requests = std::max(stats.max_requests, curcnt);
|
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
// Wait for completion
|
|
|
|
system->wait(*this);
|
2018-12-21 19:30:04 +01:00
|
|
|
assert(retval <= ssize_t(submitted_bytes));
|
|
|
|
|
2018-12-20 22:40:30 +01:00
|
|
|
// Update stats for completion phase.
|
|
|
|
stats.bytes_complete += submitted_bytes;
|
|
|
|
stats.complete++;
|
|
|
|
|
|
|
|
if(retval == -1)
|
|
|
|
{
|
|
|
|
stats.bytes_errors += submitted_bytes;
|
|
|
|
stats.errors++;
|
|
|
|
|
|
|
|
throw fs::error
|
|
|
|
{
|
|
|
|
make_error_code(errcode)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return size_t(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::fs::const_iovec_view
|
|
|
|
ircd::fs::aio::request::iovec()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
{
|
|
|
|
reinterpret_cast<const ::iovec *>(aio_buf), aio_nbytes
|
|
|
|
};
|
2018-11-02 04:15:12 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 07:42:40 +01:00
|
|
|
//
|
2018-12-29 00:02:02 +01:00
|
|
|
// system
|
2018-02-06 07:42:40 +01:00
|
|
|
//
|
|
|
|
|
2018-12-30 01:32:09 +01:00
|
|
|
decltype(ircd::fs::aio::system::eventfd_flags)
|
|
|
|
ircd::fs::aio::system::eventfd_flags
|
|
|
|
{
|
|
|
|
EFD_CLOEXEC | EFD_NONBLOCK
|
|
|
|
};
|
|
|
|
|
2018-08-19 07:51:56 +02:00
|
|
|
//
|
2018-12-29 00:02:02 +01:00
|
|
|
// system::system
|
2018-08-19 07:51:56 +02:00
|
|
|
//
|
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::system()
|
2018-08-28 23:04:43 +02:00
|
|
|
try
|
2018-12-27 21:04:48 +01:00
|
|
|
:event
|
2018-12-27 19:36:27 +01:00
|
|
|
{
|
2018-12-27 21:04:48 +01:00
|
|
|
size_t(max_events)
|
2018-12-27 19:36:27 +01:00
|
|
|
}
|
2018-12-27 21:04:48 +01:00
|
|
|
,queue
|
2018-12-27 19:36:27 +01:00
|
|
|
{
|
2018-12-27 21:04:48 +01:00
|
|
|
size_t(max_submit)
|
2018-12-27 19:36:27 +01:00
|
|
|
}
|
|
|
|
,resfd
|
2018-02-06 07:42:40 +01:00
|
|
|
{
|
2018-12-30 01:32:09 +01:00
|
|
|
ios::get(), int(syscall(::eventfd, ecount, eventfd_flags))
|
2018-02-06 07:42:40 +01:00
|
|
|
}
|
|
|
|
{
|
2018-12-27 21:04:48 +01:00
|
|
|
syscall<SYS_io_setup>(size_t(max_events), &idp);
|
2018-02-06 07:42:40 +01:00
|
|
|
set_handle();
|
2018-08-28 23:04:43 +02:00
|
|
|
log::debug
|
|
|
|
{
|
2018-12-27 21:04:48 +01:00
|
|
|
"Established AIO(%p) context (fd:%d max_events:%zu max_submit:%zu)",
|
|
|
|
this,
|
|
|
|
int(resfd.native_handle()),
|
|
|
|
size_t(max_events),
|
|
|
|
size_t(max_submit)
|
2018-08-28 23:04:43 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"Error starting AIO context %p :%s",
|
|
|
|
(const void *)this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-02-06 07:42:40 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::~system()
|
2018-08-28 23:04:43 +02:00
|
|
|
noexcept try
|
2018-02-06 07:42:40 +01:00
|
|
|
{
|
2018-12-27 19:36:27 +01:00
|
|
|
assert(qcount == 0);
|
2018-08-28 23:04:43 +02:00
|
|
|
const ctx::uninterruptible::nothrow ui;
|
|
|
|
|
2018-03-13 00:26:55 +01:00
|
|
|
interrupt();
|
2018-08-19 07:51:56 +02:00
|
|
|
wait();
|
2018-03-13 00:26:55 +01:00
|
|
|
|
|
|
|
boost::system::error_code ec;
|
|
|
|
resfd.close(ec);
|
|
|
|
|
2018-02-06 07:42:40 +01:00
|
|
|
syscall<SYS_io_destroy>(idp);
|
|
|
|
}
|
2018-08-28 23:04:43 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
"Error shutting down AIO context %p :%s",
|
|
|
|
(const void *)this,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
2018-02-06 07:42:40 +01:00
|
|
|
|
2018-03-13 00:26:55 +01:00
|
|
|
bool
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::interrupt()
|
2018-03-13 00:26:55 +01:00
|
|
|
{
|
|
|
|
if(!resfd.is_open())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
resfd.cancel();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::wait()
|
2018-03-13 00:26:55 +01:00
|
|
|
{
|
|
|
|
if(!resfd.is_open())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"Waiting for AIO context %p", this
|
|
|
|
};
|
|
|
|
|
|
|
|
dock.wait([this]
|
|
|
|
{
|
2018-12-27 21:04:48 +01:00
|
|
|
return ecount == uint64_t(-1);
|
2018-03-13 00:26:55 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-27 21:04:48 +01:00
|
|
|
void
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::wait(request &request)
|
2018-12-27 21:04:48 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(ctx::current == request.waiter);
|
|
|
|
while(request.retval == std::numeric_limits<ssize_t>::min())
|
|
|
|
ctx::wait();
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
// When the ctx is interrupted we're obligated to cancel the request.
|
|
|
|
// The handler callstack is invoked directly from here by cancel() for
|
|
|
|
// what it's worth but we rethrow the interrupt anyway.
|
|
|
|
cancel(request);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const ctx::terminated &)
|
|
|
|
{
|
|
|
|
cancel(request);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2018-12-21 19:30:04 +01:00
|
|
|
void
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::cancel(request &request)
|
2018-12-21 19:30:04 +01:00
|
|
|
{
|
2018-12-27 19:36:27 +01:00
|
|
|
const auto &cb
|
|
|
|
{
|
|
|
|
static_cast<iocb *>(&request)
|
|
|
|
};
|
2018-12-21 19:30:04 +01:00
|
|
|
|
2018-12-27 19:36:27 +01:00
|
|
|
const auto eit
|
2018-12-21 19:30:04 +01:00
|
|
|
{
|
2018-12-27 19:36:27 +01:00
|
|
|
std::remove(begin(queue), end(queue), cb)
|
|
|
|
};
|
2018-12-22 21:13:31 +01:00
|
|
|
|
2018-12-27 19:36:27 +01:00
|
|
|
const auto qcount
|
2018-12-21 19:30:04 +01:00
|
|
|
{
|
2018-12-27 19:36:27 +01:00
|
|
|
size_t(std::distance(begin(queue), eit))
|
|
|
|
};
|
|
|
|
|
|
|
|
// We know something was erased if the qcount no longer matches
|
|
|
|
const bool erased_from_queue
|
|
|
|
{
|
|
|
|
this->qcount > qcount
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make the qcount accurate again after any erasure.
|
|
|
|
assert(!erased_from_queue || this->qcount == qcount + 1);
|
|
|
|
assert(erased_from_queue || this->qcount == qcount);
|
|
|
|
if(erased_from_queue)
|
|
|
|
{
|
|
|
|
this->qcount--;
|
2018-12-27 21:04:48 +01:00
|
|
|
dock.notify_one();
|
2018-12-27 19:36:27 +01:00
|
|
|
stats.cur_queued--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup an io_event result which we will handle as a normal event
|
|
|
|
// immediately on this stack. We create our own cancel result if
|
2018-12-29 00:02:02 +01:00
|
|
|
// the request was not yet submitted to the system so the handler
|
2018-12-27 19:36:27 +01:00
|
|
|
// remains agnostic to our userspace queues.
|
|
|
|
io_event result {0};
|
|
|
|
if(erased_from_queue)
|
|
|
|
{
|
|
|
|
result.data = cb->aio_data;
|
|
|
|
result.obj = uintptr_t(cb);
|
|
|
|
result.res = -1;
|
|
|
|
result.res2 = ECANCELED;
|
2018-12-27 21:04:48 +01:00
|
|
|
} else {
|
|
|
|
syscall_nointr<SYS_io_cancel>(idp, cb, &result);
|
|
|
|
in_flight--;
|
|
|
|
stats.cur_submits--;
|
|
|
|
dock.notify_one();
|
2018-12-27 19:36:27 +01:00
|
|
|
}
|
2018-12-21 19:30:04 +01:00
|
|
|
|
2018-12-27 19:36:27 +01:00
|
|
|
handle_event(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::submit(request &request)
|
2018-12-27 19:36:27 +01:00
|
|
|
{
|
2018-12-30 05:02:22 +01:00
|
|
|
assert(request.opts);
|
2018-12-27 19:36:27 +01:00
|
|
|
assert(qcount < queue.size());
|
2018-12-27 21:04:48 +01:00
|
|
|
assert(qcount + in_flight < max_events);
|
2018-12-23 00:46:02 +01:00
|
|
|
assert(request.aio_data == uintptr_t(&request));
|
2018-12-27 19:36:27 +01:00
|
|
|
|
2018-12-23 00:46:02 +01:00
|
|
|
const ctx::critical_assertion ca;
|
2018-12-27 19:36:27 +01:00
|
|
|
queue.at(qcount++) = static_cast<iocb *>(&request);
|
|
|
|
stats.cur_queued++;
|
2018-12-27 00:16:02 +01:00
|
|
|
|
2018-12-27 23:01:05 +01:00
|
|
|
// Determine whether the user wants (or needs) to submit without delay.
|
2018-12-30 04:09:52 +01:00
|
|
|
const bool submit_now
|
2018-12-27 00:16:02 +01:00
|
|
|
{
|
2018-12-27 23:01:05 +01:00
|
|
|
// The nodelay flag is set
|
2018-12-30 05:02:22 +01:00
|
|
|
request.opts->nodelay
|
2018-12-27 23:01:05 +01:00
|
|
|
|
2018-12-27 00:16:02 +01:00
|
|
|
// The queue has reached the configured size
|
2018-12-27 23:01:05 +01:00
|
|
|
|| qcount >= size_t(max_submit)
|
2018-12-27 00:16:02 +01:00
|
|
|
|
|
|
|
// The queue has reached its maximum size
|
2018-12-27 19:36:27 +01:00
|
|
|
|| qcount >= queue.size()
|
2018-12-27 00:16:02 +01:00
|
|
|
};
|
|
|
|
|
2018-12-30 04:09:52 +01:00
|
|
|
const size_t submitted
|
2018-12-30 03:50:39 +01:00
|
|
|
{
|
2018-12-30 04:09:52 +01:00
|
|
|
submit_now? submit() : 0
|
2018-12-30 03:50:39 +01:00
|
|
|
};
|
2018-12-21 19:30:04 +01:00
|
|
|
|
2018-12-27 19:36:27 +01:00
|
|
|
if(qcount == 1)
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::post(std::bind(&system::chase, this));
|
2018-12-21 19:30:04 +01:00
|
|
|
}
|
|
|
|
|
2018-12-27 19:36:27 +01:00
|
|
|
/// The chaser is posted to the IRCd event loop after the first
|
|
|
|
/// request is queued. Ideally more requests will queue up before
|
|
|
|
/// the chaser is executed.
|
|
|
|
void
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::chase()
|
2018-12-30 03:50:39 +01:00
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
if(!qcount)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto submitted
|
|
|
|
{
|
2018-12-30 04:09:52 +01:00
|
|
|
submit()
|
2018-12-30 03:50:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(!qcount);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
2018-12-27 19:36:27 +01:00
|
|
|
{
|
2018-12-30 03:50:39 +01:00
|
|
|
throw assertive
|
|
|
|
{
|
|
|
|
"AIO(%p) system::chase() qcount:%zu :%s", this, qcount, e.what()
|
|
|
|
};
|
2018-12-27 19:36:27 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 04:09:52 +01:00
|
|
|
/// The submitter submits all queued requests and resets the count.
|
2018-12-30 03:50:39 +01:00
|
|
|
size_t
|
2018-12-30 04:09:52 +01:00
|
|
|
ircd::fs::aio::system::submit()
|
2018-12-30 03:50:39 +01:00
|
|
|
try
|
2018-12-27 19:36:27 +01:00
|
|
|
{
|
|
|
|
assert(qcount > 0);
|
2018-12-27 21:04:48 +01:00
|
|
|
assert(in_flight + qcount < MAX_EVENTS);
|
|
|
|
assert(in_flight + qcount <= size_t(max_events));
|
|
|
|
|
2018-12-30 03:50:39 +01:00
|
|
|
const auto submitted
|
|
|
|
{
|
|
|
|
syscall<SYS_io_submit>(idp, qcount, queue.data())
|
|
|
|
};
|
2018-12-27 21:04:48 +01:00
|
|
|
|
2018-12-30 03:50:39 +01:00
|
|
|
stats.cur_submits += submitted;
|
|
|
|
stats.cur_queued -= submitted;
|
2018-12-27 19:36:27 +01:00
|
|
|
stats.submits++;
|
2018-12-27 21:04:48 +01:00
|
|
|
|
2018-12-30 03:50:39 +01:00
|
|
|
in_flight += submitted;
|
|
|
|
qcount -= submitted;
|
2018-12-30 05:15:24 +01:00
|
|
|
assert(!qcount);
|
2018-12-30 03:50:39 +01:00
|
|
|
return submitted;
|
2018-12-27 19:36:27 +01:00
|
|
|
}
|
2018-12-30 03:50:39 +01:00
|
|
|
catch(const std::system_error &e)
|
2018-12-27 19:36:27 +01:00
|
|
|
{
|
2018-12-30 03:50:39 +01:00
|
|
|
using std::errc;
|
|
|
|
switch(e.code().value())
|
2018-12-27 19:36:27 +01:00
|
|
|
{
|
2018-12-30 03:50:39 +01:00
|
|
|
// EAGAIN may be thrown to prevent blocking. TODO: handle
|
|
|
|
case int(errc::resource_unavailable_try_again):
|
|
|
|
//throw;
|
|
|
|
|
|
|
|
// Manpages sez that EBADF is thrown if the fd in the FIRST iocb has
|
|
|
|
// an issue. TODO: handle this by tossing the first iocb and continue.
|
|
|
|
case int(errc::bad_file_descriptor):
|
|
|
|
//throw;
|
|
|
|
|
|
|
|
// All other errors unexpected.
|
|
|
|
default: ircd::terminate{ircd::error
|
|
|
|
{
|
2018-12-30 04:09:52 +01:00
|
|
|
"AIO(%p) system::submit() qcount:%zu :%s", this, qcount, e.what()
|
2018-12-30 03:50:39 +01:00
|
|
|
}};
|
|
|
|
}
|
2018-12-21 19:30:04 +01:00
|
|
|
}
|
|
|
|
|
2018-02-06 07:42:40 +01:00
|
|
|
void
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::set_handle()
|
2018-02-06 07:42:40 +01:00
|
|
|
{
|
2018-12-27 21:04:48 +01:00
|
|
|
ecount = 0;
|
2018-11-28 03:09:12 +01:00
|
|
|
|
|
|
|
const asio::mutable_buffers_1 bufs
|
|
|
|
{
|
2018-12-27 21:04:48 +01:00
|
|
|
&ecount, sizeof(ecount)
|
2018-11-28 03:09:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
auto handler
|
|
|
|
{
|
2018-12-29 00:02:02 +01:00
|
|
|
std::bind(&system::handle, this, ph::_1, ph::_2)
|
2018-11-28 03:09:12 +01:00
|
|
|
};
|
|
|
|
|
2018-02-06 07:42:40 +01:00
|
|
|
asio::async_read(resfd, bufs, std::move(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle notifications that requests are complete.
|
|
|
|
void
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::handle(const boost::system::error_code &ec,
|
2018-11-28 03:09:12 +01:00
|
|
|
const size_t bytes)
|
2018-03-13 00:26:55 +01:00
|
|
|
noexcept try
|
2018-02-06 07:42:40 +01:00
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
namespace errc = boost::system::errc;
|
|
|
|
|
2018-12-27 21:04:48 +01:00
|
|
|
assert((bytes == 8 && !ec && ecount >= 1) || (bytes == 0 && ec));
|
2018-02-06 07:42:40 +01:00
|
|
|
assert(!ec || ec.category() == asio::error::get_system_category());
|
|
|
|
|
|
|
|
switch(ec.value())
|
|
|
|
{
|
2018-11-09 06:18:39 +01:00
|
|
|
case errc::success:
|
2018-02-06 07:42:40 +01:00
|
|
|
handle_events();
|
|
|
|
break;
|
|
|
|
|
2018-11-09 06:18:39 +01:00
|
|
|
case errc::operation_canceled:
|
2018-03-13 00:26:55 +01:00
|
|
|
throw ctx::interrupted();
|
2018-02-06 07:42:40 +01:00
|
|
|
|
|
|
|
default:
|
2018-11-09 06:18:39 +01:00
|
|
|
throw_system_error(ec);
|
2018-02-06 07:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
set_handle();
|
|
|
|
}
|
2018-03-13 00:26:55 +01:00
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
"AIO context %p interrupted", this
|
|
|
|
};
|
|
|
|
|
2018-12-27 21:04:48 +01:00
|
|
|
ecount = -1;
|
2018-03-13 00:26:55 +01:00
|
|
|
dock.notify_all();
|
|
|
|
}
|
2018-02-06 07:42:40 +01:00
|
|
|
|
|
|
|
void
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::handle_events()
|
2018-02-06 07:42:40 +01:00
|
|
|
noexcept try
|
|
|
|
{
|
2018-03-25 09:03:11 +02:00
|
|
|
assert(!ctx::current);
|
2018-02-06 07:42:40 +01:00
|
|
|
|
|
|
|
// The number of completed requests available in events[]. This syscall
|
|
|
|
// is restarted on EINTR. After restart, it may or may not find any ready
|
|
|
|
// events but it never blocks to do so.
|
|
|
|
const auto count
|
|
|
|
{
|
|
|
|
syscall_nointr<SYS_io_getevents>(idp, 0, event.size(), event.data(), nullptr)
|
|
|
|
};
|
|
|
|
|
|
|
|
// The count should be at least 1 event. The only reason to return 0 might
|
|
|
|
// be related to an INTR; this assert will find out and may be commented.
|
2018-08-19 07:58:43 +02:00
|
|
|
//assert(count > 0);
|
2018-11-28 04:53:32 +01:00
|
|
|
assert(count >= 0);
|
|
|
|
|
2018-12-27 21:04:48 +01:00
|
|
|
in_flight -= count;
|
|
|
|
stats.cur_submits -= count;
|
2018-11-29 20:04:10 +01:00
|
|
|
stats.handles++;
|
2018-12-27 21:04:48 +01:00
|
|
|
if(likely(count))
|
|
|
|
dock.notify_one();
|
2018-02-06 07:42:40 +01:00
|
|
|
|
|
|
|
for(ssize_t i(0); i < count; ++i)
|
|
|
|
handle_event(event[i]);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-02-20 03:46:56 +01:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"AIO(%p) handle_events: %s",
|
|
|
|
this,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-02-06 07:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-12-29 00:02:02 +01:00
|
|
|
ircd::fs::aio::system::handle_event(const io_event &event)
|
2018-02-06 07:42:40 +01:00
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
// Our extended control block is passed in event.data
|
2018-02-20 03:46:56 +01:00
|
|
|
auto &request
|
2018-02-06 07:42:40 +01:00
|
|
|
{
|
2018-02-20 03:46:56 +01:00
|
|
|
*reinterpret_cast<aio::request *>(event.data)
|
2018-02-06 07:42:40 +01:00
|
|
|
};
|
|
|
|
|
2018-12-21 19:30:04 +01:00
|
|
|
auto *const iocb(reinterpret_cast<struct ::iocb *>(event.obj));
|
|
|
|
assert(iocb == static_cast<struct ::iocb *>(&request));
|
|
|
|
assert(reinterpret_cast<aio::request *>(iocb->aio_data) == &request);
|
2018-08-16 16:16:13 +02:00
|
|
|
assert(event.res2 >= 0);
|
2018-02-06 07:42:40 +01:00
|
|
|
assert(event.res == -1 || event.res2 == 0);
|
|
|
|
|
|
|
|
// Set result indicators
|
2018-08-16 16:16:13 +02:00
|
|
|
request.retval = std::max(event.res, -1LL);
|
|
|
|
request.errcode = event.res >= -1? event.res2 : std::abs(event.res);
|
2018-02-20 03:46:56 +01:00
|
|
|
|
2018-08-21 07:45:06 +02:00
|
|
|
// Notify the waiting context. Note that we are on the main async stack
|
|
|
|
// but it is safe to notify from here. The waiter may be null if it left.
|
|
|
|
assert(!request.waiter || request.waiter != ctx::current);
|
|
|
|
assert(ctx::current == nullptr);
|
|
|
|
if(likely(request.waiter))
|
2018-02-20 03:46:56 +01:00
|
|
|
ctx::notify(*request.waiter);
|
2018-12-27 21:04:48 +01:00
|
|
|
|
|
|
|
stats.events++;
|
2018-02-06 07:42:40 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-02-20 03:46:56 +01:00
|
|
|
log::critical
|
|
|
|
{
|
|
|
|
"Unhandled request(%lu) event(%p) error: %s",
|
|
|
|
event.data,
|
|
|
|
&event,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-02-06 07:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-12-20 22:40:30 +01:00
|
|
|
// internal util
|
2018-02-06 07:42:40 +01:00
|
|
|
//
|
|
|
|
|
2018-12-27 19:35:45 +01:00
|
|
|
/// Translate an ircd::fs opts priority integer to an AIO priority integer.
|
|
|
|
/// The ircd::fs priority integer is like a nice value. The AIO value is
|
|
|
|
/// positive [0, MAX_REQPRIO]. This function takes an ircd::fs value and
|
|
|
|
/// shifts it to the AIO value.
|
2018-12-20 22:40:30 +01:00
|
|
|
int
|
|
|
|
ircd::fs::aio::reqprio(int input)
|
2018-02-06 07:42:40 +01:00
|
|
|
{
|
2018-12-27 19:35:45 +01:00
|
|
|
static const auto median
|
|
|
|
{
|
|
|
|
int(MAX_REQPRIO / 2)
|
|
|
|
};
|
2018-11-28 04:54:24 +01:00
|
|
|
|
2018-12-27 19:35:45 +01:00
|
|
|
input = std::max(input, 0 - median);
|
|
|
|
input = std::min(input, median);
|
|
|
|
input = MAX_REQPRIO - (input + median);
|
|
|
|
assert(input >= 0 && input <= int(MAX_REQPRIO));
|
2018-12-20 22:40:30 +01:00
|
|
|
return input;
|
2018-11-28 04:54:24 +01:00
|
|
|
}
|