Skip to content

Conversation

@nchapman
Copy link
Collaborator

@nchapman nchapman commented Dec 5, 2025

So much.... Really nice upgrade across the board!

Implements dynamic CPU frequency scaling that monitors audio buffer
stress to automatically adjust between POWERSAVE/NORMAL/PERFORMANCE
levels.

Key features: - Uses rate control stress metric (0-1 normalized,
buffer-size independent) - Window-based averaging with hysteresis (1s
boost, 1.5s reduce) - Background thread applies changes without blocking
emulation - Panic path for immediate boost on audio underruns - Startup
grace period avoids false positives during buffer fill

Added to minarch CPU Speed menu as "Auto" option (4th choice).

Testing on Miyoo Mini shows correct behavior: - Demanding games
(Punch-Out) stay at PERFORMANCE - Light games (Tetris) reduce
appropriately - No audio glitches from frequency changes
The "Prevent Tearing" (vsync) and "Prioritize Audio" (threaded video)
settings were workarounds for audio/video sync issues. With dynamic rate
control implemented, these are obsolete - vsync should always be on, and
rate control handles timing drift via audio resampling.

- Remove thread_video, prevent_tearing variables and menu options -
Remove coreThread() and all pthread synchronization code - Remove
GFX_setVsync/PLAT_setVsync from API and all platforms - Set strict vsync
at init on miyoomini (SDL2 platforms use renderer flag) - Clean up
system.cfg and emulator config files
timing.

Moving GFX_flip() outside the video callback allows the core to run at
its natural rate instead of being throttled to display refresh rate.
This fixes the audio production deficit that required aggressive rate
control (d=2%) and enables accurate CPU performance measurement.
When vsync throttles the main loop to display rate instead of core rate,
audio production falls short of consumption causing buffer drift and
underruns.

Implements display rate correction (display_fps / core_fps) applied to
resampler ratio_adjust, compensating for structural timing mismatch.

Also separates rate control parameter (0.5%) from resampler safety clamp
(5%), extracts display measurement to measureDisplayRate() with
stability detection, and removes unused SND_getRateControlStress().
Auto CPU mode now detects available frequencies from sysfs and scales
through all steps instead of just 3 fixed levels. Includes conservative
step-down limits (max 2 at once), 75% predicted utilization safety
margin, and 8-window panic cooldown to prevent oscillation on platforms
with sparse frequency tables.
- Renamed MENU → IDLE (20% of max, for tools/utilities) - Updated all
platforms to 20/55/80/100% frequency distribution - Launcher now uses
POWERSAVE instead of IDLE for responsiveness - Changed default CPU mode
from Normal to Auto - Removed all per-core CPU overrides (Auto handles
everything)
Copilot AI review requested due to automatic review settings December 5, 2025 04:29
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements comprehensive rate control upgrades and auto CPU scaling for LessUI. It replaces the old VSync/threading system with always-on VSync + dynamic rate control, adds automatic CPU frequency scaling based on frame timing, and introduces granular frequency control using all available CPU frequencies detected from the system.

Key changes:

  • Auto CPU scaling with frame timing-based monitoring and granular frequency control
  • Unified rate measurement system (RateMeter) for display and audio with dual clock correction
  • VSync always-on approach with threading removal, decoupling flip from core.run()

Reviewed changes

Copilot reviewed 74 out of 75 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Platform files (zero28, tg5040, rgb30, etc.) CPU speed enum renamed (MENU→IDLE), frequency values updated, new frequency detection/setting functions
minarch.c Auto CPU scaling implementation, threading removal, VSync decoupling, rate measurement integration
api.c/api.h Rate meter integration, dual correction, adaptive d parameter, CPU frequency sysfs helpers
rate_meter.c/h New unified rate measurement module with stability detection
audio_resampler.c/h Safety clamp increase (1%→5%), diagnostics tracking
utils.c/h Percentile calculation functions for frame timing analysis
Config files VSync/threading options removed, CPU speed defaults updated
Documentation Comprehensive docs for auto CPU scaling and audio rate control
Tests New rate_meter unit tests

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nchapman nchapman merged commit 23b358a into develop Dec 5, 2025
4 checks passed
@nchapman nchapman deleted the feature/cpu-scaling branch December 5, 2025 04:56
@nchapman nchapman mentioned this pull request Dec 6, 2025
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants