gpuviewer-core 0.1.1

Telemetry core for gpuviewer: vendor backends, data model, event derivation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Mock backend: deterministic-ish simulation used for CI and demos (no GPU required).
//!
//! Device 0 simulates a training run: high util with periodic idle gaps (dataloader /
//! checkpoint pattern), VRAM climbing toward OOM, temperature chasing util until a thermal
//! throttle cycle kicks in. Device 1 simulates a desktop/inference box: bursty util and an
//! `ollama` process that periodically attaches with a large allocation, then exits.

use crate::backend::{BackendError, GpuBackend};
use crate::model::{
    now_ms, DeviceId, DynamicSample, ProcessKind, ProcessSample, StaticInfo, ThrottleReasons,
    Vendor,
};

const GIB: u64 = 1024 * 1024 * 1024;

/// Tiny xorshift PRNG — keeps the core crate dependency-free.
struct Rng(u64);

impl Rng {
    fn next(&mut self) -> u64 {
        let mut x = self.0;
        x ^= x << 13;
        x ^= x >> 7;
        x ^= x << 17;
        self.0 = x;
        x
    }

    /// Uniform float in [0, 1).
    fn f(&mut self) -> f64 {
        (self.next() >> 11) as f64 / (1u64 << 53) as f64
    }

    /// Uniform float in [lo, hi).
    fn range(&mut self, lo: f64, hi: f64) -> f64 {
        lo + self.f() * (hi - lo)
    }
}

struct TrainSim {
    rng: Rng,
    tick: u64,
    temp_c: f64,
    vram_python: f64,
    throttling: bool,
    fan_pct: f64,
    /// tick at which the current idle gap ends (0 = not idle).
    idle_until: u64,
}

struct DesktopSim {
    rng: Rng,
    tick: u64,
    ollama_present: bool,
    ollama_toggle_at: u64,
    util_level: f64,
}

pub struct MockBackend {
    ids: [DeviceId; 2],
    train: TrainSim,
    desktop: DesktopSim,
}

impl MockBackend {
    pub fn new() -> Self {
        Self {
            ids: [
                DeviceId("mock:0000:01:00.0".into()),
                DeviceId("mock:0000:03:00.0".into()),
            ],
            train: TrainSim {
                rng: Rng(0x9E37_79B9_7F4A_7C15),
                tick: 0,
                temp_c: 52.0,
                vram_python: 19.2 * GIB as f64,
                throttling: false,
                fan_pct: 35.0,
                idle_until: 0,
            },
            desktop: DesktopSim {
                rng: Rng(0xD1B5_4A32_D192_ED03),
                tick: 0,
                ollama_present: false,
                ollama_toggle_at: 45,
                util_level: 12.0,
            },
        }
    }
}

impl MockBackend {
    /// One simulation step for ALL devices at a synthetic timestamp, in `devices()` order —
    /// the seeding entry point for `gpuviewer demo`, which replays hours of history through
    /// the sims in seconds. The live path (`refresh_dynamic` at `now_ms()`) and this one
    /// share the same `step()`, so seeded history and live mock data are the same
    /// simulation, just on different clocks.
    pub fn tick_at(&mut self, ts_ms: u64) -> Vec<(DeviceId, DynamicSample, Vec<ProcessSample>)> {
        vec![
            (
                self.ids[0].clone(),
                self.train.step(ts_ms),
                self.train.processes(),
            ),
            (
                self.ids[1].clone(),
                self.desktop.step(ts_ms),
                self.desktop.processes(),
            ),
        ]
    }
}

impl Default for MockBackend {
    fn default() -> Self {
        Self::new()
    }
}

impl GpuBackend for MockBackend {
    fn name(&self) -> &'static str {
        "mock"
    }

    fn devices(&mut self) -> Vec<DeviceId> {
        self.ids.to_vec()
    }

    fn static_info(&mut self, dev: &DeviceId) -> Result<StaticInfo, BackendError> {
        if *dev == self.ids[0] {
            Ok(StaticInfo {
                id: dev.clone(),
                vendor: Vendor::Nvidia,
                name: "GeForce RTX 4090 (mock)".into(),
                backend: "mock".into(),
                mem_total_bytes: Some(24 * GIB),
                power_limit_mw: Some(450_000),
                max_sm_clock_mhz: Some(2_520),
                temp_slowdown_c: Some(84.0),
                driver_version: Some("mock 999.99".into()),
                process_hint: None,
                source_caveat: None,
            })
        } else if *dev == self.ids[1] {
            Ok(StaticInfo {
                id: dev.clone(),
                vendor: Vendor::Amd,
                name: "Radeon RX 7900 XTX (mock)".into(),
                backend: "mock".into(),
                mem_total_bytes: Some(24 * GIB),
                power_limit_mw: Some(355_000),
                max_sm_clock_mhz: Some(2_500),
                temp_slowdown_c: Some(110.0),
                driver_version: Some("mock amdgpu".into()),
                process_hint: None,
                source_caveat: None,
            })
        } else {
            Err(BackendError::DeviceNotFound(dev.clone()))
        }
    }

    fn refresh_dynamic(&mut self, dev: &DeviceId) -> Result<DynamicSample, BackendError> {
        let ts = now_ms();
        if *dev == self.ids[0] {
            Ok(self.train.step(ts))
        } else if *dev == self.ids[1] {
            Ok(self.desktop.step(ts))
        } else {
            Err(BackendError::DeviceNotFound(dev.clone()))
        }
    }

    fn refresh_processes(&mut self, dev: &DeviceId) -> Result<Vec<ProcessSample>, BackendError> {
        if *dev == self.ids[0] {
            Ok(self.train.processes())
        } else if *dev == self.ids[1] {
            Ok(self.desktop.processes())
        } else {
            Err(BackendError::DeviceNotFound(dev.clone()))
        }
    }
}

