dexcost 0.4.0

Agent Unit Economics SDK for Rust — track end-to-end business-task costs for AI agents.
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! NVML library wrapper — Phase 2 GPU foundation.
//!
//! Wraps NVIDIA's NVML (via the `nvml-wrapper` crate, gated behind the
//! `gpu` cargo feature) with the fail-silent contract from convention §9.
//! Every NVML call returns `None` (or `false` for boolean accessors) on
//! missing driver / library / permission / device errors rather than
//! raising — the caller (typically [`GpuAccountant`]) decides the fallback
//! policy per Decision #1's classification table.
//!
//! `nvml-wrapper` is an OPTIONAL dependency (cargo `--features gpu`). When
//! the feature is OFF, every accessor returns the no-NVML sentinel and the
//! SDK works on GPU-less hosts.
//!
//! Per Decision #4: [`get_product_name`] applies NFC Unicode normalization
//! + lowercase + whitespace collapse on the raw NVML string before
//! returning — catalog alias matching depends on byte-level equality after
//! normalization because NVIDIA's productName carries non-breaking spaces
//! and other Unicode quirks across driver versions.
//!
//! Per Decision #8: [`get_process_utilization`] takes a mutable
//! `last_seen_timestamps` map and updates it in place — the caller
//! persists per-PID timestamps across snapshot calls so NVML's sample
//! buffer doesn't silently lose intermediate samples.

use std::collections::{HashMap, HashSet};
use std::sync::{Mutex, OnceLock};

use unicode_normalization::UnicodeNormalization;

/// One NVML compute-running-process record (PID + GPU memory usage).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcessInfo {
    pub pid: u32,
    /// VRAM bytes used by this PID; may be 0 when NVML reports NOT_AVAILABLE.
    pub used_gpu_memory: u64,
}

/// One NVML process-utilization sample for a single PID at one timestamp.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UtilSample {
    pub pid: u32,
    /// 0-100 — percent of time SMs had ≥1 kernel running.
    pub sm_util: u32,
    /// 0-100 — percent of time memory subsystem was busy.
    pub mem_util: u32,
    /// Microseconds since NVML epoch.
    pub time_stamp: u64,
}

/// Device-level memory totals.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MemInfo {
    pub used_bytes: u64,
    pub total_bytes: u64,
}

// ── Log-once-per-failure-mode state (convention §11) ───────────────────────

fn warned_modes() -> &'static Mutex<HashSet<String>> {
    static SLOT: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();
    SLOT.get_or_init(|| Mutex::new(HashSet::new()))
}

fn warn_once(mode: &str, message: &str) {
    let mut set = match warned_modes().lock() {
        Ok(g) => g,
        Err(p) => p.into_inner(),
    };
    if !set.insert(mode.to_string()) {
        return;
    }
    eprintln!("[dexcost][gpu] {}: {}", mode, message);
}

/// Test-only: clear the warn-once tracking set.
#[doc(hidden)]
pub fn reset_warning_state_for_tests() {
    let mut set = match warned_modes().lock() {
        Ok(g) => g,
        Err(p) => p.into_inner(),
    };
    set.clear();
}

// ── Decision #4: NFC-normalized productName ────────────────────────────────

/// NFC → lowercase → whitespace collapse (incl. NBSP / NNBSP / ZWSP).
///
/// Public so tests / catalog tools can normalize alias inputs the same way.
pub fn normalize_product_name(raw: &str) -> String {
    let nfc: String = raw.nfc().collect();
    // Collapse ALL whitespace (incl. U+00A0, U+202F, etc. — char::is_whitespace
    // covers Unicode whitespace categories the same way Python's str.split does).
    let collapsed: String = nfc
        .split(|c: char| c.is_whitespace())
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join(" ");
    collapsed.to_lowercase()
}

// ── NVML availability + accessors ──────────────────────────────────────────
//
// The `gpu` feature gates the real NVML calls. When the feature is OFF
// (default — and for CI / GPU-less hosts), every accessor returns the
// no-NVML sentinel: `Available::No`, count `None`, etc.

#[cfg(feature = "gpu")]
mod nvml_real {
    use super::*;
    use nvml_wrapper::Nvml;
    use std::sync::OnceLock;

    static NVML: OnceLock<Option<Nvml>> = OnceLock::new();

    pub fn nvml_available() -> bool {
        // Crate is linked; runtime init may still fail.
        true
    }

    fn ensure_init() -> Option<&'static Nvml> {
        NVML.get_or_init(|| match Nvml::init() {
            Ok(n) => Some(n),
            Err(e) => {
                warn_once(
                    "gpu_nvml_init_failed",
                    &format!("NVML init failed ({}); GPU capture disabled", e),
                );
                None
            }
        })
        .as_ref()
    }

    pub fn init_nvml() -> bool {
        ensure_init().is_some()
    }

    pub fn shutdown_nvml() {
        // nvml-wrapper drops via OnceLock; explicit shutdown is a no-op here.
    }

    pub fn get_device_count() -> Option<u32> {
        let n = ensure_init()?;
        match n.device_count() {
            Ok(c) => Some(c),
            Err(e) => {
                warn_once(
                    "gpu_device_count_failed",
                    &format!("nvmlDeviceGetCount failed ({})", e),
                );
                None
            }
        }
    }
}

