146 lines
4.2 KiB
C++
146 lines
4.2 KiB
C++
#pragma once
|
|
|
|
#include <Void/Time/Clock.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <optional>
|
|
|
|
#if defined(__linux__)
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <sys/sysinfo.h>
|
|
#include <sys/times.h>
|
|
#include <unistd.h>
|
|
#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<std::uint64_t> 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<std::uint64_t>(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<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;
|
|
m.available =
|
|
(static_cast<std::uint64_t>(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<std::int64_t>(t.tms_utime) +
|
|
static_cast<std::int64_t>(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<double> 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<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
|
|
(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
|