ReloadableApplications: retained-resource ledger and failure-injection matrix; reject publish after failed drain (P-RELOADABLE-05)

This commit is contained in:
faumaray committed 2026-12-04 10:24:12 +03:00
1 parent 2489098478
commit 2da3971760
5 files changed
+382 -1

No files matched your search

@@ -0,0 +1,64 @@
#pragma once
#include <Void/Functional/Result.hpp>
#include <cstddef>
#include <cstdint>
#include <map>
#include <mutex>
namespace ReloadableHost
{
/// Aggregate retained-resource permits shared by candidate and retired
/// generations. These are host-owned budgets, not a total-process RSS
/// promise: mapped images, stacks and kernel buffers are accounted
/// separately by the platform.
struct ResourceLimits {
std::size_t max_total_bytes{256ULL * 1024ULL * 1024ULL};
std::size_t max_generation_bytes{64ULL * 1024ULL * 1024ULL};
std::size_t max_generations{4};
};
struct ResourceUsage {
std::size_t total_bytes{0};
std::size_t generations{0};
};
/// Thread-safe byte/generation ledger.
///
/// `reserve` is admission: it rejects an over-budget generation with
/// `exhausted` before any publication happens. A generation stays reserved
/// until every pin on it is gone and the host releases it, so budget
/// saturation is observable and recoverable rather than silent eviction.
class ResourceLedger
{
public:
explicit ResourceLedger(ResourceLimits limits = {});
[[nodiscard]] Void::Result::Result<void>
reserve(std::uint64_t generation, std::size_t bytes);
/// Release one reserved generation. False when it was not held.
bool release(std::uint64_t generation) noexcept;
[[nodiscard]] bool holds(std::uint64_t generation) const noexcept;
[[nodiscard]] ResourceUsage usage() const noexcept;
/// High-water mark, for retained-resource and RSS reporting.
[[nodiscard]] ResourceUsage peak_usage() const noexcept;
[[nodiscard]] const ResourceLimits& limits() const noexcept
{
return m_limits;
}
private:
mutable std::mutex m_mutex;
ResourceLimits m_limits;
std::map<std::uint64_t, std::size_t> m_reserved;
std::size_t m_total_bytes{0};
ResourceUsage m_peak{};
};
} // namespace ReloadableHost