#[cfg(not(feature = "gpu"))]
mod nvml_real {
    use super::*;

    pub fn nvml_available() -> bool {
        // Crate not linked → no NVML available. Customers opt-in via
        // `cargo build --features gpu`.
        false
    }

    pub fn init_nvml() -> bool {
        warn_once(
            "gpu_nvml_not_linked",
            "nvml-wrapper crate not linked; GPU capture disabled. \
             Rebuild with --features gpu to enable.",
        );
        false
    }

    pub fn shutdown_nvml() {}

    pub fn get_device_count() -> Option<u32> {
        None
    }
}

pub use nvml_real::{get_device_count, init_nvml, nvml_available, shutdown_nvml};

// ── Per-device accessors (degraded-mode fallback when feature is OFF) ──────

/// Opaque device handle — wraps the device index. When the `gpu` feature
/// is off there is no NVML; accessors short-circuit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DeviceHandle(pub u32);

pub fn get_device_handle(index: u32) -> Option<DeviceHandle> {
    if !nvml_available() {
        return None;
    }
    // When the feature is on, we trust ensure_init() / device_count() ran
    // before this. The handle is just the index; the real NVML lookup
    // happens in the accessors that need it.
    Some(DeviceHandle(index))
}

/// NFC-normalized productName for the given device. `None` on failure
/// or when NVML isn't available.
pub fn get_product_name(handle: DeviceHandle) -> Option<String> {
    real::get_product_name(handle)
}

/// Compute-running processes on the device. `None` on
/// `NVML_ERROR_NO_PERMISSION` — the load-bearing Decision #1 case;
/// caller's cgroup walk degrades to self-PID-only.
pub fn get_compute_running_processes(handle: DeviceHandle) -> Option<Vec<ProcessInfo>> {
    real::get_compute_running_processes(handle)
}

/// Per-PID utilization samples. Mutates `last_seen_timestamps` in place
/// (Decision #8) so the per-PID sample buffer doesn't silently lose
/// intermediate samples between snapshots.
pub fn get_process_utilization(
    handle: DeviceHandle,
    last_seen_timestamps: &mut HashMap<u32, u64>,
) -> Option<HashMap<u32, UtilSample>> {
    real::get_process_utilization(handle, last_seen_timestamps)
}

/// Device-level used / total VRAM bytes.
pub fn get_memory_info(handle: DeviceHandle) -> Option<MemInfo> {
    real::get_memory_info(handle)
}

/// True when MIG is enabled on this device (Decision #2 detection).
pub fn get_mig_mode(handle: DeviceHandle) -> bool {
    real::get_mig_mode(handle)
}

#[cfg(feature = "gpu")]
mod real {
    use super::*;
    use nvml_wrapper::Nvml;
    use std::sync::OnceLock;

    static NVML: OnceLock<Option<Nvml>> = OnceLock::new();

