use crate::error::{BarracudaError, Result};
use crate::multi_gpu::DeviceClass;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Debug, Clone)]
pub struct ResourceQuota {
pub max_vram_bytes: Option<u64>,
pub max_buffers: Option<usize>,
pub max_single_buffer_bytes: Option<u64>,
pub preferred_class: Option<DeviceClass>,
pub min_vram_bytes: Option<u64>,
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 {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn named(name: impl Into<String>) -> Self {
Self {
name: name.into(),
..Self::default()
}
}
#[must_use]
pub fn with_max_vram_bytes(mut self, bytes: u64) -> Self {
self.max_vram_bytes = Some(bytes);
self
}
#[must_use]
pub fn with_max_vram_mb(self, mb: u64) -> Self {
self.with_max_vram_bytes(mb * 1024 * 1024)
}
#[must_use]
pub fn with_max_vram_gb(self, gb: u64) -> Self {
self.with_max_vram_bytes(gb * 1024 * 1024 * 1024)
}
#[must_use]
pub fn with_max_buffers(mut self, count: usize) -> Self {
self.max_buffers = Some(count);
self
}
#[must_use]
pub fn with_max_single_buffer_bytes(mut self, bytes: u64) -> Self {
self.max_single_buffer_bytes = Some(bytes);
self
}
#[must_use]
pub fn with_preferred_class(mut self, class: DeviceClass) -> Self {
self.preferred_class = Some(class);
self
}
#[must_use]
pub fn with_min_vram_bytes(mut self, bytes: u64) -> Self {
self.min_vram_bytes = Some(bytes);
self
}
#[must_use]
pub fn with_min_vram_gb(self, gb: u64) -> Self {
self.with_min_vram_bytes(gb * 1024 * 1024 * 1024)
}
pub fn device_meets_requirements(
&self,
device_vram_bytes: u64,
device_class: DeviceClass,
) -> bool {
if let Some(min) = self.min_vram_bytes
&& device_vram_bytes < min
{
return false;
}
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
}
}
#[derive(Debug)]
pub struct QuotaTracker {
quota: ResourceQuota,
current_vram_bytes: AtomicU64,
current_buffers: AtomicU64,
peak_vram_bytes: AtomicU64,
total_allocated_bytes: AtomicU64,
quota_failures: AtomicU64,
}
impl QuotaTracker {
#[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),
}
}
#[must_use]
pub fn new_shared(quota: ResourceQuota) -> Arc<Self> {
Arc::new(Self::new(quota))
}
pub fn quota(&self) -> &ResourceQuota {
&self.quota
}
pub fn current_vram_bytes(&self) -> u64 {
self.current_vram_bytes.load(Ordering::Relaxed)
}
pub fn current_buffers(&self) -> u64 {
self.current_buffers.load(Ordering::Relaxed)
}
pub fn peak_vram_bytes(&self) -> u64 {
self.peak_vram_bytes.load(Ordering::Relaxed)
}
pub fn total_allocated_bytes(&self) -> u64 {
self.total_allocated_bytes.load(Ordering::Relaxed)
}
pub fn quota_failures(&self) -> u64 {
self.quota_failures.load(Ordering::Relaxed)
}
pub fn record_oom_failure(&self) {
self.quota_failures.fetch_add(1, Ordering::Relaxed);
}
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)
})
}
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
})
}
pub fn would_exceed_quota(&self, bytes: u64) -> bool {
if let Some(max_single) = self.quota.max_single_buffer_bytes
&& bytes > max_single
{
return true;
}
if let Some(max_vram) = self.quota.max_vram_bytes {
let current = self.current_vram_bytes();
if current + bytes > max_vram {
return true;
}
}
if let Some(max_buffers) = self.quota.max_buffers {
let current = self.current_buffers() as usize;
if current >= max_buffers {
return true;
}
}
false
}
pub fn try_allocate(&self, bytes: u64) -> Result<()> {
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
)));
}
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
)));
}
}
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
)));
}
if self
.current_vram_bytes
.compare_exchange(current, new_total, Ordering::SeqCst, Ordering::Relaxed)
.is_ok()
{
break;
}
}
} else {
self.current_vram_bytes.fetch_add(bytes, Ordering::Relaxed);
}
self.current_buffers.fetch_add(1, Ordering::Relaxed);
self.total_allocated_bytes
.fetch_add(bytes, Ordering::Relaxed);
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(())
}
pub fn deallocate(&self, bytes: u64) {
self.current_vram_bytes.fetch_sub(bytes, Ordering::Relaxed);
self.current_buffers.fetch_sub(1, Ordering::Relaxed);
}
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
)
},
)
}
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);
}
}
#[derive(Debug, Clone)]
pub struct DeviceQuota {
pub device_index: usize,
pub device_name: String,
pub device_class: DeviceClass,
pub total_vram_bytes: u64,
pub allocated_vram_bytes: u64,
pub available_vram_bytes: u64,
pub active_allocations: usize,
}
impl DeviceQuota {
#[must_use]
pub fn can_allocate(&self, bytes: u64) -> bool {
self.available_vram_bytes >= bytes
}
#[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
}
}
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")
}
}
pub mod presets {
use super::ResourceQuota;
const PRESET_SMALL_VRAM_MB: u64 = 512;
const PRESET_MEDIUM_VRAM_GB: u64 = 2;
const PRESET_LARGE_VRAM_GB: u64 = 8;
const PRESET_SCIENTIFIC_VRAM_GB: u64 = 4;
const PRESET_SCIENTIFIC_MAX_BUFFERS: usize = 1000;
const PRESET_ML_INFERENCE_VRAM_GB: u64 = 2;
const PRESET_ML_INFERENCE_MAX_BUFFERS: usize = 100;
const PRESET_ML_TRAINING_VRAM_GB: u64 = 8;
const PRESET_ML_TRAINING_MAX_BUFFERS: usize = 500;
#[must_use]
pub fn small() -> ResourceQuota {
ResourceQuota::named("small").with_max_vram_mb(PRESET_SMALL_VRAM_MB)
}
#[must_use]
pub fn medium() -> ResourceQuota {
ResourceQuota::named("medium").with_max_vram_gb(PRESET_MEDIUM_VRAM_GB)
}
#[must_use]
pub fn large() -> ResourceQuota {
ResourceQuota::named("large").with_max_vram_gb(PRESET_LARGE_VRAM_GB)
}
#[must_use]
pub fn unlimited() -> ResourceQuota {
ResourceQuota::named("unlimited")
}
#[must_use]
pub fn scientific() -> ResourceQuota {
ResourceQuota::named("scientific")
.with_max_vram_gb(PRESET_SCIENTIFIC_VRAM_GB)
.with_max_buffers(PRESET_SCIENTIFIC_MAX_BUFFERS)
}
#[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)
}
#[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;