From 475d6b2527b92d2888783ecc100a8c3667c475df Mon Sep 17 00:00:00 2001 From: Kadamitas <29363268+Kadamitas@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:02:05 -0600 Subject: [PATCH] present: look up retained frame tokens by age Origin waits append consecutive tokens to the 64-record history. Index the matching record from the back and reuse it during present, statistics and retirement. Advance the issued counter only after appending succeeds, so a failed allocation cannot leave a hole in the history. --- runtime/src/frame_pacer.h | 69 +++++++++++++++++-------------- runtime/test/frame_pacer_test.cpp | 29 +++++++++++++ 2 files changed, 66 insertions(+), 32 deletions(-) diff --git a/runtime/src/frame_pacer.h b/runtime/src/frame_pacer.h index 5588d73ea4..9dc352eeea 100644 --- a/runtime/src/frame_pacer.h +++ b/runtime/src/frame_pacer.h @@ -278,9 +278,7 @@ class FramePacer { if (!count) return pacer_hr::invalid_arg; std::lock_guard guard(mutex_); retire_locked(clock_.now_us()); - const Record* record = nullptr; - for (const Record& r : records_) - if (r.token == token) record = &r; + const Record* record = token_record_locked(token); FrameStatistic found[3]; std::uint32_t n = 0, expected = 0; auto add = [&](std::uint32_t type, bool present, std::uint64_t time) { @@ -343,9 +341,9 @@ class FramePacer { retire_locked(now); timing.period_us = period_us_; if (!pending_.empty()) return timing; - for (const Record& r : records_) - if (r.token == token && !r.presented && token > last_presented_ && now > r.due_us) - timing.late_us = now - r.due_us; + if (const auto* record = token_record_locked(token); + record && !record->presented && token > last_presented_ && now > record->due_us) + timing.late_us = now - record->due_us; return timing; } @@ -357,11 +355,14 @@ class FramePacer { std::lock_guard guard(mutex_); const std::uint64_t now = clock_.now_us(); retire_locked(now); - record_present_timing_locked(now, token); + Record* record = token_record_locked(token); + record_present_timing_locked(now, token ? record : nullptr); last_presented_ = std::max(last_presented_, token); if (!interval_set_ || !any_armed_locked() || uncapped_) { // not paced or uncapped: shows at once - for (Record& r : records_) - if (r.token == token) { r.presented = r.displayed = true; r.present_us = r.display_us = now; } + if (record) { + record->presented = record->displayed = true; + record->present_us = record->display_us = now; + } return; } const bool queued_before = !pending_.empty(); @@ -370,14 +371,15 @@ class FramePacer { if (sync == PresentSync::immediate && !queued_before) { flip = now; std::uint64_t late_us = 0; - for (const Record& r : records_) - if (r.token == token && now > r.due_us) late_us = now - r.due_us; + if (record && now > record->due_us) late_us = now - record->due_us; percent = scan_percent(late_us, period_us_); ++immediate_presents_; } pending_.push_back(Pending{token, flip, percent}); - for (Record& r : records_) - if (r.token == token) { r.presented = true; r.present_us = now; } + if (record) { + record->presented = true; + record->present_us = now; + } retire_locked(now); cv_.notify_all(); } @@ -386,7 +388,7 @@ class FramePacer { void note_untokened_present() { std::lock_guard guard(mutex_); const std::uint64_t now = clock_.now_us(); - record_present_timing_locked(now, 0); + record_present_timing_locked(now, nullptr); if (!interval_set_ || !any_armed_locked() || uncapped_) return; retire_locked(now); const std::uint64_t flip = queue_flip_locked(now); @@ -504,7 +506,17 @@ class FramePacer { return flip; } - void record_present_timing_locked(std::uint64_t now, std::uint64_t token) { + Record* token_record_locked(std::uint64_t token) { + if (records_.empty()) return nullptr; + const auto newest = records_.back().token; + // A shared token source and unregistered polls can leave gaps between local records. + // Compare unsigned distances from the newest retained token to preserve their order at rollover. + const auto found = std::lower_bound(records_.begin(), records_.end(), newest - token, + [newest](const Record& record, std::uint64_t age) { return newest - record.token > age; }); + return found != records_.end() && found->token == token ? &*found : nullptr; + } + + void record_present_timing_locked(std::uint64_t now, const Record* record) { ++presents_recorded_; if (has_last_present_ && now >= last_present_us_) { interval_ring_[interval_head_] = now - last_present_us_; @@ -513,29 +525,22 @@ class FramePacer { } has_last_present_ = true; last_present_us_ = now; - if (token != 0) { - for (const Record& r : records_) { - if (r.token == token) { - if (now >= r.origin_us) { - latency_ring_[latency_head_] = now - r.origin_us; - latency_head_ = (latency_head_ + 1) % frame_pacing_window; - if (latency_count_ < frame_pacing_window) ++latency_count_; - } - break; - } - } + if (record && now >= record->origin_us) { + latency_ring_[latency_head_] = now - record->origin_us; + latency_head_ = (latency_head_ + 1) % frame_pacing_window; + if (latency_count_ < frame_pacing_window) ++latency_count_; } } // Frames whose vblank has come are shown. void retire_locked(std::uint64_t now) { while (!pending_.empty() && pending_.front().flip_us <= now) { - for (Record& r : records_) - if (r.token == pending_.front().token && pending_.front().token) { - r.displayed = true; - r.display_us = pending_.front().flip_us; - r.percent_scanned = pending_.front().percent_scanned; - } + const auto& presented = pending_.front(); + if (auto* record = presented.token ? token_record_locked(presented.token) : nullptr) { + record->displayed = true; + record->display_us = presented.flip_us; + record->percent_scanned = presented.percent_scanned; + } pending_.pop_front(); } } diff --git a/runtime/test/frame_pacer_test.cpp b/runtime/test/frame_pacer_test.cpp index bb632793b9..22944e0e98 100644 --- a/runtime/test/frame_pacer_test.cpp +++ b/runtime/test/frame_pacer_test.cpp @@ -120,6 +120,21 @@ int main() { EXPECT(tokens->issue() == outside + 2); EXPECT(pacer.check_present_token(outside + 2) == pacer_hr::ok); EXPECT(pacer.check_present_token(outside + 3) == pacer_hr::invalid_arg); + FrameStatistic event{}; + std::uint32_t count = 1; + EXPECT(pacer.get_frame_statistics(token, stat_frame_event, &count, &event) == pacer_hr::ok && + event.time_us == period); + clock.advance_us(period); + std::uint64_t newest = 0; + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &newest) == pacer_hr::ok && newest == token + 2); + count = 1; + EXPECT(pacer.get_frame_statistics(token + 1, stat_frame_event, &count, &event) == pacer_hr::false_ && count == 0); + pacer.commit_present_token(newest, 1); + count = 1; + EXPECT(pacer.get_frame_statistics(newest, stat_frame_event, &count, &event) == pacer_hr::ok && + event.time_us == 2 * period); + count = 1; + EXPECT(pacer.get_frame_statistics(token, stat_present, &count, &event) == pacer_hr::false_ && count == 0); } // An offset delays the event inside the period: origin n comes at n * period + offset. @@ -193,6 +208,20 @@ int main() { EXPECT(blocked(next)); clock.advance_us(period * 3 / 4); EXPECT(next.get().token == 4); + + pacer.suspend(); + pacer.resume(); + std::uint64_t unregistered = 0; + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, wait_flag_current_token, &unregistered) == pacer_hr::ok && + unregistered == 5); + EXPECT(setup(pacer, 1)); + clock.advance_us(period); + EXPECT(pacer.wait_frame_event(frame_event_origin, 0, 0, &token) == pacer_hr::ok && token == 6); + count = 1; + EXPECT(pacer.get_frame_statistics(unregistered, stat_frame_event, &count, &stat) == pacer_hr::false_ && count == 0); + count = 1; + EXPECT(pacer.get_frame_statistics(token, stat_frame_event, &count, &stat) == pacer_hr::ok && + stat.time_us == clock.now_us()); } // Timeouts: 0 polls, a finite one counts fake time, the token is left alone, and a later wait still works.