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
CpuInfostruct. - Hybrid Architecture Support: Differentiates between performance and efficiency cores.
- Thread Affinity: (Via the
affinitymodule) Pin threads to specific logical or physical cores. - Thread Priority: (Via the
affinitymodule) 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
Structs§
- Affinity
Mask - A cross-platform CPU affinity mask representing a set of logical processors.
- Cache
Info - Represents detailed information about a specific CPU cache.
- Core
Info - 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.
- Socket
Info - Represents information about a single CPU socket (physical CPU package).
Enums§
- Cache
Level - Represents the hierarchical level of a CPU cache within the memory system.
- Cache
Type - Describes the designated purpose or type of content a CPU cache holds.
- Core
Type - 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-cpuscrate. - Scheduling
Policy - Represents a Linux scheduling policy and its associated parameter.
- Thread
Priority - 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
trueif 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
Resulttype forgdt-cpusoperations.