primal-barracuda 0.4.0

barraCuda — BARrier-free Rust Abstracted Cross-platform Unified Dimensional Algebra. Vendor-agnostic GPU compute via WGSL.
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Resource Quota Management for GPU Compute
//!
//! Provides VRAM budget enforcement and resource tracking for multi-tenant
//! GPU workloads. Enables fair resource sharing across tasks and prevents
//! memory exhaustion.
//!
//! # Architecture
//!
//! ```text
//! ResourceQuota (per-task budget)
//!     ↓
//! QuotaTracker (enforces limits, tracks usage)
//!     ↓
//! DeviceQuota (per-device allocation + usage)
//!     ↓
//! QuotaPool (multi-device quota-aware pool)
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use barracuda::resource_quota::{ResourceQuota, QuotaPool};
//!
//! // Create a quota: 2GB VRAM, prefer discrete GPUs
//! let quota = ResourceQuota::new()
//!     .with_max_vram_bytes(2 * 1024 * 1024 * 1024)
//!     .with_preferred_class(DeviceClass::DiscreteGpu);
//!
//! // Get a device from the pool that fits the quota
//! let pool = QuotaPool::new().await?;
//! let device = pool.acquire_device(&quota)?;
//!
//! // Track allocations against quota
//! let tracker = QuotaTracker::new(quota);
//! tracker.try_allocate(1024 * 1024)?;  // 1 MB - succeeds
//! tracker.try_allocate(3 * 1024 * 1024 * 1024)?;  // 3 GB - fails (exceeds quota)
//! ```
//!
//! # Deep Debt Compliance
//!
//! - Modern idiomatic Rust (parameter-based APIs, no global state mutation)
//! - Zero unsafe code
//! - Capability-based (devices discovered at runtime)
//! - Proper error handling (Result types, no panics)

use crate::error::{BarracudaError, Result};
use crate::multi_gpu::DeviceClass;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};

// ============================================================================
// ResourceQuota - Budget Definition
// ============================================================================

/// Resource quota defining limits for a task/tenant
///
/// Quotas are immutable once created. Use the builder pattern to configure.
#[derive(Debug, Clone)]
pub struct ResourceQuota {
    /// Maximum VRAM bytes allowed (None = unlimited)
    pub max_vram_bytes: Option<u64>,

    /// Maximum number of buffers allowed (None = unlimited)
    pub max_buffers: Option<usize>,

    /// Maximum single buffer size (None = unlimited)
    pub max_single_buffer_bytes: Option<u64>,

    /// Preferred device class (None = no preference)
    pub preferred_class: Option<DeviceClass>,

    /// Minimum VRAM required to be usable
    pub min_vram_bytes: Option<u64>,

    /// Quota identifier for logging/debugging
    pub name: String,
}

impl Default for ResourceQuota {
    fn default() -> Self {
        Self {
            max_vram_bytes: None,
            max_buffers: None,
            max_single_buffer_bytes: None,
            preferred_class: None,
            min_vram_bytes: None,
            name: "default".to_string(),
        }
    }
}

impl ResourceQuota {
    /// Create a new quota with default (unlimited) settings
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a quota with a specific name
    pub fn named(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            ..Self::default()
        }
    }

    /// Set maximum VRAM bytes
    #[must_use]
    pub fn with_max_vram_bytes(mut self, bytes: u64) -> Self {
        self.max_vram_bytes = Some(bytes);
        self
    }

    /// Set maximum VRAM in megabytes (convenience)
    #[must_use]
    pub fn with_max_vram_mb(self, mb: u64) -> Self {
        self.with_max_vram_bytes(mb * 1024 * 1024)
    }

    /// Set maximum VRAM in gigabytes (convenience)
    #[must_use]
    pub fn with_max_vram_gb(self, gb: u64) -> Self {
        self.with_max_vram_bytes(gb * 1024 * 1024 * 1024)
    }

    /// Set maximum number of buffers
    #[must_use]
    pub fn with_max_buffers(mut self, count: usize) -> Self {
        self.max_buffers = Some(count);
        self
    }

    /// Set maximum single buffer size
    #[must_use]
    pub fn with_max_single_buffer_bytes(mut self, bytes: u64) -> Self {
        self.max_single_buffer_bytes = Some(bytes);
        self
    }

    /// Set preferred device class
    #[must_use]
    pub fn with_preferred_class(mut self, class: DeviceClass) -> Self {
        self.preferred_class = Some(class);
        self
    }

    /// Set minimum VRAM required
    #[must_use]
    pub fn with_min_vram_bytes(mut self, bytes: u64) -> Self {
        self.min_vram_bytes = Some(bytes);
        self
    }

    /// Set minimum VRAM in gigabytes (convenience)
    #[must_use]
    pub fn with_min_vram_gb(self, gb: u64) -> Self {
        self.with_min_vram_bytes(gb * 1024 * 1024 * 1024)
    }

    /// Check if a device meets the minimum requirements
    pub fn device_meets_requirements(
        &self,
        device_vram_bytes: u64,
        device_class: DeviceClass,
    ) -> bool {
        // Check minimum VRAM
        if let Some(min) = self.min_vram_bytes
            && device_vram_bytes < min
        {
            return false;
        }

        // Device class preference is soft (doesn't disqualify)
        if let Some(pref) = self.preferred_class
            && pref != device_class
        {
            tracing::debug!(
                "Quota '{}': device class {:?} doesn't match preference {:?}",
                self.name,
                device_class,
                pref
            );
        }

        true
    }
}

