okami.mom/strata

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

Niko1221Merge branch 'f-139b-spd' into f-139-rel4f5e6c841

main
2.7 KiB64 linesraw
1#pragma once
2
3#if defined(_WIN32)
4#ifndef WIN32_LEAN_AND_MEAN
5#define WIN32_LEAN_AND_MEAN
6#endif
7#include <windows.h>
8
9#include <cstdio>
10#include <cstddef>
11#include <vector>
12
13namespace strata::kernels::cpu::detail {
14
15// Hard affinity is only for pool-owned threads, which terminate with the pool. A caller's implicit
16// all-group affinity cannot be restored from PreviousGroupAffinity, so the host uses CPU Sets below.
17inline bool set_thread_group_affinity(int core, int worker) {
18    if (core < 0) return false;
19    GROUP_AFFINITY target{};
20    target.Group = (WORD) (core / 64);
21    target.Mask = KAFFINITY(1) << (core & 63);
22    if (SetThreadGroupAffinity(GetCurrentThread(), &target, nullptr)) return true;
23    std::fprintf(stderr, "strata cpu pool: SetThreadGroupAffinity for worker %d (group %u, mask 0x%llx) failed: %lu; previous affinity kept\n",
24                 worker, (unsigned) target.Group, (unsigned long long) target.Mask, (unsigned long) GetLastError());
25    return false;
26}
27
28inline bool get_thread_cpu_sets(std::vector<ULONG>& ids) {
29    ULONG count = 0;
30    if (!GetThreadSelectedCpuSets(GetCurrentThread(), nullptr, 0, &count) &&
31        GetLastError() != ERROR_INSUFFICIENT_BUFFER) return false;
32    ids.resize(count);
33    if (count == 0) return true;
34    if (!GetThreadSelectedCpuSets(GetCurrentThread(), ids.data(), count, &count)) return false;
35    ids.resize(count);
36    return true;
37}
38
39// CPU Set IDs are opaque: find the entry by both group and group-relative processor number.
40inline bool cpu_set_for_core(int core, ULONG& id) {
41    ULONG bytes = 0;
42    if (!GetSystemCpuSetInformation(nullptr, 0, &bytes, GetCurrentProcess(), 0) &&
43        GetLastError() != ERROR_INSUFFICIENT_BUFFER) return false;
44    std::vector<unsigned char> buffer(bytes);
45    if (bytes && !GetSystemCpuSetInformation(reinterpret_cast<PSYSTEM_CPU_SET_INFORMATION>(buffer.data()),
46                                            bytes, &bytes, GetCurrentProcess(), 0)) return false;
47    constexpr size_t header_size = offsetof(SYSTEM_CPU_SET_INFORMATION, CpuSet);
48    for (size_t offset = 0; offset + header_size <= bytes;) {
49        const auto* info = reinterpret_cast<const SYSTEM_CPU_SET_INFORMATION*>(buffer.data() + offset);
50        if (info->Size < header_size || info->Size > bytes - offset) break;
51        if (info->Type == CpuSetInformation && info->Size >= sizeof(SYSTEM_CPU_SET_INFORMATION) && info->CpuSet.Group == core / 64 &&
52            info->CpuSet.LogicalProcessorIndex == (core & 63) &&
53            (!info->CpuSet.Allocated || info->CpuSet.AllocatedToTargetProcess)) {
54            id = info->CpuSet.Id;
55            return true;
56        }
57        offset += info->Size;
58    }
59    SetLastError(ERROR_NOT_FOUND);
60    return false;
61}
62
63}  // namespace strata::kernels::cpu::detail
64#endif