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.
|
|
|
|
|
2020-12-07 20:19:33 +01:00
|
|
|
/// Logging facility
|
|
|
|
decltype(ircd::ios::log)
|
|
|
|
ircd::ios::log
|
|
|
|
{
|
|
|
|
"ios"
|
|
|
|
};
|
|
|
|
|
2018-10-17 14:12:10 +02:00
|
|
|
/// "main" thread for IRCd; the one the main context landed on.
|
|
|
|
decltype(ircd::ios::main_thread_id)
|
2021-01-03 13:12:56 +01:00
|
|
|
ircd::ios::main_thread_id
|
|
|
|
{
|
|
|
|
std::this_thread::get_id()
|
|
|
|
};
|
2018-10-17 14:12:10 +02:00
|
|
|
|
2021-03-11 07:49:18 +01:00
|
|
|
/// True only for the main thread.
|
|
|
|
decltype(ircd::ios::is_main_thread)
|
|
|
|
thread_local
|
|
|
|
ircd::ios::is_main_thread;
|
|
|
|
|
2021-12-22 00:07:04 +01:00
|
|
|
/// The embedder/executable's (library user) provided executor handle.
|
2018-10-17 14:12:10 +02:00
|
|
|
decltype(ircd::ios::user)
|
|
|
|
ircd::ios::user;
|
|
|
|
|
2021-12-22 00:07:04 +01:00
|
|
|
/// Our library's execution strand instance.
|
|
|
|
decltype(ircd::ios::primary)
|
|
|
|
ircd::ios::primary;
|
|
|
|
|
|
|
|
/// Our library-specific/isolate executor handle.
|
2020-02-27 19:11:59 +01:00
|
|
|
decltype(ircd::ios::main)
|
|
|
|
ircd::ios::main;
|
|
|
|
|
2019-06-01 01:06:55 +02:00
|
|
|
decltype(ircd::boost_version_api)
|
|
|
|
ircd::boost_version_api
|
|
|
|
{
|
|
|
|
"boost", info::versions::API, BOOST_VERSION,
|
|
|
|
{
|
|
|
|
BOOST_VERSION / 100000,
|
|
|
|
BOOST_VERSION / 100 % 1000,
|
|
|
|
BOOST_VERSION % 100,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::boost_version_abi)
|
|
|
|
ircd::boost_version_abi
|
|
|
|
{
|
2019-06-02 23:21:40 +02:00
|
|
|
"boost", info::versions::ABI //TODO: get this
|
2019-06-01 01:06:55 +02:00
|
|
|
};
|
|
|
|
|
2022-06-14 23:15:53 +02:00
|
|
|
[[gnu::visibility("hidden")]]
|
2018-10-17 14:12:10 +02:00
|
|
|
void
|
2020-02-27 19:11:59 +01:00
|
|
|
ircd::ios::init(asio::executor &&user)
|
2018-10-17 14:12:10 +02:00
|
|
|
{
|
|
|
|
// 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
|
2020-05-05 01:07:30 +02:00
|
|
|
// to pass.
|
2018-10-17 14:12:10 +02:00
|
|
|
main_thread_id = std::this_thread::get_id();
|
|
|
|
|
2021-03-11 07:49:18 +01:00
|
|
|
// Set the thread-local bit to true; all other threads will see false.
|
|
|
|
is_main_thread = true;
|
|
|
|
|
2021-12-22 00:07:04 +01:00
|
|
|
// Save a reference handle to the user's executor.
|
|
|
|
ios::user = user;
|
|
|
|
|
|
|
|
// Create our strand instance.
|
|
|
|
ios::primary.emplace(static_cast<asio::io_context &>(user.context()));
|
2020-02-27 19:11:59 +01:00
|
|
|
|
2021-12-22 00:07:04 +01:00
|
|
|
// Set the reference handle to our executor.
|
|
|
|
ios::main = *ios::primary;
|
2018-10-17 14:12:10 +02:00
|
|
|
}
|
|
|
|
|
2022-06-29 22:53:37 +02:00
|
|
|
[[using gnu: cold]]
|
|
|
|
void
|
|
|
|
ircd::ios::exiting_quick()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(run::level == run::level::HALT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(ircd::debugmode)
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log::star, log::level::DEBUG,
|
|
|
|
"IRCd notified of quick exit() in runlevel:%s ctx:%p",
|
|
|
|
reflect(run::level),
|
|
|
|
(const void *)ctx::current,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[using gnu: cold]]
|
|
|
|
void
|
|
|
|
ircd::ios::exiting()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(run::level == run::level::HALT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(ircd::debugmode)
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log::star, log::level::DEBUG,
|
|
|
|
"IRCd notified of exit() in runlevel:%s ctx:%p",
|
|
|
|
reflect(run::level),
|
|
|
|
(const void *)ctx::current,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[using gnu: cold]]
|
|
|
|
void
|
|
|
|
ircd::ios::continuing()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(run::level == run::level::HALT)
|
|
|
|
return;
|
|
|
|
|
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log::star, log::level::DEBUG,
|
|
|
|
"IRCd notified of SIGCONT in runlevel:%s ctx:%p",
|
|
|
|
reflect(run::level),
|
|
|
|
(const void *)ctx::current,
|
|
|
|
};
|
|
|
|
|
|
|
|
switch(run::level)
|
|
|
|
{
|
|
|
|
case run::level::RUN:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
log::star, "IRCd resuming service in runlevel %s.",
|
|
|
|
reflect(run::level),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
[[using gnu: cold]]
|
2020-12-13 01:07:42 +01:00
|
|
|
void
|
|
|
|
ircd::ios::forking()
|
|
|
|
{
|
2021-12-22 00:07:04 +01:00
|
|
|
#if BOOST_VERSION >= 107000 && BOOST_VERSION < 107400
|
2020-12-13 01:07:42 +01:00
|
|
|
get().context().notify_fork(asio::execution_context::fork_prepare);
|
|
|
|
#else
|
|
|
|
get().notify_fork(asio::execution_context::fork_prepare);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-06-29 22:53:37 +02:00
|
|
|
[[using gnu: cold]]
|
2020-12-13 01:07:42 +01:00
|
|
|
void
|
|
|
|
ircd::ios::forked_child()
|
|
|
|
{
|
2021-12-22 00:07:04 +01:00
|
|
|
#if BOOST_VERSION >= 107000 && BOOST_VERSION < 107400
|
2020-12-13 01:07:42 +01:00
|
|
|
get().context().notify_fork(asio::execution_context::fork_child);
|
|
|
|
#else
|
|
|
|
get().notify_fork(asio::execution_context::fork_child);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-06-29 22:53:37 +02:00
|
|
|
[[using gnu: cold]]
|
2020-12-13 01:07:42 +01:00
|
|
|
void
|
|
|
|
ircd::ios::forked_parent()
|
|
|
|
{
|
2021-12-22 00:07:04 +01:00
|
|
|
#if BOOST_VERSION >= 107000 && BOOST_VERSION < 107400
|
2020-12-13 01:07:42 +01:00
|
|
|
get().context().notify_fork(asio::execution_context::fork_parent);
|
|
|
|
#else
|
|
|
|
get().notify_fork(asio::execution_context::fork_parent);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::ios::available()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return bool(main);
|
|
|
|
}
|
|
|
|
|
2020-12-14 07:44:33 +01:00
|
|
|
//
|
|
|
|
// emption
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::ios::empt
|
|
|
|
{
|
|
|
|
[[gnu::visibility("internal")]]
|
|
|
|
extern const string_view freq_help;
|
|
|
|
|
|
|
|
[[gnu::visibility("internal")]]
|
|
|
|
extern uint64_t stats[9];
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::ios::empt::freq_help)
|
|
|
|
ircd::ios::empt::freq_help
|
|
|
|
{R"(
|
|
|
|
Coarse frequency to make non-blocking polls to the kernel for events at the
|
|
|
|
beginning of every iteration of the core event loop. boost::asio takes an
|
|
|
|
opportunity to first make a non-blocking poll to gather more events from
|
|
|
|
the kernel even when one or more tasks are already queued, this setting
|
|
|
|
allows more such tasks to first be executed and reduce syscall overhead
|
2022-02-21 20:04:33 +01:00
|
|
|
including a large number of unnecessary calls as would be the case by
|
|
|
|
default without this.
|
2020-12-14 07:44:33 +01:00
|
|
|
|
|
|
|
When the frequency is set to 1, the above-described default behavior is
|
|
|
|
unaltered. When greater than 1, voluntary non-blocking polls are only made
|
|
|
|
after N number of tasks. This reduces syscalls to increase overall
|
|
|
|
performance, but may cost in responsiveness and cause stalls. For example,
|
|
|
|
when set to 2, kernel context-switch is made every other userspace context
|
|
|
|
switch. When set to 0, voluntary non-blocking polls are never made.
|
|
|
|
|
|
|
|
This value may be rounded down to nearest base2 so we can avoid invoking
|
|
|
|
the FPU in the core event loop's codepath.
|
|
|
|
)"};
|
|
|
|
|
|
|
|
decltype(ircd::ios::empt::stats)
|
|
|
|
ircd::ios::empt::stats;
|
|
|
|
|
|
|
|
/// Voluntary kernel poll frequency.
|
|
|
|
decltype(ircd::ios::empt::freq)
|
|
|
|
ircd::ios::empt::freq
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ios.empt.freq" },
|
2020-12-25 17:24:23 +01:00
|
|
|
{ "default", 512 },
|
2020-12-14 07:44:33 +01:00
|
|
|
{ "help", freq_help },
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Non-blocking call count.
|
|
|
|
decltype(ircd::ios::empt::peek)
|
|
|
|
ircd::ios::empt::peek
|
|
|
|
{
|
|
|
|
stats + 0,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ios.empt.peek" },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Skipped call count.
|
|
|
|
decltype(ircd::ios::empt::skip)
|
|
|
|
ircd::ios::empt::skip
|
|
|
|
{
|
|
|
|
stats + 1,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ios.empt.skip" },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Non-skipped call count.
|
|
|
|
decltype(ircd::ios::empt::call)
|
|
|
|
ircd::ios::empt::call
|
|
|
|
{
|
|
|
|
stats + 2,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ios.empt.call" },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Count of calls which reported zero ready events.
|
|
|
|
decltype(ircd::ios::empt::none)
|
|
|
|
ircd::ios::empt::none
|
|
|
|
{
|
|
|
|
stats + 3,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ios.empt.none" },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Total number of events reported from all calls.
|
|
|
|
decltype(ircd::ios::empt::result)
|
|
|
|
ircd::ios::empt::result
|
|
|
|
{
|
|
|
|
stats + 4,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ios.empt.result" },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Count of calls which reported more events than the low threshold.
|
|
|
|
decltype(ircd::ios::empt::load_low)
|
|
|
|
ircd::ios::empt::load_low
|
|
|
|
{
|
|
|
|
stats + 5,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ios.empt.load.low" },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Count of calls which reported more events than the medium threshold.
|
|
|
|
decltype(ircd::ios::empt::load_med)
|
|
|
|
ircd::ios::empt::load_med
|
|
|
|
{
|
|
|
|
stats + 6,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ios.empt.load.med" },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Count of calls which reported more events than the high threshold.
|
|
|
|
decltype(ircd::ios::empt::load_high)
|
|
|
|
ircd::ios::empt::load_high
|
|
|
|
{
|
|
|
|
stats + 7,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ios.empt.load.high" },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Count of calls which reported the maximum number of events.
|
|
|
|
decltype(ircd::ios::empt::load_stall)
|
|
|
|
ircd::ios::empt::load_stall
|
|
|
|
{
|
|
|
|
stats + 8,
|
|
|
|
{
|
|
|
|
{ "name", "ircd.ios.empt.load.stall" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-27 00:53:31 +01:00
|
|
|
//
|
|
|
|
// descriptor
|
|
|
|
//
|
|
|
|
|
2019-04-17 05:48:00 +02:00
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::ios::descriptor>::allocator)
|
|
|
|
ircd::util::instance_list<ircd::ios::descriptor>::allocator
|
|
|
|
{};
|
|
|
|
|
2019-03-27 00:53:31 +01:00
|
|
|
template<>
|
|
|
|
decltype(ircd::util::instance_list<ircd::ios::descriptor>::list)
|
|
|
|
ircd::util::instance_list<ircd::ios::descriptor>::list
|
2019-04-17 05:48:00 +02:00
|
|
|
{
|
|
|
|
allocator
|
|
|
|
};
|
2019-03-27 00:53:31 +01:00
|
|
|
|
|
|
|
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)
|
2020-12-11 22:58:22 +01:00
|
|
|
noexcept
|
2019-09-22 23:41:31 +02:00
|
|
|
:name
|
|
|
|
{
|
|
|
|
name
|
|
|
|
}
|
|
|
|
,stats
|
|
|
|
{
|
2020-12-18 05:29:00 +01:00
|
|
|
std::make_unique<struct stats>(*this)
|
2019-09-22 23:41:31 +02:00
|
|
|
}
|
|
|
|
,allocator
|
|
|
|
{
|
|
|
|
allocator?: default_allocator
|
|
|
|
}
|
|
|
|
,deallocator
|
|
|
|
{
|
|
|
|
deallocator?: default_deallocator
|
|
|
|
}
|
2020-08-02 14:45:35 +02:00
|
|
|
,history
|
|
|
|
(
|
|
|
|
256, {0}
|
|
|
|
)
|
2020-08-21 03:00:48 +02:00
|
|
|
,history_pos
|
|
|
|
{
|
|
|
|
0
|
|
|
|
}
|
2019-09-22 23:41:31 +02:00
|
|
|
,continuation
|
|
|
|
{
|
|
|
|
continuation
|
|
|
|
}
|
2019-03-27 00:53:31 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ios::descriptor::~descriptor()
|
|
|
|
noexcept
|
|
|
|
{
|
2022-04-29 04:44:05 +02:00
|
|
|
if(likely(stats) && unlikely(stats->queued))
|
2022-04-29 04:45:08 +02:00
|
|
|
log::critical
|
2022-04-29 04:44:05 +02:00
|
|
|
{
|
2022-04-29 04:45:08 +02:00
|
|
|
log, "descriptor(%p) '%s' leaking queued:%zd allocs:%zd bytes:%zd",
|
2022-04-29 04:44:05 +02:00
|
|
|
this,
|
|
|
|
name,
|
2022-04-29 04:45:08 +02:00
|
|
|
ssize_t(stats->queued),
|
|
|
|
ssize_t(stats->allocs) - ssize_t(stats->frees),
|
|
|
|
ssize_t(stats->alloc_bytes) - ssize_t(stats->free_bytes),
|
2022-04-29 04:44:05 +02:00
|
|
|
};
|
|
|
|
|
2022-04-29 04:45:08 +02:00
|
|
|
assert(!stats || stats->queued == 0);
|
2020-10-06 06:27:28 +02:00
|
|
|
assert(!stats || stats->allocs == stats->frees);
|
|
|
|
assert(!stats || stats->alloc_bytes == stats->free_bytes);
|
2019-03-27 00:53:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 01:09:46 +01:00
|
|
|
//
|
|
|
|
// descriptor::stats
|
|
|
|
//
|
|
|
|
|
2020-12-18 05:29:00 +01:00
|
|
|
namespace ircd::ios
|
2019-03-29 01:09:46 +01:00
|
|
|
{
|
2020-12-18 05:29:00 +01:00
|
|
|
static thread_local char stats_name_buf[128];
|
|
|
|
static string_view stats_name(const descriptor &d, const string_view &key);
|
2019-03-29 01:09:46 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 05:29:00 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::ios::stats_name(const descriptor &d,
|
|
|
|
const string_view &key)
|
|
|
|
{
|
2022-05-20 00:25:21 +02:00
|
|
|
return string_view
|
2020-12-18 05:29:00 +01:00
|
|
|
{
|
2022-06-24 04:19:08 +02:00
|
|
|
stats_name_buf, unsigned(::snprintf
|
2022-05-20 00:25:21 +02:00
|
|
|
(
|
|
|
|
stats_name_buf, sizeof(stats_name_buf),
|
|
|
|
"ircd.ios.%s.%s",
|
2022-06-29 22:20:52 +02:00
|
|
|
d.name? d.name.c_str(): "",
|
|
|
|
key? key.c_str(): ""
|
2022-06-24 04:19:08 +02:00
|
|
|
))
|
2020-12-18 05:29:00 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ios::descriptor::stats::stats(descriptor &d)
|
|
|
|
:value{0}
|
|
|
|
,items{0}
|
|
|
|
,queued
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", stats_name(d, "queued") },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,calls
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", stats_name(d, "calls") },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,faults
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", stats_name(d, "faults") },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,allocs
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", stats_name(d, "allocs") },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,alloc_bytes
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", stats_name(d, "alloc_bytes") },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,frees
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", stats_name(d, "frees") },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,free_bytes
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", stats_name(d, "free_bytes") },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,slice_total
|
2019-03-29 01:09:46 +01:00
|
|
|
{
|
2020-12-18 05:29:00 +01:00
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", stats_name(d, "slice_total") },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,slice_last
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", stats_name(d, "slice_last") },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,latency_total
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", stats_name(d, "latency_total") },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,latency_last
|
|
|
|
{
|
|
|
|
value + items++,
|
|
|
|
{
|
|
|
|
{ "name", stats_name(d, "latency_last") },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assert(items <= (sizeof(value) / sizeof(value[0])));
|
2019-03-29 01:09:46 +01:00
|
|
|
}
|
|
|
|
|
2020-12-18 05:29:00 +01:00
|
|
|
ircd::ios::descriptor::stats::~stats()
|
|
|
|
noexcept
|
2019-03-29 01:09:46 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-03-27 00:53:31 +01:00
|
|
|
//
|
|
|
|
// handler
|
|
|
|
//
|
|
|
|
|
2019-07-16 01:02:40 +02:00
|
|
|
decltype(ircd::ios::handler::current)
|
|
|
|
thread_local
|
2019-03-27 10:06:55 +01:00
|
|
|
ircd::ios::handler::current;
|
|
|
|
|
2019-07-16 01:02:40 +02:00
|
|
|
decltype(ircd::ios::handler::epoch)
|
|
|
|
thread_local
|
|
|
|
ircd::ios::handler::epoch;
|
|
|
|
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::cold]]
|
2019-03-27 00:53:31 +01:00
|
|
|
bool
|
|
|
|
ircd::ios::handler::fault(handler *const &handler)
|
2019-07-16 01:01:35 +02:00
|
|
|
noexcept
|
2019-03-27 00:53:31 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2020-12-09 02:54:34 +01:00
|
|
|
bool ret
|
2019-03-27 06:03:48 +01:00
|
|
|
{
|
2020-12-09 02:54:34 +01:00
|
|
|
false
|
|
|
|
};
|
|
|
|
|
2022-07-16 23:20:04 +02:00
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log, log::level::DERROR,
|
|
|
|
"FAULT %5u %-30s [%11lu] faults[%9lu] q:%-4lu :%s",
|
|
|
|
descriptor.id,
|
|
|
|
trunc(descriptor.name, 30),
|
|
|
|
uint64_t(stats.calls),
|
|
|
|
uint64_t(stats.faults),
|
|
|
|
uint64_t(stats.queued),
|
|
|
|
what(std::current_exception()),
|
|
|
|
};
|
2020-08-21 13:18:41 +02:00
|
|
|
|
2020-12-09 02:54:34 +01:00
|
|
|
// Our API sez if this function returns true, caller is responsible for
|
|
|
|
// calling leave(), otherwise they must not call leave().
|
|
|
|
if(likely(!ret))
|
|
|
|
leave(handler);
|
2019-03-27 06:03:48 +01:00
|
|
|
|
|
|
|
return ret;
|
2019-03-27 00:53:31 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2019-03-27 00:53:31 +01:00
|
|
|
void
|
|
|
|
ircd::ios::handler::leave(handler *const &handler)
|
2019-07-16 01:01:35 +02:00
|
|
|
noexcept
|
2019-03-27 00:53:31 +01:00
|
|
|
{
|
|
|
|
assert(handler && handler->descriptor);
|
|
|
|
auto &descriptor(*handler->descriptor);
|
2019-03-29 01:09:46 +01:00
|
|
|
|
|
|
|
assert(descriptor.stats);
|
|
|
|
auto &stats(*descriptor.stats);
|
2019-09-15 03:15:09 +02:00
|
|
|
|
2020-08-21 13:18:41 +02:00
|
|
|
const auto slice_start
|
2019-09-22 23:43:14 +02:00
|
|
|
{
|
2020-08-21 13:18:41 +02:00
|
|
|
std::exchange(handler->ts, cycles())
|
2019-09-22 23:43:14 +02:00
|
|
|
};
|
|
|
|
|
2019-09-15 03:15:09 +02:00
|
|
|
// NOTE: will fail without constant_tsc;
|
|
|
|
// NOTE: may fail without nonstop_tsc after OS suspend mode
|
2020-08-21 13:18:41 +02:00
|
|
|
assert(handler->ts >= slice_start);
|
|
|
|
stats.slice_last = handler->ts - slice_start;
|
2019-03-29 01:09:46 +01:00
|
|
|
stats.slice_total += stats.slice_last;
|
|
|
|
|
2020-12-13 01:07:42 +01:00
|
|
|
if constexpr(profile::history)
|
2020-08-02 14:45:35 +02:00
|
|
|
{
|
|
|
|
assert(descriptor.history_pos < descriptor.history.size());
|
|
|
|
descriptor.history[descriptor.history_pos][0] = handler::epoch;
|
|
|
|
descriptor.history[descriptor.history_pos][1] = stats.slice_last;
|
|
|
|
++descriptor.history_pos;
|
|
|
|
}
|
|
|
|
|
2020-12-13 01:07:42 +01:00
|
|
|
if constexpr(profile::logging)
|
2020-12-09 02:54:34 +01:00
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log, log::level::DEBUG,
|
2020-12-12 05:49:36 +01:00
|
|
|
"LEAVE %5u %-30s [%11lu] cycles[%9lu] q:%-4lu",
|
2020-12-09 02:54:34 +01:00
|
|
|
descriptor.id,
|
2020-12-12 05:49:36 +01:00
|
|
|
trunc(descriptor.name, 30),
|
2020-12-18 05:29:00 +01:00
|
|
|
uint64_t(stats.calls),
|
|
|
|
uint64_t(stats.slice_last),
|
|
|
|
uint64_t(stats.queued),
|
2020-12-09 02:54:34 +01:00
|
|
|
};
|
|
|
|
|
2019-03-27 10:06:55 +01:00
|
|
|
assert(handler::current == handler);
|
2022-07-16 22:09:14 +02:00
|
|
|
|
|
|
|
#if defined(RB_ASSERT_OPTIMISTIC)
|
|
|
|
ircd::assertion.point();
|
|
|
|
#endif
|
|
|
|
|
2019-03-27 10:06:55 +01:00
|
|
|
handler::current = nullptr;
|
2019-03-27 00:53:31 +01:00
|
|
|
}
|
|
|
|
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2019-03-27 00:53:31 +01:00
|
|
|
void
|
|
|
|
ircd::ios::handler::enter(handler *const &handler)
|
2019-07-16 01:01:35 +02:00
|
|
|
noexcept
|
2019-03-27 00:53:31 +01:00
|
|
|
{
|
2019-09-22 23:43:14 +02:00
|
|
|
assert(!handler::current);
|
2020-08-02 14:45:35 +02:00
|
|
|
handler::current = handler;
|
|
|
|
++handler::epoch;
|
|
|
|
|
2019-03-27 00:53:31 +01:00
|
|
|
assert(handler && handler->descriptor);
|
|
|
|
auto &descriptor(*handler->descriptor);
|
2019-03-29 01:09:46 +01:00
|
|
|
|
|
|
|
assert(descriptor.stats);
|
|
|
|
auto &stats(*descriptor.stats);
|
2019-09-22 23:43:14 +02:00
|
|
|
++stats.calls;
|
|
|
|
|
|
|
|
const auto last_ts
|
|
|
|
{
|
|
|
|
std::exchange(handler->ts, cycles())
|
|
|
|
};
|
|
|
|
|
|
|
|
stats.latency_last = handler->ts - last_ts;
|
|
|
|
stats.latency_total += stats.latency_last;
|
2019-03-27 05:22:22 +01:00
|
|
|
|
2020-12-13 01:07:42 +01:00
|
|
|
if constexpr(profile::logging)
|
2020-12-09 02:54:34 +01:00
|
|
|
log::logf
|
|
|
|
{
|
|
|
|
log, log::level::DEBUG,
|
2020-12-12 05:49:36 +01:00
|
|
|
"ENTER %5u %-30s [%11lu] latent[%9lu] q:%-4lu",
|
2020-12-09 02:54:34 +01:00
|
|
|
descriptor.id,
|
2020-12-12 05:49:36 +01:00
|
|
|
trunc(descriptor.name, 30),
|
2020-12-18 05:29:00 +01:00
|
|
|
uint64_t(stats.calls),
|
|
|
|
uint64_t(stats.latency_last),
|
|
|
|
uint64_t(stats.queued),
|
2020-12-09 02:54:34 +01:00
|
|
|
};
|
2019-03-29 00:56:55 +01:00
|
|
|
}
|
|
|
|
|
2019-05-16 08:20:13 +02:00
|
|
|
//
|
2020-12-13 01:07:42 +01:00
|
|
|
// dispatch
|
2019-05-16 08:20:13 +02:00
|
|
|
//
|
|
|
|
|
2020-12-20 14:13:18 +01:00
|
|
|
ircd::ios::dispatch::dispatch(descriptor &descriptor,
|
|
|
|
defer_t,
|
|
|
|
yield_t)
|
2019-07-06 02:34:42 +02:00
|
|
|
:dispatch
|
|
|
|
{
|
2022-06-24 04:18:05 +02:00
|
|
|
descriptor, defer, yield, []() noexcept
|
2020-12-20 14:13:18 +01:00
|
|
|
{
|
|
|
|
}
|
2019-07-06 02:34:42 +02:00
|
|
|
}
|
2019-05-16 08:20:13 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-12-20 14:13:18 +01:00
|
|
|
ircd::ios::dispatch::dispatch(descriptor &descriptor,
|
|
|
|
defer_t,
|
|
|
|
yield_t,
|
2019-05-16 08:20:13 +02:00
|
|
|
const std::function<void ()> &function)
|
2019-07-06 02:34:42 +02:00
|
|
|
{
|
2020-12-20 14:13:18 +01:00
|
|
|
const ctx::uninterruptible::nothrow ui;
|
|
|
|
ctx::latch latch{1};
|
|
|
|
dispatch
|
|
|
|
{
|
|
|
|
descriptor, defer, [&function, &latch]
|
|
|
|
{
|
|
|
|
const unwind uw
|
|
|
|
{
|
|
|
|
[&latch]
|
|
|
|
{
|
|
|
|
latch.count_down();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
latch.wait();
|
2019-07-06 02:34:42 +02:00
|
|
|
}
|
|
|
|
|
2020-12-20 14:13:18 +01:00
|
|
|
[[gnu::hot]]
|
2019-07-06 02:34:42 +02:00
|
|
|
ircd::ios::dispatch::dispatch(descriptor &descriptor,
|
2020-12-20 14:13:18 +01:00
|
|
|
defer_t,
|
|
|
|
std::function<void ()> function)
|
2019-05-16 08:20:13 +02:00
|
|
|
{
|
2020-12-20 14:13:18 +01:00
|
|
|
boost::asio::post(get(), handle(descriptor, std::move(function)));
|
2019-05-16 08:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ios::dispatch::dispatch(descriptor &descriptor,
|
2020-12-20 14:13:18 +01:00
|
|
|
yield_t,
|
2019-05-16 08:20:13 +02:00
|
|
|
const std::function<void ()> &function)
|
|
|
|
{
|
2020-08-23 13:20:48 +02:00
|
|
|
assert(function);
|
2020-12-13 03:06:09 +01:00
|
|
|
assert(ctx::current && handler::current);
|
|
|
|
|
|
|
|
const ctx::uninterruptible ui;
|
2020-08-23 13:20:48 +02:00
|
|
|
ctx::continuation
|
2019-05-16 08:20:13 +02:00
|
|
|
{
|
2020-08-23 13:20:48 +02:00
|
|
|
continuation::false_predicate, continuation::noop_interruptor, [&descriptor, &function]
|
|
|
|
(auto &yield)
|
2019-05-16 08:20:13 +02:00
|
|
|
{
|
2020-12-13 03:06:09 +01:00
|
|
|
assert(!ctx::current && !handler::current);
|
|
|
|
boost::asio::dispatch(get(), handle(descriptor, function));
|
|
|
|
|
|
|
|
assert(!ctx::current && !handler::current);
|
2020-08-23 13:20:48 +02:00
|
|
|
}
|
|
|
|
};
|
2019-05-16 08:20:13 +02:00
|
|
|
}
|
|
|
|
|
2019-09-11 00:11:25 +02:00
|
|
|
[[gnu::hot]]
|
2019-05-16 08:20:13 +02:00
|
|
|
ircd::ios::dispatch::dispatch(descriptor &descriptor,
|
|
|
|
std::function<void ()> function)
|
|
|
|
{
|
2020-12-13 03:06:09 +01:00
|
|
|
const auto parent(handler::current); try
|
|
|
|
{
|
|
|
|
assert(!ctx::current && handler::current);
|
|
|
|
ios::handler::leave(parent);
|
|
|
|
|
|
|
|
assert(!ctx::current && !handler::current);
|
|
|
|
boost::asio::dispatch(get(), handle(descriptor, std::move(function)));
|
|
|
|
|
|
|
|
assert(!ctx::current && !handler::current);
|
|
|
|
ios::handler::enter(parent);
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
assert(!ctx::current && !handler::current);
|
|
|
|
ios::handler::enter(parent);
|
|
|
|
|
|
|
|
assert(!ctx::current && handler::current == parent);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!ctx::current && handler::current == parent);
|
2019-05-16 08:20:13 +02:00
|
|
|
}
|