// ============================================================================
// QuotaTracker - Usage Tracking and Enforcement
// ============================================================================

/// Tracks resource usage against a quota
///
/// Thread-safe via atomic operations. Create one per task/session.
#[derive(Debug)]
pub struct QuotaTracker {
    /// The quota being enforced
    quota: ResourceQuota,

    /// Current VRAM usage in bytes
    current_vram_bytes: AtomicU64,

    /// Current buffer count
    current_buffers: AtomicU64,

    /// Peak VRAM usage (for diagnostics)
    peak_vram_bytes: AtomicU64,

    /// Total bytes allocated (including freed)
    total_allocated_bytes: AtomicU64,

    /// Number of allocation failures due to quota
    quota_failures: AtomicU64,
}

impl QuotaTracker {
    /// Create a new tracker for the given quota
    #[must_use]
    pub fn new(quota: ResourceQuota) -> Self {
        Self {
            quota,
            current_vram_bytes: AtomicU64::new(0),
            current_buffers: AtomicU64::new(0),
            peak_vram_bytes: AtomicU64::new(0),
            total_allocated_bytes: AtomicU64::new(0),
            quota_failures: AtomicU64::new(0),
        }
    }

    /// Create a tracker wrapped in Arc for sharing
    #[must_use]
    pub fn new_shared(quota: ResourceQuota) -> Arc<Self> {
        Arc::new(Self::new(quota))
    }

    /// Get the quota being tracked
    pub fn quota(&self) -> &ResourceQuota {
        &self.quota
    }

    /// Current VRAM usage in bytes
    pub fn current_vram_bytes(&self) -> u64 {
        self.current_vram_bytes.load(Ordering::Relaxed)
    }

    /// Current buffer count
    pub fn current_buffers(&self) -> u64 {
        self.current_buffers.load(Ordering::Relaxed)
    }

    /// Peak VRAM usage in bytes
    pub fn peak_vram_bytes(&self) -> u64 {
        self.peak_vram_bytes.load(Ordering::Relaxed)
    }

    /// Total bytes allocated since creation
    pub fn total_allocated_bytes(&self) -> u64 {
        self.total_allocated_bytes.load(Ordering::Relaxed)
    }

    /// Number of quota failures
    pub fn quota_failures(&self) -> u64 {
        self.quota_failures.load(Ordering::Relaxed)
    }

    /// Record an OOM failure detected during migration.
    ///
    /// Called by [`MultiDevicePool::execute_with_migration_quota`] when a device
    /// returns an OOM error. Increments the failure counter for diagnostics.
    pub fn record_oom_failure(&self) {
        self.quota_failures.fetch_add(1, Ordering::Relaxed);
    }

    /// Remaining VRAM budget (None if unlimited)
    pub fn remaining_vram_bytes(&self) -> Option<u64> {
        self.quota.max_vram_bytes.map(|max| {
            let current = self.current_vram_bytes();
            max.saturating_sub(current)
        })
    }

    /// Usage as percentage of quota (0-100, or None if unlimited)
    pub fn usage_percent(&self) -> Option<f64> {
        self.quota.max_vram_bytes.map(|max| {
            let current = self.current_vram_bytes();
            (current as f64 / max as f64) * 100.0
        })
    }

    /// Check if an allocation would exceed quota (without actually allocating)
    pub fn would_exceed_quota(&self, bytes: u64) -> bool {
        // Check single buffer limit
        if let Some(max_single) = self.quota.max_single_buffer_bytes
            && bytes > max_single
        {
            return true;
        }

        // Check total VRAM limit
        if let Some(max_vram) = self.quota.max_vram_bytes {
            let current = self.current_vram_bytes();
            if current + bytes > max_vram {
                return true;
            }
        }

        // Check buffer count limit
        if let Some(max_buffers) = self.quota.max_buffers {
            let current = self.current_buffers() as usize;
            if current >= max_buffers {
                return true;
            }
        }

        false
    }

