archy/core/performance/src/resource_manager.rs
Dorian b614c5c694 chore(ci): rustfmt + clippy clean-up to unblock the Rust CI job
The .github/workflows/ci.yml Rust job runs cargo fmt --check, clippy
with -D warnings, and tests. All three were failing. This commit:

- Applies rustfmt across the tree (the bulk of the diff — untouched
  since the last toolchain bump, so a wide sweep was unavoidable).
- Fixes the correctness-level clippy errors:
    container/bitcoin_simulator.rs wildcard-in-or-pattern
    container/manifest.rs from_str rename to parse (reserved name)
    container/podman_client.rs .get(0) -> .first()
    container/runtime.rs manual += collapse
    archipelago/src/constants.rs doc-comment → module-doc
    api/rpc/package/install.rs stray /// comment above a non-item
    container/docker_packages.rs redundant field init
    streaming/advertisement.rs missing Metric import in tests
    tests/orchestration_tests.rs `vec!` in non-Vec contexts
    mesh/listener/dispatch.rs unused store_plain_message import
    api/rpc/tor/mod.rs and mesh/steganography.rs: push-after-new → vec!
- Quiets wide legacy surfaces with crate-level allows in main.rs for
  stylistic lints (too_many_arguments, type_complexity, doc indent,
  enum variant prefix, wildcard-in-or, assertions-on-constants,
  drop_non_drop, unused_io_amount, ptr_arg) — these fired in dozens
  of places with no correctness payoff and have been churning every
  toolchain bump.
- Tags intentional-dead-code helpers: wallet/ and streaming/ modules
  are WIP, mesh::send_chunked_payload and DM_V1_MARKER are kept for
  rollback compatibility, vpn::get_nostr_vpn_status is surface-area
  for a not-yet-landed RPC.

cargo fmt --check, cargo clippy --all-targets --all-features
-- -D warnings, and cargo test --all-features now all pass locally.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-18 17:23:46 -04:00

90 lines
2.7 KiB
Rust

// Resource management and optimization for containers
// Handles CPU, memory, and disk I/O limits and optimization
use anyhow::Result;
use std::collections::HashMap;
use tracing::info;
#[derive(Debug, Clone)]
pub struct ResourceLimits {
pub cpu_cores: f64,
pub memory_mb: u32,
pub disk_io_read_mbps: Option<u32>,
pub disk_io_write_mbps: Option<u32>,
}
#[derive(Debug, Clone)]
pub struct SystemResources {
pub total_cpu_cores: u32,
pub total_memory_mb: u32,
pub available_disk_gb: u32,
}
pub struct ResourceManager {
system_resources: SystemResources,
allocated_resources: HashMap<String, ResourceLimits>,
}
impl ResourceManager {
pub fn new(system_resources: SystemResources) -> Self {
Self {
system_resources,
allocated_resources: HashMap::new(),
}
}
/// Check if resources are available for a new container
pub fn can_allocate(&self, requested: &ResourceLimits) -> Result<bool> {
let mut used_cpu = 0.0;
let mut used_memory = 0;
for limits in self.allocated_resources.values() {
used_cpu += limits.cpu_cores;
used_memory += limits.memory_mb;
}
let available_cpu = self.system_resources.total_cpu_cores as f64 - used_cpu;
let available_memory = self.system_resources.total_memory_mb - used_memory;
if requested.cpu_cores > available_cpu {
return Ok(false);
}
if requested.memory_mb > available_memory {
return Ok(false);
}
Ok(true)
}
/// Allocate resources for a container
pub fn allocate(&mut self, container_id: String, limits: ResourceLimits) -> Result<()> {
if !self.can_allocate(&limits)? {
return Err(anyhow::anyhow!("Insufficient resources"));
}
self.allocated_resources.insert(container_id, limits);
info!("Allocated resources for container");
Ok(())
}
/// Release resources for a container
pub fn release(&mut self, container_id: &str) {
self.allocated_resources.remove(container_id);
info!("Released resources for container: {}", container_id);
}
/// Get current resource usage
pub fn get_usage(&self) -> (f64, u32) {
let cpu: f64 = self.allocated_resources.values().map(|r| r.cpu_cores).sum();
let memory: u32 = self.allocated_resources.values().map(|r| r.memory_mb).sum();
(cpu, memory)
}
/// Optimize resource allocation (reduce limits for low-priority containers)
pub fn optimize_allocation(&mut self) {
// TODO: Implement dynamic resource adjustment based on usage
info!("Optimizing resource allocation");
}
}