mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
* audio: count microphone overloads on the realtime thread, log them coalesced CoreAudio reports kAudioDeviceProcessorOverload as ErrorKind::Xrun from its real-time thread. Hex's handler ran tracing::warn! there (formatting plus a blocking write to stderr and process.log behind a mutex) and locked STREAM_GLITCHED_AT, once per overload, hundreds of times a day, on a thread that had already missed its cycle. The error callback now only moves atomics (a total, a refused real-time promotion count, and the last overload time against a clock taken before the stream runs). AudioInput::recv_timeout, the poll that drains the stream for dictation and meetings, reports them: one line per burst with count and realtime_denied, at most every 10 s, and the count a throttled poll holds back is taken by the next one rather than lost. since_last_stream_glitch keeps its signature and semantics from the same counters. On macOS the first chunk of each open also logs the callback's frames and period, since cpal exposes no readback for BufferSize::Default and overloads arrive sooner on a smaller hardware period. No FFI or unsafe was added. Moved the counters into audio/overloads.rs to keep audio.rs under the 1,500-line shape limit. * audio: write a closing stream's leftover overloads, and stop calling them CoreAudio's Review follow-ups to the coalesced overload log: - impl Drop for AudioInput drops the stream first, so no callback races, then overloads::flush writes any count the 10 s window still held. Without it, a burst left over at close surfaced on the next open, hours later, next to "microphone stream opened" and read as a cold-start burst. Flush is swap-based and skips the rate limit; nothing pending costs one atomic load, and the fixture and test inputs (no stream) drop as before. - cpal 0.18 emits RealtimeDenied only from the aaudio and jack hosts, so the separate REALTIME_DENIED counter and the realtime_denied log field are gone: record() takes no argument and both kinds just count. Comments and docs no longer call these CoreAudio's overloads, since the poll reports any host's. - now_nanos reads the clock cell before Instant::now, so a first-ever record cannot land on the 0 that means never. - report_period skips HEX_AUDIO_FIXTURE, whose chunks are the file's rather than a device's, and says so. docs/features/recovery.md gains the close-time flush and uses the "Changed after dev" heading; the unsupported "hundreds a day" claim is gone. * audio: clamp the overload clock reading to at least one get_or_init(Instant::now) and the next Instant::now() can return the same tick on Apple Silicon and Windows, so a first-ever reading could still be 0, the sentinel for "never". now_nanos returns at least 1, and the GLITCHED_AT doc says the reading is clamped rather than merely later than the clock.
130 lines
4.8 KiB
Rust
130 lines
4.8 KiB
Rust
//! Microphone stream overloads: a glitch the host reports while the stream
|
|
//! keeps running, and the one coalesced line the audio owner writes about
|
|
//! them.
|
|
//!
|
|
//! Some hosts report these from the thread that runs the stream, where a log
|
|
//! line or a lock blocks an already-late cycle, so `record` only moves
|
|
//! counters. `report` runs on the thread that drains the stream, and `flush` on
|
|
//! the one that drops it, where formatting and a blocking write are harmless.
|
|
|
|
use std::sync::OnceLock;
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
|
use std::time::{Duration, Instant};
|
|
|
|
/// Overloads reported since the last line.
|
|
static GLITCHES: AtomicU64 = AtomicU64::new(0);
|
|
/// The last overload, and the last line written about them, in nanoseconds on
|
|
/// [`CLOCK`]. Zero means never, which a real reading cannot be: it is clamped
|
|
/// to at least one.
|
|
static GLITCHED_AT: AtomicU64 = AtomicU64::new(0);
|
|
static REPORTED_AT: AtomicU64 = AtomicU64::new(0);
|
|
/// The base those two are measured from, taken before a stream runs so the
|
|
/// host's thread only ever reads an initialized cell.
|
|
static CLOCK: OnceLock<Instant> = OnceLock::new();
|
|
|
|
/// Starts the clock overload times are kept against, before a stream runs.
|
|
pub(super) fn start_clock() {
|
|
CLOCK.get_or_init(Instant::now);
|
|
}
|
|
|
|
/// How long ago a stream last reported an overload it continued through.
|
|
pub(super) fn since_last() -> Option<Duration> {
|
|
let glitched_at = GLITCHED_AT.load(Ordering::Relaxed);
|
|
if glitched_at == 0 {
|
|
return None;
|
|
}
|
|
Some(Duration::from_nanos(
|
|
now_nanos().saturating_sub(glitched_at),
|
|
))
|
|
}
|
|
|
|
/// Records an overload from the thread that runs the stream: no logging,
|
|
/// allocation, or locks, only clock and atomic reads, because a callback that
|
|
/// sees one has already missed its cycle.
|
|
pub(super) fn record() {
|
|
GLITCHES.fetch_add(1, Ordering::Relaxed);
|
|
GLITCHED_AT.store(now_nanos(), Ordering::Relaxed);
|
|
}
|
|
|
|
/// The shortest gap between two overload lines. Overloads arrive in bursts of
|
|
/// tens per second while a cold device settles, and a loaded machine can keep
|
|
/// one stream glitching for minutes, so one line per burst keeps the log
|
|
/// readable. A burst held back by this window is counted by the next line, at
|
|
/// most this much later.
|
|
const LOG_INTERVAL: Duration = Duration::from_secs(10);
|
|
|
|
/// Whether the overloads a poll found are reported now, given how long ago the
|
|
/// last line was written: the first burst reports at once, and a continuing
|
|
/// one at most every [`LOG_INTERVAL`].
|
|
fn report_due(since_last_report: Option<Duration>) -> bool {
|
|
since_last_report.is_none_or(|since| since >= LOG_INTERVAL)
|
|
}
|
|
|
|
/// Reports the overloads recorded since the last line from a thread that may
|
|
/// block, as one line per burst. The pending count is taken only when a line
|
|
/// is due, so a burst this rate limit holds back is reported late rather than
|
|
/// lost.
|
|
pub(super) fn report() {
|
|
if GLITCHES.load(Ordering::Relaxed) == 0 {
|
|
return;
|
|
}
|
|
let now = now_nanos();
|
|
let reported_at = REPORTED_AT.load(Ordering::Relaxed);
|
|
let since_last_report =
|
|
(reported_at != 0).then(|| Duration::from_nanos(now.saturating_sub(reported_at)));
|
|
if !report_due(since_last_report) {
|
|
return;
|
|
}
|
|
// A meeting's stream and dictation's poll separately; one reports the burst.
|
|
if REPORTED_AT
|
|
.compare_exchange(reported_at, now, Ordering::AcqRel, Ordering::Relaxed)
|
|
.is_err()
|
|
{
|
|
return;
|
|
}
|
|
write_pending();
|
|
}
|
|
|
|
/// Writes the count a closing stream leaves behind, without the rate limit: its
|
|
/// burst ended with the stream, so it does not belong beside the next open's
|
|
/// lines.
|
|
pub(super) fn flush() {
|
|
if GLITCHES.load(Ordering::Relaxed) == 0 {
|
|
return;
|
|
}
|
|
write_pending();
|
|
}
|
|
|
|
/// Writes the pending count, if there is one, exactly once.
|
|
fn write_pending() {
|
|
let count = GLITCHES.swap(0, Ordering::AcqRel);
|
|
if count == 0 {
|
|
return;
|
|
}
|
|
tracing::warn!(count, "microphone stream glitched; continuing");
|
|
}
|
|
|
|
/// The clock reading. Two `Instant::now()` calls can share a tick, so the
|
|
/// result is clamped to at least one, which keeps zero meaning never.
|
|
fn now_nanos() -> u64 {
|
|
let base = *CLOCK.get_or_init(Instant::now);
|
|
let since = Instant::now().saturating_duration_since(base);
|
|
u64::try_from(since.as_nanos()).unwrap_or(u64::MAX).max(1)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
/// One line per burst: the first reports at once, and a continuing one
|
|
/// waits out the window, so the count it holds is reported by the next
|
|
/// poll rather than lost.
|
|
#[test]
|
|
fn a_burst_writes_one_line_and_holds_the_rest() {
|
|
assert!(report_due(None));
|
|
assert!(!report_due(Some(Duration::ZERO)));
|
|
assert!(!report_due(Some(LOG_INTERVAL - Duration::from_millis(1))));
|
|
assert!(report_due(Some(LOG_INTERVAL)));
|
|
assert!(report_due(Some(LOG_INTERVAL * 3)));
|
|
}
|
|
}
|