Run a virtual iPhone on an Apple Silicon Mac.
Swift
Queue the lead from the I/O thread, so a restart keeps its cushion
Jacky23feea5d7dbd
diff --git a/VPhoneGuestComponents/Tests/VirtIOSoundTests.c b/VPhoneGuestComponents/Tests/VirtIOSoundTests.cindex e419139a..535a2ecc 100644--- a/VPhoneGuestComponents/Tests/VirtIOSoundTests.c+++ b/VPhoneGuestComponents/Tests/VirtIOSoundTests.c@@ -179,6 +179,44 @@ static void testRingWrapsWritesAndSplitsSubmissions(void) {VPVirtIOSoundRingDestroy(&ring);}+static void testRingQueuesSilenceAhead(void) {+ VPVirtIOSoundRing ring;+ CHECK(VPVirtIOSoundRingInit(&ring, 4 * 64, 64));+ uint8_t frames[160];+ fill(frames, sizeof(frames), 4);+ CHECK(VPVirtIOSoundRingWrite(&ring, frames, sizeof(frames)));+ CHECK(VPVirtIOSoundRingQueued(&ring) == 160);+ uint32_t offset = 0;+ uint32_t length = 0;+ for (int i = 0; i < 2; i++) {+ CHECK(VPVirtIOSoundRingNextSubmission(&ring, false, &offset, &length));+ VPVirtIOSoundRingDidSubmit(&ring, length);+ }+ CHECK(VPVirtIOSoundRingNextSubmission(&ring, true, &offset, &length));+ VPVirtIOSoundRingDidSubmit(&ring, length);+ VPVirtIOSoundRingDidComplete(&ring, 64);+ // In flight counts as queued until the device returns it.+ CHECK(VPVirtIOSoundRingQueued(&ring) == 96);++ // Silence lands behind what is queued, wrapping, and goes out in+ // periods like any other bytes.+ CHECK(VPVirtIOSoundRingWriteSilence(&ring, 128));+ CHECK(VPVirtIOSoundRingQueued(&ring) == 224);+ CHECK(ring.bytes[159] == 4);+ for (unsigned i = 160; i < 256; i++) {+ CHECK(ring.bytes[i] == 0);+ }+ CHECK(ring.bytes[0] == 0 && ring.bytes[31] == 0);+ CHECK(ring.bytes[32] == 4);+ CHECK(VPVirtIOSoundRingNextSubmission(&ring, false, &offset, &length));+ CHECK(offset == 160 && length == 64);+ // All or nothing, as a mix write: 64 bytes do not fit in the 32 left.+ CHECK(!VPVirtIOSoundRingWriteSilence(&ring, 64));+ CHECK(VPVirtIOSoundRingWriteSilence(&ring, 32));+ CHECK(VPVirtIOSoundRingQueued(&ring) == 256);+ VPVirtIOSoundRingDestroy(&ring);+}+static void testRingResetAndValidation(void) {VPVirtIOSoundRing ring;CHECK(!VPVirtIOSoundRingInit(&ring, 100, 64));@@ -203,6 +241,7 @@ int main(void) {testRingSubmitsWholePeriods();testRingKeepsInFlightBytes();testRingWrapsWritesAndSplitsSubmissions();+ testRingQueuesSilenceAhead();testRingResetAndValidation();if (failures) {fprintf(stderr, "VirtIOSoundTests: %d failure(s)\n", failures);diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.mindex 06394f74..7531aaf4 100644--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundPlugin.m@@ -371,12 +371,17 @@ typedef struct {float *scratch;uint32_t bytesPerFrame;uint32_t wireRate;- /// Since the last device start: frames the HAL handed over, bytes that- /// went into the ring, and bytes the ring refused because the device had- /// not returned the space.+ /// The lead, in bytes, that the first write after a start queues ahead+ /// of its frames. The stream's queue sets it at the start; the I/O thread+ /// takes it.+ _Atomic uint32_t leadRequest;+ /// Since the last start: frames the HAL handed over, bytes that went into+ /// the ring, bytes the ring refused because the device had not returned+ /// the space, and bytes of silence queued as the lead._Atomic uint64_t framesIn;_Atomic uint64_t bytesOut;_Atomic uint64_t dropped;+ _Atomic uint64_t leadOut;} VPMixState;static void VPMixRingWrite(VPMixState *mix, const void *bytes, uint32_t length) {@@ -387,7 +392,32 @@ static void VPMixRingWrite(VPMixState *mix, const void *bytes, uint32_t length)}}+/// Tops what is queued ahead of the host up to `lead` bytes, right before+/// the first frames of a run. From here on the mix arrives in real time, so+/// whatever is queued now is the cushion every later period is handed over+/// with — however long the HAL took to start its I/O after `startStream`.+/// What a draining run still has queued counts, except the oldest period in+/// flight: the host may have all but played it, and counting it whole left+/// restarts a period short.+static void VPMixQueueLead(VPMixState *mix, uint32_t lead) {+ uint64_t queued = VPVirtIOSoundRingQueued(mix->ring);+ uint64_t ahead = queued > mix->ring->period ? queued - mix->ring->period : 0;+ if (ahead >= lead) {+ return;+ }+ uint32_t silence = (uint32_t)(lead - ahead);+ if (VPVirtIOSoundRingWriteSilence(mix->ring, silence)) {+ atomic_fetch_add_explicit(&mix->leadOut, silence, memory_order_relaxed);+ } else {+ atomic_fetch_add_explicit(&mix->dropped, silence, memory_order_relaxed);+ }+}+static int VPMixWrite(VPMixState *mix, void *buffer, UInt32 frameCount) {+ uint32_t lead = atomic_exchange_explicit(&mix->leadRequest, 0, memory_order_acquire);+ if (lead > 0) {+ VPMixQueueLead(mix, lead);+ }atomic_fetch_add_explicit(&mix->framesIn, frameCount, memory_order_relaxed);uint32_t halRate = atomic_load(mix->halRate);if (halRate == mix->wireRate) {@@ -461,12 +491,27 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UIBOOL _reportedTransferError;/// Periods of silence queued ahead of the mix at each device start.uint32_t _leadPeriods;- void *_silence;- /// Since the last device start: when it was, writes handed to the device,- /// and how many of them found it with nothing left to play.+ /// Since the last start: when it was, what the device still held then,+ /// writes handed to the device, and how many of them found it with+ /// nothing left to play.uint64_t _startedAt;+ uint64_t _inFlightAtStart;uint64_t _submissions;uint64_t _starved;+ /// The fewest and most bytes in flight a write after the first found, for+ /// the run and for the minute `logProgress` last covered.+ uint64_t _minInFlight;+ uint64_t _maxInFlight;+ uint64_t _windowMinInFlight;+ uint64_t _windowMaxInFlight;+ uint64_t _reportedAt;+ /// The host's consumption once the run has settled: when the first write+ /// came back, the bytes returned after it, and when the last one did;+ /// and the longest wait between two returns.+ uint64_t _firstCompletedAt;+ uint64_t _lastCompletedAt;+ uint64_t _completedAfterFirst;+ uint64_t _maxCompletionGap;/// The rate CoreAudio runs the stream at, which is the wire rate until a/// 44100 aggregate switches the device. The mix block reads it, the rate/// change writes it, both lock-free.@@ -519,9 +564,8 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI}IONotificationPortSetDispatchQueue(_port, _queue);_leadPeriods = VPLeadPeriods();- _silence = calloc(1, _periodBytes);_resampleScratch = malloc(kResampleScratchFrames * format.bytesPerFrame);- if (!_silence || !_resampleScratch) {+ if (!_resampleScratch) {return nil;}@@ -593,7 +637,6 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UIif (_port) {IONotificationPortDestroy(_port);}- free(_silence);free(_resampleScratch);VPVirtIOSoundRingDestroy(&_ring);}@@ -694,12 +737,6 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UIreturn NO;}_reportedTransferError = NO;- _submissions = 0;- _starved = 0;- atomic_store(&_mix.framesIn, 0);- atomic_store(&_mix.bytesOut, 0);- atomic_store(&_mix.dropped, 0);- _startedAt = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);return YES;}@@ -707,34 +744,105 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI/// real time, so with nothing queued ahead every late period is a gap on the/// host: measured, a third to a half of all writes found the device with/// nothing left to play. A few periods of silence first put that much audio-/// between the guest's writes and the host's playback. A restart that finds-/// the device still draining tops up what the drain consumed.+/// between the guest's writes and the host's playback. The I/O thread queues+/// them itself, ahead of its first write (`VPMixQueueLead`): only then is it+/// known what a draining run still holds and how long the HAL took to start,+/// and `written` keeps its one writer.- (void)queueLead {dispatch_assert_queue(_queue);- uint32_t queued = (uint32_t)(VPVirtIOSoundRingInFlight(&_ring) / _periodBytes);- for (uint32_t period = queued; period < _leadPeriods; period++) {- VPVirtIOSoundRingWrite(&_ring, _silence, _periodBytes);- }- [self submitPending:YES];+ atomic_store_explicit(&_mix.leadRequest, _leadPeriods * _periodBytes, memory_order_release);}- (UInt32)queuedLatencyFrames {return (_leadPeriods + 1) * (_periodBytes / _format.bytesPerFrame);}+// MARK: Counters++- (void)resetCounters {+ dispatch_assert_queue(_queue);+ _startedAt = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);+ _reportedAt = _startedAt;+ _inFlightAtStart = VPVirtIOSoundRingInFlight(&_ring);+ _submissions = 0;+ _starved = 0;+ _minInFlight = UINT64_MAX;+ _maxInFlight = 0;+ _windowMinInFlight = UINT64_MAX;+ _windowMaxInFlight = 0;+ _firstCompletedAt = 0;+ // A draining run's returns carry on into this one, so a gap between its+ // tail and this run's lead still shows.+ if (_inFlightAtStart == 0) {+ _lastCompletedAt = 0;+ }+ _completedAfterFirst = 0;+ _maxCompletionGap = 0;+ atomic_store(&_mix.framesIn, 0);+ atomic_store(&_mix.bytesOut, 0);+ atomic_store(&_mix.dropped, 0);+ atomic_store(&_mix.leadOut, 0);+}++- (double)periodsForBytes:(uint64_t)bytes {+ return bytes == UINT64_MAX ? 0 : (double)bytes / _periodBytes;+}++/// Wire frames a second the host has played since the run settled, from the+/// device's completions. The mix arrives at the wire rate of the guest's+/// clock, so against it this is how far the host's audio clock runs from+/// the guest's, and how fast the cushion would erode or grow.+- (double)hostRate {+ if (_firstCompletedAt == 0 || _lastCompletedAt <= _firstCompletedAt) {+ return 0;+ }+ return (double)_completedAfterFirst / _format.bytesPerFrame / ((_lastCompletedAt - _firstCompletedAt) / 1e9);+}++- (double)hostDriftPPM {+ double rate = self.hostRate;+ return rate > 0 ? (rate / _format.sampleRate - 1) * 1e6 : 0;+}+/// One line per run of the device, for telling a gap on the host (`starved`)/// from an unhappy HAL clock (`in` far from the nominal rate) from a full-/// ring (`dropped`).+/// ring (`dropped`). `start` is what a draining run still held when this one+/// started, `lead` the silence queued ahead of it, `in flight` the cushion+/// writes after the first found (whole periods: the device returns whole+/// writes, each a period after the last while it plays), `host` the rate+/// the device played at, and `returns` the longest wait between two returns,+/// which a stall on either side stretches.- (void)logCounters {double seconds = (double)(clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - _startedAt) / 1e9;uint64_t framesIn = atomic_load(&_mix.framesIn);uint64_t framesOut = atomic_load(&_mix.bytesOut) / _format.bytesPerFrame;VPLogToFile("stream %u: %.2f s, %llu writes, %llu starved, %llu bytes dropped, "- "in %llu frames (%.0f/s, hal %u), out %llu frames (%.0f/s)",+ "in %llu frames (%.0f/s, hal %u), out %llu frames (%.0f/s); "+ "start %.2f, lead %.2f, in flight %.2f-%.2f periods; host %.1f/s (%+.0f ppm), returns <= %.0f ms apart",_streamID, seconds, (unsigned long long)_submissions, (unsigned long long)_starved,(unsigned long long)atomic_load(&_mix.dropped),(unsigned long long)framesIn, seconds > 0 ? framesIn / seconds : 0, atomic_load(&_halRate),- (unsigned long long)framesOut, seconds > 0 ? framesOut / seconds : 0);+ (unsigned long long)framesOut, seconds > 0 ? framesOut / seconds : 0,+ [self periodsForBytes:_inFlightAtStart], [self periodsForBytes:atomic_load(&_mix.leadOut)],+ [self periodsForBytes:_minInFlight], [self periodsForBytes:_maxInFlight],+ self.hostRate, self.hostDriftPPM, _maxCompletionGap / 1e6);+}++/// A line a minute while the device runs, so a long playback shows whether+/// the cushion holds.+- (void)logProgress {+ uint64_t now = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);+ if (now - _reportedAt < 60 * NSEC_PER_SEC) {+ return;+ }+ VPLogToFile("stream %u: running %.0f s, %llu writes, %llu starved, in flight %.2f-%.2f periods "+ "this minute; host %.1f/s (%+.0f ppm)",+ _streamID, (now - _startedAt) / 1e9, (unsigned long long)_submissions, (unsigned long long)_starved,+ [self periodsForBytes:_windowMinInFlight], [self periodsForBytes:_windowMaxInFlight],+ self.hostRate, self.hostDriftPPM);+ _reportedAt = now;+ _windowMinInFlight = UINT64_MAX;+ _windowMaxInFlight = 0;}- (void)stopDevice {@@ -764,8 +872,15 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UIuint64_t scalar = _streamID;// Nothing in flight when a period goes out means the device already// played everything it had: a gap on the host.- if (_submissions > 0 && VPVirtIOSoundRingInFlight(&_ring) == 0) {- _starved++;+ uint64_t inFlight = VPVirtIOSoundRingInFlight(&_ring);+ if (_submissions > 0) {+ if (inFlight == 0) {+ _starved++;+ }+ _minInFlight = MIN(_minInFlight, inFlight);+ _maxInFlight = MAX(_maxInFlight, inFlight);+ _windowMinInFlight = MIN(_windowMinInFlight, inFlight);+ _windowMaxInFlight = MAX(_windowMaxInFlight, inFlight);}_submissions++;VPVirtIOSoundRingDidSubmit(&_ring, length);@@ -782,6 +897,21 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UI- (void)transferCompletedWithLength:(uint32_t)length result:(IOReturn)result {dispatch_assert_queue(_queue);VPVirtIOSoundRingDidComplete(&_ring, length);+ uint64_t now = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);+ if (_lastCompletedAt > 0) {+ uint64_t gap = now - _lastCompletedAt;+ _maxCompletionGap = MAX(_maxCompletionGap, gap);+ }+ _lastCompletedAt = now;+ // The host's rate counts once the run has settled: its first returns+ // follow the start, a draining run's tail and the host's own buffering.+ if (now - _startedAt >= 5 * NSEC_PER_SEC) {+ if (_firstCompletedAt == 0) {+ _firstCompletedAt = now;+ } else {+ _completedAfterFirst += length;+ }+ }if (result != kIOReturnSuccess && !_reportedTransferError) {_reportedTransferError = YES;os_log_error(VPLog(), "stream %u: write of %u bytes failed: 0x%x", _streamID, length, result);@@ -799,6 +929,7 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UIreturn;}if (self->_state != VPStreamStateRunning) {+ [self resetCounters];[self queueLead];}self->_state = VPStreamStateRunning;@@ -811,6 +942,7 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UIdispatch_source_set_event_handler(_timer, ^{if (unretained->_state == VPStreamStateRunning) {[unretained submitPending:NO];+ [unretained logProgress];}});dispatch_resume(_timer);@@ -825,6 +957,8 @@ static void VPWriteCompleted(void *refcon, IOReturn result, void **arguments, UIreturn;}[self logCounters];+ // A start the HAL never wrote after leaves no lead behind.+ atomic_store(&self->_mix.leadRequest, 0);[self submitPending:YES];self->_state = VPStreamStateDraining;if (VPVirtIOSoundRingInFlight(&self->_ring) == 0) {diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.c b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.cindex c656514d..4876fd83 100644--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.c+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.c@@ -35,7 +35,16 @@ void VPVirtIOSoundRingReset(VPVirtIOSoundRing *ring) {ring->submitted = 0;}-bool VPVirtIOSoundRingWrite(VPVirtIOSoundRing *ring, const void *source, uint32_t length) {+/// Copies `length` bytes from `source`, or zeroes them when it is NULL.+static void copyInto(uint8_t *destination, const uint8_t *source, uint32_t length) {+ if (source) {+ memcpy(destination, source, length);+ } else {+ memset(destination, 0, length);+ }+}++static bool append(VPVirtIOSoundRing *ring, const uint8_t *source, uint32_t length) {if (length == 0) {return true;}@@ -47,15 +56,28 @@ bool VPVirtIOSoundRingWrite(VPVirtIOSoundRing *ring, const void *source, uint32_uint32_t offset = (uint32_t)(written % ring->capacity);uint32_t first = ring->capacity - offset;if (first >= length) {- memcpy(ring->bytes + offset, source, length);+ copyInto(ring->bytes + offset, source, length);} else {- memcpy(ring->bytes + offset, source, first);- memcpy(ring->bytes, (const uint8_t *)source + first, length - first);+ copyInto(ring->bytes + offset, source, first);+ copyInto(ring->bytes, source ? source + first : NULL, length - first);}atomic_store_explicit(&ring->written, written + length, memory_order_release);return true;}+bool VPVirtIOSoundRingWrite(VPVirtIOSoundRing *ring, const void *source, uint32_t length) {+ return append(ring, source, length);+}++bool VPVirtIOSoundRingWriteSilence(VPVirtIOSoundRing *ring, uint32_t length) {+ return append(ring, NULL, length);+}++uint64_t VPVirtIOSoundRingQueued(const VPVirtIOSoundRing *ring) {+ return atomic_load_explicit(&ring->written, memory_order_relaxed)+ - atomic_load_explicit(&ring->completed, memory_order_acquire);+}+bool VPVirtIOSoundRingNextSubmission(const VPVirtIOSoundRing *ring,bool partial,diff --git a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.h b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.hindex 01f45022..80d42c0e 100644--- a/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.h+++ b/VPhoneGuestComponents/VirtIOSound/VPVirtIOSoundRing.h@@ -4,12 +4,13 @@//// completed <= submitted <= written//-// The real-time I/O thread appends mixed frames and advances `written`. The-// stream's serial queue hands `[submitted, written)` to the device and-// advances `submitted`, and advances `completed` when the device returns the-// bytes. Only `[written, completed + capacity)` may be overwritten: the-// kernel reads a submitted region from the caller's pages until it-// completes, so the space is not free before then.+// The real-time I/O thread appends mixed frames, and the silence queued+// ahead of them, and advances `written`; nothing else appends. The stream's+// serial queue hands `[submitted, written)` to the device and advances+// `submitted`, and advances `completed` when the device returns the bytes.+// Only `[written, completed + capacity)` may be overwritten: the kernel reads+// a submitted region from the caller's pages until it completes, so the+// space is not free before then.#ifndef VPVirtIOSoundRing_h#define VPVirtIOSoundRing_h@@ -41,6 +42,14 @@ void VPVirtIOSoundRingReset(VPVirtIOSoundRing *ring);/// frames are dropped rather than overwriting bytes still in flight.bool VPVirtIOSoundRingWrite(VPVirtIOSoundRing *ring, const void *source, uint32_t length);+/// Append `length` zero bytes, all or none, as `VPVirtIOSoundRingWrite`+/// does. Real-time safe.+bool VPVirtIOSoundRingWriteSilence(VPVirtIOSoundRing *ring, uint32_t length);++/// Bytes appended and not yet returned by the device: in flight plus+/// pending. For the writer, the I/O thread.+uint64_t VPVirtIOSoundRingQueued(const VPVirtIOSoundRing *ring);+/// The next region to submit, contiguous in memory. Normally a whole period/// or nothing; with `partial`, whatever is pending (used when the stream/// stops, so its tail is not left behind). A region never crosses the end