| 863 | } |
| 864 | |
| 865 | ExpertPool::ExpertPool(int n_workers, bool pin, bool host_works, PoolAffinity affinity, int tasks) |
| 866 | : tasks_(tasks), host_works_(host_works), affinity_(affinity), topo_(detect_cpu_topology(true, affinity)) { |
| 867 | if (tasks < 0 || tasks > kMaxTasks) throw std::invalid_argument("expert pool tasks must be in 0..4096"); |
| 868 | pinned_ = pin; |
| 869 | layout_ = logical_cpu_layout(); // now, before anything pins the caller (Linux reads the calling thread's allowed CPUs) |
| 870 | if (const char* e = std::getenv("STRATA_POOL_SPIN_US")) // a test knob; see kSpinBeforeSleep |
| 871 | spin_before_sleep_ = std::chrono::microseconds((std::max)(0, std::atoi(e))); |
| 872 | // STRATA_NUMA_SPLIT, read here for the worker count (the split itself is decided below, once the count is known). |
| 873 | // 0.2.2: with 2 or more NUMA nodes and neither variable set the split is tried by itself, and STRATA_NUMA_POOL=0 turns that |
| 874 | // off (numa_pool.hpp). One node: pick_numa_pool says Unchanged and nothing below differs from the pool before the split. |
| 875 | const char* env = std::getenv("STRATA_NUMA_SPLIT"); |
| 876 | const int numa_nodes = numa_node_count(); |
| 877 | const NumaPoolPick pool_pick = pick_numa_pool(std::getenv("STRATA_NUMA_POOL"), env, numa_nodes); |
| 878 | if (!pool_pick.log.empty()) std::fprintf(stderr, "%s\n", pool_pick.log.c_str()); |
| 879 | if (pool_pick.choice == NumaPoolChoice::Off) env = nullptr; |
| 880 | bool auto_pool = pool_pick.choice == NumaPoolChoice::Auto; // the split without STRATA_NUMA_SPLIT: only if it can run |
| 881 | bool env_off = !auto_pool && (env == nullptr || *env == '\0' || std::strcmp(env, "0") == 0); |
| 882 | bool env_known = auto_pool || (!env_off && (std::strcmp(env, "1") == 0 || std::strcmp(env, "test") == 0)); |
| 883 | const PoolPlanInput plan_in = pool_plan_input(topo_); |
| 884 | const PoolPlan wplan = plan_pool_workers(plan_in); |
| 885 | // --pool-cores (experimental, off by default): the person's own list of logical processors for the workers. The plan above |
| 886 | // was made from the real topology; only the cores the workers are pinned to (and, by default, their count) come from the list. |
| 887 | if (!pool_cores_setting().empty()) { |
| 888 | const PoolCoresCheck chk = check_pool_cores(pool_cores_setting(), layout_, topo_.host_core); |
| 889 | static std::atomic<bool> told{false}; // (a throw-away pool of --pool-affinity measure builds the same list again) |
| 890 | const bool say = !told.exchange(true); |
| 891 | if (say) |
| 892 | for (const std::string& w : chk.warnings) std::fprintf(stderr, "strata cpu pool: --pool-cores: %s\n", w.c_str()); |
| 893 | if (chk.cpus.empty()) { |
| 894 | if (say) |
| 895 | std::fprintf(stderr, "strata cpu pool: --pool-cores: no usable logical processor in the list; the pool uses its " |
| 896 | "default placement\n"); |
| 897 | } else { |
| 898 | topo_.worker_cores = chk.cpus; |
| 899 | topo_.worker_package.assign(chk.cpus.size(), -1); |
| 900 | explicit_cores_ = true; |
| 901 | } |
| 902 | } |
| 903 | // the split needs workers on both nodes: its cores are taken round-robin over the nodes (the packages when a core's node |
| 904 | // is unknown), so any count under the core count (the default or a configured one) spans them evenly (one node: the order |
| 905 | // is kept) |
| 906 | auto interleave_cores = [&] { |
| 907 | if (explicit_cores_) return; // --pool-cores: the person's order stays |
| 908 | if (topo_.worker_package.size() != topo_.worker_cores.size()) return; |
| 909 | std::vector<int> key = topo_.worker_package; |
| 910 | #if defined(__linux__) |
| 911 | std::vector<int> by_node; |
| 912 | for (int c : topo_.worker_cores) by_node.push_back(cpu_numa_node(c)); |
| 913 | if (std::none_of(by_node.begin(), by_node.end(), [](int n) { return n < 0; })) key = by_node; |
| 914 | #elif defined(_WIN32) |
| 915 | std::vector<int> by_node; |
| 916 | for (int c : topo_.worker_cores) by_node.push_back(cpu_numa_node_win(c)); |
| 917 | if (std::none_of(by_node.begin(), by_node.end(), [](int n) { return n < 0; })) key = by_node; |
| 918 | #endif |
| 919 | std::vector<int> cores, pkgs; |
| 920 | for (size_t i : interleave_packages(key)) { |
| 921 | cores.push_back(topo_.worker_cores[i]); |
| 922 | pkgs.push_back(topo_.worker_package[i]); |
nothing calls this directly
no test coverage detected