Files
Library/include/Void/Runtime/Options.hpp
T
faumarayandClaude Opus 5.5 7f95f89d52 chore(wave11): plan execution-path backfill, reserve error codes, update area plans
- Audit roadmap vs TASKS (D-033); scope Wave 11 to M-FUNCTIONAL-03, M-TIME-03,
  M-ASYNC-04/05, S-IO-03/04, S-RUNTIME-02/03 (D-034..D-036)
- Schedule VoidForge upstream requests after Wave 11 (D-037)
- Persist planner output, PREFLIGHT findings and contract addendum (D-038)
- Add AsyncErrorCode::closed, IoErrorCode::sync_failed,
  RuntimeErrorCode::{self_join, shutdown_incomplete}
- Update Functional/IO/Runtime area plans and MIGRATION per C-13

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 18:31:14 +03:00

172 lines
5.5 KiB
C++

#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,
self_join = 8,
shutdown_incomplete = 9
};
[[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:
case RuntimeErrorCode::self_join:
case RuntimeErrorCode::shutdown_incomplete:
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