sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use crate::ProcessRefreshKind;
use crate::sys::cpu::Query;

use super::ProcessInner;

pub(super) struct GpuInfo {
    pub(super) gpu_usage: Option<f32>,
    pub(super) gpu_memory: Option<u64>,
    pub(super) percent_key: Option<String>,
    pub(super) memory_key: Option<String>,
}

impl GpuInfo {
    pub(super) fn new() -> Self {
        Self {
            gpu_usage: None,
            gpu_memory: None,
            percent_key: None,
            memory_key: None,
        }
    }
}

pub(super) fn refresh_gpu(
    p: &mut ProcessInner,
    refresh_kind: ProcessRefreshKind,
    gpu_query: &mut Option<Query>,
) {
    if !refresh_kind.gpu_usage() && !refresh_kind.gpu_memory() {
        return;
    }
    if gpu_query.is_none() {
        *gpu_query = Query::new(false);
    }
    if let Some(gpu_query) = gpu_query {
        if refresh_kind.gpu_usage() {
            if p.gpu_info.percent_key.is_none() {
                let key = format!(r"\GPU Engine(pid_{}_*)\Utilization Percentage", p.pid.0);
                if gpu_query.add_english_counter(&key) {
                    p.gpu_info.percent_key = Some(key);
                }
            // Skip the first query because the counter needs time to settle.
            } else if let Some(ref key) = p.gpu_info.percent_key {
                p.gpu_info.gpu_usage = gpu_query.get_array(key).map(|arr| arr.into_iter().sum());
            }
        }

        if refresh_kind.gpu_memory() {
            if p.gpu_info.memory_key.is_none() {
                let key = format!(r"\GPU Process Memory(pid_{}_*)\Local Usage", p.pid.0);
                if gpu_query.add_english_counter(&key) {
                    p.gpu_info.memory_key = Some(key);
                }
            // Skip the first query because the counter needs time to settle.
            } else if let Some(ref key) = p.gpu_info.memory_key {
                p.gpu_info.gpu_memory = gpu_query.get(key).map(|v| v as u64);
            }
        }
    }
}