#include "crucible/task.h"
#include <chrono>
+#include <functional>
+#include <ostream>
#include <sstream>
#include <sys/stat.h>
using namespace crucible;
using namespace std;
+BeesReportToken::~BeesReportToken() = default;
+
+namespace {
+
+/// Stateless token: renders by calling a captured function. Used for the
+/// majority of tokens, which are pure functions of global state.
+class FunctionToken : public BeesReportToken {
+ function<void(ostream &)> m_fn;
+public:
+ explicit FunctionToken(function<void(ostream &)> fn) : m_fn(std::move(fn)) {}
+ void render(ostream &os) override { m_fn(os); }
+};
+
+/// Renders the not-implemented placeholder for an unrecognised token name.
+/// Keeps a newer config degrading gracefully on an older binary rather than
+/// failing at load.
+class PlaceholderToken : public BeesReportToken {
+ string m_name;
+public:
+ explicit PlaceholderToken(string name) : m_name(std::move(name)) {}
+ void render(ostream &os) override {
+ os << "# token '" << m_name << "' not implemented in this version of bees\n";
+ }
+};
+
+}
+
BeesReporter::BeesReporter(shared_ptr<BeesContext> ctx) :
m_ctx(ctx)
{
}
+shared_ptr<BeesReportToken>
+BeesReporter::make_token(const string &name)
+{
+ auto ctx = m_ctx;
+ if (name == "totals") {
+ return make_shared<FunctionToken>([](ostream &os) {
+ os << "TOTAL:\n\t" << BeesStats::s_global << "\n";
+ });
+ } else if (name == "rates") {
+ return make_shared<FunctionToken>([ctx](ostream &os) {
+ const auto stats = BeesStats::s_global;
+ os << "RATES:\n\t" << (stats / ctx->total_timer().age()) << "\n";
+ });
+ } else if (name == "threads") {
+ return make_shared<FunctionToken>([](ostream &os) {
+ const auto load = TaskMaster::get_current_load();
+ os << "THREADS (work queue " << TaskMaster::get_queue_count()
+ << " of " << Task::instance_count() << " tasks, "
+ << TaskMaster::get_thread_count() << " workers, load: current "
+ << load.current_load << " target " << load.thread_target
+ << " average " << load.loadavg << "):\n";
+ for (const auto &t : BeesNote::get_status()) {
+ os << "\ttid " << t.first << ": " << t.second << "\n";
+ }
+ });
+ } else if (name == "progress") {
+ return make_shared<FunctionToken>([ctx](ostream &os) {
+ os << "PROGRESS:\n" << ctx->get_progress();
+ });
+ } else if (name == "workers") {
+ return make_shared<FunctionToken>([](ostream &os) { TaskMaster::print_workers(os); });
+ } else if (name == "queue") {
+ return make_shared<FunctionToken>([](ostream &os) { TaskMaster::print_queue(os); });
+ } else if (name == "idle") {
+ return make_shared<FunctionToken>([](ostream &os) { TaskMaster::print_idle(os); });
+ } else if (name == "tasks") {
+ return make_shared<FunctionToken>([](ostream &os) { Task::print_tree(os); });
+ } else if (name == "hashtable") {
+ return make_shared<FunctionToken>([ctx](ostream &os) {
+ if (auto ht = ctx->hash_table()) {
+ ht->print_occupancy(os);
+ os << "\n";
+ }
+ });
+ } else {
+ return make_shared<PlaceholderToken>(name);
+ }
+}
+
void
BeesReporter::add_report(const BeesReportConfig &cfg)
{
m_reports.push_back(cfg);
+ // Build the token objects once, fresh per report, so stateful tokens
+ // keep state independent of other reports rendering the same token.
+ auto &stored = m_reports.back();
+ for (const auto &name : stored.tokens) {
+ stored.token_objs.push_back(make_token(name));
+ }
}
void
{
if (cfg.filename.empty()) return;
- // Render content
+ // Render content by asking each token object in order.
ostringstream oss;
- for (const auto &token : cfg.tokens) {
- if (token == "totals") {
- auto stats = BeesStats::s_global;
- oss << "TOTAL:\n\t" << stats << "\n";
- } else if (token == "rates") {
- auto stats = BeesStats::s_global;
- auto age = m_ctx->total_timer().age();
- oss << "RATES:\n\t" << (stats / age) << "\n";
- } else if (token == "threads") {
- const auto load = TaskMaster::get_current_load();
- oss << "THREADS (work queue " << TaskMaster::get_queue_count()
- << " of " << Task::instance_count() << " tasks, "
- << TaskMaster::get_thread_count() << " workers, load: current "
- << load.current_load << " target " << load.thread_target
- << " average " << load.loadavg << "):\n";
- for (const auto &t : BeesNote::get_status()) {
- oss << "\ttid " << t.first << ": " << t.second << "\n";
- }
- } else if (token == "progress") {
- oss << "PROGRESS:\n" << m_ctx->get_progress();
- } else if (token == "workers") {
- TaskMaster::print_workers(oss);
- } else if (token == "queue") {
- TaskMaster::print_queue(oss);
- } else if (token == "idle") {
- TaskMaster::print_idle(oss);
- } else if (token == "tasks") {
- Task::print_tree(oss);
- } else if (token == "hashtable") {
- // Hash table page-occupancy histogram, summed from the
- // per-extent fragments. Skipped when no hash table exists
- // (e.g. before init or in a configuration without one).
- if (auto ht = m_ctx->hash_table()) {
- ht->print_occupancy(oss);
- oss << "\n";
- }
- } else {
- oss << "# token '" << token << "' not implemented in this version of bees\n";
- }
+ for (const auto &token : cfg.token_objs) {
+ token->render(oss);
}
const auto content = oss.str();
#include "crucible/fd.h"
#include <condition_variable>
+#include <iosfwd>
#include <memory>
#include <mutex>
#include <string>
using namespace std;
using namespace crucible;
+/// One renderable content token (e.g. "totals"). Built once per report from
+/// the configured token names; see BeesReporter::make_token. Subclasses may
+/// carry per-report state (e.g. delta counters).
+class BeesReportToken {
+public:
+ virtual ~BeesReportToken(); ///< Out-of-line anchor for the vtable.
+ /// Append this token's content to @p os.
+ virtual void render(ostream &os) = 0;
+};
+
/// Configuration for a single report output section (e.g. [report.legacy]).
struct BeesReportConfig {
string name; ///< Section name (e.g. "legacy").
mode_t mode = 0400; ///< fchmod value applied before rename.
uid_t uid = uid_t(-1); ///< fchown uid; -1 = no chown.
gid_t gid = gid_t(-1); ///< fchown gid; -1 = no chown.
- vector<string> tokens; ///< Content tokens in order.
+ vector<string> tokens; ///< Configured content token names, in order.
+ vector<shared_ptr<BeesReportToken>> token_objs; ///< Built tokens (one per name); fresh per report so stateful tokens keep independent state.
/// Pre-resolved at startup: openat(dir_fd, rel_filename) writes the report.
/// Both are set only for active reports with non-empty filenames.
Fd dir_fd; ///< Pre-opened directory fd (shared value; closed when last copy drops).
/// own thread so a stalled target filesystem blocks only this report.
void run_one(size_t index);
void write_report(const BeesReportConfig &cfg);
+ /// Build the token object for a configured name (unknown -> placeholder).
+ shared_ptr<BeesReportToken> make_token(const string &name);
public:
/// @p ctx is used for get_progress() and total_timer().