gdt-cpus 0.2606.1

Game Developer's Toolkit for CPU Management
Documentation
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! Linux-specific thread affinity and priority management.
//!
//! This module provides functions to control thread affinity and thread priority
//! (via `nice` values under `SCHED_OTHER`) on Linux systems.
//!
//! **Important Notes for Linux:**
//! - **Thread Affinity:** Uses `sched_setaffinity` to restrict the current thread
//!   to a set of logical cores specified by an [`AffinityMask`]. The mask indices
//!   are mapped to OS-level logical processor IDs.
//! - **Thread Priority:** adjusts the thread's `nice` value with `setpriority`.
//!   Lower `nice` values (e.g., -20) mean higher priority. Negative values need
//!   privilege (`CAP_SYS_NICE` or a raised `RLIMIT_NICE`); when denied, the
//!   cascade asks rtkit for the negative nice (feature `rtkit`) and finally,
//!   if no broker delivers it, keeps the level the thread already has (reported
//!   as data, never an error). Real-time scheduling (`SCHED_RR`) is reachable
//!   only through the consent API ([`crate::promote_thread_to_realtime`]).
//!
//! The main functions provided are [`set_thread_affinity`] and [`set_thread_priority`].

use libc::{SYS_gettid, c_int, syscall};

use crate::{
    AffinityMask, AppliedPriority, BrokerError, Error, FallbackReason, Grant, Mechanism,
    MechanismPolicy, Result, ThreadPriority,
    platform::linux::scheduling_policy::{level_for_nice, nice_for},
};

/// Sets the CPU affinity of the current thread on Linux.
///
/// This function restricts the current thread to execute only on the logical
/// cores specified in the [`AffinityMask`]. It uses `libc::sched_setaffinity`
/// to apply the affinity mask.
///
/// # Arguments
///
/// * `mask`: An [`AffinityMask`] specifying which logical cores the thread may run on.
///   Core indices in the mask are mapped to OS-level logical processor IDs.
///
/// # Returns
///
/// - `Ok(())` if the affinity was successfully set.
/// - `Error::Affinity` if the mask is empty, no valid cores could be added, or
///   `sched_setaffinity` fails.
/// - Errors from [`crate::CpuInfo::detect()`] if CPU information cannot be retrieved.
///
/// # Safety
///
/// This function uses `unsafe` blocks for FFI calls to `libc::sched_setaffinity`,
/// `libc::CPU_ZERO`, and `libc::CPU_SET`. These are standard Linux system calls
/// and are safe when provided with valid arguments (a valid `cpu_set_t` and a TID of 0
/// for the current thread).
pub(crate) fn set_thread_affinity(mask: &AffinityMask) -> Result<()> {
    if mask.is_empty() {
        return Err(Error::Affinity(
            "Cannot set thread affinity with an empty mask".to_string(),
        ));
    }

    // NOTE: no topology lookup here - validating against detected LPs forced a
    // full (cached) detection inside an affinity call. The kernel validates
    // membership itself and returns EINVAL for CPUs outside the allowed set;
    // we only bound-check against cpu_set_t's capacity.
    let max_cpus = libc::CPU_SETSIZE as usize;

    // SAFETY: Zero-initializes the cpu_set_t structure.
    // cpu_set_t is POD without non-zeroable invariants; zeroing yields a valid struct.
    let mut cpuset: libc::cpu_set_t = unsafe { std::mem::zeroed() };
    // SAFETY: CPU_ZERO is safe to call with a valid cpu_set_t pointer.
    unsafe {
        libc::CPU_ZERO(&mut cpuset);
    }

    // Set all cores in the mask
    for core_idx in mask.iter() {
        if core_idx < max_cpus {
            // SAFETY: CPU_SET is safe with a valid cpu_set_t pointer and valid CPU index.
            unsafe {
                libc::CPU_SET(core_idx, &mut cpuset);
            }
        } else {
            return Err(Error::Affinity(format!(
                "OS core {} exceeds CPU_SETSIZE {}",
                core_idx, max_cpus
            )));
        }
    }

    // SAFETY: sched_setaffinity is a system call that sets the CPU affinity for the calling thread.
    // pid == 0 means the calling thread, and the size of the cpu_set_t is passed.
    let res =
        unsafe { libc::sched_setaffinity(0, std::mem::size_of::<libc::cpu_set_t>(), &cpuset) };

    if res == -1 {
        let err = std::io::Error::last_os_error();
        Err(map_sched_setaffinity_error(err))
    } else {
        Ok(())
    }
}