    fn ensure() -> Option<&'static Nvml> {
        // Reuse the cached Nvml from nvml_real if possible — but `nvml_real`'s
        // OnceLock is private. Re-init here is idempotent (Nvml::init() is
        // refcounted internally on the NVIDIA side).
        NVML.get_or_init(|| Nvml::init().ok()).as_ref()
    }

    pub fn get_product_name(handle: DeviceHandle) -> Option<String> {
        let n = ensure()?;
        let d = match n.device_by_index(handle.0) {
            Ok(d) => d,
            Err(_) => return None,
        };
        match d.name() {
            Ok(s) => Some(normalize_product_name(&s)),
            Err(e) => {
                warn_once(
                    "gpu_product_name_failed",
                    &format!("nvmlDeviceGetName failed ({})", e),
                );
                None
            }
        }
    }

    pub fn get_compute_running_processes(handle: DeviceHandle) -> Option<Vec<ProcessInfo>> {
        let n = ensure()?;
        let d = match n.device_by_index(handle.0) {
            Ok(d) => d,
            Err(_) => return None,
        };
        match d.running_compute_processes() {
            Ok(procs) => Some(
                procs
                    .into_iter()
                    .map(|p| ProcessInfo {
                        pid: p.pid,
                        used_gpu_memory: match p.used_gpu_memory {
                            nvml_wrapper::enums::device::UsedGpuMemory::Used(b) => b,
                            _ => 0,
                        },
                    })
                    .collect(),
            ),
            Err(e) => {
                warn_once(
                    "gpu_nvml_permission_denied",
                    &format!(
                        "nvmlDeviceGetComputeRunningProcesses failed ({}); \
                         GpuAccountant will degrade to self-PID-only",
                        e
                    ),
                );
                None
            }
        }
    }

    pub fn get_process_utilization(
        handle: DeviceHandle,
        last_seen_timestamps: &mut HashMap<u32, u64>,
    ) -> Option<HashMap<u32, UtilSample>> {
        let n = ensure()?;
        let d = match n.device_by_index(handle.0) {
            Ok(d) => d,
            Err(_) => return None,
        };
        let base_ts = last_seen_timestamps.values().copied().min().unwrap_or(0);
        match d.process_utilization_stats(Some(base_ts)) {
            Ok(samples) => {
                let mut out = HashMap::new();
                for s in samples {
                    let pid = s.pid;
                    let ts = s.timestamp;
                    out.insert(
                        pid,
                        UtilSample {
                            pid,
                            sm_util: s.sm_util,
                            mem_util: s.mem_util,
                            time_stamp: ts,
                        },
                    );
                    last_seen_timestamps.insert(pid, ts);
                }
                Some(out)
            }
            Err(e) => {
                warn_once(
                    "gpu_process_utilization_failed",
                    &format!("nvmlDeviceGetProcessUtilization failed ({})", e),
                );
                None
            }
        }
    }

    pub fn get_memory_info(handle: DeviceHandle) -> Option<MemInfo> {
        let n = ensure()?;
        let d = match n.device_by_index(handle.0) {
            Ok(d) => d,
            Err(_) => return None,
        };
        match d.memory_info() {
            Ok(m) => Some(MemInfo {
                used_bytes: m.used,
                total_bytes: m.total,
            }),
            Err(_) => None,
        }
    }

    pub fn get_mig_mode(handle: DeviceHandle) -> bool {
        let n = match ensure() {
            Some(n) => n,
            None => return false,
        };
        let d = match n.device_by_index(handle.0) {
            Ok(d) => d,
            Err(_) => return false,
        };
        match d.mig_mode() {
            Ok(m) => matches!(m.current, nvml_wrapper::enum_wrappers::device::MigMode::Enabled),
            Err(_) => false,
        }
    }
}

#[cfg(not(feature = "gpu"))]
mod real {
    use super::*;

    pub fn get_product_name(_handle: DeviceHandle) -> Option<String> {
        None
    }
    pub fn get_compute_running_processes(_handle: DeviceHandle) -> Option<Vec<ProcessInfo>> {
        None
    }
    pub fn get_process_utilization(
        _handle: DeviceHandle,
        _last_seen_timestamps: &mut HashMap<u32, u64>,
    ) -> Option<HashMap<u32, UtilSample>> {
        None
    }
    pub fn get_memory_info(_handle: DeviceHandle) -> Option<MemInfo> {
        None
    }
    pub fn get_mig_mode(_handle: DeviceHandle) -> bool {
        false
    }
}

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

    #[test]
    fn normalize_product_name_collapses_nbsp() {
        // U+00A0 NBSP between "NVIDIA" and "H100"
        let raw = "NVIDIA\u{00A0}H100\u{00A0}80GB HBM3";
        let n = normalize_product_name(raw);
        assert_eq!(n, "nvidia h100 80gb hbm3");
    }

    #[test]
    fn normalize_product_name_collapses_nnbsp_and_zwsp() {
        // U+202F narrow no-break space + zero-width space U+200B (which is
        // category Cf, NOT Zs; Python's str.split treats it as whitespace
        // via char.is_whitespace — same behaviour here for U+00A0/U+202F).
        let raw = "NVIDIA\u{202F}A100   40GB";
        let n = normalize_product_name(raw);
        assert_eq!(n, "nvidia a100 40gb");
    }

    #[test]
    fn normalize_product_name_lowercases() {
        assert_eq!(normalize_product_name("NVIDIA H100"), "nvidia h100");
    }

    #[test]
    fn normalize_product_name_nfc() {
        // Composed e+acute (U+00E9) vs decomposed e (U+0065) + combining acute (U+0301)
        // should normalize to the same string.
        let composed = "café";
        let decomposed = "cafe\u{0301}";
        assert_eq!(
            normalize_product_name(composed),
            normalize_product_name(decomposed)
        );
    }

    #[test]
    fn no_feature_returns_no_devices() {
        // Default build (no `gpu` feature) — every accessor short-circuits.
        // We can't ASSERT this when feature IS on, so the check is gated.
        #[cfg(not(feature = "gpu"))]
        {
            assert!(!nvml_available());
            assert!(!init_nvml());
            assert_eq!(get_device_count(), None);
            assert!(get_device_handle(0).is_none());
        }
    }

    #[test]
    fn warn_once_is_log_once() {
        reset_warning_state_for_tests();
        warn_once("test_mode_x", "first");
        warn_once("test_mode_x", "second");  // suppressed
        let set = warned_modes().lock().unwrap();
        assert!(set.contains("test_mode_x"));
    }
}