#ifndef COHORT_HOTPLUG_HPP #define COHORT_HOTPLUG_HPP #include #include #include #include namespace hotplug { constexpr int max_cpus = 16; constexpr int max_keys = 31; constexpr int max_states = 8; constexpr int invalid = -22; constexpr int absent = -2; constexpr int duplicate = -17; constexpr int not_ready = -19; constexpr int full = -28; constexpr int callback_failed = -5; enum class StopReason { rollback = 1, offline = 2, removal = 3 }; class CpuSet { public: CpuSet(); explicit CpuSet(std::uint32_t bits); static bool valid_count(long long count); static bool valid_mask(long long mask, int count); bool contains(int cpu) const; bool insert(int cpu); bool erase(int cpu); bool empty() const; int count() const; std::uint32_t bits() const; std::vector ascending() const; std::vector descending() const; CpuSet difference(const CpuSet &other) const; CpuSet intersect(const CpuSet &other) const; bool subset_of(const CpuSet &other) const; bool operator==(const CpuSet &other) const; private: std::uint32_t bits_; }; struct Descriptor { int key = 0; int priority = 0; int tag = 0; int replay = 0; }; struct Lease { bool held = false; std::uint64_t ticket = 0; int tag = 0; }; struct Event { char kind = 'S'; int key = 0; int cpu = 0; std::uint64_t ticket = 0; int detail = 0; }; struct Counters { std::uint64_t attempts = 0; std::uint64_t failures = 0; std::uint64_t stops = 0; std::uint64_t registrations = 0; std::uint64_t removals = 0; std::uint64_t ups = 0; std::uint64_t downs = 0; std::uint64_t syncs = 0; }; class Ledger { public: Ledger(); void reset(); void begin_operation(); void arm(int key, int cpu, int count); int budget(int key, int cpu) const; int start(const Descriptor &state, int cpu); void stop(int key, int cpu, StopReason reason); bool held(int key, int cpu) const; const Lease &lease(int key, int cpu) const; CpuSet cohort(int key) const; std::uint64_t next_ticket() const; const std::vector &events() const; Counters &counters(); const Counters &counters() const; std::vector held_keys() const; bool empty() const; private: std::array, max_keys + 1> leases_; std::array, max_keys + 1> budgets_; std::uint64_t next_; Counters counters_; std::vector events_; }; struct Command { std::string name; std::vector args; }; class Engine { public: Engine(); int execute(const Command &command); bool ready() const; int cpu_count() const; CpuSet online() const; const Ledger &ledger() const; std::vector descriptors() const; std::string observe(int result) const; private: bool ready_; int cpu_count_; CpuSet online_; std::vector states_; Ledger ledger_; static bool valid_key(long long key); bool valid_cpu(long long cpu) const; const Descriptor *find(int key) const; std::vector ordered() const; int reset(const std::vector &args); int arm(const std::vector &args); int add(const std::vector &args); int remove(const std::vector &args); int up(const std::vector &args); int down(const std::vector &args); int sync(const std::vector &args); int replay(const Descriptor &state, const CpuSet &targets, std::vector &acquired); void rollback_state(int key, const std::vector &acquired); void rollback_cpu(int cpu, const std::vector &acquired); }; } #endif