fn map_sched_setaffinity_error(err: std::io::Error) -> Error {
    match err.raw_os_error() {
        Some(libc::EINVAL) => Error::InvalidParameter(format!(
            "Invalid affinity mask for sched_setaffinity: {}",
            err
        )),
        _ => Error::Affinity(format!("sched_setaffinity failed: {}", err)),
    }
}

/// Reads the current thread's CPU affinity into an [`AffinityMask`] via
/// `sched_getaffinity(0)`.
///
/// # Returns
///
/// - `Ok(mask)` with one bit set per OS LP the thread may run on.
/// - `Error::Affinity` if `sched_getaffinity` fails.
pub(crate) fn current_affinity() -> Result<AffinityMask> {
    // SAFETY: cpu_set_t is POD; zeroing yields a valid (empty) set.
    let mut cpuset: libc::cpu_set_t = unsafe { std::mem::zeroed() };

    // SAFETY: sched_getaffinity fills `cpuset` for the calling thread (pid 0).
    let res =
        unsafe { libc::sched_getaffinity(0, std::mem::size_of::<libc::cpu_set_t>(), &mut cpuset) };
    if res == -1 {
        let err = std::io::Error::last_os_error();
        return Err(Error::Affinity(format!("sched_getaffinity failed: {err}")));
    }

    let mut mask = AffinityMask::empty();
    for cpu in 0..(libc::CPU_SETSIZE as usize) {
        // SAFETY: CPU_ISSET is safe with a valid cpu_set_t pointer and an index
        // below CPU_SETSIZE.
        if unsafe { libc::CPU_ISSET(cpu, &cpuset) } {
            mask.add(cpu);
        }
    }

    Ok(mask)
}

/// Returns the current thread's kernel TID (the id `setpriority` and rtkit
/// address threads by).
pub(crate) fn current_tid() -> Result<libc::id_t> {
    // SAFETY: syscall(SYS_gettid) is the standard way to get the current thread ID on Linux.
    // Returns -1 on error.
    let tid = unsafe { syscall(SYS_gettid) };
    if tid <= 0 {
        let err = std::io::Error::last_os_error();
        return Err(Error::SystemCall(format!(
            "Failed to get thread ID via gettid(): {}",
            err
        )));
    }

    Ok(tid as libc::id_t)
}

/// The current thread's `nice` value, via a raw `getpriority` syscall. The
/// kernel returns `20 - nice` (always positive, sidestepping the cooked
/// wrapper's `-1`/`errno` ambiguity) - decode it. Used to report the level a
/// thread ACTUALLY sits at when a priority request could not be applied.
///
/// # Errors
///
/// [`Error::SystemCall`] if `getpriority` fails (only a missing TID, which
/// cannot happen for the calling thread).
pub(crate) fn current_nice() -> Result<c_int> {
    let tid = current_tid()?;

    // SAFETY: raw getpriority for the current thread's TID; returns 20 - nice on
    // success, -1 (with errno) on failure.
    let rc = unsafe { libc::syscall(libc::SYS_getpriority, libc::PRIO_PROCESS, tid as c_int) };
    if rc < 0 {
        let err = std::io::Error::last_os_error();
        return Err(Error::SystemCall(format!(
            "getpriority for TID {}: {}",
            tid, err
        )));
    }

    Ok(20 - rc as c_int)
}

