#include "hotplug.hpp" #include namespace hotplug { Engine::Engine() : ready_(false), cpu_count_(0), online_() { } bool Engine::valid_key(long long key) { return key >= 1 && key <= max_keys; } bool Engine::valid_cpu(long long cpu) const { return cpu >= 0 && cpu < cpu_count_; } const Descriptor *Engine::find(int key) const { for (const auto &state : states_) { if (state.key == key) { return &state; } } return nullptr; } std::vector Engine::ordered() const { auto result = states_; std::sort(result.begin(), result.end(), [](const auto &left, const auto &right) { if (left.priority != right.priority) { return left.priority < right.priority; } return left.key < right.key; }); return result; } int Engine::execute(const Command &command) { ledger_.begin_operation(); if (command.name == "RESET") { return reset(command.args); } if (command.name == "ARM") { return arm(command.args); } if (command.name == "REG") { return add(command.args); } if (command.name == "REMOVE") { return remove(command.args); } if (command.name == "UP") { return up(command.args); } if (command.name == "DOWN") { return down(command.args); } if (command.name == "SYNC") { return sync(command.args); } if (command.name == "SNAP" && command.args.empty()) { return ready_ ? 0 : not_ready; } return invalid; } int Engine::reset(const std::vector &args) { if (args.size() != 2 || !CpuSet::valid_count(args[0])) { return invalid; } if (!CpuSet::valid_mask(args[1], static_cast(args[0]))) { return invalid; } ready_ = true; cpu_count_ = static_cast(args[0]); online_ = CpuSet(static_cast(args[1])); states_.clear(); ledger_.reset(); return 0; } int Engine::arm(const std::vector &args) { if (args.size() != 3 || !valid_key(args[0]) || args[1] < 0 || args[1] >= max_cpus || args[2] < 0 || args[2] > 4) { return invalid; } if (!ready_) { return not_ready; } if (!valid_cpu(args[1])) { return invalid; } ledger_.arm(static_cast(args[0]), static_cast(args[1]), static_cast(args[2])); return 0; } int Engine::replay(const Descriptor &state, const CpuSet &targets, std::vector &acquired) { for (int cpu : targets.ascending()) { if (ledger_.held(state.key, cpu)) { continue; } const int result = ledger_.start(state, cpu); if (result != 0) { return result; } acquired.push_back(cpu); } return 0; } void Engine::rollback_state(int key, const std::vector &acquired) { for (auto position = acquired.rbegin(); position != acquired.rend(); ++position) { ledger_.stop(key, *position, StopReason::rollback); } } void Engine::rollback_cpu(int cpu, const std::vector &acquired) { for (auto position = acquired.rbegin(); position != acquired.rend(); ++position) { ledger_.stop(*position, cpu, StopReason::rollback); } } int Engine::add(const std::vector &args) { if (args.size() != 4 || !valid_key(args[0]) || args[1] < -100 || args[1] > 100 || args[2] < 0 || args[2] > 65535 || (args[3] != 0 && args[3] != 1)) { return invalid; } if (!ready_) { return not_ready; } const int key = static_cast(args[0]); if (find(key) != nullptr) { return duplicate; } if (states_.size() == max_states) { return full; } const Descriptor state{key, static_cast(args[1]), static_cast(args[2]), static_cast(args[3])}; std::vector acquired; if (state.replay != 0) { const int result = replay(state, online_, acquired); if (result != 0) { rollback_state(key, acquired); return result; } } states_.push_back(state); ++ledger_.counters().registrations; return 0; } int Engine::remove(const std::vector &args) { if (args.size() != 1 || !valid_key(args[0])) { return invalid; } if (!ready_) { return not_ready; } const int key = static_cast(args[0]); if (find(key) == nullptr) { return absent; } for (int cpu : ledger_.cohort(key).descending()) { ledger_.stop(key, cpu, StopReason::removal); } states_.erase(std::remove_if(states_.begin(), states_.end(), [key](const auto &state) { return state.key == key; }), states_.end()); ++ledger_.counters().removals; return 0; } int Engine::up(const std::vector &args) { if (args.size() != 1 || args[0] < 0 || args[0] >= max_cpus) { return invalid; } if (!ready_) { return not_ready; } if (!valid_cpu(args[0])) { return invalid; } const int cpu = static_cast(args[0]); if (online_.contains(cpu)) { return 0; } std::vector acquired; for (const auto &state : ordered()) { const int result = ledger_.start(state, cpu); if (result != 0) { rollback_cpu(cpu, acquired); return result; } acquired.push_back(state.key); } online_.insert(cpu); ++ledger_.counters().ups; return 0; } int Engine::down(const std::vector &args) { if (args.size() != 1 || args[0] < 0 || args[0] >= max_cpus) { return invalid; } if (!ready_) { return not_ready; } if (!valid_cpu(args[0])) { return invalid; } const int cpu = static_cast(args[0]); if (!online_.contains(cpu)) { return 0; } const auto sequence = ordered(); for (auto position = sequence.rbegin(); position != sequence.rend(); ++position) { ledger_.stop(position->key, cpu, StopReason::offline); } online_.erase(cpu); ++ledger_.counters().downs; return 0; } int Engine::sync(const std::vector &args) { if (args.size() != 1 || !valid_key(args[0])) { return invalid; } if (!ready_) { return not_ready; } const int key = static_cast(args[0]); const Descriptor *state = find(key); if (state == nullptr) { return absent; } const CpuSet missing = online_.difference(ledger_.cohort(key)); if (missing.empty()) { return 0; } std::vector acquired; const int result = replay(*state, missing, acquired); if (result != 0) { rollback_state(key, acquired); return result; } ++ledger_.counters().syncs; return 0; } bool Engine::ready() const { return ready_; } int Engine::cpu_count() const { return cpu_count_; } CpuSet Engine::online() const { return online_; } const Ledger &Engine::ledger() const { return ledger_; } std::vector Engine::descriptors() const { auto result = states_; std::sort(result.begin(), result.end(), [](const auto &left, const auto &right) { return left.key < right.key; }); return result; } }