#pragma once #include #include #include #include #if defined(__linux__) #include #include #include #include #include #endif namespace Void::SystemInfo { /// Resident set size (physical memory) of the current process, in bytes. /// nullopt if it cannot be determined on this platform. [[nodiscard]] inline std::optional process_memory_bytes() { #if defined(__linux__) 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(page); } } return std::nullopt; #else 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 }; /// Total and (approximately) available system memory in bytes. [[nodiscard]] inline std::optional 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(info.totalram) * unit; m.available = (static_cast(info.freeram) + info.bufferram) * unit; return m; #else return std::nullopt; #endif } struct CpuSample { double ratio{0.0}; bool valid{false}; }; /// 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(); } void reset() { wall_ = clock_->now(); baseline_valid_ = false; #if defined(__linux__) struct ::tms t{}; if (::times(&t) != static_cast<::clock_t>(-1)) { last_ticks_ = static_cast(t.tms_utime) + static_cast(t.tms_stime); baseline_valid_ = true; } #endif } [[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 wall = now - wall_; wall_ = now; #if defined(__linux__) 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(t.tms_utime) + static_cast(t.tms_stime); baseline_valid_ = true; } return CpuSample{0.0, false}; } const std::int64_t ticks = static_cast(t.tms_utime) + static_cast(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(delta_ticks) / static_cast(hz); return CpuSample{cpu_seconds / wall.count(), true}; #else (void)wall; return CpuSample{0.0, false}; #endif } [[nodiscard]] double usage() { const auto s = sample(); return s.valid ? s.ratio : 0.0; } 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}; #endif }; } // namespace Void::SystemInfo