/// Sets the `nice` value for the current thread on Linux - one direct
/// `setpriority` call, no fallback (the cascade lives in
/// [`set_thread_priority`]).
///
/// A lower `nice` value means higher priority. Raising the value (lowering
/// priority) always succeeds; lowering it requires `CAP_SYS_NICE` or a raised
/// `RLIMIT_NICE`. NOTE(linux): the errno for an unprivileged negative-nice
/// attempt is `EACCES`, not `EPERM` (see setpriority(2)) - callers must match
/// both. The check compares against the thread's CURRENT nice, so even
/// `nice(0)` can be denied for a thread sitting at a positive value (the
/// one-way ratchet) - the rtkit step of the cascade covers that case too.
///
/// # Safety
///
/// Uses `unsafe` for `syscall(SYS_gettid)` and `libc::setpriority` - standard
/// Linux system calls, safe with a valid TID and an in-range nice value.
fn set_thread_nice_value(nice_value: c_int) -> Result<()> {
    let tid = current_tid()?;

    // SAFETY: setpriority is used to set the nice value for a specific thread (using tid).
    // tid is a valid thread ID, and nice_value is the expected c_int value.
    let res = unsafe { libc::setpriority(libc::PRIO_PROCESS, tid, nice_value) };

    if res == -1 {
        let err = std::io::Error::last_os_error();
        match err.raw_os_error() {
            Some(libc::EACCES | libc::EPERM) => Err(Error::PermissionDenied(format!(
                "Setting nice value {} for TID {}: {}",
                nice_value, tid, err
            ))),
            Some(libc::ESRCH) => Err(Error::NotFound(format!(
                "Thread with TID {} not found for setpriority: {}",
                tid, err
            ))),
            Some(libc::EINVAL) => Err(Error::InvalidParameter(format!(
                "Invalid nice value {} for setpriority",
                nice_value
            ))),
            _ => Err(Error::SystemCall(format!(
                "setpriority failed for nice value {} for TID {}: {}",
                nice_value, tid, err
            ))),
        }
    } else {
        Ok(())
    }
}

/// Sets the priority of the current thread on Linux.
///
/// Every [`ThreadPriority`] level maps to a `nice` value under `SCHED_OTHER`
/// (table in `scheduling_policy.rs`) - the default path never requests a
/// real-time class. For values the kernel denies (negative nice without
/// privilege, or the one-way ratchet on a thread sitting above its target),
/// the cascade is:
///
/// 1. direct `setpriority` - works for non-negative values, and for negative
///    ones under `CAP_SYS_NICE` / a raised `RLIMIT_NICE`;
/// 2. rtkit `MakeThreadHighPriority` (feature `rtkit`, on by default) - the
///    desktop's broker for exactly this, leash-free (the thread stays
///    `SCHED_OTHER`, so rtkit's `RLIMIT_RTTIME`/SIGKILL enforcement never
///    applies); the request is clamped to the daemon's `MinNiceLevel`
///    (default -15);
/// 3. **keep the level you have** - a denied request leaves the thread's nice
///    untouched (a denied `setpriority` changes nothing), so the result reports
///    the level the thread ACTUALLY sits at with [`FallbackReason::NoBroker`].
///    A mere permission denial is therefore never an error - uniformly with the
///    other platforms' best-effort semantics. Only a genuine `setpriority`
///    failure (a kernel error other than the permission denial) is an error.
///
/// # Errors
///
/// [`Error::SystemCall`] / [`Error::InvalidParameter`] / [`Error::NotFound`]
/// for a `setpriority` failure that is NOT a permission denial. A permission
/// denial is reported in the returned [`AppliedPriority`] (degraded to the
/// current level), not as an error.
pub(crate) fn set_thread_priority(priority: ThreadPriority) -> Result<AppliedPriority> {
    // Map the portable level to its nice value (table in scheduling_policy.rs).
    let value = nice_for(priority);
    match set_thread_nice_value(value) {
        Ok(()) => Ok(AppliedPriority::new(
            priority,
            priority,
            Grant::Direct,
            Mechanism {
                policy: MechanismPolicy::Nice,
                value: value as i8,
            },
        )),
        Err(Error::PermissionDenied(_)) => {
            // Why the stronger request failed - refined by the rtkit attempt.
            // Defaults to NoBroker for the feature-off / no-tid paths.
            #[cfg_attr(not(feature = "rtkit"), allow(unused_mut))]
            let mut reason = FallbackReason::NoBroker;

            // The typed reason the broker REFUSED, when it answered with a D-Bus
            // ERROR - carried as data (NOT free text) so a caller can branch on it.
            #[cfg_attr(not(feature = "rtkit"), allow(unused_mut))]
            let mut broker_error: Option<BrokerError> = None;

            #[cfg(feature = "rtkit")]
            {
                if let Ok(tid) = current_tid() {
                    match crate::platform::linux::rtkit::try_high_priority(tid as u64, value) {
                        Ok(granted) if granted == value => {
                            return Ok(AppliedPriority::new(
                                priority,
                                priority,
                                Grant::Brokered,
                                Mechanism {
                                    policy: MechanismPolicy::Nice,
                                    value: granted as i8,
                                },
                            ));
                        }
                        // Granted, but weaker than asked (the broker's ceiling):
                        // keep the level, flag the clamp as a fall-short reason.
                        Ok(granted) => {
                            return Ok(AppliedPriority::new(
                                priority,
                                priority,
                                Grant::Brokered,
                                Mechanism {
                                    policy: MechanismPolicy::Nice,
                                    value: granted as i8,
                                },
                            )
                            .with_reason(FallbackReason::Clamped));
                        }
                        Err((r, be)) => {
                            reason = r;
                            broker_error = be;
                        }
                    }
                }
            }

            // No broker delivered it. Best-effort: a denied setpriority left the
            // thread's nice untouched, so report the level it ACTUALLY sits at
            // (NOT a hardcoded Normal - that could even DEMOTE a thread already
            // above normal) with WHY, as data. Never an error on a mere denial.
            let current = current_nice().unwrap_or(nice_for(ThreadPriority::Normal));

            // The mechanism is the nice the thread actually KEEPS; the structured
            // `reason` + `broker_error` carry the classification.
            let mut applied = AppliedPriority::new(
                priority,
                level_for_nice(current),
                Grant::Direct,
                Mechanism {
                    policy: MechanismPolicy::Nice,
                    value: current as i8,
                },
            )
            .with_reason(reason);

            if let Some(be) = broker_error {
                applied = applied.with_broker_error(be);
            }

            Ok(applied)
        }

        Err(other) => Err(other),
    }
}

