J: E116 time-bucket accounting

EptStateMachine now retains per-state cumulative dwell time so the
host can read it as SVIDs.  The implementation is zero-overhead while
the FSM is idle (no timers, no background work) — on every transition
we add the prior state's dwell to its bucket and reset the entered_
timestamp.  Live dwell in the current state is included in
accumulated() via a now-vs-entered_ delta at read time.

New public API:
  accumulated(EptState)   per-state cumulative ms (incl. live dwell)
  total_elapsed()         denominator for utilization ratios
  reset_history()         S2F43-style history clear

This closes the gap I called out: previously we emitted CEIDs on
transition but didn't accumulate the bucket the host actually queries
for utilization metrics.  Wiring these into specific SVIDs is the
application's job (equipment.yaml declares SVIDs against any read
callable); the runtime data is now there.

4 new test cases cover accumulation, live-dwell inclusion, and reset.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-08 08:41:09 +02:00
parent 28dac8e9c8
commit 06f664dfab
3 changed files with 85 additions and 1 deletions
+26 -1
View File
@@ -78,7 +78,8 @@ EptTable default_ept_table() {
EptStateMachine::EptStateMachine()
: table_(default_ept_table()),
entered_(std::chrono::steady_clock::now()) {}
entered_(std::chrono::steady_clock::now()),
reset_anchor_(entered_) {}
bool EptStateMachine::on_event(EptEvent e) {
const auto* row = table_.find(state_, e);
@@ -88,6 +89,7 @@ bool EptStateMachine::on_event(EptEvent e) {
auto now = std::chrono::steady_clock::now();
auto dwell = std::chrono::duration_cast<std::chrono::milliseconds>(
now - entered_);
buckets_[static_cast<size_t>(prev)] += dwell;
state_ = *row->to;
entered_ = now;
if (on_change_) on_change_(prev, state_, e, dwell);
@@ -95,4 +97,27 @@ bool EptStateMachine::on_event(EptEvent e) {
return true;
}
std::chrono::milliseconds EptStateMachine::accumulated(EptState s) const {
const auto idx = static_cast<size_t>(s);
if (idx >= buckets_.size()) return std::chrono::milliseconds{0};
auto base = buckets_[idx];
if (state_ == s) {
const auto now = std::chrono::steady_clock::now();
base += std::chrono::duration_cast<std::chrono::milliseconds>(now - entered_);
}
return base;
}
std::chrono::milliseconds EptStateMachine::total_elapsed() const {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - reset_anchor_);
}
void EptStateMachine::reset_history() {
buckets_.fill(std::chrono::milliseconds{0});
const auto now = std::chrono::steady_clock::now();
entered_ = now;
reset_anchor_ = now;
}
} // namespace secsgem::gem