feat(System): harden OwnedFd/Child process spawn, full-duplex bounds, durable replace_file, safe TempFile/TempDir, and SystemOnly consumer (M-SYSTEM-01, M-SYSTEM-02, M-SYSTEM-05)
This commit is contained in:
1 parent
3f67b1ecb7
commit
fcdbc063b2
14 files changed
+3538
-890
No files matched your search
+105
-84
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
@@ -12,113 +14,132 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
// Generic, app-agnostic process/system resource queries. Used to feed monitors
|
||||
// (e.g. Void::Resilience::ResourceMonitor) with real samples. Linux is fully
|
||||
// supported; other platforms return nullopt / 0 so callers degrade gracefully.
|
||||
|
||||
namespace Void::SystemInfo
|
||||
{
|
||||
|
||||
// ── memory ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// Resident set size (physical memory) of the current process, in bytes.
|
||||
/// nullopt if it cannot be determined on this platform.
|
||||
[[nodiscard]] inline std::optional<std::uint64_t> process_memory_bytes()
|
||||
{
|
||||
/// Resident set size (physical memory) of the current process, in bytes.
|
||||
/// nullopt if it cannot be determined on this platform.
|
||||
[[nodiscard]] inline std::optional<std::uint64_t> process_memory_bytes()
|
||||
{
|
||||
#if defined(__linux__)
|
||||
// /proc/self/statm: "size resident shared text lib data dt" in pages.
|
||||
std::ifstream statm{"/proc/self/statm"};
|
||||
std::uint64_t total_pages = 0, resident_pages = 0;
|
||||
if (statm >> total_pages >> resident_pages) {
|
||||
const long page = ::sysconf(_SC_PAGESIZE);
|
||||
if (page > 0) {
|
||||
return resident_pages * static_cast<std::uint64_t>(page);
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
std::ifstream statm{"/proc/self/statm"};
|
||||
std::uint64_t total_pages = 0, resident_pages = 0;
|
||||
if (statm >> total_pages >> resident_pages) {
|
||||
const long page = ::sysconf(_SC_PAGESIZE);
|
||||
if (page > 0) {
|
||||
return resident_pages * static_cast<std::uint64_t>(page);
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
#else
|
||||
return std::nullopt;
|
||||
return std::nullopt;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
struct SystemMemory {
|
||||
std::uint64_t total = 0; ///< total physical RAM, bytes
|
||||
std::uint64_t available = 0; ///< approx. RAM available to new work
|
||||
};
|
||||
struct SystemMemory {
|
||||
std::uint64_t total = 0; ///< total physical RAM, bytes
|
||||
std::uint64_t available = 0; ///< approx. RAM available to new work
|
||||
};
|
||||
|
||||
/// Total and (approximately) available system memory in bytes.
|
||||
[[nodiscard]] inline std::optional<SystemMemory> system_memory()
|
||||
{
|
||||
/// Total and (approximately) available system memory in bytes.
|
||||
[[nodiscard]] inline std::optional<SystemMemory> system_memory()
|
||||
{
|
||||
#if defined(__linux__)
|
||||
struct ::sysinfo info{};
|
||||
if (::sysinfo(&info) != 0) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const std::uint64_t unit = info.mem_unit ? info.mem_unit : 1;
|
||||
SystemMemory m;
|
||||
m.total = static_cast<std::uint64_t>(info.totalram) * unit;
|
||||
// freeram excludes reclaimable cache/buffers, so this is conservative.
|
||||
m.available =
|
||||
(static_cast<std::uint64_t>(info.freeram) + info.bufferram) * unit;
|
||||
return m;
|
||||
struct ::sysinfo info{};
|
||||
if (::sysinfo(&info) != 0) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const std::uint64_t unit = info.mem_unit ? info.mem_unit : 1;
|
||||
SystemMemory m;
|
||||
m.total = static_cast<std::uint64_t>(info.totalram) * unit;
|
||||
m.available =
|
||||
(static_cast<std::uint64_t>(info.freeram) + info.bufferram) * unit;
|
||||
return m;
|
||||
#else
|
||||
return std::nullopt;
|
||||
return std::nullopt;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// ── CPU ─────────────────────────────────────────────────────────────────
|
||||
struct CpuSample {
|
||||
double ratio{0.0};
|
||||
bool valid{false};
|
||||
};
|
||||
|
||||
/// Samples this process's CPU usage between successive calls. usage()
|
||||
/// returns the fraction of a single core consumed since the previous
|
||||
/// usage() call (0.0 = idle, 1.0 = one core saturated; may exceed 1.0 on
|
||||
/// multi-core workloads). The first call establishes the baseline and
|
||||
/// returns 0.
|
||||
class CpuSampler
|
||||
{
|
||||
public:
|
||||
/// Samples this process's CPU usage between successive calls.
|
||||
/// `sample()` explicitly distinguishes real 0.0 from unavailable sampling.
|
||||
class CpuSampler
|
||||
{
|
||||
public:
|
||||
explicit CpuSampler(
|
||||
const Void::Time::MonotonicClock* clock =
|
||||
&Void::Time::SystemMonotonicClock::instance())
|
||||
: clock_(clock != nullptr ? clock
|
||||
: &Void::Time::SystemMonotonicClock::instance())
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
CpuSampler() { reset(); }
|
||||
|
||||
void reset()
|
||||
{
|
||||
wall_ = std::chrono::steady_clock::now();
|
||||
void reset()
|
||||
{
|
||||
wall_ = clock_->now();
|
||||
baseline_valid_ = false;
|
||||
#if defined(__linux__)
|
||||
struct ::tms t{};
|
||||
last_ticks_ = (::times(&t) == static_cast<::clock_t>(-1))
|
||||
? 0
|
||||
: (t.tms_utime + t.tms_stime);
|
||||
struct ::tms t{};
|
||||
if (::times(&t) != static_cast<::clock_t>(-1)) {
|
||||
last_ticks_ = static_cast<std::int64_t>(t.tms_utime) +
|
||||
static_cast<std::int64_t>(t.tms_stime);
|
||||
baseline_valid_ = true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] double usage()
|
||||
{
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
const std::chrono::duration<double> wall = now - wall_;
|
||||
wall_ = now;
|
||||
[[nodiscard]] CpuSample sample(
|
||||
const Void::Time::MonotonicClock* override_clock = nullptr)
|
||||
{
|
||||
const Void::Time::MonotonicClock* active =
|
||||
(override_clock != nullptr) ? override_clock : clock_;
|
||||
const auto now = active->now();
|
||||
const std::chrono::duration<double> wall = now - wall_;
|
||||
wall_ = now;
|
||||
#if defined(__linux__)
|
||||
const long hz = ::sysconf(_SC_CLK_TCK);
|
||||
struct ::tms t{};
|
||||
if (hz <= 0 || ::times(&t) == static_cast<::clock_t>(-1) ||
|
||||
wall.count() <= 0.0) {
|
||||
return 0.0;
|
||||
}
|
||||
const std::int64_t ticks = t.tms_utime + t.tms_stime;
|
||||
const double cpu_seconds =
|
||||
static_cast<double>(ticks - last_ticks_) /
|
||||
static_cast<double>(hz);
|
||||
last_ticks_ = ticks;
|
||||
return cpu_seconds / wall.count();
|
||||
const long hz = ::sysconf(_SC_CLK_TCK);
|
||||
struct ::tms t{};
|
||||
if (!baseline_valid_ || hz <= 0 ||
|
||||
::times(&t) == static_cast<::clock_t>(-1) || wall.count() <= 0.0) {
|
||||
if (::times(&t) != static_cast<::clock_t>(-1)) {
|
||||
last_ticks_ = static_cast<std::int64_t>(t.tms_utime) +
|
||||
static_cast<std::int64_t>(t.tms_stime);
|
||||
baseline_valid_ = true;
|
||||
}
|
||||
return CpuSample{0.0, false};
|
||||
}
|
||||
const std::int64_t ticks = static_cast<std::int64_t>(t.tms_utime) +
|
||||
static_cast<std::int64_t>(t.tms_stime);
|
||||
const std::int64_t delta_ticks =
|
||||
(ticks >= last_ticks_) ? (ticks - last_ticks_) : 0;
|
||||
last_ticks_ = ticks;
|
||||
const double cpu_seconds =
|
||||
static_cast<double>(delta_ticks) / static_cast<double>(hz);
|
||||
return CpuSample{cpu_seconds / wall.count(), true};
|
||||
#else
|
||||
return 0.0;
|
||||
(void)wall;
|
||||
return CpuSample{0.0, false};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
[[nodiscard]] double usage()
|
||||
{
|
||||
const auto s = sample();
|
||||
return s.valid ? s.ratio : 0.0;
|
||||
}
|
||||
|
||||
std::chrono::steady_clock::time_point wall_;
|
||||
private:
|
||||
const Void::Time::MonotonicClock* clock_;
|
||||
Void::Time::MonotonicClock::time_point wall_{};
|
||||
bool baseline_valid_{false};
|
||||
#if defined(__linux__)
|
||||
std::int64_t last_ticks_ = 0;
|
||||
std::int64_t last_ticks_{0};
|
||||
#endif
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace Void::SystemInfo
|
||||
Reference in new issue
Block a user