SolverMonitor class
#include <gtopt/solver_monitor.hpp>
Background thread that samples workpool statistics and writes JSON.
Start by calling start(pool, thread_name). Stop by calling stop() or letting the object go out of scope (RAII — the std::jthread destructor automatically requests a stop and joins).
The history is collected in m_history_ under m_mutex_. Call append_history_json(json) to emit the "realtime" JSON block, and write_status(content, path) for atomic file writes.
Public types
- using Interval = std::chrono::milliseconds
- Update interval for the background sampling thread.
Public static variables
- static std::size_t max_history_size
Public static functions
- static void write_status(const std::string& content, const std::string& path) noexcept
- Write content atomically to path (write tmp, rename).
Constructors, destructors, conversion operators
- SolverMonitor(const SolverMonitor&) deleted
- SolverMonitor(Interval update_interval = Interval {500}) explicit noexcept
- SolverMonitor(SolverMonitor&&) deleted
- ~SolverMonitor()
Public functions
- void append_history_json(std::string& json) const
- Append the
"realtime"JSON block tojsonusing collected history. - void build_realtime_status_json(std::string& json) const
- auto history() const -> std::vector<MonitorPoint>
- Access the collected history (caller must hold no other locks).
- auto operator=(const SolverMonitor&) -> SolverMonitor& deleted
- auto operator=(SolverMonitor&&) -> SolverMonitor& deleted
-
template<typename Pool>void start(Pool& pool, std::chrono::steady_clock::time_point start_time, const char* thread_name = "SolverMonitor")
- void stop() noexcept
- Request the background thread to stop (non-blocking).
- void update_status(std::string iteration_json, std::string path)
- void write_final_status(std::string iteration_json, const std::string& path)
Function documentation
gtopt:: SolverMonitor:: ~SolverMonitor()
Stop and join the sampling thread before any other member is destroyed. m_thread_ is declared ahead of the history / status members, so the implicit destructor would tear those down while the sampler could still be inside its loop body (stop() only requests a stop) — a use-after-destroy race reported by ThreadSanitizer.
void gtopt:: SolverMonitor:: build_realtime_status_json(std::string& json) const
Append the fresh pool/realtime block (and the document-closing }) to a partial iteration JSON document.
Emits the top-level pool_memory_percent / pool_process_rss_mb / pool_available_memory_mb scalars from the most recent sampled ring point (so they stay live between iterations), then the "realtime" history block, then closes the JSON object.
json is expected to be the partial document produced by build_iteration_status_json() (begins with {, no trailing }).
template<typename Pool>
void gtopt:: SolverMonitor:: start(Pool& pool,
std::chrono::steady_clock::time_point start_time,
const char* thread_name = "SolverMonitor")
| Parameters | |
|---|---|
| pool | The work pool to sample statistics from. |
| start_time | Reference time-point for timestamp computation. |
| thread_name | Name to assign the background thread (Linux only). |
Start the background sampling thread.
void gtopt:: SolverMonitor:: update_status(std::string iteration_json,
std::string path)
Register the cached iteration-portion JSON and target path so the background sampling thread rewrites the status file every tick with FRESH realtime/pool numbers (and the last iteration's unchanged iteration data). Called on the solver thread once per iteration — cheap (a guarded string move).
void gtopt:: SolverMonitor:: write_final_status(std::string iteration_json,
const std::string& path)
Write the terminal status document and stop periodic refreshes.
Appends the realtime block to iteration_json (a partial document from build_iteration_status_json()), writes it, and marks the monitor final so do_periodic_write() never overwrites it — both writers hold m_write_mutex_, so a refresh that already copied the previous ("running") document cannot land after this one.
Variable documentation
static std::size_t gtopt:: SolverMonitor:: max_history_size
Maximum number of monitor points to retain. At 500ms intervals, 7200 points ≈ 1 hour of data.