diff --git a/src/kira_sysinfo.rs b/src/kira_sysinfo.rs new file mode 100644 index 0000000..0d2ae1e --- /dev/null +++ b/src/kira_sysinfo.rs @@ -0,0 +1,97 @@ +// +// Copyright (C) <2026> + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +/* + This file contains functions to query system information. +*/ + +use std::collections::HashMap; +use std::fs; + +/// +/// vis_vram_total - size of vram in bytes +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct GpuInfo { + pub id: u8, + pub name: String, + pub vis_vram_total: u64, +} + +/// See /proc/meminfo output format and units (kB) +pub fn get_meminfo() -> HashMap { + let mem_info_str = fs::read_to_string("/proc/meminfo").unwrap(); + + mem_info_str + .lines() + .filter_map(|l| { + l.split_once(':').and_then(|(k, v)| { + u64::from_str_radix(v.trim_end_matches("kB").trim(), 10) + .ok() + .and_then(|v_num| Some((k.to_string(), v_num))) + }) + }) + .collect() +} + +// cat /sys/class/drm/card1/device/mem_info_vis_vram_total +pub fn get_gpus_list() -> Vec { + let drm_devs_dirs: Vec<(String, u8)> = fs::read_dir("/sys/class/drm/") + .unwrap() + .filter_map(|maybe_entry| { + maybe_entry.ok().and_then(|entry| { + entry.file_type().ok().and_then(|ft| { + if ft.is_symlink() || ft.is_dir() { + entry.file_name().into_string().ok() + } else { + None + } + }) + }) + }) + .filter(|f_name| f_name.len() == 5 && f_name.starts_with("card")) + .filter_map(|dir_name| { + dir_name.split_once("card").and_then(|name_touple| { + u8::from_str_radix(name_touple.1, 10) + .ok() + .and_then(|gpu_id| Some((dir_name.clone(), gpu_id))) + }) + }) + .collect(); + + let res: Vec = drm_devs_dirs + .iter() + .filter_map(|(d, id)| { + fs::read_to_string(format!( + "/sys/class/drm/{}/device/mem_info_vis_vram_total", + d + )) + .ok() + .and_then(|mem_str| { + u64::from_str_radix(mem_str.trim(), 10) + .ok() + .and_then(|mem_size| { + Some(GpuInfo { + id: *id, + name: d.clone(), + vis_vram_total: mem_size, + }) + }) + }) + }) + .collect(); + + res +}