Run a virtual iPhone on an Apple Silicon Mac.
Swift
Disarm the virtio sound speaker's flush timer when it stops
Jackya7a5d131f864
diff --git a/Research/Guest/virtio_sound.md b/Research/Guest/virtio_sound.mdindex 5ba9c76a..65f40428 100644--- a/Research/Guest/virtio_sound.md+++ b/Research/Guest/virtio_sound.md@@ -68,6 +68,15 @@ A virtio stream is released when CoreAudio stops and the last transfer has comeback, so an idle guest does not keep the host audio device running and the hostmicrophone is open only while the guest records.+The speaker's 1/24 s flush timer is armed in `startStream` and disarmed when+`stopStream` starts the drain; draining needs no flush, since the device's+returns end it. Until 2026-10-09 the timer was created at the first start and+never stopped, so after the first sound of a boot audiomxd woke 24 times a+second with nothing playing: 21,700–22,500 wakeups and 1.8–2.6 s of CPU per 15+idle minutes on an iPhone 27.0 guest, against under 1,000 wakeups and 0.1 s+with the timer disarmed (see+[`service_trimming.md`](service_trimming.md#idle-cost-of-vphones-own-components)).+The input side (the macOS plugin's receive path, why the microphone is asecond device named `Digital Mic`, what VirtualAudio needed, and what isverified so far) is in `virtio_sound_microphone.md`.diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.mindex 49b5e275..bd44aac7 100644--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m@@ -1228,21 +1228,25 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI// MARK: ASDStream+/// The flush timer runs only while the stream does. audiomxd keeps its+/// streams for its whole life, so a timer left running after the first+/// sound woke an idle audiomxd 24 times a second, about 21,600 times in 15+/// minutes with nothing playing. Arming and disarming happen on `_queue`,+/// in the same blocks that change `_state`.+- (void)armFlushTimer:(BOOL)armed {+ dispatch_assert_queue(_queue);+ if (armed) {+ dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, kFlushIntervalNanoseconds),+ kFlushIntervalNanoseconds, kFlushIntervalNanoseconds / 10);+ } else {+ dispatch_source_set_timer(_timer, DISPATCH_TIME_FOREVER, 0, 0);+ }+}+- (void)startStream {- dispatch_sync(_queue, ^{- if (self->_state == VPStreamStateIdle && ![self startDevice]) {- return;- }- if (self->_state != VPStreamStateRunning) {- [self resetCounters];- [self queueLead];- }- self->_state = VPStreamStateRunning;- });if (!_timer) {_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _queue);- dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, kFlushIntervalNanoseconds),- kFlushIntervalNanoseconds, kFlushIntervalNanoseconds / 10);+ dispatch_source_set_timer(_timer, DISPATCH_TIME_FOREVER, 0, 0);__unsafe_unretained VPVirtIOSoundOutputStream *unretained = self;dispatch_source_set_event_handler(_timer, ^{if (unretained->_state == VPStreamStateRunning) {@@ -1253,6 +1257,17 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI});dispatch_resume(_timer);}+ dispatch_sync(_queue, ^{+ if (self->_state == VPStreamStateIdle && ![self startDevice]) {+ return;+ }+ if (self->_state != VPStreamStateRunning) {+ [self resetCounters];+ [self queueLead];+ [self armFlushTimer:YES];+ }+ self->_state = VPStreamStateRunning;+ });[self installMixBlock];[super startStream];}@@ -1266,6 +1281,8 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI// A start the HAL never wrote after leaves no lead behind.atomic_store(&self->_mix.leadRequest, 0);[self submitPending:YES];+ // Draining needs no flush: the device's returns end it.+ [self armFlushTimer:NO];self->_state = VPStreamStateDraining;if (VPVirtIOSoundRingInFlight(&self->_ring) == 0) {[self stopDevice];