#include "hotplug.hpp" namespace hotplug { Ledger::Ledger() { reset(); } void Ledger::reset() { for (auto &row : leases_) { for (auto &entry : row) { entry = Lease{}; } } for (auto &row : budgets_) { row.fill(0); } next_ = 1; counters_ = Counters{}; events_.clear(); } void Ledger::begin_operation() { events_.clear(); } void Ledger::arm(int key, int cpu, int count) { budgets_[key][cpu] = count; } int Ledger::budget(int key, int cpu) const { return budgets_[key][cpu]; } int Ledger::start(const Descriptor &state, int cpu) { ++counters_.attempts; int &remaining = budgets_[state.key][cpu]; if (remaining > 0) { --remaining; ++counters_.failures; events_.push_back(Event{'S', state.key, cpu, 0, callback_failed}); return callback_failed; } Lease &target = leases_[state.key][cpu]; target.held = true; target.ticket = next_++; target.tag = state.tag; events_.push_back(Event{'S', state.key, cpu, target.ticket, 0}); return 0; } void Ledger::stop(int key, int cpu, StopReason reason) { Lease &target = leases_[key][cpu]; if (!target.held) { return; } ++counters_.stops; events_.push_back(Event{'T', key, cpu, target.ticket, static_cast(reason)}); target = Lease{}; } bool Ledger::held(int key, int cpu) const { return leases_[key][cpu].held; } const Lease &Ledger::lease(int key, int cpu) const { return leases_[key][cpu]; } CpuSet Ledger::cohort(int key) const { CpuSet result; for (int cpu = 0; cpu < max_cpus; ++cpu) { if (held(key, cpu)) { result.insert(cpu); } } return result; } std::uint64_t Ledger::next_ticket() const { return next_; } const std::vector &Ledger::events() const { return events_; } Counters &Ledger::counters() { return counters_; } const Counters &Ledger::counters() const { return counters_; } std::vector Ledger::held_keys() const { std::vector result; for (int key = 1; key <= max_keys; ++key) { if (!cohort(key).empty()) { result.push_back(key); } } return result; } bool Ledger::empty() const { for (int key = 1; key <= max_keys; ++key) { if (!cohort(key).empty()) { return false; } } return true; } }