#include "crucible/string.h"
#include "crucible/task.h"
-#include <algorithm>
#include <chrono>
-#include <queue>
#include <sstream>
#include <sys/stat.h>
void
BeesReporter::start()
{
- m_thread = make_shared<BeesThread>("reporter");
- m_thread->exec([this]() {
- catch_all([this]() {
- run();
+ // One thread per active report stanza, named "report_<section>". Each
+ // thread owns a single report and just sleeps for its interval between
+ // writes, so a report whose target filesystem stalls blocks only its
+ // own thread, not the others. (A stuck target was already reachable
+ // via an absolute report path; per-stanza threads contain that fault
+ // instead of serialising every report behind it.)
+ for (size_t i = 0; i < m_reports.size(); ++i) {
+ if (!m_reports[i].active || m_reports[i].filename.empty()) {
+ continue;
+ }
+ auto thread = make_shared<BeesThread>("report_" + m_reports[i].name);
+ thread->exec([this, i]() {
+ catch_all([this, i]() {
+ run_one(i);
+ });
});
- });
+ m_threads.push_back(thread);
+ }
}
void
void
BeesReporter::stop_wait()
{
- if (m_thread) {
- m_thread->join();
+ for (auto &thread : m_threads) {
+ if (thread) {
+ thread->join();
+ }
}
}
}
void
-BeesReporter::run()
+BeesReporter::run_one(size_t index)
{
- using clock = chrono::steady_clock;
-
- // Per-report state: (next_write_deadline, index_into_m_reports)
- struct Entry {
- clock::time_point deadline;
- size_t index;
- bool operator>(const Entry &o) const { return deadline > o.deadline; }
- };
-
- // Initialise the heap with immediate deadlines for all active reports
- priority_queue<Entry, vector<Entry>, greater<Entry>> pq;
- for (size_t i = 0; i < m_reports.size(); ++i) {
- if (m_reports[i].active && !m_reports[i].filename.empty()) {
- pq.push({ clock::now(), i });
- }
- }
-
+ const auto &cfg = m_reports.at(index);
+ const auto interval = chrono::duration_cast<chrono::steady_clock::duration>(
+ chrono::duration<double>(cfg.interval_s));
+
+ // Write immediately, then once per interval. No central scheduler:
+ // each report's cadence is just its own wait_for. The predicate makes
+ // wait_for return true only when stop is requested; a plain timeout
+ // (predicate still false) returns false and falls through to the next
+ // write, so a spurious wakeup never causes an early write.
while (true) {
- unique_lock<mutex> lock(m_mutex);
- if (m_stop) break;
-
- if (pq.empty()) {
- // No active reports; wait for stop signal
- m_condvar.wait(lock, [this]() { return m_stop; });
- break;
- }
-
- const auto next = pq.top().deadline;
- const auto now = clock::now();
-
- if (next > now) {
- m_condvar.wait_until(lock, next, [this]() { return m_stop; });
- if (m_stop) break;
- continue;
- }
-
- // Pop the earliest entry and write it
- const auto entry = pq.top();
- pq.pop();
- lock.unlock();
-
catch_all([&]() {
- BEESNOTE("writing report '" << m_reports[entry.index].name << "'");
- write_report(m_reports[entry.index]);
+ BEESNOTE("writing report '" << cfg.name << "'");
+ write_report(cfg);
});
-
- // Reschedule at the next interval boundary. If writing the report
- // overran that boundary, start a fresh interval from now.
- const auto interval = chrono::duration_cast<clock::duration>(
- chrono::duration<double>(m_reports[entry.index].interval_s));
- auto deadline = entry.deadline + interval;
- const auto now_after_write = clock::now();
- if (deadline <= now_after_write) {
- deadline = now_after_write + interval;
+ unique_lock<mutex> lock(m_mutex);
+ if (m_condvar.wait_for(lock, interval, [this]() { return m_stop; })) {
+ break;
}
- pq.push({ deadline, entry.index });
}
}