impl TrainSim {
    fn step(&mut self, ts_ms: u64) -> DynamicSample {
        self.tick += 1;

        // Idle gaps: every ~90 ticks, go idle for 8-20 ticks (checkpoint/validation pattern).
        if self.idle_until == 0 && self.tick.is_multiple_of(90) {
            self.idle_until = self.tick + 8 + (self.rng.next() % 13);
        }
        let idle = self.idle_until > self.tick;
        if !idle {
            self.idle_until = 0;
        }

        let util = if idle {
            self.rng.range(0.0, 4.0)
        } else {
            self.rng.range(91.0, 99.5)
        };

        // VRAM: python allocation climbs ~4.5 MiB/tick (~270 MiB/min at 1s ticks) so the
        // pressure event fires within a few minutes of watching; resets when OOM-adjacent
        // to keep the demo looping.
        self.vram_python += self.rng.range(3.0, 6.0) * 1024.0 * 1024.0;
        if self.vram_python > 22.8 * GIB as f64 {
            self.vram_python = 16.5 * GIB as f64;
        }

        // Temperature chases util; fan chases temperature; throttle with hysteresis at the
        // slowdown threshold.
        let target = if idle { 48.0 } else { 87.0 };
        let cooling = (self.fan_pct - 30.0) * 0.06;
        self.temp_c += (target - self.temp_c) * 0.06 - cooling * 0.02 + self.rng.range(-0.3, 0.3);
        self.fan_pct += ((self.temp_c - 55.0).max(0.0) * 2.6 - self.fan_pct) * 0.08;
        self.fan_pct = self.fan_pct.clamp(28.0, 100.0);

        if !self.throttling && self.temp_c >= 84.0 {
            self.throttling = true;
        } else if self.throttling && self.temp_c <= 79.0 {
            self.throttling = false;
        }

        let max_clock = 2520.0;
        let clock = if self.throttling {
            self.rng.range(1750.0, 1860.0)
        } else if idle {
            self.rng.range(210.0, 420.0)
        } else {
            self.rng.range(max_clock - 90.0, max_clock)
        };

        let power = if idle {
            self.rng.range(28_000.0, 45_000.0)
        } else if self.throttling {
            self.rng.range(300_000.0, 330_000.0)
        } else {
            self.rng.range(390_000.0, 448_000.0)
        };

        DynamicSample {
            ts_ms,
            util_pct: Some(util as f32),
            util_engine: None,
            mem_used_bytes: Some(self.vram_python as u64 + 700 * 1024 * 1024),
            power_mw: Some(power as u32),
            temp_c: Some(self.temp_c as f32),
            fan_pct: Some(self.fan_pct as f32),
            sm_clock_mhz: Some(clock as u32),
            mem_clock_mhz: Some(10_500),
            encoder_pct: Some(0.0),
            decoder_pct: Some(0.0),
            // The mock OBSERVES throttling by design (it scripts it) — always `Some`,
            // never the unobservable `None` (design §5.4: mock stays Some).
            throttle: Some(ThrottleReasons {
                thermal: self.throttling,
                ..Default::default()
            }),
        }
    }

    fn processes(&mut self) -> Vec<ProcessSample> {
        let idle = self.idle_until > self.tick;
        vec![
            ProcessSample {
                pid: 4521,
                name: "python".into(),
                kind: ProcessKind::Compute,
                mem_bytes: Some(self.vram_python as u64),
                util_pct: Some(if idle { 1.0 } else { 96.0 }),
                // A dataloader pegging a few cores while the GPU works (more during an idle
                // gap, the CPU-bound stall fingerprint) — gives the CPU% column live coverage.
                cpu_pct: Some(if idle { 320.0 } else { 180.0 }),
                container: None,
            },
            ProcessSample {
                pid: 1203,
                name: "Xorg".into(),
                kind: ProcessKind::Graphics,
                mem_bytes: Some(420 * 1024 * 1024),
                util_pct: Some(2.0),
                cpu_pct: Some(6.0),
                container: None,
            },
        ]
    }
}