/// Puts the current thread on `SCHED_RR` at `priority` - one direct
/// `pthread_setschedparam` call, no fallback. Used by the consent API
/// ([`crate::promote_thread_to_realtime`]); never by [`set_thread_priority`].
pub(crate) fn set_thread_realtime_rr(priority: c_int) -> Result<()> {
    // SAFETY: These functions are safe to call; SCHED_RR is a valid policy.
    // They return -1 on error (e.g., if the policy is not supported, which is unlikely for SCHED_RR).
    let (rt_min, rt_max) = unsafe {
        (
            libc::sched_get_priority_min(libc::SCHED_RR),
            libc::sched_get_priority_max(libc::SCHED_RR),
        )
    };

    if rt_min == -1 || rt_max == -1 {
        let err = std::io::Error::last_os_error();
        return Err(Error::SystemCall(format!(
            "Failed to get SCHED_RR priority range: {}",
            err
        )));
    }

    if priority < rt_min || priority > rt_max {
        return Err(Error::InvalidParameter(format!(
            "Absolute priority {} is out of range [{}, {}] for SCHED_RR",
            priority, rt_min, rt_max
        )));
    }

    // SAFETY: pthread_self() always returns a valid handle to the current thread.
    let current_thread = unsafe { libc::pthread_self() };

    // SAFETY: sched_param is a POD structure; zeroing it is safe.
    // sched_priority is then set to the validated priority.
    let mut param: libc::sched_param = unsafe { std::mem::zeroed() };
    param.sched_priority = priority;

    let policy = libc::SCHED_RR | libc::SCHED_RESET_ON_FORK;

    // SAFETY: Sets the SCHED_RR policy for the current thread.
    // current_thread is valid, param.sched_priority is within [rt_min, rt_max].
    let res = unsafe { libc::pthread_setschedparam(current_thread, policy, &param) };

    if res != 0 {
        let err = std::io::Error::from_raw_os_error(res); // res is errno in this case
        match res {
            libc::EPERM => Err(Error::PermissionDenied(format!(
                "Setting SCHED_RR with priority {}: {}",
                priority, err
            ))),
            libc::EINVAL => Err(Error::InvalidParameter(format!(
                "Invalid parameters for SCHED_RR: priority={}. Error: {}",
                priority, err
            ))),
            libc::ESRCH => Err(Error::NotFound(format!(
                "Thread not found for pthread_setschedparam. Error: {}",
                err
            ))),
            _ => Err(Error::SystemCall(format!(
                "pthread_setschedparam failed for SCHED_RR with priority {}. Error: {}",
                priority, err
            ))),
        }
    } else {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sched_setaffinity_einval_maps_to_invalid_parameter() {
        let err = std::io::Error::from_raw_os_error(libc::EINVAL);
        assert!(matches!(
            map_sched_setaffinity_error(err),
            Error::InvalidParameter(_)
        ));
    }
}