100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth range given to batch 22), each with at least one independent contract-edge defect and plain public docs. Claude reviewed 100/100 USABLE against current digests; no ID or source overlap with main or batch 22. Difficulty is unmeasured. progress.md records the wrap-up: all Codex sessions and Qwen schedulers stopped for the user's rollout, Qwen results so far (57 HARD of 373), and what is left on each batch branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
256 lines
7.2 KiB
C++
256 lines
7.2 KiB
C++
#include "hotplug.hpp"
|
|
#include <algorithm>
|
|
|
|
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<Descriptor> 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<long long> &args) {
|
|
if (args.size() != 2 || !CpuSet::valid_count(args[0])) {
|
|
return invalid;
|
|
}
|
|
if (!CpuSet::valid_mask(args[1], static_cast<int>(args[0]))) {
|
|
return invalid;
|
|
}
|
|
ready_ = true;
|
|
cpu_count_ = static_cast<int>(args[0]);
|
|
online_ = CpuSet(static_cast<std::uint32_t>(args[1]));
|
|
states_.clear();
|
|
ledger_.reset();
|
|
return 0;
|
|
}
|
|
int Engine::arm(const std::vector<long long> &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<int>(args[0]), static_cast<int>(args[1]),
|
|
static_cast<int>(args[2]));
|
|
return 0;
|
|
}
|
|
int Engine::replay(const Descriptor &state, const CpuSet &targets,
|
|
std::vector<int> &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<int> &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<int> &acquired) {
|
|
for (auto position = acquired.rbegin() + (acquired.empty() ? 0 : 1); position != acquired.rend(); ++position) {
|
|
ledger_.stop(*position, cpu, StopReason::rollback);
|
|
}
|
|
}
|
|
int Engine::add(const std::vector<long long> &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<int>(args[0]);
|
|
if (find(key) != nullptr) {
|
|
return duplicate;
|
|
}
|
|
if (states_.size() == max_states) {
|
|
return full;
|
|
}
|
|
const Descriptor state{key, static_cast<int>(args[1]),
|
|
static_cast<int>(args[2]), static_cast<int>(args[3])};
|
|
std::vector<int> 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<long long> &args) {
|
|
if (args.size() != 1 || !valid_key(args[0])) {
|
|
return invalid;
|
|
}
|
|
if (!ready_) {
|
|
return not_ready;
|
|
}
|
|
const int key = static_cast<int>(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<long long> &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<int>(args[0]);
|
|
if (online_.contains(cpu)) {
|
|
return 0;
|
|
}
|
|
std::vector<int> 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<long long> &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<int>(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<long long> &args) {
|
|
if (args.size() != 1 || !valid_key(args[0])) {
|
|
return invalid;
|
|
}
|
|
if (!ready_) {
|
|
return not_ready;
|
|
}
|
|
const int key = static_cast<int>(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<int> 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<Descriptor> 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;
|
|
}
|
|
}
|