1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! GDT-CPUs: Game Developer's Toolkit for CPU Management
//!
//! This crate provides detailed CPU information and thread management capabilities
//! specifically designed for game developers: CPU topology (including hybrid
//! P/E/LP-E core kinds and L3 cache domains), thread affinity and thread priority.
//!
//! # Key Features
//!
//! * **Flat topology model**: one [`Lp`] record per logical processor plus a
//! first-class [`L3Domain`] table - chiplet CPUs (multiple CCDs per socket)
//! and hybrid designs are represented faithfully.
//! * **Core kinds**: [`CoreKind::Performance`] / [`CoreKind::Efficiency`] /
//! [`CoreKind::LpEfficiency`] - modern silicon ships more than two kinds.
//! * **L3 cache domains**: group cooperating threads by shared L3
//! ([`CpuInfo::l3_domain_mask`]) - cross-domain latency is the real cliff.
//! * **Thread Affinity**: pin threads to logical cores or sets of them.
//! * **Thread Priority**: 7 portable levels mapped to each OS's scheduler.
//! * **No global state**: [`CpuInfo::detect()`] returns a plain value you own.
//!
//! # Getting Started
//!
//! ```
//! use gdt_cpus::CpuInfo;
//!
//! fn main() -> Result<(), gdt_cpus::Error> {
//! let info = CpuInfo::detect()?;
//!
//! println!("CPU: {} ({})", info.model_name, info.vendor);
//! println!("{} cores / {} threads", info.core_count, info.lps.len());
//!
//! if info.is_hybrid() {
//! println!("hybrid: {}P + {}E + {}LP-E",
//! info.num_performance_cores(),
//! info.num_efficiency_cores(),
//! info.num_lp_efficiency_cores());
//! }
//!
//! for (i, d) in info.l3_domains.iter().enumerate() {
//! println!("L3 domain {}: {} MiB, {} cores", i, d.size_bytes >> 20, d.core_count);
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! # Thread placement: what goes where (rules of thumb)
//!
//! Two independent levers exist on Linux/Windows: **placement** (affinity -
//! WHERE a thread may run) and **priority** (WHO wins when threads compete).
//! On macOS QoS fuses both (and Apple Silicon ignores pinning entirely), so
//! treat affinity as best-effort and priority as the portable lever.
//!
//! | Work | Cores | Priority |
//! |---|---|---|
//! | Main / render thread | best Performance core (highest [`Lp::perf_hint`], `smt_index == 0`) | `AboveNormal`-`Highest` |
//! | Simulation / job workers | one per remaining Performance core primary; keep cooperating sets inside ONE L3 domain | `Normal` |
//! | Audio / haptics feeder | any Performance core - do NOT pin it onto the busiest one | `TimeCritical` (dedicate the thread; on macOS it permanently leaves the QoS system). For hard deadlines, [`promote_thread_to_realtime`] |
//! | Asset streaming / decompression | Efficiency cores **if present** - the mask CAN be empty, always fall back to Performance | `BelowNormal` |
//! | Shader/PSO compilation, navmesh & lighting bakes, batch processing | wherever there's room - these want throughput, not latency | `Lowest` |
//! | Telemetry, autosave compression, platform callbacks | LpEfficiency island if present (trickle work only - these islands often have weak interconnects), else unpinned | `Background` |
//!
//! Further rules:
//!
//! * **Don't pin everything.** Pinning removes the scheduler's freedom; it
//! pays off only for threads with a real reason - latency (audio, render)
//! or cache locality (a cooperating producer/consumer set). Leave the
//! rest soft. On Windows prefer [`set_thread_soft_affinity`] (CPU Sets) -
//! the scheduler keeps an escape hatch.
//! * **One heavy thread per physical core**: build worker pools from
//! [`CpuInfo::primary_thread_mask`] (`smt_index == 0`), not from all LPs -
//! two heavy threads on SMT siblings share one core's execution
//! resources. Siblings are fine for light helpers.
//! * **Group by L3, not by core id**: cross-L3-domain communication costs
//! multiples of in-domain (3.6× measured on a dual-CCD 5950X - run
//! `examples/l3_domains.rs`). Place cooperating threads with
//! [`CpuInfo::l3_domain_mask`]; never assume core ids imply locality.
//! * **Within a kind, rank with [`Lp::perf_hint`]** - chips ship Performance
//! tiers spanning several frequency bins (ARM prime-vs-mid, Intel favored
//! cores). Compare it only within the same detected machine and kind; the
//! source scale differs per OS. Equal hints = indistinguishable, don't
//! invent an order.
//! * **Kinds are classes, not guarantees**: a machine may have NO
//! Efficiency cores (only P + LP-E), or nothing but Performance. Write
//! fallbacks: `efficiency_core_mask()` empty -> use Performance at lower
//! priority.
//! * **Check what priority can deliver**: on a locked-down Linux box
//! (no rtkit, default rlimits) every level above `Normal` silently
//! resolves to `Normal`. [`priority_capabilities`] predicts this up
//! front; [`promote_thread_to_realtime`] is the explicit escape hatch
//! for the one thread with a hard deadline.
//!
//! ```no_run
//! use gdt_cpus::{CoreKind, CpuInfo, ThreadPriority, pin_thread_to_core, set_thread_priority};
//!
//! # fn main() -> Result<(), gdt_cpus::Error> {
//! let info = CpuInfo::detect()?;
//!
//! // Best Performance-core primaries first - render thread gets the top one.
//! let mut p_cores: Vec<_> = info.lps.iter()
//! .filter(|lp| lp.kind == CoreKind::Performance && lp.smt_index == 0)
//! .collect();
//!
//! p_cores.sort_by_key(|lp| std::cmp::Reverse(lp.perf_hint));
//!
//! if let Some(best) = p_cores.first() {
//! let _ = pin_thread_to_core(best.os_id as usize); // macOS: Unsupported - fine
//! let applied = set_thread_priority(ThreadPriority::Highest)?;
//!
//! eprintln!("render priority: {applied}");
//! }
//!
//! // Background telemetry: LP-E island when it exists, otherwise just priority.
//! let smol = info.kind_mask(CoreKind::LpEfficiency);
//!
//! if !smol.is_empty() {
//! let _ = gdt_cpus::set_thread_affinity(&smol);
//! }
//!
//! let _applied = set_thread_priority(ThreadPriority::Background)?;
//!
//! # Ok(())
//! # }
//! ```
//!
//! # Cargo Features
//!
//! * `rtkit` *(default)*: on Linux, negotiate priority through rtkit and
//! the xdg realtime portal (hand-rolled minimal D-Bus client, no extra
//! dependencies) when direct syscalls are denied. Opt out with
//! `default-features = false`.
//! * `serde`: serialization for the CPU information structures.
// Modules
// Re-exports - Public API
pub use *;
pub use AffinityMask;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
/// Total number of physical cores (SMT siblings counted once).
///
/// Convenience detection path; prefer holding a [`CpuInfo`] from
/// [`CpuInfo::detect()`] and reading `core_count`.
/// Total number of logical processors (hardware threads).
/// Number of physical cores classified as Performance.
///
/// Equals `num_physical_cores()` on homogeneous machines (the classification
/// invariant: homogeneous means all Performance).
/// Number of physical cores classified as Efficiency (0 on non-hybrid machines).
/// Number of physical cores classified as LpEfficiency (0 on non-hybrid machines).
/// `true` if more than one core kind is present (P/E/LP-E).