    /// Try to allocate bytes, respecting quota
    ///
    /// Returns Ok(()) if allocation is allowed, Err if it would exceed quota.
    /// On success, the allocation is tracked. Call `deallocate` when done.
    ///
    /// # Errors
    ///
    /// Returns [`Err`] if buffer allocation, GPU dispatch, or buffer
    /// readback fails (e.g. device lost or out of memory).
    pub fn try_allocate(&self, bytes: u64) -> Result<()> {
        // Check single buffer limit
        if let Some(max_single) = self.quota.max_single_buffer_bytes
            && bytes > max_single
        {
            self.quota_failures.fetch_add(1, Ordering::Relaxed);
            return Err(BarracudaError::resource_exhausted(format!(
                "Quota '{}': single buffer {} bytes exceeds limit {} bytes",
                self.quota.name, bytes, max_single
            )));
        }

        // Check buffer count limit
        if let Some(max_buffers) = self.quota.max_buffers {
            let current = self.current_buffers() as usize;
            if current >= max_buffers {
                self.quota_failures.fetch_add(1, Ordering::Relaxed);
                return Err(BarracudaError::resource_exhausted(format!(
                    "Quota '{}': buffer count {} at limit {}",
                    self.quota.name, current, max_buffers
                )));
            }
        }

        // Atomically try to add to VRAM usage
        if let Some(max_vram) = self.quota.max_vram_bytes {
            loop {
                let current = self.current_vram_bytes.load(Ordering::Relaxed);
                let new_total = current + bytes;

                if new_total > max_vram {
                    self.quota_failures.fetch_add(1, Ordering::Relaxed);
                    return Err(BarracudaError::resource_exhausted(format!(
                        "Quota '{}': allocation of {} bytes would exceed limit ({} + {} > {})",
                        self.quota.name, bytes, current, bytes, max_vram
                    )));
                }

                // Try to atomically update
                if self
                    .current_vram_bytes
                    .compare_exchange(current, new_total, Ordering::SeqCst, Ordering::Relaxed)
                    .is_ok()
                {
                    break;
                }
                // If CAS failed, loop and retry
            }
        } else {
            // No VRAM limit, just track
            self.current_vram_bytes.fetch_add(bytes, Ordering::Relaxed);
        }

        // Update buffer count
        self.current_buffers.fetch_add(1, Ordering::Relaxed);

        // Update stats
        self.total_allocated_bytes
            .fetch_add(bytes, Ordering::Relaxed);

        // Update peak
        let current = self.current_vram_bytes();
        let mut peak = self.peak_vram_bytes.load(Ordering::Relaxed);
        while current > peak {
            match self.peak_vram_bytes.compare_exchange(
                peak,
                current,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(p) => peak = p,
            }
        }

        Ok(())
    }

    /// Record deallocation of bytes
    pub fn deallocate(&self, bytes: u64) {
        self.current_vram_bytes.fetch_sub(bytes, Ordering::Relaxed);
        self.current_buffers.fetch_sub(1, Ordering::Relaxed);
    }

    /// Get a summary string for logging/debugging
    pub fn summary(&self) -> String {
        let current = self.current_vram_bytes();
        let peak = self.peak_vram_bytes();
        let buffers = self.current_buffers();
        let failures = self.quota_failures();

        self.quota.max_vram_bytes.map_or_else(
            || {
                format!(
                    "Quota '{}': {} bytes used (unlimited), {} buffers, peak {} bytes",
                    self.quota.name,
                    format_bytes(current),
                    buffers,
                    format_bytes(peak)
                )
            },
            |max| {
                let percent = (current as f64 / max as f64) * 100.0;
                format!(
                    "Quota '{}': {:.1}% used ({} / {} bytes), {} buffers, peak {} bytes, {} failures",
                    self.quota.name,
                    percent,
                    format_bytes(current),
                    format_bytes(max),
                    buffers,
                    format_bytes(peak),
                    failures
                )
            },
        )
    }

    /// Reset usage tracking (does not affect quota limits)
    pub fn reset(&self) {
        self.current_vram_bytes.store(0, Ordering::Relaxed);
        self.current_buffers.store(0, Ordering::Relaxed);
        self.peak_vram_bytes.store(0, Ordering::Relaxed);
        self.total_allocated_bytes.store(0, Ordering::Relaxed);
        self.quota_failures.store(0, Ordering::Relaxed);
    }
}

// ============================================================================
// DeviceQuota - Per-Device Allocation
// ============================================================================

/// Per-device quota information
#[derive(Debug, Clone)]
pub struct DeviceQuota {
    /// Device index in the pool
    pub device_index: usize,

    /// Device name
    pub device_name: String,

    /// Device class
    pub device_class: DeviceClass,

    /// Total VRAM on device (bytes)
    pub total_vram_bytes: u64,

