Skip to content
Open
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
53 changes: 34 additions & 19 deletions Documentation/ABI/testing/sysfs-driver-asahi-agx-stats
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -154,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.
Expand All @@ -187,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.
Expand Down
14 changes: 10 additions & 4 deletions Documentation/gpu/asahi-usage-stats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 23 additions & 7 deletions drivers/gpu/drm/asahi/g17/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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<u32>,
) -> Result {
let (state, entries, capacity, allowed_types) = ring.geometry(init);
Expand Down Expand Up @@ -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);
Expand Down
147 changes: 125 additions & 22 deletions drivers/gpu/drm/asahi/g17/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
}
Expand All @@ -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<u64>,
/// 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<u64>,
busy_ns: u64,
/// End of the latest performance state interval (tag 0x19), in timer ticks.
pstate_interval_end: Option<u64>,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
Loading
Loading