Skip to main content

ravenrs_ai/
machine.rs

1//! What a machine has room for.
2
3use std::path::Path;
4
5use sysinfo::{Disks, MemoryRefreshKind, RefreshKind, System};
6
7/// What a machine has, for deciding which models to offer.
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub struct Machine {
10    /// All its memory, in bytes.
11    pub memory: u64,
12    /// The free space on the disk that would hold the model, in bytes.
13    pub free_disk: u64,
14}
15
16impl Machine {
17    /// This machine, with the disk that holds `folder`, which may not exist yet. A disk
18    /// it cannot find counts as full, so nothing new is offered.
19    pub fn detect(folder: &Path) -> Self {
20        let system = System::new_with_specifics(
21            RefreshKind::nothing().with_memory(MemoryRefreshKind::nothing().with_ram()),
22        );
23        Self { memory: system.total_memory(), free_disk: free_disk(folder) }
24    }
25}
26
27/// The free space of the disk mounted deepest above `folder`.
28fn free_disk(folder: &Path) -> u64 {
29    let absolute = std::path::absolute(folder).unwrap_or_else(|_| folder.to_path_buf());
30    let disks = Disks::new_with_refreshed_list();
31    disks
32        .list()
33        .iter()
34        .filter(|disk| absolute.starts_with(disk.mount_point()))
35        .max_by_key(|disk| disk.mount_point().as_os_str().len())
36        .map_or(0, sysinfo::Disk::available_space)
37}