    /// Currently allocated VRAM (bytes)
    pub allocated_vram_bytes: u64,

    /// Available VRAM (bytes)
    pub available_vram_bytes: u64,

    /// Number of active allocations
    pub active_allocations: usize,
}

impl DeviceQuota {
    /// Check if this device can accommodate an allocation
    #[must_use]
    pub fn can_allocate(&self, bytes: u64) -> bool {
        self.available_vram_bytes >= bytes
    }

    /// Get usage percentage (0-100)
    #[must_use]
    pub fn usage_percent(&self) -> f64 {
        if self.total_vram_bytes == 0 {
            return 0.0;
        }
        (self.allocated_vram_bytes as f64 / self.total_vram_bytes as f64) * 100.0
    }
}

// ============================================================================
// Helpers
// ============================================================================

/// Format bytes as human-readable string
fn format_bytes(bytes: u64) -> String {
    if bytes >= 1024 * 1024 * 1024 {
        format!("{:.2} GB", bytes as f64 / (1024.0 * 1024.0 * 1024.0))
    } else if bytes >= 1024 * 1024 {
        format!("{:.2} MB", bytes as f64 / (1024.0 * 1024.0))
    } else if bytes >= 1024 {
        format!("{:.2} KB", bytes as f64 / 1024.0)
    } else {
        format!("{bytes} B")
    }
}

// ============================================================================
// Preset Quotas
// ============================================================================

/// Common quota presets
pub mod presets {
    use super::ResourceQuota;

    /// VRAM limit for small task preset (512 MiB).
    const PRESET_SMALL_VRAM_MB: u64 = 512;

    /// VRAM limit for medium task preset (2 GiB).
    const PRESET_MEDIUM_VRAM_GB: u64 = 2;

    /// VRAM limit for large task preset (8 GiB).
    const PRESET_LARGE_VRAM_GB: u64 = 8;

    /// VRAM limit for scientific preset (4 GiB).
    const PRESET_SCIENTIFIC_VRAM_GB: u64 = 4;

    /// Buffer limit for scientific preset.
    const PRESET_SCIENTIFIC_MAX_BUFFERS: usize = 1000;

    /// VRAM limit for ML inference preset (2 GiB).
    const PRESET_ML_INFERENCE_VRAM_GB: u64 = 2;

    /// Buffer limit for ML inference preset.
    const PRESET_ML_INFERENCE_MAX_BUFFERS: usize = 100;

    /// VRAM limit for ML training preset (8 GiB).
    const PRESET_ML_TRAINING_VRAM_GB: u64 = 8;

    /// Buffer limit for ML training preset.
    const PRESET_ML_TRAINING_MAX_BUFFERS: usize = 500;

    /// Small task: 512 MB VRAM
    #[must_use]
    pub fn small() -> ResourceQuota {
        ResourceQuota::named("small").with_max_vram_mb(PRESET_SMALL_VRAM_MB)
    }

    /// Medium task: 2 GB VRAM
    #[must_use]
    pub fn medium() -> ResourceQuota {
        ResourceQuota::named("medium").with_max_vram_gb(PRESET_MEDIUM_VRAM_GB)
    }

    /// Large task: 8 GB VRAM
    #[must_use]
    pub fn large() -> ResourceQuota {
        ResourceQuota::named("large").with_max_vram_gb(PRESET_LARGE_VRAM_GB)
    }

    /// Unlimited (for testing or privileged tasks)
    #[must_use]
    pub fn unlimited() -> ResourceQuota {
        ResourceQuota::named("unlimited")
    }

    /// Scientific computing: 4 GB VRAM, high buffer limits
    #[must_use]
    pub fn scientific() -> ResourceQuota {
        ResourceQuota::named("scientific")
            .with_max_vram_gb(PRESET_SCIENTIFIC_VRAM_GB)
            .with_max_buffers(PRESET_SCIENTIFIC_MAX_BUFFERS)
    }

    /// ML inference: 2 GB VRAM per model
    #[must_use]
    pub fn ml_inference() -> ResourceQuota {
        ResourceQuota::named("ml_inference")
            .with_max_vram_gb(PRESET_ML_INFERENCE_VRAM_GB)
            .with_max_buffers(PRESET_ML_INFERENCE_MAX_BUFFERS)
    }

    /// ML training: 8 GB VRAM, high buffer limits
    #[must_use]
    pub fn ml_training() -> ResourceQuota {
        ResourceQuota::named("ml_training")
            .with_max_vram_gb(PRESET_ML_TRAINING_VRAM_GB)
            .with_max_buffers(PRESET_ML_TRAINING_MAX_BUFFERS)
    }
}

#[cfg(test)]
#[path = "resource_quota_tests.rs"]
mod tests;