| 208 | #endif |
| 209 | |
| 210 | static void fill_plan_fields(CpuTopology& topo, bool skip_first) { |
| 211 | topo.cpu_quota = container_cpu_quota(); |
| 212 | topo.worker_package.assign(topo.worker_cores.size(), -1); |
| 213 | #if defined(_WIN32) |
| 214 | topo.logical = (int) GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); |
| 215 | // #6523: populate NUMA node / package per physical core across all processor groups so multi-socket servers |
| 216 | // (>64 logical CPUs) plan workers across both sockets instead of collapsing into a single group. |
| 217 | bool known = !topo.worker_cores.empty() || topo.host_core >= 0; |
| 218 | if (topo.host_core >= 0) { |
| 219 | topo.host_package = cpu_numa_node_win(topo.host_core); |
| 220 | if (topo.host_package < 0) known = false; |
| 221 | } |
| 222 | for (size_t i = 0; i < topo.worker_cores.size(); ++i) { |
| 223 | topo.worker_package[i] = cpu_numa_node_win(topo.worker_cores[i]); |
| 224 | if (topo.worker_package[i] < 0) known = false; |
| 225 | } |
| 226 | if (known && skip_first && !topo.is_hybrid && topo.host_core >= 0 && topo.host_package >= 0) { |
| 227 | std::vector<size_t> order(topo.worker_cores.size()); |
| 228 | for (size_t i = 0; i < order.size(); ++i) order[i] = i; |
| 229 | std::stable_partition(order.begin(), order.end(), |
| 230 | [&](size_t i) { return topo.worker_package[i] == topo.host_package; }); |
| 231 | std::vector<int> cores, pkgs; |
| 232 | for (size_t i : order) { cores.push_back(topo.worker_cores[i]); pkgs.push_back(topo.worker_package[i]); } |
| 233 | topo.worker_cores = std::move(cores); |
| 234 | topo.worker_package = std::move(pkgs); |
| 235 | } |
| 236 | return; |
| 237 | #elif defined(__APPLE__) |
| 238 | (void) skip_first; |
| 239 | topo.logical = (int) std::thread::hardware_concurrency(); |
| 240 | return; // no SMT on Apple silicon and no sysfs: the cores are the processors |
| 241 | #else |
| 242 | std::vector<unsigned long> mask; |
| 243 | std::vector<int> allowed; |
| 244 | topo.logical = detail::get_thread_affinity(mask, &allowed) == 0 ? (int) allowed.size() |
| 245 | : (int) std::thread::hardware_concurrency(); |
| 246 | auto read = [](int cpu, const char* what) -> long { |
| 247 | char path[96]; |
| 248 | std::snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%d/topology/%s", cpu, what); |
| 249 | long v = -1; |
| 250 | if (std::FILE* f = std::fopen(path, "r")) { |
| 251 | if (std::fscanf(f, "%ld", &v) != 1) v = -1; |
| 252 | std::fclose(f); |
| 253 | } |
| 254 | return v; |
| 255 | }; |
| 256 | bool known = !topo.worker_cores.empty() || topo.host_core >= 0; |
| 257 | auto pkg_of = [&](int cpu) { |
| 258 | const long pkg = read(cpu, "physical_package_id"), core = read(cpu, "core_id"); |
| 259 | if (pkg < 0 || core < 0) known = false; |
| 260 | return (int) pkg; |
| 261 | }; |
| 262 | if (topo.host_core >= 0) topo.host_package = pkg_of(topo.host_core); |
| 263 | for (size_t i = 0; i < topo.worker_cores.size(); ++i) topo.worker_package[i] = pkg_of(topo.worker_cores[i]); |
| 264 | topo.layout_known = known; |
| 265 | if (!known) return; |
| 266 | if (skip_first && !topo.is_hybrid && topo.host_core >= 0 && topo.host_package >= 0) { |
| 267 | // the host socket first (stable inside each socket): the default workers are the local ones |
no test coverage detected