1pub use crate::dtype::{EmbeddingDtype, dtype_of_array, read_subset_as_f32};
4pub use crate::metric::{Metric, calculate_distances, negative_squared_distances};
5
6const DEFAULT_MEMORY_LIMIT_RAM_FRACTION: f64 = 0.8;
8
9pub fn default_memory_limit_bytes() -> usize {
13 let system = sysinfo::System::new_with_specifics(
14 sysinfo::RefreshKind::nothing()
15 .with_memory(sysinfo::MemoryRefreshKind::nothing().with_ram()),
16 );
17 let total_ram_bytes = match system.cgroup_limits() {
18 Some(limits) => limits.total_memory,
19 None => {
20 if cfg!(target_os = "linux") {
23 log::warn!(
24 "failed to read this Linux host's cgroup memory limit, \
25 falling back to its total RAM"
26 );
27 }
28 system.total_memory()
29 }
30 };
31 default_memory_limit_bytes_for(total_ram_bytes)
32}
33
34fn default_memory_limit_bytes_for(total_ram_bytes: u64) -> usize {
37 const GIB: f64 = (1024 * 1024 * 1024) as f64;
38 let total_gib = total_ram_bytes as f64 / GIB;
39 let default_gib = (total_gib * DEFAULT_MEMORY_LIMIT_RAM_FRACTION).floor();
40 (default_gib * GIB) as usize
41}
42
43#[cfg(test)]
44#[path = "utests/utils.rs"]
45mod tests;