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>
162 lines
5.9 KiB
C++
162 lines
5.9 KiB
C++
#include "hotplug.hpp"
|
|
#include <algorithm>
|
|
#include <utility>
|
|
|
|
namespace hotplug {
|
|
Engine::Engine() : ready_(false), cpu_count_(0), online_() {}
|
|
bool Engine::valid_key(long long value) { return 1 <= value && value <= max_keys; }
|
|
bool Engine::valid_cpu(long long value) const { return 0 <= value && value < cpu_count_; }
|
|
const Descriptor *Engine::find(int key) const {
|
|
auto found = std::find_if(states_.begin(), states_.end(),
|
|
[key](const Descriptor &entry) { return entry.key == key; });
|
|
return found == states_.end() ? nullptr : &*found;
|
|
}
|
|
std::vector<Descriptor> Engine::ordered() const {
|
|
std::vector<Descriptor> result;
|
|
for (const auto &entry : states_) {
|
|
auto place = std::lower_bound(result.begin(), result.end(), entry,
|
|
[](const Descriptor &left, const Descriptor &right) {
|
|
return std::make_pair(left.priority, left.key) < std::make_pair(right.priority, right.key);
|
|
});
|
|
result.insert(place, entry);
|
|
}
|
|
return result;
|
|
}
|
|
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 entries = states_;
|
|
std::sort(entries.begin(), entries.end(),
|
|
[](const Descriptor &a, const Descriptor &b) { return a.key < b.key; });
|
|
return entries;
|
|
}
|
|
int Engine::execute(const Command &command) {
|
|
ledger_.begin_operation();
|
|
const auto &a = command.args;
|
|
const auto &name = command.name;
|
|
enum class Kind { reset, arm, add, remove, up, down, sync, snap } kind;
|
|
if (name == "RESET") kind = Kind::reset;
|
|
else if (name == "ARM") kind = Kind::arm;
|
|
else if (name == "REG") kind = Kind::add;
|
|
else if (name == "REMOVE") kind = Kind::remove;
|
|
else if (name == "UP") kind = Kind::up;
|
|
else if (name == "DOWN") kind = Kind::down;
|
|
else if (name == "SYNC") kind = Kind::sync;
|
|
else if (name == "SNAP") kind = Kind::snap;
|
|
else return invalid;
|
|
bool valid = false;
|
|
switch (kind) {
|
|
case Kind::reset:
|
|
valid = a.size() == 2 && CpuSet::valid_count(a[0]) && CpuSet::valid_mask(a[1], int(a[0]));
|
|
break;
|
|
case Kind::arm:
|
|
valid = a.size() == 3 && valid_key(a[0]) && 0 <= a[1] && a[1] < max_cpus && 0 <= a[2] && a[2] <= 4;
|
|
break;
|
|
case Kind::add:
|
|
valid = a.size() == 4 && valid_key(a[0]) && -100 <= a[1] && a[1] <= 100 &&
|
|
0 <= a[2] && a[2] <= 65535 && (a[3] == 0 || a[3] == 1);
|
|
break;
|
|
case Kind::remove:
|
|
case Kind::sync:
|
|
valid = a.size() == 1 && valid_key(a[0]);
|
|
break;
|
|
case Kind::up:
|
|
case Kind::down:
|
|
valid = a.size() == 1 && 0 <= a[0] && a[0] < max_cpus;
|
|
break;
|
|
case Kind::snap:
|
|
valid = a.empty();
|
|
break;
|
|
}
|
|
if (!valid) return invalid;
|
|
if (kind == Kind::reset) {
|
|
states_.clear();
|
|
cpu_count_ = int(a[0]);
|
|
online_ = CpuSet(std::uint32_t(a[1]));
|
|
ledger_.reset();
|
|
ready_ = true;
|
|
return 0;
|
|
}
|
|
if (!ready_) return not_ready;
|
|
if (kind == Kind::snap) return 0;
|
|
if (kind == Kind::arm) {
|
|
if (!valid_cpu(a[1])) return invalid;
|
|
ledger_.arm(int(a[0]), int(a[1]), int(a[2]));
|
|
return 0;
|
|
}
|
|
const int id = int(a[0]);
|
|
if ((kind == Kind::up || kind == Kind::down) && !valid_cpu(id)) return invalid;
|
|
if (kind == Kind::add) {
|
|
if (find(id)) return duplicate;
|
|
if (states_.size() >= max_states) return full;
|
|
}
|
|
if ((kind == Kind::remove || kind == Kind::sync) && !find(id)) return absent;
|
|
if (kind == Kind::remove) {
|
|
auto cpus = ledger_.cohort(id).ascending();
|
|
while (!cpus.empty()) {
|
|
ledger_.stop(id, cpus.back(), StopReason::removal);
|
|
cpus.pop_back();
|
|
}
|
|
auto entry = std::find_if(states_.begin(), states_.end(),
|
|
[id](const Descriptor &s) { return s.key == id; });
|
|
states_.erase(entry);
|
|
++ledger_.counters().removals;
|
|
return 0;
|
|
}
|
|
if (kind == Kind::down) {
|
|
if (!online_.contains(id)) return 0;
|
|
auto states = ordered();
|
|
while (!states.empty()) {
|
|
ledger_.stop(states.back().key, id, StopReason::offline);
|
|
states.pop_back();
|
|
}
|
|
online_.erase(id);
|
|
++ledger_.counters().downs;
|
|
return 0;
|
|
}
|
|
// Compile callback work into a plan before touching callback resources.
|
|
std::vector<std::pair<Descriptor, int>> plan;
|
|
Descriptor added;
|
|
if (kind == Kind::add) {
|
|
added = Descriptor{id, int(a[1]), int(a[2]), int(a[3])};
|
|
if (added.replay) {
|
|
for (int cpu : online_.ascending()) plan.emplace_back(added, cpu);
|
|
}
|
|
} else if (kind == Kind::sync) {
|
|
const auto descriptor = *find(id);
|
|
for (int cpu : online_.ascending()) {
|
|
if (!ledger_.held(id, cpu)) plan.emplace_back(descriptor, cpu);
|
|
}
|
|
if (plan.empty()) return 0;
|
|
} else {
|
|
if (online_.contains(id)) return 0;
|
|
for (const auto &descriptor : ordered()) plan.emplace_back(descriptor, id);
|
|
}
|
|
std::size_t completed = 0;
|
|
while (completed < plan.size()) {
|
|
const auto &job = plan[completed];
|
|
if (ledger_.start(job.first, job.second) != 0) {
|
|
while (completed > 0) {
|
|
--completed;
|
|
const auto &undo = plan[completed];
|
|
ledger_.stop(undo.first.key, undo.second, StopReason::rollback);
|
|
}
|
|
return callback_failed;
|
|
}
|
|
++completed;
|
|
}
|
|
if (kind == Kind::add) {
|
|
states_.push_back(added);
|
|
++ledger_.counters().registrations;
|
|
} else if (kind == Kind::sync) {
|
|
++ledger_.counters().syncs;
|
|
} else {
|
|
online_.insert(id);
|
|
++ledger_.counters().ups;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|