From 8629b26b28ae5b2ead824fde283212e42077de88 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 14 Nov 2026 18:41:31 +0700 Subject: [PATCH 1/2] drm/asahi: agx_stats: Say why pstate and its frequency aren't valid pstate and achieved_frequency_hz printed "unsupported" whenever pstate_status wasn't valid. On a firmware that reports its state (M5, A18 Pro), the state is "stale" after the cores power up, until the firmware's next state report, and the file then read as a missing feature. Print the status word instead. achieved_frequency_hz keeps "unsupported" for a valid state with no frequency in the board's table. busy_ns also gets a "stale" reading, for a total that stopped (used by the next change); every other non-valid busy status still reads "unsupported". Where pstate is unsupported (25G83, and always there through g17::stats::qualify), the status word is "unsupported", so that output is byte for byte the same. The integration test's formatter cases follow the new readings and check "busy_ns stale". Assisted-by: Claude Opus 5.5 Signed-off-by: Nick --- .../ABI/testing/sysfs-driver-asahi-agx-stats | 9 +++++++-- drivers/gpu/drm/asahi/sysfs.c | 17 +++++++++++++++-- tools/asahi/g17-telemetry-integration-tests.py | 13 ++++++++++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-driver-asahi-agx-stats b/Documentation/ABI/testing/sysfs-driver-asahi-agx-stats index eab183c793ab8..11f6ea6a92e17 100644 --- a/Documentation/ABI/testing/sysfs-driver-asahi-agx-stats +++ b/Documentation/ABI/testing/sysfs-driver-asahi-agx-stats @@ -93,8 +93,13 @@ Description: its state in other records (see below). utilisation_status is never unsupported on these firmwares. Numeric metrics are printed only while - their capability is valid; otherwise their value is - the string unsupported. + their capability is valid. Otherwise utilisation + values read unsupported; pstate and + achieved_frequency_hz read pstate_status (stale, + unavailable or unsupported), and achieved_frequency_hz + reads unsupported for a valid state with no + frequency in the board's table; busy_ns reads stale + when its total stopped and unsupported otherwise. util_tiler_percent, util_renderer_percent, util_device_percent Unsigned integer percent (0..100), from tag 0x13 diff --git a/drivers/gpu/drm/asahi/sysfs.c b/drivers/gpu/drm/asahi/sysfs.c index f7004b10a400e..18dcd1b9489d0 100644 --- a/drivers/gpu/drm/asahi/sysfs.c +++ b/drivers/gpu/drm/asahi/sysfs.c @@ -96,10 +96,16 @@ static ssize_t agx_qualified_stats_show(struct asahi_stats_attribute *stats, cha */ n += sysfs_emit_at(buf, n, "pstate_status %s\n", asahi_stats_quality(snap.pstate_status)); + /* + * A pstate that is not valid reads its status word, so stale or + * unavailable is not mistaken for a missing feature. Where it is + * unsupported, the output is unchanged. + */ if (snap.pstate_status == 1) n += sysfs_emit_at(buf, n, "pstate %u\n", snap.pstate); else - n += sysfs_emit_at(buf, n, "pstate unsupported\n"); + n += sysfs_emit_at(buf, n, "pstate %s\n", + asahi_stats_quality(snap.pstate_status)); /* No host frequency request, measured power or temperature. */ n += sysfs_emit_at(buf, n, "requested_frequency_hz unsupported\n"); /* @@ -109,11 +115,18 @@ static ssize_t agx_qualified_stats_show(struct asahi_stats_attribute *stats, cha if (snap.pstate_status == 1 && (snap.achieved_frequency_hz || !snap.pstate)) n += sysfs_emit_at(buf, n, "achieved_frequency_hz %llu\n", (unsigned long long)snap.achieved_frequency_hz); - else + else if (snap.pstate_status == 1) + /* A valid state with no frequency in the board's table. */ n += sysfs_emit_at(buf, n, "achieved_frequency_hz unsupported\n"); + else + n += sysfs_emit_at(buf, n, "achieved_frequency_hz %s\n", + asahi_stats_quality(snap.pstate_status)); if (snap.busy_status == 1) n += sysfs_emit_at(buf, n, "busy_ns %llu\n", (unsigned long long)snap.busy_ns); + else if (snap.busy_status == 2) + /* M5: the total stopped (a statistics ring was quarantined). */ + n += sysfs_emit_at(buf, n, "busy_ns stale\n"); else n += sysfs_emit_at(buf, n, "busy_ns unsupported\n"); n += sysfs_emit_at(buf, n, "power_mw unsupported\n" diff --git a/tools/asahi/g17-telemetry-integration-tests.py b/tools/asahi/g17-telemetry-integration-tests.py index 5420559c2fb84..8457a50d7bcf5 100644 --- a/tools/asahi/g17-telemetry-integration-tests.py +++ b/tools/asahi/g17-telemetry-integration-tests.py @@ -614,13 +614,16 @@ def item(source, signature): assert(strstr(now,"power_mw unsupported\n")); assert(strstr(now,"temperature_raw unsupported\n")); assert(strstr(now,"requested_frequency_hz unsupported\n")); - assert(strstr(now,"achieved_frequency_hz unsupported\n")); if(status==1) { + /* A valid state with no frequency in the board's table. */ + assert(strstr(now,"achieved_frequency_hz unsupported\n")); assert(strstr(now,"util_tiler_percent 30\nutil_renderer_percent 80\nutil_device_percent 57\n")); assert(strstr(now,"pstate 6\n")); } else { + /* Not valid: the pstate and its frequency say why. */ + assert(strstr(now,status==2?"pstate stale\n":"pstate unavailable\n")); + assert(strstr(now,status==2?"achieved_frequency_hz stale\n":"achieved_frequency_hz unavailable\n")); assert(strstr(now,"util_device_percent unsupported\n")); - assert(strstr(now,"pstate unsupported\n")); assert(strstr(now,status==2?"pstate_status stale\n":"pstate_status unavailable\n")); assert(strstr(now,status==2?"utilisation_status stale\n":"utilisation_status unavailable\n")); } @@ -634,7 +637,11 @@ def item(source, signature): assert(strstr(now,"pstate 0\nrequested_frequency_hz unsupported\nachieved_frequency_hz 0\n")); current.pstate_status=2; agx_stats_show(NULL,&stats.attr,now); - assert(strstr(now,"pstate unsupported\nrequested_frequency_hz unsupported\nachieved_frequency_hz unsupported\n")); + assert(strstr(now,"pstate stale\nrequested_frequency_hz unsupported\nachieved_frequency_hz stale\n")); + /* M5: a stopped busy total reads stale. */ + current=(struct asahi_qualified_stats){.util_status=1,.pstate_status=1,.pstate=6,.busy_status=2,.busy_ns=77}; + agx_stats_show(NULL,&stats.attr,now); + assert(strstr(now,"busy_ns stale\n") && !strstr(now,"busy_ns 77")); /* G16S: pstate unsupported by firmware, utilisation still valid. */ current=(struct asahi_qualified_stats){.util_status=1,.pstate_status=3,.tiler=30,.renderer=80,.device=57,.pstate=0, .util_samples=19,.util_host_ns=123}; From 99daf59587c76b6888c3b2a6090b6bc21c9787d2 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 14 Nov 2026 18:41:31 +0700 Subject: [PATCH 2/2] drm/asahi: g17: count busy time across power gates, in timestamp order On a J815 (M5) under a frame-paced load (es2gears + vkcube, 20 s), the cores power-gate about 95 times a second (power_epoch 10861), usually with one utilisation record per power epoch. busy_ns stayed 0 although the 0x13 records kept arriving: - busy time needed two records in one epoch, because every boundary cleared the window start (busy_ts); - the records queued before a boundary, and those drained while the cores read gated, never reached the decoder. Busy time now has its own ordering, separate from the utilisation epoch, on every firmware whose Protocol integrates it (M5 and A18 Pro): - The decoder keeps the firmware timestamp up to which it has credited busy time (busy_mark). Epoch boundaries keep it. A record at or before it adds nothing, so an older queued record, a repeat, or the other role's record for the same time is never credited twice, whatever the drain order. - A later record closes the window since the mark when that is at most two utilisation periods, as before. After a longer gap it closes only its own period (8 ms on M5, 16 ms on the A18 Pro), which lies after the mark: the gap is never credited. Before, such a record only set the baseline. The first record after a gate now credits its period at its own device utilisation. - Records a power boundary keeps out of the averaging epoch (cut off, or drained while gated) are decoded for busy time only (record_busy). The utilisation average, its delivery cadence and the performance state do not see them. Suspend, a real invalidation's discard and quarantine still keep everything out. - A record more than 60 s behind the mark cannot still be queued; it means the firmware clock started again and only sets a new mark. - busy_ns reads stale once a statistics ring is quarantined, since nothing adds to it any more. Credited windows never overlap and each lies at or before its own record's timestamp, so busy_ns stays bounded by elapsed time. 25G83 integrates no busy time and is unchanged. The gated performance state (off, valid 0) is left as it is. Host tests: tools/asahi/run-host-tests.py --only telemetry passes: the decoder's 13 cases, the integration fixture and every listed mutation. New decoder cases: gates between every record, records kept out of the epoch, interleaved roles, clock restart. The A18 Pro window case now expects a record after a gap longer than two periods to credit its own 16 ms period. New ring case: records cut off at a gate and drained while gated count, a repeat from the other role does not, suspend keeps them out, quarantine reads stale. The fixture's quarantine mutation now hits both the admission and the read, through statistics_quarantined(). Not yet run on hardware in this form; the M5 behaviour is the one measured on the J815 before the A18 Pro protocol work. Assisted-by: Claude Opus 5.5 Signed-off-by: Nick --- .../ABI/testing/sysfs-driver-asahi-agx-stats | 44 ++++-- Documentation/gpu/asahi-usage-stats.rst | 14 +- drivers/gpu/drm/asahi/g17/channel.rs | 30 +++- drivers/gpu/drm/asahi/g17/telemetry.rs | 147 +++++++++++++++--- .../asahi/g17-telemetry-integration-tests.py | 41 +++++ 5 files changed, 226 insertions(+), 50 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-driver-asahi-agx-stats b/Documentation/ABI/testing/sysfs-driver-asahi-agx-stats index 11f6ea6a92e17..99f51757a88df 100644 --- a/Documentation/ABI/testing/sysfs-driver-asahi-agx-stats +++ b/Documentation/ABI/testing/sysfs-driver-asahi-agx-stats @@ -159,18 +159,26 @@ Description: busy_ns Cumulative u64 since the driver was bound. The tag 0x13 records of this firmware carry a timestamp in 24 MHz ticks (words 1 and 2), one every 8 ms on M5 - and every 16 ms on the A18 Pro. Each record credits - the window since the previous record of its epoch at - its device utilisation. A record more than two - periods after the previous one, or with an earlier - timestamp, only starts a new window, so time the GPU - spent gated is never credited, and a load sparser - than one period reads low. MEASURED on T6050 - (J716c): 871 and 863 ms for three 300 ms compute jobs - at full clock (905 and 919 ms of work). MEASURED on - T8140 (J700), 24 traces of GPU stress loads: within - about 20% of the union of the commands' own execution - intervals for loads busy more than half the time. + and every 16 ms on the A18 Pro, and are credited in + that order at their device utilisation, from both + roles and across power epochs. The driver keeps the + timestamp up to which it has credited busy time; a + record at or before it gets nothing, so no interval + is credited twice. A later record credits the window + since that mark when it is at most two periods, and + only its own period after a longer gap, so time the + GPU spent gated is never credited. Records a power + boundary keeps out of the utilisation average (see + below) still count here; suspend, a real epoch + invalidation's discard and quarantine keep them out. + busy_ns reads stale once a statistics ring is + quarantined. MEASURED on T6050 (J716c): 871 and 863 + ms for three 300 ms compute jobs at full clock (905 + and 919 ms of work). MEASURED on T8140 (J700), 24 + traces of GPU stress loads, with windows that ended + at each power epoch: within about 20% of the union + of the commands' own execution intervals for loads + busy more than half the time. requested_frequency_hz, power_mw, temperature_raw Unsupported. @@ -192,15 +200,17 @@ Description: Suspend/resume, restart, statistics-ring quarantine, observed core gating and teardown end validity. After an epoch boundary, each ring's already queued records are discarded before new - baselines are accepted. A quarantined statistics ring disables - both roles' exports until reprobe; a trace-ring error does not. + baselines are accepted; where busy_ns is integrated, those a + power boundary kept out still reach it, as described there. A + quarantined statistics ring disables both roles' exports until + reprobe; a trace-ring error does not. Reads take the device mutex and do not wake the GPU. The firmware_reports debugfs view retains per-role raw packets, counts and host monotonic receive times, plus capability status, - policy, timestamp and diagnostic tick deltas. Tick deltas are - never accumulated as GPU busy time. Firmware role streams are - merged in drain order without deduplication. + policy, timestamp and diagnostic tick deltas. The tag 0x10 tick + deltas are never accumulated as GPU busy time. Firmware role + streams are merged in drain order without deduplication. The diagnostic also identifies the platform device, records the transport-admitted count and last-admission decision per role/tag, and shows suspension, queued-record discard and quarantine state. diff --git a/Documentation/gpu/asahi-usage-stats.rst b/Documentation/gpu/asahi-usage-stats.rst index c2cda44ce1e45..33bb598bfa351 100644 --- a/Documentation/gpu/asahi-usage-stats.rst +++ b/Documentation/gpu/asahi-usage-stats.rst @@ -156,10 +156,16 @@ there: * ``busy_ns``: the utilisation records carry their own timestamps (words 1 and 2, 24 MHz ticks), one every 8 ms on the M5 and every 16 ms on the A18 Pro, so each record closes a window of known length, which is credited at the - record's device utilisation. A record that follows the previous one by more - than two periods, or whose timestamp went back, only starts a new window: - time the GPU spent gated is never credited, and a load sparser than one - period reads low. The total keeps counting across power epochs. + record's device utilisation. Records are credited in timestamp order, from + both roles and across power epochs: the driver keeps the timestamp up to + which it has counted, and a record at or before it adds nothing. A record + within two periods of that mark closes the window since it; after a longer + gap it closes only its own period, so time the GPU spent gated is never + credited. Under a frame-paced load the cores gate many times a second, often + with one record per power epoch, so the records a power boundary keeps out of + the utilisation average still count here. Suspend, recovery and quarantine + keep them out, and ``busy_ns`` reads ``stale`` once a statistics ring is + quarantined. The driver asks for full performance while work is in flight, which the firmware takes as a floor for its state. An A18 Pro at work therefore normally diff --git a/drivers/gpu/drm/asahi/g17/channel.rs b/drivers/gpu/drm/asahi/g17/channel.rs index 5276e7758f22a..2c73d0727d182 100644 --- a/drivers/gpu/drm/asahi/g17/channel.rs +++ b/drivers/gpu/drm/asahi/g17/channel.rs @@ -167,7 +167,19 @@ impl Rings { } pub(super) fn read_statistics(&self, now_ns: u64) -> super::telemetry::Readout { - self.telemetry.read(now_ns, self.max_age_ns) + let mut view = self.telemetry.read(now_ns, self.max_age_ns); + // A quarantined statistics ring adds nothing more: the total is no longer current. + if view.busy_status == super::telemetry::VALID && self.statistics_quarantined() { + view.busy_status = super::telemetry::STALE; + } + view + } + + /// Either role's statistics ring is quarantined, which stops both roles' exports. + fn statistics_quarantined(&self) -> bool { + self.quarantined + .iter() + .any(|rings| rings[ReportRing::Statistics as usize]) } pub(super) fn write_reports(&self, f: &mut kernel::fmt::Formatter<'_>) -> kernel::fmt::Result { @@ -198,13 +210,13 @@ impl Rings { return; } let statistics = matches!(ring, ReportRing::Statistics); - let accept = !self.suspended - && self.statistics_powered + // Suspend, a real invalidation's discard and quarantine keep every record out. + let accept_busy = !self.suspended && !self.discard_statistics[role as usize] - && !self - .quarantined - .iter() - .any(|rings| rings[ReportRing::Statistics as usize]); + && !self.statistics_quarantined(); + // A power boundary also keeps the records before it out of the averaging epoch; + // their own timestamps still place them for busy time (Telemetry::record_busy). + let accept = accept_busy && self.statistics_powered; if let Err(error) = Self::consume_report( init, role, @@ -213,6 +225,7 @@ impl Rings { &mut self.telemetry, self.max_age_ns, accept, + accept_busy, &mut self.statistics_cutoff[role as usize], ) { self.stop_report(dev, role, ring, error); @@ -243,6 +256,7 @@ impl Rings { telemetry: &mut super::telemetry::Telemetry, max_age_ns: u64, accept_statistics: bool, + accept_busy: bool, statistics_cutoff: &mut Option, ) -> Result { let (state, entries, capacity, allowed_types) = ring.geometry(init); @@ -282,6 +296,8 @@ impl Rings { reports.record(role, words, host_ns, allowed_types, admitted); if admitted { telemetry.record(&words, host_ns, max_age_ns); + } else if accept_busy { + telemetry.record_busy(&words); } } consumer = (consumer + 1) & (capacity - 1); diff --git a/drivers/gpu/drm/asahi/g17/telemetry.rs b/drivers/gpu/drm/asahi/g17/telemetry.rs index 23f66a7fcced0..3d6b215908f40 100644 --- a/drivers/gpu/drm/asahi/g17/telemetry.rs +++ b/drivers/gpu/drm/asahi/g17/telemetry.rs @@ -46,6 +46,9 @@ const _: () = { }; const TIMER_HZ: u64 = 24_000_000; +/// A record this far behind the busy mark cannot still be queued (a statistics ring holds +/// 256 records): the firmware clock started again, and the record only sets a new mark. +const BUSY_REBASE_TICKS: u64 = 60 * TIMER_HZ; /// What a firmware reports beyond utilisation. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] @@ -84,8 +87,9 @@ impl Protocol { self.util_period_ticks != 0 } - /// A utilisation record more than two intervals after the previous one follows a gap the - /// firmware did not report (gated or lost records): it only sets the baseline. + /// A utilisation record at most two intervals after the previous one closes the window + /// since it. One after a longer gap the firmware did not report (the GPU was gated, or + /// records were lost) closes only its own sampling period. fn max_window_ticks(self) -> u64 { 2 * self.util_period_ticks } @@ -109,9 +113,10 @@ pub(super) struct Telemetry { gated: bool, /// The records decoded beyond utilisation. protocol: Protocol, - /// Timestamp of the latest utilisation record in this epoch: the start of the window the - /// next one closes. - busy_ts: Option, + /// Firmware timestamp up to which busy time has been credited. It is kept across + /// epochs: power gates, suspend and restarts end the utilisation average, but never + /// let a record at or before this mark be credited again. + busy_mark: Option, busy_ns: u64, /// End of the latest performance state interval (tag 0x19), in timer ticks. pstate_interval_end: Option, @@ -141,8 +146,7 @@ impl Telemetry { self.delivery_ns = None; self.delivery_intervals = [None; 2]; self.gated = false; - // Busy time keeps counting across epochs; only the window start is lost. - self.busy_ts = None; + // Busy time keeps counting across epochs, ordered by its own mark. self.pstate_interval_end = None; } @@ -257,20 +261,47 @@ impl Telemetry { } } - /// Credits the window a utilisation record closes, at its device utilisation. The window - /// starts at the previous record of the epoch; a record after a longer gap than - /// [`Protocol::max_window_ticks`] (the GPU was gated or records were lost) or one whose - /// timestamp went back only sets the baseline, so an idle gap is never credited. + /// A utilisation record that is kept out of the utilisation average: one a power boundary + /// cut off, or one drained while the cores read gated. The firmware produced it before + /// the boundary, so it describes powered time, which its own timestamp places. Only busy + /// time takes it; the average, the delivery cadence and the performance state are left + /// as they are. + pub(super) fn record_busy(&mut self, words: &[u32; 18]) { + if !self.protocol.integrates_busy() + || words[0] != 0x13 + || [words[3], words[4], words[6]].iter().any(|&v| v > 100) + { + return; + } + self.integrate_busy(u64::from(words[1]) | (u64::from(words[2]) << 32), words[6]); + } + + /// Credits the window a utilisation record closes, at its device utilisation. + /// + /// Records are credited in firmware timestamp order, from both roles and across + /// epochs: a record at or before [`Self::busy_mark`] was already covered and gets + /// nothing, so a time interval is never credited twice. A later record closes the + /// window since the mark when that is at most [`Protocol::max_window_ticks`]; after a + /// longer gap (the cores were gated, or records were lost) it closes only its own + /// sampling period, which starts after the mark, so the gap itself is never credited. + /// The first record only sets the mark. fn integrate_busy(&mut self, timestamp: u64, device: u32) { - if let Some(last) = self.busy_ts { - let window = timestamp.wrapping_sub(last); - if timestamp > last && window <= self.protocol.max_window_ticks() { + match self.busy_mark { + Some(mark) if timestamp > mark => { + let gap = timestamp - mark; + let window = if gap <= self.protocol.max_window_ticks() { + gap + } else { + self.protocol.util_period_ticks + }; // At most two 16 ms periods (768000 ticks) x 100 x 10^9: no overflow. let ns = window * u64::from(device) * 1_000_000_000 / (100 * TIMER_HZ); self.busy_ns = self.busy_ns.saturating_add(ns); } + Some(mark) if mark - timestamp <= BUSY_REBASE_TICKS => return, + _ => {} } - self.busy_ts = Some(timestamp); + self.busy_mark = Some(timestamp); } pub(super) fn read(&self, now_ns: u64, max_age_ns: u64) -> Readout { @@ -584,22 +615,93 @@ mod tests { half[6] = 50; stats.record(&half, 103, AGE); assert_eq!(stats.read(103, AGE).busy_ns, windows + 4_000_000); - // A gate, a long gap or a timestamp going back only sets a new baseline. + // A gate ends the utilisation epoch, not the busy time: the next record still + // closes the window since the last one. let before = stats.read(103, AGE).busy_ns; stats.gate(); let mut after = half; after[1] += 192_000; stats.record(&after, 104, AGE); - assert_eq!(stats.read(104, AGE).busy_ns, before); - after[1] += 3 * 192_000; + assert_eq!(stats.read(104, AGE).busy_ns, before + 4_000_000); + // After a longer gap the record closes only its own 8 ms period. + let before = stats.read(104, AGE).busy_ns; + after[1] += 10 * 192_000; stats.record(&after, 105, AGE); - assert_eq!(stats.read(105, AGE).busy_ns, before); + assert_eq!(stats.read(105, AGE).busy_ns, before + 4_000_000); + // A record at or before the mark was already covered. + let before = stats.read(105, AGE).busy_ns; after[1] -= 1000; stats.record(&after, 106, AGE); + after[1] += 1000; + stats.record(&after, 106, AGE); assert_eq!(stats.read(106, AGE).busy_ns, before); - // An epoch boundary keeps the total. + // An epoch boundary keeps the total and the mark. stats.invalidate(); assert_eq!(stats.read(107, AGE).busy_ns, before); + stats.record(&after, 108, AGE); + assert_eq!(stats.read(108, AGE).busy_ns, before); + } + + fn m5_util(ticks: u64, device: u32) -> [u32; 18] { + let mut words = M5_U[0]; + (words[1], words[2], words[6]) = (ticks as u32, (ticks >> 32) as u32, device); + words + } + + #[test] + fn m5_busy_time_counts_through_frequent_gates() { + // A frame-paced load: one record per epoch, a gate between any two. + let mut stats = Telemetry::new(Protocol::M5); + let mut ticks = 0x59_0000_0000u64; + stats.record(&m5_util(ticks, 100), 1, AGE); + for n in 0..10u64 { + stats.gate(); + stats.invalidate(); + ticks += 250_000; // 10.4 ms: more than one period, at most two. + stats.record(&m5_util(ticks, 100), 2 + n, AGE); + } + assert_eq!(stats.read(20, AGE).busy_ns, 10 * ticks_ns(250_000)); + } + + #[test] + fn m5_records_kept_out_of_the_epoch_still_count_once() { + let base = 0x59_0000_0000u64; + let mut stats = Telemetry::new(Protocol::M5); + stats.record(&m5_util(base, 100), 1, AGE); + // Cut off by a boundary, decoded by its own timestamp. + stats.gate(); + stats.record_busy(&m5_util(base + 192_000, 100)); + assert_eq!(stats.read(2, AGE).busy_ns, 8_000_000); + // It does not start a utilisation sample in the new epoch. + assert_eq!(stats.read(2, AGE).util_samples, 0); + // A copy of it, from the queue or from the other role, is not counted again, + // nor an older record drained later. + stats.record(&m5_util(base + 192_000, 100), 3, AGE); + stats.record_busy(&m5_util(base + 100_000, 100)); + assert_eq!(stats.read(3, AGE).busy_ns, 8_000_000); + // Interleaved roles split the time between them and never overlap. + stats.record_busy(&m5_util(base + 288_000, 100)); + stats.record(&m5_util(base + 384_000, 100), 4, AGE); + stats.record_busy(&m5_util(base + 336_000, 100)); + assert_eq!(stats.read(4, AGE).busy_ns, 16_000_000); + // Records the decoder would refuse are ignored here too. + let mut bad = m5_util(base + 576_000, 100); + bad[4] = 101; + stats.record_busy(&bad); + let mut other = m5_util(base + 576_000, 100); + other[0] = 0x1b; + stats.record_busy(&other); + assert_eq!(stats.read(5, AGE).busy_ns, 16_000_000); + // A firmware clock that started again only sets a new mark. + stats.record_busy(&m5_util(1_000, 100)); + assert_eq!(stats.read(6, AGE).busy_ns, 16_000_000); + stats.record_busy(&m5_util(1_000 + 192_000, 50)); + assert_eq!(stats.read(7, AGE).busy_ns, 20_000_000); + // A firmware that integrates no busy time decodes none of it. + let mut legacy = Telemetry::default(); + legacy.record_busy(&m5_util(base, 100)); + legacy.record_busy(&m5_util(base + 192_000, 100)); + assert_eq!(legacy.read(8, AGE).busy_ns, 0); } #[test] @@ -644,13 +746,14 @@ mod tests { words[6] = 25; stats.record(&words, 102, AGE); assert_eq!(stats.read(102, AGE).busy_ns, 20_000_000); - // Up to two periods is one window; longer is a gap the GPU spent gated. + // Up to two periods is one window; after a longer gap, which the GPU spent gated, + // a record closes only its own 16 ms period. words[1] += 768_000; stats.record(&words, 103, AGE); assert_eq!(stats.read(103, AGE).busy_ns, 28_000_000); words[1] += 768_001; stats.record(&words, 104, AGE); - assert_eq!(stats.read(104, AGE).busy_ns, 28_000_000); + assert_eq!(stats.read(104, AGE).busy_ns, 32_000_000); } #[test] diff --git a/tools/asahi/g17-telemetry-integration-tests.py b/tools/asahi/g17-telemetry-integration-tests.py index 8457a50d7bcf5..763ee00abc314 100644 --- a/tools/asahi/g17-telemetry-integration-tests.py +++ b/tools/asahi/g17-telemetry-integration-tests.py @@ -502,6 +502,47 @@ def item(source, signature): } } #[test] + fn m5_busy_time_takes_records_a_power_boundary_kept_out() { + use {channel::Rings,fw::variant::InitGraph,Role::*}; + let dev=crate::device::Device; + let init=initdata::InitData::new(); + let mut rings=Rings::new(InitGraph::G17P,true).unwrap(); + // 8 ms windows at 100%, timestamps in words 1 and 2. + let util=|ticks:u32|[0x13,ticks,0x59,0,0,100,100,0,0,0,0,0,0,0,0,0,0,0]; + init.push(Primary,util(0)); + rings.drain_statistics(&dev,&init,Primary); + assert_eq!(rings.read_statistics(1).busy_ns,0); + // Queued at a gate: cut off from the average, still busy time. + init.push(Primary,util(192_000)); + rings.observe_statistics_power(&dev,&init,&[Primary,Secondary],false,1); + rings.drain_statistics(&dev,&init,Primary); + assert_eq!(rings.read_statistics(1).busy_ns,8_000_000); + // Drained while gated, from the other role: the same. + init.push(Secondary,util(384_000)); + rings.drain_statistics(&dev,&init,Secondary); + assert_eq!(rings.read_statistics(1).busy_ns,16_000_000); + // Powered again: one record per epoch keeps counting, a repeat does not. + rings.observe_statistics_power(&dev,&init,&[Primary,Secondary],true,2); + init.push(Primary,util(576_000)); + init.push(Secondary,util(576_000)); + rings.drain_statistics(&dev,&init,Primary); + rings.drain_statistics(&dev,&init,Secondary); + assert_eq!(rings.read_statistics(1).busy_ns,24_000_000); + assert_eq!(rings.read_statistics(1).busy_status,telemetry::VALID); + // Suspend's discard keeps queued records out. + init.push(Primary,util(768_000)); + rings.suspend_statistics(true); + rings.drain_statistics(&dev,&init,Primary); + rings.suspend_statistics(false); + rings.drain_statistics(&dev,&init,Primary); + assert_eq!(rings.read_statistics(1).busy_ns,24_000_000); + // Quarantine stops the total: it reads stale. + init.cursors[1][1].store(fw::channels::STATISTICS_SLOTS,Ordering::Relaxed); + rings.drain_statistics(&dev,&init,Secondary); + let view=rings.read_statistics(1); + assert_eq!((view.busy_status,view.busy_ns),(telemetry::STALE,24_000_000)); + } + #[test] fn pstate_uses_drain_order_without_role_deduplication() { use {channel::Rings,fw::variant::InitGraph,Role::*}; let dev=crate::device::Device;