pulsatrix
Loading...
Searching...
No Matches
system_monitor.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <chrono>
8#include <cstddef>
9#include <functional>
10#include <memory>
11#include <optional>
12#include <string>
13#include <vector>
14
15namespace pulsatrix {
16
17namespace detail {
18class PlatformSources;
19} // namespace detail
20
27struct GpuSample {
28 int index = 0;
29 std::string name;
30 std::string vendor;
31 std::optional<double> utilization_percent;
32 std::optional<double> memory_used_bytes;
33 std::optional<double> memory_total_bytes;
35 std::optional<double> memory_gtt_used_bytes;
36 std::optional<double> temperature_c;
37 std::optional<double> power_watts;
38};
39
49 std::chrono::system_clock::time_point timestamp;
50 double elapsed_seconds = 0.0;
51 std::optional<double> cpu_utilization_percent;
53 std::optional<double> process_cpu_percent;
54 std::optional<double> memory_used_bytes;
55 std::optional<double> memory_total_bytes;
56 std::optional<double> process_rss_bytes;
57 std::optional<double> cpu_temperature_c;
58 std::vector<GpuSample> gpus;
59 std::string label;
60};
61
67 std::string metric;
68 bool available = false;
69 std::string detail;
70};
71
73enum class LogFormat {
74 JsonLines,
75 Csv
76};
77
114public:
116 struct Options {
117 std::chrono::milliseconds interval{1000};
118 std::string log_path = "";
122 bool append = false;
123 std::size_t history_capacity = 3600;
124 };
125
128
131
136 SystemMonitor(Options options, std::unique_ptr<detail::PlatformSources> sources);
137
140
141 SystemMonitor(const SystemMonitor&) = delete;
143
148 void start();
149
156 void stop();
157
159 [[nodiscard]] bool running() const;
160
166 [[nodiscard]] SystemSample sample_now();
167
169 [[nodiscard]] std::optional<SystemSample> latest() const;
170
172 [[nodiscard]] std::vector<SystemSample> history() const;
173
180 void on_sample(std::function<void(const SystemSample&)> callback);
181
186 void mark(std::string label);
187
189 [[nodiscard]] std::vector<MetricCapability> capabilities() const;
190
192 [[nodiscard]] const Options& options() const noexcept;
193
194private:
195 struct Impl;
196 std::unique_ptr<Impl> impl_;
197};
198
200std::string format_iso8601_utc(std::chrono::system_clock::time_point tp);
201
203std::string sample_to_json(const SystemSample& sample);
204
205} // namespace pulsatrix
Samples CPU/GPU utilization, memory use and temperatures on a background thread, keeps a bounded in-m...
Definition system_monitor.hpp:113
bool running() const
True between start() and stop().
void start()
Opens the log (if any) and starts the background sampling thread. No-op if running.
~SystemMonitor()
Stops the background thread (if running) and closes the log.
void stop()
Stops and joins the background thread, then closes the log. No-op if not running.
void mark(std::string label)
Sets the label carried by subsequent samples (e.g. "epoch 3", "explain:lrp"), and if the log is open ...
std::vector< SystemSample > history() const
Background samples, oldest first, at most Options::history_capacity of them.
const Options & options() const noexcept
The options this monitor was constructed with.
SystemMonitor(Options options)
void on_sample(std::function< void(const SystemSample &)> callback)
Registers a callback run on the monitor thread after each background sample (after it is logged and s...
std::vector< MetricCapability > capabilities() const
Per-metric availability on this machine, with the source or the reason it is missing.
SystemMonitor & operator=(const SystemMonitor &)=delete
std::optional< SystemSample > latest() const
The most recent background sample, or std::nullopt before the first one.
SystemSample sample_now()
Takes one synchronous measurement on the calling thread. Works without start().
SystemMonitor(const SystemMonitor &)=delete
SystemMonitor()
Default options: 1 s interval, no log.
SystemMonitor(Options options, std::unique_ptr< detail::PlatformSources > sources)
Testing hook: reads from sources instead of the host (see system_monitor_detail.hpp).
LogFormat
Log record encoding.
Definition system_monitor.hpp:73
@ JsonLines
One JSON object per line; missing values are JSON null.
@ Csv
Header row then one row per record; missing values are empty cells.
Definition acquisition_functions.hpp:16
std::string format_iso8601_utc(std::chrono::system_clock::time_point tp)
Formats tp as ISO-8601 UTC with milliseconds, e.g. "2026-10-02T12:34:56.789Z".
std::string sample_to_json(const SystemSample &sample)
Encodes sample as one JSON object (no trailing newline), the JSON Lines log record.
One GPU's readings within a SystemSample.
Definition system_monitor.hpp:27
std::optional< double > power_watts
Board/package power draw, watts.
Definition system_monitor.hpp:37
std::string name
Driver-reported name (NVML), or "AMD GPU <pci id> (<card>)" for sysfs devices.
Definition system_monitor.hpp:29
std::string vendor
"AMD" or "NVIDIA".
Definition system_monitor.hpp:30
int index
Position in SystemSample::gpus (AMD sysfs devices first, then NVML devices).
Definition system_monitor.hpp:28
std::optional< double > memory_total_bytes
Definition system_monitor.hpp:33
std::optional< double > memory_gtt_used_bytes
AMD only: GTT (system memory mapped for the GPU). On APUs this is where most allocations live.
Definition system_monitor.hpp:35
std::optional< double > temperature_c
Die/edge temperature, degrees Celsius.
Definition system_monitor.hpp:36
std::optional< double > utilization_percent
0-100, busy fraction of the GPU since the driver's last window.
Definition system_monitor.hpp:31
std::optional< double > memory_used_bytes
Dedicated memory (VRAM / APU carve-out) in use.
Definition system_monitor.hpp:32
Whether one metric can be read on this machine, and from where – or why not.
Definition system_monitor.hpp:66
std::string metric
e.g. "cpu_temperature_c", "gpu0.power_watts", "gpus".
Definition system_monitor.hpp:67
std::string detail
Source when available (e.g. "/proc/stat"); the reason when not.
Definition system_monitor.hpp:69
bool available
Definition system_monitor.hpp:68
Construction options.
Definition system_monitor.hpp:116
LogFormat format
Definition system_monitor.hpp:119
bool append
Definition system_monitor.hpp:122
std::string log_path
Log file; empty = no log.
Definition system_monitor.hpp:118
std::chrono::milliseconds interval
Sampling period of the background thread (> 0).
Definition system_monitor.hpp:117
std::size_t history_capacity
Ring-buffer size for history(); 0 keeps none.
Definition system_monitor.hpp:123
One point-in-time measurement of the host.
Definition system_monitor.hpp:48
std::optional< double > memory_used_bytes
System RAM in use (total - available).
Definition system_monitor.hpp:54
std::optional< double > cpu_utilization_percent
Definition system_monitor.hpp:51
double elapsed_seconds
Seconds since SystemMonitor::start() (or construction, before start()).
Definition system_monitor.hpp:50
std::optional< double > memory_total_bytes
System RAM capacity.
Definition system_monitor.hpp:55
std::optional< double > cpu_temperature_c
CPU package/die temperature, degrees Celsius.
Definition system_monitor.hpp:57
std::string label
Label set by the most recent SystemMonitor::mark().
Definition system_monitor.hpp:59
std::optional< double > process_cpu_percent
This process's CPU use, in percent of ONE logical CPU: a process saturating 8 cores reads ~800.
Definition system_monitor.hpp:53
std::optional< double > process_rss_bytes
This process's resident set / working set.
Definition system_monitor.hpp:56
std::vector< GpuSample > gpus
Every GPU detected (possibly empty).
Definition system_monitor.hpp:58
std::chrono::system_clock::time_point timestamp
Wall-clock time of the reading.
Definition system_monitor.hpp:49