sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use std::ffi::c_void;
use std::mem::MaybeUninit;
use std::ptr::null_mut;

use super::super::super::sysfs::convert_to_string;
use super::super::types::*;
use super::Vulkan;
use crate::{Gpu, PCI};

impl Vulkan {
    pub(crate) fn get_vulkan_info(&self, gpus: &mut [Gpu]) {
        unsafe {
            let mut device_count: u32 = 0;
            (self.enumerate_physical_devices)(self.instance, &mut device_count, null_mut());

            if device_count == 0 {
                return;
            }
            let mut devices = vec![null_mut(); device_count as usize];
            (self.enumerate_physical_devices)(
                self.instance,
                &mut device_count,
                devices.as_mut_ptr(),
            );

            for device in devices {
                let mut pci_properties = VkPhysicalDevicePCIBusInfoPropertiesEXT {
                    s_type: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT,
                    p_next: null_mut(),
                    pci_domain: 0,
                    pci_bus: 0,
                    pci_device: 0,
                    pci_function: 0,
                };

                let mut props2 = VkPhysicalDeviceProperties2 {
                    s_type: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
                    p_next: &mut pci_properties as *mut _ as *mut c_void,
                    properties: std::mem::zeroed(),
                };

                (self.get_physical_device_properties_2_khr)(device, &mut props2);

                // Format the extracted PCI string explicitly to match the `NVML`/`sys/drm` format.
                let pci = PCI {
                    domain: pci_properties.pci_domain,
                    bus: pci_properties.pci_bus,
                    device: pci_properties.pci_device,
                    function: pci_properties.pci_function,
                };

                // Discover no new GPUs here; only add extra information to existing ones.
                let Some(gpu) = gpus
                    .iter_mut()
                    .find(|gpu| !gpu.inner.updated && gpu.inner.pci == pci)
                else {
                    continue;
                };

                if gpu.inner.model.is_none() {
                    gpu.inner.model = convert_to_string(&props2.properties.device_name);
                }

                if let Some(get_physical_device_memory_properties_2) =
                    self.get_physical_device_memory_properties_2
                {
                    let mut budget_properties = VkPhysicalDeviceMemoryBudgetPropertiesEXT {
                        s_type: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT,
                        p_next: null_mut(),
                        heap_budget: [0; VK_MAX_MEMORY_HEAPS],
                        heap_usage: [0; VK_MAX_MEMORY_HEAPS],
                    };
                    let mut mem_props2 = VkPhysicalDeviceMemoryProperties2 {
                        s_type: VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2,
                        p_next: &mut budget_properties as *mut _ as *mut c_void,
                        memory_properties: std::mem::zeroed(),
                    };
                    get_physical_device_memory_properties_2(device, &mut mem_props2);

                    let heap_count = mem_props2.memory_properties.memory_heap_count as usize;

                    let mut total_memory: u64 = 0;
                    let mut used_memory: u64 = 0;
                    let mut found_vram = false;
                    for h in 0..heap_count {
                        let heap = &mem_props2.memory_properties.memory_heaps[h];
                        let is_vram = (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0;
                        // Embedded GPUs do not always have their own memory and instead use the system's.
                        if !is_vram {
                            continue;
                        }

                        found_vram = true;
                        total_memory = total_memory.saturating_add(heap.size);
                        used_memory = used_memory.saturating_add(budget_properties.heap_usage[h]);
                    }
                    if found_vram {
                        gpu.inner.total_memory = Some(total_memory);
                        gpu.inner.used_memory = Some(used_memory);
                    } else {
                        gpu.inner.total_memory = None;
                        gpu.inner.used_memory = None;
                    }
                } else {
                    // When used memory is not accessible.
                    let mut mem_props = MaybeUninit::<VkPhysicalDeviceMemoryProperties>::uninit();
                    (self.get_physical_device_memory_properties)(device, mem_props.as_mut_ptr());
                    let mem_props = mem_props.assume_init();

                    let mut total_memory: u64 = 0;
                    let mut found_total = false;
                    #[allow(clippy::needless_range_loop)]
                    for h in 0..(mem_props.memory_heap_count as usize) {
                        let heap = &mem_props.memory_heaps[h];
                        let is_vram = (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0;
                        // Embedded GPUs do not always have their own memory and instead use the system's.
                        if !is_vram {
                            continue;
                        }
                        found_total = true;
                        total_memory = total_memory.saturating_add(mem_props.memory_heaps[h].size);
                    }
                    if found_total {
                        gpu.inner.total_memory = Some(total_memory);
                    } else {
                        gpu.inner.total_memory = None;
                    }
                }
                gpu.inner.updated = true;
            }
        }
    }
}