Crate gdt_cpus

Crate gdt_cpus 

Source
Expand description

GDT-CPUs: Game Developer’s Toolkit for CPU Management

This crate provides detailed CPU information and thread management capabilities specifically designed for game developers. It aims to simplify tasks such as querying CPU features, understanding core architecture (including hybrid designs like P-cores and E-cores), and managing thread affinity and priority.

§Key Features

  • Detailed CPU Information: Access vendor, model name, supported instruction sets (e.g., AVX2, NEON), cache details, and core topology via the CpuInfo struct.
  • Hybrid Architecture Support: Differentiates between performance and efficiency cores.
  • Thread Affinity: (Via the affinity module) Pin threads to specific logical or physical cores.
  • Thread Priority: (Via the affinity module) Set thread priorities for different operating systems.
  • Platform Abstraction: Provides a consistent API across Windows, macOS, and Linux.
  • Lazy Initialization: CPU information is detected once and cached globally.

§Getting Started

The primary way to get CPU information is through the cpu_info() function:

use gdt_cpus::{cpu_info, CpuInfo, Error, CpuFeatures};

fn main() -> Result<(), Error> {
    let info = cpu_info()?;

    println!("CPU Vendor: {}", info.vendor);
    println!("CPU Model: {}", info.model_name);
    println!("Total Physical Cores: {}", info.total_physical_cores);
    println!("Total Logical Processors: {}", info.total_logical_processors);

    if info.is_hybrid() {
        println!("This is a hybrid CPU with:");
        println!("  Performance Cores: {}", info.total_performance_cores);
        println!("  Efficiency Cores: {}", info.total_efficiency_cores);
    }

    #[cfg(target_arch = "x86_64")]
    if info.features.contains(CpuFeatures::AVX2) {
        println!("AVX2 is supported!");
    }
    #[cfg(target_arch = "aarch64")]
    if info.features.contains(CpuFeatures::NEON) {
        println!("NEON is supported!");
    }

    // You can also use helper functions:
    let phys_cores = gdt_cpus::num_physical_cores()?;
    println!("Physical cores (via helper): {}", phys_cores);

    Ok(())
}

§Cargo Features

  • serde: Enables serialization and deserialization of CPU information structures (like CpuInfo, CoreInfo, etc.) using the Serde library.

Structs§

AffinityMask
A cross-platform CPU affinity mask representing a set of logical processors.
CacheInfo
Represents detailed information about a specific CPU cache.
CoreInfo
Represents detailed information about a single physical CPU core.
CpuFeatures
Represents a set of CPU features and instruction set extensions available on an x86_64 architecture.
CpuInfo
Provides a comprehensive overview of the system’s CPU capabilities.
SocketInfo
Represents information about a single CPU socket (physical CPU package).

Enums§

CacheLevel
Represents the hierarchical level of a CPU cache within the memory system.
CacheType
Describes the designated purpose or type of content a CPU cache holds.
CoreType
Describes the type or class of a CPU core, particularly relevant for hybrid architectures.
Error
The primary error enum for all operations within the gdt-cpus crate.
SchedulingPolicy
Represents a Linux scheduling policy and its associated parameter.
ThreadPriority
Represents different priority levels that can be assigned to a thread.
Vendor
Represents the manufacturer or vendor of a CPU.

Functions§

cpu_info
Retrieves a static reference to the globally detected CPU information.
get_scheduling_policies
Retrieves a static slice representing the system’s or default scheduling policies.
is_hybrid
Returns true if the system CPU has a hybrid architecture (e.g., both P-cores and E-cores).
num_efficiency_cores
Returns the total number of efficiency-type physical cores in the system.
num_logical_cores
Returns the total number of logical cores (hardware threads) in the system.
num_performance_cores
Returns the total number of performance-type physical cores in the system.
num_physical_cores
Returns the total number of physical cores in the system.
pin_thread_to_core
Pins the current thread to a specific logical core ID.
set_thread_affinity
set_thread_priority
Sets the priority of the current thread.

Type Aliases§

Result
A specialized Result type for gdt-cpus operations.