alith_devices/devices/
ram.rs1use sysinfo;
2
3#[derive(Debug, Clone)]
4pub struct RamConfig {
5 pub total_ram_bytes: u64,
6 pub available_ram_bytes: u64,
7 pub used_ram_bytes: u64,
8 pub use_ram_bytes: u64,
9 pub use_percentage: f32,
10}
11
12impl Default for RamConfig {
13 fn default() -> Self {
14 let mut sys = sysinfo::System::new_all();
15 sys.refresh_all();
16 Self {
17 total_ram_bytes: sys.total_memory(),
18 available_ram_bytes: sys.available_memory(),
19 used_ram_bytes: sys.used_memory(),
20 use_ram_bytes: 0,
21 use_percentage: 0.70,
22 }
23 }
24}
25
26impl RamConfig {
27 pub(crate) fn initialize(&mut self, error_on_config_issue: bool) -> crate::Result<()> {
28 if self.use_ram_bytes == 0 {
29 self.use_ram_bytes = self.percentage_of_total(error_on_config_issue)?;
30 } else if self.use_ram_bytes >= self.likely_ram_bytes() {
31 if error_on_config_issue {
32 crate::bail!(
33 "use_ram_bytes {:.2} is greater than the available system RAM {:.2}",
34 (self.use_ram_bytes as f64) / 1_073_741_824.0,
35 (self.likely_ram_bytes() as f64) / 1_073_741_824.0
36 );
37 } else {
38 crate::warn!(
39 "use_ram_bytes {:.2} is greater than the available system RAM {:.2}. Falling back to percentage of total RAM",
40 (self.use_ram_bytes as f64) / 1_073_741_824.0,
41 (self.likely_ram_bytes() as f64) / 1_073_741_824.0
42 );
43 self.use_ram_bytes = self.percentage_of_total(error_on_config_issue)?;
44 }
45 }
46 Ok(())
47 }
48
49 pub(crate) fn likely_ram_bytes(&self) -> u64 {
50 std::cmp::min(
51 self.total_ram_bytes - self.used_ram_bytes,
52 self.available_ram_bytes,
53 )
54 }
55
56 fn percentage_of_total(&mut self, error_on_config_issue: bool) -> crate::Result<u64> {
57 if self.use_percentage > 1.0 || self.use_percentage < 0.0 {
58 if error_on_config_issue {
59 crate::bail!(
60 "Percentage of total RAM must be between 0.0 and 1.0. use_percentage: {:.2}",
61 self.use_percentage
62 );
63 } else {
64 crate::warn!(
65 "Percentage of total RAM must be between 0.0 and 1.0. use_percentage: {}. Falling back to default value of 0.70",
66 self.use_percentage
67 );
68 self.use_percentage = 0.70;
69 }
70 }
71
72 Ok((self.likely_ram_bytes() as f32 * self.use_percentage) as u64)
73 }
74}
75
76impl std::fmt::Display for RamConfig {
77 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78 writeln!(f, "RamConfig:")?;
79 crate::i_nlns(
80 f,
81 &[
82 format_args!(
83 "Total system RAM: {:.2} GB",
84 (self.total_ram_bytes as f64) / 1_073_741_824.0
85 ),
86 format_args!(
87 "Available system RAM: {:.2} GB",
88 (self.available_ram_bytes as f64) / 1_073_741_824.0
89 ),
90 format_args!(
91 "Specified RAM for Inference: {:.2} GB",
92 (self.use_ram_bytes as f64) / 1_073_741_824.0
93 ),
94 ],
95 )
96 }
97}