impl DesktopSim {
    fn step(&mut self, ts_ms: u64) -> DynamicSample {
        self.tick += 1;

        // ollama attaches/leaves on a cycle to exercise process lifecycle events.
        if self.tick >= self.ollama_toggle_at {
            self.ollama_present = !self.ollama_present;
            let hold = if self.ollama_present { 70 } else { 50 };
            self.ollama_toggle_at = self.tick + hold + (self.rng.next() % 30);
        }

        let target = if self.ollama_present {
            self.rng.range(55.0, 92.0)
        } else {
            self.rng.range(3.0, 28.0)
        };
        self.util_level += (target - self.util_level) * 0.3;

        let used = if self.ollama_present {
            (12.4 * GIB as f64) + self.rng.range(-0.2, 0.2) * GIB as f64
        } else {
            1.1 * GIB as f64
        };

        DynamicSample {
            ts_ms,
            util_pct: Some(self.util_level as f32),
            util_engine: None,
            mem_used_bytes: Some(used as u64),
            power_mw: Some(if self.ollama_present { 248_000 } else { 41_000 }),
            temp_c: Some(if self.ollama_present { 71.0 } else { 44.0 }),
            fan_pct: Some(if self.ollama_present { 58.0 } else { 0.0 }),
            sm_clock_mhz: Some(if self.ollama_present { 2_390 } else { 350 }),
            mem_clock_mhz: None, // exercise the Option path: not every metric exists
            encoder_pct: None,
            decoder_pct: None,
            throttle: Some(ThrottleReasons::default()),
        }
    }

    fn processes(&mut self) -> Vec<ProcessSample> {
        let mut v = vec![ProcessSample {
            pid: 980,
            name: "gnome-shell".into(),
            kind: ProcessKind::Graphics,
            mem_bytes: Some(610 * 1024 * 1024),
            util_pct: Some(3.0),
            cpu_pct: Some(12.0),
            container: None,
        }];
        if self.ollama_present {
            v.push(ProcessSample {
                pid: 7777,
                name: "ollama".into(),
                kind: ProcessKind::Compute,
                mem_bytes: Some(11 * GIB + 350 * 1024 * 1024),
                util_pct: Some(74.0),
                // Runs in a container (the cluster-operator's "which pod" column) and burns a
                // core or so serving the model — gives both new columns mock coverage.
                cpu_pct: Some(140.0),
                container: Some("docker:3f2a9c1b4d5e".into()),
            });
        }
        v
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Two fresh backends stepped with the same timestamp sequence must produce identical
    /// frames — `gpuviewer demo` relies on this: the seeded story is reproducible, and any
    /// hidden wall-clock dependence in the sims (which would make the demo unrepeatable)
    /// fails here.
    #[test]
    fn tick_at_is_deterministic_across_instances() {
        let mut a = MockBackend::new();
        let mut b = MockBackend::new();
        for i in 0..500u64 {
            let ts = 1_700_000_000_000 + i * 1_000;
            let fa = a.tick_at(ts);
            let fb = b.tick_at(ts);
            assert_eq!(fa, fb, "frames diverged at tick {i}");
            assert_eq!(fa.len(), 2, "tick_at must cover BOTH mock devices");
            for (_, sample, procs) in &fa {
                assert_eq!(sample.ts_ms, ts, "samples carry the synthetic timestamp");
                assert!(!procs.is_empty(), "mock devices always have processes");
            }
        }
    }

    /// `tick_at` and the live `refresh_dynamic` route through the same simulation step, so
    /// driving one backend via `tick_at` and another via the trait methods yields the same
    /// per-tick *state evolution* (only the timestamps differ — the live path stamps
    /// `now_ms()`). Throttling within 500 ticks proves the seeded story actually contains
    /// the throttle onset the demo scrolls back to.
    #[test]
    fn tick_at_drives_the_same_simulation_as_refresh_dynamic() {
        use crate::backend::GpuBackend;
        let mut seeded = MockBackend::new();
        let mut live = MockBackend::new();
        let train = live.devices()[0].clone();
        let mut seeded_throttled = false;
        for i in 0..500u64 {
            let frame = seeded.tick_at(i * 1_000);
            let (_, s, _) = &frame[0];
            let mut l = live.refresh_dynamic(&train).unwrap();
            // Same evolution apart from the clock: align it and compare everything else.
            l.ts_ms = s.ts_ms;
            assert_eq!(*s, l, "sim state diverged at tick {i}");
            let _ = live.refresh_processes(&train).unwrap();
            seeded_throttled |= s.throttle.is_some_and(|t| t.any());
        }
        assert!(
            seeded_throttled,
            "500 ticks of the training sim must include a throttle episode"
        );
    }
}