okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-mac-rel (Mac Metal support) into f-144-rel6a74320ca

main
3.3 KiB87 linesraw
1#pragma once
2
3#if defined(__linux__)
4#include <pthread.h>
5#include <sched.h>
6
7#include <cerrno>
8#include <climits>
9#include <cstring>
10#include <vector>
11
12namespace strata::kernels::cpu::detail {
13
14// CPU_ALLOC and the sized macros also handle kernels whose CPU mask exceeds CPU_SETSIZE.
15// Keep the native mask opaque: copy its bytes for storage and use CPU_*_S to inspect it.
16class LinuxCpuSet {
17public:
18    explicit LinuxCpuSet(int cpus) : bytes(CPU_ALLOC_SIZE(cpus)), set(CPU_ALLOC(cpus)) {
19        if (set) CPU_ZERO_S(bytes, set);
20    }
21    ~LinuxCpuSet() { CPU_FREE(set); }
22    LinuxCpuSet(const LinuxCpuSet&) = delete;
23    LinuxCpuSet& operator=(const LinuxCpuSet&) = delete;
24
25    const size_t bytes;
26    cpu_set_t* const set;
27};
28
29// pthread affinity functions return an error number directly; they do not set errno.
30// A too-small query buffer returns EINVAL, even when the number of online CPUs is small.
31inline int get_thread_affinity(std::vector<unsigned long>& mask, std::vector<int>* cpus = nullptr) {
32    for (int count = CPU_SETSIZE;;) {
33        LinuxCpuSet current(count);
34        if (!current.set) return ENOMEM;
35        const int error = pthread_getaffinity_np(pthread_self(), current.bytes, current.set);
36        if (error == 0) {
37            mask.resize(current.bytes / sizeof(unsigned long));
38            std::memcpy(mask.data(), current.set, current.bytes);
39            if (cpus) {
40                cpus->clear();
41                for (int cpu = 0; cpu < count; ++cpu)
42                    if (CPU_ISSET_S(cpu, current.bytes, current.set)) cpus->push_back(cpu);
43            }
44            return 0;
45        }
46        if (error != EINVAL || count > INT_MAX / 2) return error;
47        count *= 2;
48    }
49}
50
51inline int set_thread_affinity(const std::vector<unsigned long>& mask) {
52    constexpr size_t word_bits = sizeof(unsigned long) * CHAR_BIT;
53    if (mask.empty() || mask.size() > INT_MAX / word_bits) return EINVAL;
54    LinuxCpuSet target(static_cast<int>(mask.size() * word_bits));
55    if (!target.set) return ENOMEM;
56    std::memcpy(target.set, mask.data(), target.bytes);
57    return pthread_setaffinity_np(pthread_self(), target.bytes, target.set);
58}
59
60inline int pin_thread_to_cpu(int core) {
61    if (core < 0 || core == INT_MAX) return EINVAL;
62    LinuxCpuSet target(core < CPU_SETSIZE ? CPU_SETSIZE : core + 1);
63    if (!target.set) return ENOMEM;
64    CPU_SET_S(core, target.bytes, target.set);
65    return pthread_setaffinity_np(pthread_self(), target.bytes, target.set);
66}
67
68}  // namespace strata::kernels::cpu::detail
69#elif defined(__APPLE__)
70#include <thread>
71#include <vector>
72
73// macOS has no thread-to-core pinning (the scheduler places threads; affinity tags are hints at most): every CPU is
74// allowed and a pin is a no-op, so the pool keeps its threads where the OS puts them.
75namespace strata::kernels::cpu::detail {
76inline int get_thread_affinity(std::vector<unsigned long>& mask, std::vector<int>* cpus = nullptr) {
77    mask.assign(1, ~0ul);
78    if (cpus) {
79        cpus->clear();
80        for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) cpus->push_back((int) i);
81    }
82    return 0;
83}
84inline int set_thread_affinity(const std::vector<unsigned long>&) { return 0; }
85inline int pin_thread_to_cpu(int) { return 0; }   // nothing to do: the OS places the thread
86}  // namespace strata::kernels::cpu::detail
87#endif