feat(Runtime): add inert Host, validated RuntimeLimits, EmbeddedResources, and topological ResourceGraph (S-RUNTIME-01)
This commit is contained in:
1 parent
673bb7e8b4
commit
1b3331e3a8
9 files changed
+812
-1
No files matched your search
@@ -0,0 +1,168 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/IO/Types.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Runtime
|
||||
{
|
||||
|
||||
inline constexpr std::uint32_t kRuntimeErrorDomain = 11;
|
||||
|
||||
enum class RuntimeErrorCode : std::int32_t {
|
||||
none = 0,
|
||||
invalid_argument = 1,
|
||||
not_found = 2,
|
||||
conflict = 3,
|
||||
cycle_detected = 4,
|
||||
invalid_state = 5,
|
||||
unsupported = 6,
|
||||
internal = 7
|
||||
};
|
||||
|
||||
[[nodiscard]] inline Void::Result::Error make_runtime_error(
|
||||
RuntimeErrorCode code, std::string message = {})
|
||||
{
|
||||
using Void::Result::Error;
|
||||
using Void::Result::ErrorCode;
|
||||
using Void::Result::ErrorKind;
|
||||
|
||||
ErrorKind kind = ErrorKind::internal;
|
||||
switch (code) {
|
||||
case RuntimeErrorCode::none:
|
||||
kind = ErrorKind::internal;
|
||||
break;
|
||||
case RuntimeErrorCode::invalid_argument:
|
||||
kind = ErrorKind::invalid_argument;
|
||||
break;
|
||||
case RuntimeErrorCode::not_found:
|
||||
kind = ErrorKind::not_found;
|
||||
break;
|
||||
case RuntimeErrorCode::conflict:
|
||||
case RuntimeErrorCode::cycle_detected:
|
||||
case RuntimeErrorCode::invalid_state:
|
||||
kind = ErrorKind::conflict;
|
||||
break;
|
||||
case RuntimeErrorCode::unsupported:
|
||||
kind = ErrorKind::unsupported;
|
||||
break;
|
||||
case RuntimeErrorCode::internal:
|
||||
kind = ErrorKind::internal;
|
||||
break;
|
||||
}
|
||||
|
||||
return Error{
|
||||
kind,
|
||||
std::move(message),
|
||||
ErrorCode{kRuntimeErrorDomain, static_cast<std::int32_t>(code)}};
|
||||
}
|
||||
|
||||
struct RuntimeLimits {
|
||||
std::size_t max_cpu_tasks{4096};
|
||||
std::size_t max_io_operations{8192};
|
||||
std::size_t max_file_queue{256};
|
||||
std::size_t max_timers{1024};
|
||||
std::size_t max_services{256};
|
||||
std::size_t max_owned_memory_bytes{256ULL * 1024ULL * 1024ULL};
|
||||
};
|
||||
|
||||
struct ShutdownPolicy {
|
||||
std::chrono::milliseconds drain_grace{5000};
|
||||
std::chrono::milliseconds export_flush_timeout{5000};
|
||||
std::chrono::milliseconds total_timeout{30000};
|
||||
};
|
||||
|
||||
[[nodiscard]] inline std::uint32_t default_cpu_workers() noexcept
|
||||
{
|
||||
const std::uint32_t hw = std::thread::hardware_concurrency();
|
||||
return std::clamp<std::uint32_t>(hw, 1u, 8u);
|
||||
}
|
||||
|
||||
struct Options {
|
||||
std::uint32_t cpu_workers{default_cpu_workers()};
|
||||
std::uint32_t file_workers{4};
|
||||
bool embedded_mode{false};
|
||||
RuntimeLimits limits{};
|
||||
ShutdownPolicy shutdown{};
|
||||
IO::BackendOptions io{};
|
||||
};
|
||||
|
||||
[[nodiscard]] inline Void::Result::Result<void> validate_options(const Options& opts)
|
||||
{
|
||||
if (!opts.embedded_mode) {
|
||||
if (opts.cpu_workers == 0) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"Runtime Options::cpu_workers must be >= 1 in managed mode")};
|
||||
}
|
||||
if (opts.file_workers == 0) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"Runtime Options::file_workers must be >= 1 in managed mode")};
|
||||
}
|
||||
}
|
||||
|
||||
const auto& l = opts.limits;
|
||||
if (l.max_cpu_tasks == 0 ||
|
||||
l.max_io_operations == 0 ||
|
||||
l.max_file_queue == 0 ||
|
||||
l.max_timers == 0 ||
|
||||
l.max_services == 0 ||
|
||||
l.max_owned_memory_bytes == 0) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"All RuntimeLimits capacity bounds must be non-zero")};
|
||||
}
|
||||
|
||||
const auto& s = opts.shutdown;
|
||||
if (s.drain_grace.count() <= 0 ||
|
||||
s.export_flush_timeout.count() <= 0 ||
|
||||
s.total_timeout.count() <= 0) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"ShutdownPolicy durations must be strictly positive")};
|
||||
}
|
||||
if (s.drain_grace > s.total_timeout || s.export_flush_timeout > s.total_timeout) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"ShutdownPolicy drain_grace and export_flush_timeout must not exceed total_timeout")};
|
||||
}
|
||||
|
||||
if (auto io_ok = IO::validate_limits(opts.io.limits); !io_ok.has_value()) {
|
||||
return std::unexpected{std::move(io_ok.error())};
|
||||
}
|
||||
|
||||
// Submodule IO caps cannot exceed global host caps.
|
||||
if (opts.io.limits.max_operations > l.max_io_operations) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"IO::IoLimits::max_operations exceeds RuntimeLimits::max_io_operations")};
|
||||
}
|
||||
if (opts.io.limits.max_timers > l.max_timers && opts.io.limits.max_timers != 4096) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"IO::IoLimits::max_timers exceeds RuntimeLimits::max_timers")};
|
||||
}
|
||||
if (opts.io.limits.max_file_queue > l.max_file_queue) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"IO::IoLimits::max_file_queue exceeds RuntimeLimits::max_file_queue")};
|
||||
}
|
||||
if (opts.io.limits.max_owned_buffer_bytes > l.max_owned_memory_bytes) {
|
||||
return std::unexpected{make_runtime_error(
|
||||
RuntimeErrorCode::invalid_argument,
|
||||
"IO::IoLimits::max_owned_buffer_bytes exceeds RuntimeLimits::max_owned_memory_bytes")};
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace Void::Runtime
|
||||
Reference in new issue
Block a user