2018-10-17 14:12:10 +02: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 <ircd/asio.h>
|
|
|
|
|
2018-11-02 04:14:00 +01:00
|
|
|
/// Boost version indicator for compiled header files.
|
|
|
|
decltype(ircd::boost_version)
|
|
|
|
ircd::boost_version
|
|
|
|
{
|
|
|
|
BOOST_VERSION / 100000,
|
|
|
|
BOOST_VERSION / 100 % 1000,
|
|
|
|
BOOST_VERSION % 100,
|
|
|
|
};
|
|
|
|
|
|
|
|
char ircd_boost_version_str_buf[32];
|
|
|
|
decltype(ircd::boost_version_str)
|
|
|
|
ircd::boost_version_str
|
|
|
|
(
|
|
|
|
ircd_boost_version_str_buf,
|
|
|
|
::snprintf(ircd_boost_version_str_buf, sizeof(ircd_boost_version_str_buf),
|
|
|
|
"%u.%u.%u",
|
|
|
|
boost_version[0],
|
|
|
|
boost_version[1],
|
|
|
|
boost_version[2])
|
|
|
|
);
|
|
|
|
|
2018-10-17 14:12:10 +02:00
|
|
|
/// Record of the ID of the thread static initialization took place on.
|
|
|
|
decltype(ircd::ios::static_thread_id)
|
|
|
|
ircd::ios::static_thread_id
|
|
|
|
{
|
|
|
|
std::this_thread::get_id()
|
|
|
|
};
|
|
|
|
|
|
|
|
/// "main" thread for IRCd; the one the main context landed on.
|
|
|
|
decltype(ircd::ios::main_thread_id)
|
|
|
|
ircd::ios::main_thread_id;
|
|
|
|
|
|
|
|
decltype(ircd::ios::user)
|
|
|
|
ircd::ios::user;
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ios::init(asio::io_context &user)
|
|
|
|
{
|
|
|
|
// Sample the ID of this thread. Since this is the first transfer of
|
|
|
|
// control to libircd after static initialization we have nothing to
|
|
|
|
// consider a main thread yet. We need something set for many assertions
|
|
|
|
// to pass until ircd::main() is entered which will reset this to where
|
|
|
|
// ios.run() is really running.
|
|
|
|
main_thread_id = std::this_thread::get_id();
|
|
|
|
|
|
|
|
// Set a reference to the user's ios_service
|
|
|
|
ios::user = &user;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ios::post(std::function<void ()> function)
|
|
|
|
{
|
2019-03-27 01:29:50 +01:00
|
|
|
static descriptor descriptor
|
|
|
|
{
|
|
|
|
"ircd::ios post"
|
|
|
|
};
|
|
|
|
|
|
|
|
post(descriptor, std::move(function));
|
2018-10-17 14:12:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ios::dispatch(std::function<void ()> function)
|
|
|
|
{
|
2019-03-27 01:29:50 +01:00
|
|
|
static descriptor descriptor
|
|
|
|
{
|
|
|
|
"ircd::ios dispatch"
|
|
|
|
};
|
|
|
|
|
|
|
|
dispatch(descriptor, std::move(function));
|
2018-10-17 14:12:10 +02:00
|
|
|
}
|
|
|
|
|
2019-03-27 00:53:31 +01:00
|
|
|
void
|
|
|
|
ircd::ios::post(descriptor &descriptor,
|
|
|
|
std::function<void ()> function)
|
|
|
|
{
|
|
|
|
boost::asio::post(get(), handle(descriptor, std::move(function)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ios::dispatch(descriptor &descriptor,
|
|
|
|
std::function<void ()> function)
|
|
|
|
{
|
|
|
|
boost::asio::dispatch(get(), handle(descriptor, std::move(function)));
|
|
|
|
}
|
|
|
|
|
2018-10-17 14:12:10 +02:00
|
|
|
boost::asio::io_context &
|
|
|
|
ircd::ios::get()
|
|
|
|
{
|
|
|
|
assert(user);
|
|
|
|
return *user;
|
|
|
|
}
|
2018-12-28 22:05:03 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::ios::available()
|
|
|
|
{
|
|
|
|
return bool(user);
|
|
|
|
}
|
2019-03-27 00:53:31 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// descriptor
|
|
|
|
//
|
|
|
|
|
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::ios::descriptor>::list)
|
|
|
|
ircd::util::instance_list<ircd::ios::descriptor>::list
|
|
|
|
{};
|
|
|
|
|
|
|
|
decltype(ircd::ios::descriptor::ids)
|
|
|
|
ircd::ios::descriptor::ids;
|
|
|
|
|
|
|
|
//
|
|
|
|
// descriptor::descriptor
|
|
|
|
//
|
|
|
|
|
2019-03-27 10:49:28 +01:00
|
|
|
ircd::ios::descriptor::descriptor(const string_view &name,
|
|
|
|
const decltype(allocator) &allocator,
|
2019-03-29 00:56:55 +01:00
|
|
|
const decltype(deallocator) &deallocator,
|
|
|
|
const bool &continuation)
|
2019-03-27 00:53:31 +01:00
|
|
|
:name{name}
|
2019-03-29 01:09:46 +01:00
|
|
|
,stats{std::make_unique<struct stats>()}
|
2019-03-27 10:49:28 +01:00
|
|
|
,allocator{allocator}
|
|
|
|
,deallocator{deallocator}
|
2019-03-29 00:56:55 +01:00
|
|
|
,continuation{continuation}
|
2019-03-27 00:53:31 +01:00
|
|
|
{
|
2019-03-27 10:49:28 +01:00
|
|
|
assert(allocator);
|
|
|
|
assert(deallocator);
|
2019-03-27 00:53:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ios::descriptor::~descriptor()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-27 10:49:28 +01:00
|
|
|
void
|
|
|
|
ircd::ios::descriptor::default_deallocator(handler &handler,
|
|
|
|
void *const &ptr,
|
|
|
|
const size_t &size)
|
|
|
|
{
|
|
|
|
::operator delete(ptr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
ircd::ios::descriptor::default_allocator(handler &handler,
|
|
|
|
const size_t &size)
|
|
|
|
{
|
|
|
|
return ::operator new(size);
|
|
|
|
}
|
|
|
|
|
2019-03-29 01:09:46 +01:00
|
|
|
//
|
|
|
|
// descriptor::stats
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::ios::descriptor::stats::stats()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ios::descriptor::stats::~stats()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-29 01:16:47 +01:00
|
|
|
struct ircd::ios::descriptor::stats &
|
2019-03-29 01:09:46 +01:00
|
|
|
ircd::ios::descriptor::stats::operator+=(const stats &o)
|
|
|
|
&
|
|
|
|
{
|
2019-04-11 07:52:33 +02:00
|
|
|
queued += o.queued;
|
2019-03-29 01:09:46 +01:00
|
|
|
calls += o.calls;
|
|
|
|
faults += o.faults;
|
|
|
|
allocs += o.allocs;
|
|
|
|
alloc_bytes += o.alloc_bytes;
|
|
|
|
frees += o.frees;
|
|
|
|
free_bytes += o.free_bytes;
|
|
|
|
slice_total += o.slice_total;
|
|
|
|
slice_last += o.slice_last;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-27 00:53:31 +01:00
|
|
|
//
|
|
|
|
// handler
|
|
|
|
//
|
|
|
|
|
2019-03-27 10:06:55 +01:00
|
|
|
decltype(ircd::ios::handler::current) thread_local
|
|
|
|
ircd::ios::handler::current;
|
|
|
|
|
2019-03-27 00:53:31 +01:00
|
|
|
bool
|
|
|
|
ircd::ios::handler::fault(handler *const &handler)
|
|
|
|
{
|
|
|
|
assert(handler && handler->descriptor);
|
|
|
|
auto &descriptor(*handler->descriptor);
|
2019-03-27 06:03:48 +01:00
|
|
|
|
2019-03-29 01:09:46 +01:00
|
|
|
assert(descriptor.stats);
|
|
|
|
auto &stats(*descriptor.stats);
|
|
|
|
++stats.faults;
|
|
|
|
|
|
|
|
bool ret(false);
|
2019-03-27 06:03:48 +01:00
|
|
|
// leave() isn't called if we return false so the tsc counter
|
|
|
|
// needs to be tied off here instead.
|
|
|
|
if(!ret)
|
|
|
|
{
|
2019-04-02 20:09:37 +02:00
|
|
|
stats.slice_last = cycles() - handler->slice_start;
|
2019-03-29 01:09:46 +01:00
|
|
|
stats.slice_total += stats.slice_last;
|
2019-03-30 21:11:55 +01:00
|
|
|
|
|
|
|
assert(handler::current == handler);
|
|
|
|
handler::current = nullptr;
|
2019-03-27 06:03:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2019-03-27 00:53:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ios::handler::leave(handler *const &handler)
|
|
|
|
{
|
|
|
|
assert(handler && handler->descriptor);
|
|
|
|
auto &descriptor(*handler->descriptor);
|
2019-03-29 01:09:46 +01:00
|
|
|
|
|
|
|
assert(descriptor.stats);
|
|
|
|
auto &stats(*descriptor.stats);
|
2019-04-02 20:09:37 +02:00
|
|
|
stats.slice_last = cycles() - handler->slice_start;
|
2019-03-29 01:09:46 +01:00
|
|
|
stats.slice_total += stats.slice_last;
|
|
|
|
|
2019-03-27 10:06:55 +01:00
|
|
|
assert(handler::current == handler);
|
|
|
|
handler::current = nullptr;
|
2019-03-27 00:53:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::ios::handler::enter(handler *const &handler)
|
|
|
|
{
|
|
|
|
assert(handler && handler->descriptor);
|
|
|
|
auto &descriptor(*handler->descriptor);
|
2019-03-29 01:09:46 +01:00
|
|
|
|
|
|
|
assert(descriptor.stats);
|
|
|
|
auto &stats(*descriptor.stats);
|
|
|
|
++stats.calls;
|
|
|
|
|
2019-03-27 10:06:55 +01:00
|
|
|
assert(!handler::current);
|
|
|
|
handler::current = handler;
|
2019-04-02 20:09:37 +02:00
|
|
|
handler->slice_start = cycles();
|
2019-03-27 00:53:31 +01:00
|
|
|
}
|
2019-03-27 05:22:22 +01:00
|
|
|
|
2019-03-29 00:56:55 +01:00
|
|
|
bool
|
|
|
|
ircd::ios::handler::continuation(handler *const &handler)
|
|
|
|
{
|
|
|
|
assert(handler && handler->descriptor);
|
|
|
|
auto &descriptor(*handler->descriptor);
|
|
|
|
return descriptor.continuation;
|
|
|
|
}
|
|
|
|
|
2019-03-27 05:22:22 +01:00
|
|
|
void
|
|
|
|
ircd::ios::handler::deallocate(handler *const &handler,
|
|
|
|
void *const &ptr,
|
|
|
|
const size_t &size)
|
|
|
|
{
|
|
|
|
assert(handler && handler->descriptor);
|
|
|
|
auto &descriptor(*handler->descriptor);
|
2019-03-29 01:09:46 +01:00
|
|
|
|
2019-03-27 10:49:28 +01:00
|
|
|
descriptor.deallocator(*handler, ptr, size);
|
2019-03-29 01:09:46 +01:00
|
|
|
|
|
|
|
assert(descriptor.stats);
|
|
|
|
auto &stats(*descriptor.stats);
|
|
|
|
stats.free_bytes += size;
|
|
|
|
++stats.frees;
|
2019-03-27 05:22:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
ircd::ios::handler::allocate(handler *const &handler,
|
|
|
|
const size_t &size)
|
|
|
|
{
|
|
|
|
assert(handler && handler->descriptor);
|
|
|
|
auto &descriptor(*handler->descriptor);
|
2019-03-29 01:09:46 +01:00
|
|
|
|
|
|
|
assert(descriptor.stats);
|
|
|
|
auto &stats(*descriptor.stats);
|
|
|
|
stats.alloc_bytes += size;
|
|
|
|
++stats.allocs;
|
|
|
|
|
2019-03-27 10:49:28 +01:00
|
|
|
return descriptor.allocator(*handler, size);
|
2019-03-27 05:22:22 +01:00
|
|
|
}
|