Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,15 @@ Keep the unit's `RuntimeDirectory=speakerguardd` and
`/run/speakerguardd` for its model state and keep it across restarts. A unit
without them leaves the state unwritten, and every restart starts cold.

The first start of a boot starts from evidence (`src/boot.rs`) rather than
latched at the coils' limit. Before its first unlock of a boot the daemon
writes `/run/speakerguardd.unlocked` (the boot id), beside the
RuntimeDirectory rather than in it, so cleaning or removing
`/run/speakerguardd` cannot make a later start of the boot look like the
first. The daemon needs `/run` writable for it, or it never takes the
lock. Replace the binary only across a reboot: an older build without the
marker that ran earlier in the same boot can look like no daemon at all.

The PCM udev event and `multi-user.target` can both precede ALSA card
discovery. The daemon retries a missing card for up to 30 seconds, using a
monotonic deadline and 100 ms polling, before returning the discovery error.
Expand Down
8 changes: 7 additions & 1 deletion speakerguardd.service
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ Description=Speaker Protection Daemon (feed-forward)
# Restarts are safe: until the daemon renews the card's volume lock the
# speakers sit at the card's safe level. A restarted daemon resumes the
# thermal model from /run/speakerguardd/state only after a clean stop, and
# otherwise starts it at the coils' limit. A crash loop gives up after five
# otherwise starts it at the coils' limit. The first start of a boot
# (no /run state and no /run/speakerguardd.unlocked marker of this boot)
# starts from evidence (boot_resume = evidence, on machines where every
# owner governs one model): the hottest any protected past can have left
# the speakers, narrowed by the last run's record in /var/lib/speakerguardd
# (a clean stop's, or a checkpoint written every 10 s while running).
# The marker sits outside the RuntimeDirectory, so cleaning it keeps it. A crash loop gives up after five
# attempts and leaves the speakers at that level.
# One daemon owns the speaker sense PCM and the volume interlock: whichever
# takes it first protects the speakers, and the other cannot start. This
Expand Down
688 changes: 688 additions & 0 deletions src/boot.rs

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,19 @@ pub struct Globals {
/// to the kernel. Needs V/I sense on every speaker.
pub guard: Option<Governor>,
/// What the first start of a boot assumes of the time since the last
/// one (persist.rs): `latched` (the default) starts at the coils'
/// limit; `idle` resumes the state a clean stop at shutdown left, the
/// one: `evidence` (the default, boot.rs) starts from the hottest any
/// owner that kept the coils under their hard limit can have left
/// them, narrowed by the last run's record, where every owner governs
/// one model (guard.rs `one_model`; latched elsewhere); `latched` starts at the
/// coils' limit (persist.rs); `idle` resumes the state a clean stop at shutdown left, the
/// gap at the card's safe level, which is only sound on a machine no
/// other OS drives the speakers on.
pub boot_resume: BootResume,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BootResume {
Evidence,
Latched,
Idle,
}
Expand Down Expand Up @@ -345,9 +349,10 @@ impl Config {
Some(v) => return Err(ConfigError(format!("[Globals] guard {v:?}: none or asahi"))),
},
boot_resume: match g.get("boot_resume").map(String::as_str) {
None | Some("latched") => BootResume::Latched,
None | Some("evidence") => BootResume::Evidence,
Some("latched") => BootResume::Latched,
Some("idle") => BootResume::Idle,
Some(v) => return Err(ConfigError(format!("[Globals] boot_resume {v:?}: latched or idle"))),
Some(v) => return Err(ConfigError(format!("[Globals] boot_resume {v:?}: evidence, latched or idle"))),
},
};
if !(globals.max_reduction.is_finite() && globals.max_reduction > 0.0) {
Expand Down
54 changes: 54 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,60 @@ impl Engine {

/// Nothing is known about the speakers: every model at its hard limit,
/// latched.
/// The first start of a boot from evidence (boot.rs): every model node
/// from the hottest a protected past can have left it, `saved` (the
/// last run's record, if usable) narrowing the magnet, `d` the longest
/// another owner can have had since it, at least `uptime` at the
/// card's safe level since, and no colder than `floor` (a chassis
/// sensor plus margin). Nothing is latched that is under its limit.
pub fn assume_evidence(&mut self, saved: Option<&crate::state::Saved>, d: Option<f64>, uptime: f64, floor: f64) {
use crate::boot::{budget_history, protected_energy, start_bound_in, SENSOR_MARGIN_K};
let g = self.cfg.globals.clone();
let ta = g.t_ambient;
// a chassis sensor over the model's ambient is the bound's ambient
let ta_hi = if floor.is_finite() { ta.max(floor - SENSOR_MARGIN_K) } else { ta };
let safe = 10f64.powf(-g.safe_reduction.max(0.0) / 10.0);
let names: Vec<String> = self.cfg.speakers.iter().map(|s| state_name(&s.name)).collect();
for (k, s) in self.asahi.iter_mut().flatten().enumerate() {
let rec = saved.and_then(|x| x.asahi.iter().find(|a| a.name == names[k])).map(|a| (a.t_coil, a.t_magnet));
let p = s.v_worst * s.v_worst / s.z_min * safe;
let (c, m) = start_bound_in(&s.th, s.t_limit, ta_hi, ta, p, uptime, rec, d);
s.th.t_coil = c.max(floor);
s.th.t_magnet = m.max(floor);
s.coil_peak = s.th.t_coil;
s.magnet_peak = s.th.t_magnet;
s.coil_hyst = s.th.t_coil;
s.magnet_hyst = s.th.t_magnet;
s.gain = 0.0;
s.hard = !(s.coil_peak < s.t_limit && s.magnet_peak < s.t_limit);
}
for (k, s) in self.unified.iter_mut().flatten().enumerate() {
let rec = saved.and_then(|x| x.unified.iter().find(|u| u.name == names[k]));
let p_ff = s.v_worst * s.v_worst / s.r_ff * safe;
let (c, m) = start_bound_in(&s.ff, s.t_limit, ta_hi, ta, p_ff, uptime, rec.map(|u| u.ff), d);
s.ff.t_coil = c.max(floor);
s.ff.t_magnet = m.max(floor);
let mut peak = hi(s.ff.t_coil, s.ff.t_magnet);
let z_min = s.z_min;
if let Some(v) = s.vi.as_mut() {
let p_vi = s.v_worst * s.v_worst / z_min * safe;
let (c, m) = start_bound_in(v, s.t_limit, ta_hi, ta, p_vi, uptime, rec.and_then(|u| u.vi), d);
v.t_coil = c.max(floor);
v.t_magnet = m.max(floor);
peak = hi(peak, hi(v.t_coil, v.t_magnet));
}
s.peak = peak;
s.hard = !(peak < s.t_limit);
s.reduction = 0.0;
let len = 60.0 - uptime.clamp(0.0, 60.0);
let older = protected_energy(&s.ff, s.t_limit, ta, len)
.max(rec.map_or(0.0, |u| 60.0 * u.mean_60s))
.min(s.v_worst * s.v_worst / s.r_ff * len);
s.history = budget_history(uptime, p_ff, older).into_iter().collect();
s.history_s = s.history.iter().map(|e| e.0).sum();
}
}

pub fn assume_latched(&mut self) {
let g = self.cfg.globals.clone();
for s in self.asahi.iter_mut().flatten() {
Expand Down
Loading