From 8511add273f8e72bd203a47e02b2412d5dce6899 Mon Sep 17 00:00:00 2001 From: qiyinxi Date: Tue, 17 Nov 2026 11:59:58 +0100 Subject: [PATCH] emu: keep the guest on the TSC clocksource (tsc=nowatchdog) After hours of running, the guest clocksource watchdog can give up on the TSC when HPET reads are slow three times in a row, leaving the emulated HPET as the only clock; every clock read then exits to the host and games drop to a few fps until the next cold boot. Pass tsc=nowatchdog on Windows so the watchdog no longer checks the TSC; the boot-time TSC sync check still runs. Refs ModAVD/ModAVD#110 --- changelog.d/110-tsc-nowatchdog.fix.md | 1 + internal/emu/launch.go | 16 ++++++++++++++++ internal/emu/launch_test.go | 4 ++-- 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 changelog.d/110-tsc-nowatchdog.fix.md diff --git a/changelog.d/110-tsc-nowatchdog.fix.md b/changelog.d/110-tsc-nowatchdog.fix.md new file mode 100644 index 0000000..e94f8a0 --- /dev/null +++ b/changelog.d/110-tsc-nowatchdog.fix.md @@ -0,0 +1 @@ +模拟器连续开很久以后不再突然变得很卡 (ModAVD/ModAVD#110) diff --git a/internal/emu/launch.go b/internal/emu/launch.go index d5723e0..9479290 100644 --- a/internal/emu/launch.go +++ b/internal/emu/launch.go @@ -42,6 +42,8 @@ const adbServerStartTimeout = 20 * time.Second // // qemuExtra comes last, still inside the -qemu part: the manager's own QEMU arguments (syncIOQemuArgs). // +// On Windows the guest kernel also gets tsc=nowatchdog (tscNoWatchdogArgs). +// // The window is not an instance option: it is decided at boot by whoever starts it. func buildLaunchArgs(avdName string, console, memoryMB int, headless bool, qemuExtra ...string) []string { args := []string{ @@ -78,9 +80,23 @@ func buildLaunchArgs(avdName string, console, memoryMB int, headless bool, qemuE if root.CurrentPlatform().MirrorOS != "macos" { args = append(args, "-device", "virtio-balloon-pci,free-page-reporting=on") } + if root.CurrentPlatform().MirrorOS == "win" { + args = append(args, tscNoWatchdogArgs...) + } return append(args, qemuExtra...) } +// tscNoWatchdogArgs keeps the guest on the TSC clocksource for the whole run (ModAVD/ModAVD#110). WHPX gives the +// guest no kvm-clock, so when the kernel's clocksource watchdog drops the TSC the only clock left is the HPET, which +// QEMU emulates: every clock read exits to the host. The watchdog compares the TSC against that HPET and gives up on +// the TSC when an HPET read is slow three times in a row ("hpet read-back delay ..., attempt 3, marking unstable"), +// which a busy host eventually causes. On a production instance it happened after 13.8 hours; from then on +// Arknights' render thread spent 72% of its time in read_hpet and the game fell to about 5 fps until the next cold +// boot. tsc=nowatchdog only takes the TSC out of the watchdog; the boot-time TSC synchronization check still runs, so +// vCPUs whose TSCs really differ are still caught (unlike tsc=reliable, which skips that check too). "-append" after +// -qemu is merged into the kernel command line by the emulator, not passed on to QEMU as a replacement. +var tscNoWatchdogArgs = []string{"-append", "tsc=nowatchdog"} + // launchPlan is what prepareLaunch settled before emulator.exe is started. type launchPlan struct { args []string diff --git a/internal/emu/launch_test.go b/internal/emu/launch_test.go index 86212b5..7bd8b46 100644 --- a/internal/emu/launch_test.go +++ b/internal/emu/launch_test.go @@ -27,13 +27,13 @@ var ( "-feature", "-QuickbootFileBacked", "-feature", "-VirtioSndCard", "-feature", "VulkanBatchedDescriptorSetUpdate", "-feature", "-WiFiPacketStream", "-feature", "-BluetoothEmulation", "-feature", "-Uwb", "-qemu", "-global", "virtio-blk-device.write-cache=off", "-global", "virtio-blk-device.config-wce=off", - "-device", "virtio-balloon-pci,free-page-reporting=on"} + "-device", "virtio-balloon-pci,free-page-reporting=on", "-append", "tsc=nowatchdog"} masArgsWindow = []string{"-avd", "mas_3", "-no-snapshot", "-gpu", "host", "-ports", "20030,20031", "-grpc", "20032", "-grpc-use-token", "-audio", "none", "-memory", "4096", "-feature", "-QuickbootFileBacked", "-feature", "-VirtioSndCard", "-feature", "VulkanBatchedDescriptorSetUpdate", "-feature", "-WiFiPacketStream", "-feature", "-BluetoothEmulation", "-feature", "-Uwb", "-qemu", "-global", "virtio-blk-device.write-cache=off", "-global", "virtio-blk-device.config-wce=off", - "-device", "virtio-balloon-pci,free-page-reporting=on"} + "-device", "virtio-balloon-pci,free-page-reporting=on", "-append", "tsc=nowatchdog"} ) func TestBuildLaunchArgs(t *testing.T) {