]> git.hungrycats.org Git - bees/commitdiff
reporter: run each report stanza on its own thread
authorZygo Blaxell <bees@furryterror.org>
Wed, 17 Jun 2026 02:35:48 +0000 (22:35 -0400)
committerZygo Blaxell <bees@furryterror.org>
Tue, 6 Oct 2026 05:51:40 +0000 (01:51 -0400)
The reporter ran every report from a single thread driven by a deadline
priority queue.  That serialises all reports behind the slowest write: a
report whose target filesystem stalls (already reachable today via an
absolute report path, and about to become common once a report can target
$BEESHOME) would freeze the fast /run status reports that monitoring depends
on.

Give each active report stanza its own thread, named report_<section>.  Each
thread writes its report and then waits its own interval on the shared
condvar, so a stalled target blocks only that one report.  This also removes
the central scheduler: there is no deadline heap and no reschedule-on-overrun
bookkeeping, just a wait_for loop per report.  The wait_for predicate returns
true only on stop request, so a spurious wakeup falls through to a normal
write rather than firing early.  Shutdown signals stop once and joins every
thread.

Assisted-by: Claude-Code:claude-opus-4-8
Signed-off-by: Zygo Blaxell <bees@furryterror.org>
src/bees-reporter.cc
src/bees-reporter.h

index 0f02b3f7ff898f13c0fe3b9dab537f87475e4e9e..480d2b21ddb41c61e8e5a128b57ccea8d910e5b6 100644 (file)
@@ -3,9 +3,7 @@
 #include "crucible/string.h"
 #include "crucible/task.h"
 
-#include <algorithm>
 #include <chrono>
-#include <queue>
 #include <sstream>
 #include <sys/stat.h>
 
@@ -26,12 +24,24 @@ BeesReporter::add_report(const BeesReportConfig &cfg)
 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
@@ -45,8 +55,10 @@ BeesReporter::stop_request()
 void
 BeesReporter::stop_wait()
 {
-       if (m_thread) {
-               m_thread->join();
+       for (auto &thread : m_threads) {
+               if (thread) {
+                       thread->join();
+               }
        }
 }
 
@@ -124,63 +136,25 @@ BeesReporter::write_report(const BeesReportConfig &cfg)
 }
 
 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 });
        }
 }
index db934c1626285387d9113a41c0e849d519157167..63895e5caff73d8ff5efc7e4ef2abe1ed1f6e61d 100644 (file)
@@ -38,13 +38,15 @@ struct BeesReportConfig {
 class BeesReporter {
        shared_ptr<BeesContext>        m_ctx;
        vector<BeesReportConfig>       m_reports;
-       shared_ptr<BeesThread>         m_thread;
+       vector<shared_ptr<BeesThread>> m_threads;
 
        bool                           m_stop = false;
        mutex                          m_mutex;
        condition_variable             m_condvar;
 
-       void run();
+       /// Write loop for one report stanza (index into m_reports), run on its
+       /// own thread so a stalled target filesystem blocks only this report.
+       void run_one(size_t index);
        void write_report(const BeesReportConfig &cfg);
 
 public: