gpu_telemetry 0.2.1

High-level GPU telemetry background logger
Documentation
//! GPU system implementations.
//!
//! The backend for the monitor is implemented here. Including Vulkan and DirectX.

use crate::{
    errors::{Error, ErrorKind},
    monitor::telemetry::GpuSample,
};
use std::time::Duration;

#[cfg(target_os = "windows")]
mod windows;

#[cfg(any(target_os = "windows", target_os = "linux"))]
mod vulkan;

/// GPU telemetry backend.
///
/// Generic over all devices. To implement custom monitor backends,
/// you must first implement this trait.
pub trait GpuTelemetry: Sized {
    /// Instantiates a new backend provided the requested device (probably 0).
    fn new(adapter: usize) -> Result<Self, Error>;

    /// Waits at most the provided interval, potentially returning early if an event occurs.
    fn wait_at_most(&self, interval: Duration) -> Result<(), Error>;

    /// Queries the GPU for a telemetry sample.
    fn query(&self) -> Result<GpuSample, Error>;
}

#[cfg(target_os = "windows")]
pub use windows::DxGpuQuery;

#[cfg(any(target_os = "windows", target_os = "linux"))]
pub use vulkan::VulkanGpuQuery;

/// A no-op fallback backend.
pub struct NopQuery {}

impl GpuTelemetry for NopQuery {
    fn new(_adapter: usize) -> Result<Self, Error> {
        Ok(Self {})
    }

    fn query(&self) -> Result<GpuSample, Error> {
        Err(Error {
            msg: "using nop backend",
            kind: ErrorKind::UnsupportedFeature,
        })
    }

    fn wait_at_most(&self, _interval: Duration) -> Result<(), Error> {
        Err(Error {
            msg: "using nop backend",
            kind: ErrorKind::UnsupportedFeature,
        })
    }
}