#include #include #include #include #include struct IdleState { std::string_view name; std::uint64_t target_residency_us; std::uint64_t exit_latency_us; bool needs_shared_domain; }; // Ordered from shallower to deeper; all numbers are teaching values. constexpr std::array states{ {{"MODEL_IDLE_LIGHT", 20, 10, false}, {"MODEL_IDLE_DEEP", 400, 100, true}}}; const IdleState* select_idle(std::uint64_t predicted_gap_us, std::uint64_t wake_budget_us, bool shared_domain_ready) { const IdleState* selected = nullptr; for (const auto& state : states) { const bool long_enough = predicted_gap_us >= state.target_residency_us; const bool fast_enough = state.exit_latency_us <= wake_budget_us; const bool dependencies_ready = !state.needs_shared_domain || shared_domain_ready; if (long_enough && fast_enough && dependencies_ready) { selected = &state; } } return selected; // nullptr: this model has no eligible idle state. } void check(std::string_view label, std::uint64_t gap_us, std::uint64_t budget_us, bool shared_ready, std::string_view expected) { const auto* state = select_idle(gap_us, budget_us, shared_ready); const std::string_view actual = state ? state->name : "NONE_ELIGIBLE"; if (actual != expected) { throw std::runtime_error("idle-policy self-test failed"); } std::cout << "PASS: " << label << " -> " << actual << '\n'; } int main() { try { check("long gap", 2000, 150, true, "MODEL_IDLE_DEEP"); check("tight wake budget", 2000, 50, true, "MODEL_IDLE_LIGHT"); check("short gap", 200, 150, true, "MODEL_IDLE_LIGHT"); check("shared domain busy", 2000, 150, false, "MODEL_IDLE_LIGHT"); check("no eligible state", 5, 150, true, "NONE_ELIGIBLE"); check("boundary equality", 400, 100, true, "MODEL_IDLE_DEEP"); } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } return 0; }