| 122 | const std::vector<int>& pool_cores_setting() { return g_pool_cores; } |
| 123 | |
| 124 | std::vector<LogicalCpuInfo> logical_cpu_layout() { |
| 125 | std::vector<LogicalCpuInfo> out; |
| 126 | #if defined(_WIN32) |
| 127 | DWORD len = 0; |
| 128 | GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &len); |
| 129 | if (len == 0) return out; |
| 130 | std::vector<char> buf(len); |
| 131 | if (!GetLogicalProcessorInformationEx(RelationProcessorCore, (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX) buf.data(), &len)) |
| 132 | return out; |
| 133 | // a `start /affinity` or a job limit on a PC with one processor group: only those processors are the process's |
| 134 | DWORD_PTR proc_mask = 0, sys_mask = 0; |
| 135 | const bool limited = GetActiveProcessorGroupCount() == 1 && GetProcessAffinityMask(GetCurrentProcess(), &proc_mask, &sys_mask); |
| 136 | long core = 0; |
| 137 | for (const char* p = buf.data(); p < buf.data() + len; p += ((const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) p)->Size) { |
| 138 | const auto* e = (const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*) p; |
| 139 | if (e->Relationship != RelationProcessorCore) continue; |
| 140 | for (WORD group = 0; group < e->Processor.GroupCount; ++group) { |
| 141 | const GROUP_AFFINITY& g = e->Processor.GroupMask[group]; |
| 142 | for (int bit = 0; bit < 64; ++bit) |
| 143 | if ((g.Mask & (KAFFINITY(1) << bit)) && (!limited || (proc_mask & (DWORD_PTR(1) << bit)))) |
| 144 | out.push_back({(int) (g.Group * 64 + bit), core}); |
| 145 | } |
| 146 | ++core; |
| 147 | } |
| 148 | #elif defined(__linux__) |
| 149 | std::vector<unsigned long> mask; |
| 150 | std::vector<int> allowed; |
| 151 | if (detail::get_thread_affinity(mask, &allowed) != 0) return out; |
| 152 | auto read = [](int cpu, const char* what) -> long { |
| 153 | char path[96]; |
| 154 | std::snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/topology/%s", cpu, what); |
| 155 | long v = -1; |
| 156 | if (std::FILE* f = std::fopen(path, "r")) { |
| 157 | if (std::fscanf(f, "%ld", &v) != 1) v = -1; |
| 158 | std::fclose(f); |
| 159 | } |
| 160 | return v; |
| 161 | }; |
| 162 | for (int cpu : allowed) { |
| 163 | const long pkg = read(cpu, "physical_package_id"), core = read(cpu, "core_id"); |
| 164 | out.push_back({cpu, pkg >= 0 && core >= 0 ? pkg * 1000000 + core : -1}); |
| 165 | } |
| 166 | #else |
| 167 | for (unsigned i = 0; i < std::thread::hardware_concurrency(); ++i) out.push_back({(int) i, -1}); |
| 168 | #endif |
| 169 | return out; |
| 170 | } |
| 171 | |
| 172 | static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affinity); |
| 173 | |