diff --git a/include/strata/kernels/cpu/pool_plan.hpp b/include/strata/kernels/cpu/pool_plan.hpp index 95e93b51..d53f4579 100644 --- a/include/strata/kernels/cpu/pool_plan.hpp +++ b/include/strata/kernels/cpu/pool_plan.hpp @@ -45,8 +45,23 @@ struct PoolPlanInput { int e_cores = 0; }; +/// #3262: is a CPU an efficiency-class core? `pmu_core` / `pmu_atom` / `pmu_lowpower` are Intel's hybrid PMU lists +/// (/sys/devices/cpu_core/cpus, cpu_atom/cpus, cpu_lowpower/cpus; the last exists on Meteor Lake and Arrow Lake-H, where +/// the LP-E cores of the SoC tile are in neither of the other two). A CPU in the P list is a P-core; one in the atom +/// or low-power list is an E-core (the LP-E cores sort last by capacity). A CPU in no list falls back to its capacity +/// (an E-core under 90% of the largest). `pmu`: both the core and the atom list were read. +inline bool hybrid_cpu_is_efficiency(int cpu, long cap, long max_cap, bool pmu, const std::vector& pmu_core, + const std::vector& pmu_atom, const std::vector& pmu_lowpower) { + auto in = [&](const std::vector& v) { return std::find(v.begin(), v.end(), cpu) != v.end(); }; + if (pmu) { + if (in(pmu_atom) || in(pmu_lowpower)) return true; + if (in(pmu_core)) return false; + } + return max_cap > 0 && cap > 0 && cap * 10 < max_cap * 9; +} + struct PoolPlan { - int workers = 1; ///< the default count of pool workers (the host thread works beside them) + int workers = 1; ///< the default count of pool workers (the host thread works beside them) int cores = 0; ///< physical cores counted (all packages) int cores_local = 0; ///< ... on the host's package int packages = 1; diff --git a/setup.py b/setup.py index aa39757b..5d4295d7 100644 --- a/setup.py +++ b/setup.py @@ -974,7 +974,7 @@ def linux_core_classes(sys_root: str = "/sys"): one under 90% of the largest - Turbo Boost Max 3.0 gives the "favored" P-cores a slightly higher capacity (1024 against 1012), so "below the maximum" counted 2 of a Core Ultra 7 270K Plus's 8 P-cores.""" pmu = linux_hybrid_pmu(sys_root + "/devices") - seen = {} + seen, top = {}, 0 for cpu in sorted((Path(sys_root) / "devices" / "system" / "cpu").glob("cpu[0-9]*"), key=lambda p: int(p.name[3:])): cap = cpu / "cpu_capacity" pkg, core = cpu / "topology" / "physical_package_id", cpu / "topology" / "core_id" @@ -982,15 +982,17 @@ def linux_core_classes(sys_root: str = "/sys"): return None key = (pkg.read_text(encoding="utf-8").strip(), core.read_text(encoding="utf-8").strip()) \ if pkg.exists() and core.exists() else cpu.name - if pmu is not None: - seen.setdefault(key, 0 if int(cpu.name[3:]) in pmu[1] else 1) - else: - seen.setdefault(key, int(cap.read_text(encoding="utf-8").strip())) - classes = list(seen.values()) - if pmu is None: - top = max(classes, default=0) - classes = [1 if c * 10 >= top * 9 else 0 for c in classes] - return classes + num = int(cpu.name[3:]) + capacity = int(cap.read_text(encoding="utf-8").strip()) if cap.exists() else 0 + top = max(top, capacity) + if pmu is not None and num in pmu[1]: + seen.setdefault(key, None) # a listed E-core (the atom list and, #3262, the LP-E list) + elif pmu is not None and num in pmu[0]: + seen.setdefault(key, False) # a listed P-core + else: # in no list (or no lists at all): its capacity decides + seen.setdefault(key, capacity) + # an E-core is under 90% of the largest capacity; the PMU lists (None: E, False: P) decide where they say + return [0 if c is None else 1 if c is False else (1 if c * 10 >= top * 9 else 0) for c in seen.values()] def cpulist(text: str) -> set: @@ -1011,6 +1013,10 @@ def linux_hybrid_pmu(root: str = "/sys/devices"): e = cpulist((Path(root) / "cpu_atom" / "cpus").read_text(encoding="utf-8")) except OSError: return None + try: # #3262: Meteor Lake / Arrow Lake-H list their LP-E cores in a third set, in neither of the two above + e |= cpulist((Path(root) / "cpu_lowpower" / "cpus").read_text(encoding="utf-8")) + except OSError: + pass return (p, e) if p and e else None diff --git a/src/kernels/cpu/pool.cpp b/src/kernels/cpu/pool.cpp index fd6952eb..80161b9a 100644 --- a/src/kernels/cpu/pool.cpp +++ b/src/kernels/cpu/pool.cpp @@ -429,12 +429,14 @@ static CpuTopology detect_cpu_topology_impl(bool skip_first, PoolAffinity affini // CPU is an E-core when its capacity is under 90% of the largest. Turbo Boost Max 3.0 gives the "favored" // P-cores a slightly higher capacity (1024 against 1012 on a Core Ultra 7 270K Plus): "capacity == the maximum" // counted 2 of its 8 P-cores and started 1 pool worker. - std::vector pmu_core, pmu_atom; + // #3262: Meteor Lake and Arrow Lake-H list their LP-E cores (SoC tile, no L3, ~2.5 GHz) in a third set, + // /sys/devices/cpu_lowpower/cpus, that is in neither of the other two; they are E-cores too (and sort last). + std::vector pmu_core, pmu_atom, pmu_lowpower; const bool pmu = cpulist_read("/sys/devices/cpu_core/cpus", pmu_core) && cpulist_read("/sys/devices/cpu_atom/cpus", pmu_atom); + cpulist_read("/sys/devices/cpu_lowpower/cpus", pmu_lowpower); auto is_e = [&](const CoreLinux& cl) { - if (pmu) return std::find(pmu_atom.begin(), pmu_atom.end(), cl.cpu) != pmu_atom.end(); - return max_cap > 0 && cl.cap > 0 && cl.cap * 10 < max_cap * 9; + return hybrid_cpu_is_efficiency(cl.cpu, cl.cap, max_cap, pmu, pmu_core, pmu_atom, pmu_lowpower); }; bool any_e = false, any_p = false; for (const auto& cl : all_cpus) (is_e(cl) ? any_e : any_p) = true; diff --git a/src/kernels/cpu/pool_plan_test.cpp b/src/kernels/cpu/pool_plan_test.cpp index 9cd81136..a30fb98f 100644 --- a/src/kernels/cpu/pool_plan_test.cpp +++ b/src/kernels/cpu/pool_plan_test.cpp @@ -100,6 +100,29 @@ int main() { expect(cpu::plan_pool_workers(h3).workers == 15, "8P+8E: not more E-cores than P-cores, every core but the host's"); } + // #3262: Core Ultra 7 255H (6P + 8E + 2 LP-E, no SMT). The LP-E cores (CPUs 14-15) are in cpu_lowpower/cpus, in + // neither the core nor the atom list; they are efficiency cores. + { + const std::vector core = {0, 1, 2, 3, 4, 5}, atom = {6, 7, 8, 9, 10, 11, 12, 13}, lp = {14, 15}; + int e = 0, p = 0; + for (int c = 0; c < 16; ++c) { + const long cap = c < 6 ? 1024 : c < 14 ? 774 : 312; + (cpu::hybrid_cpu_is_efficiency(c, cap, 1024, true, core, atom, lp) ? e : p)++; + } + expect(p == 6 && e == 10, "255H: 6 P-cores, 8 E + 2 LP-E counted as efficiency cores"); + expect(cpu::hybrid_cpu_is_efficiency(14, 312, 1024, true, core, atom, lp), "LP-E core 14 is an E-core"); + expect(!cpu::hybrid_cpu_is_efficiency(5, 1024, 1024, true, core, atom, lp), "P-core 5 stays a P-core"); + // a CPU in the lists of no kind falls back to its capacity (312 < 90% of 1024: an E-core) + expect(cpu::hybrid_cpu_is_efficiency(14, 312, 1024, true, core, atom, {}), "LP-E core with no third list: capacity says E"); + // a CPU in no list with a favoured-P capacity is a P-core + expect(!cpu::hybrid_cpu_is_efficiency(20, 1012, 1024, true, core, atom, lp), "unlisted CPU, capacity 1012/1024: P"); + // the plan with the right counts: 6P + 10E -> 5 + 5 workers, not the 15 of "every core but the host's" + auto h = cpu_of(16, {16}); + h.p_cores = 6; + h.e_cores = 10; + expect(cpu::plan_pool_workers(h).workers == 10, "255H -> 10 workers (the measured 9-13 are flat; 15 is -35%)"); + } + // a container quota const auto q = cpu_of(128, {64}, 4.0); line("64c, container quota 4 CPUs", q); diff --git a/tools/test_setup_hybrid.py b/tools/test_setup_hybrid.py index 3a5882ee..bc1d08a5 100644 --- a/tools/test_setup_hybrid.py +++ b/tools/test_setup_hybrid.py @@ -31,7 +31,7 @@ def fake_sys(root: Path, caps: list, pmu=None, smt=False) -> None: (d / "cpu_capacity").write_text(str(cap)) n += 1 if pmu: - for name, lst in zip(("cpu_core", "cpu_atom"), pmu): + for name, lst in zip(("cpu_core", "cpu_atom", "cpu_lowpower"), pmu): (root / "devices" / name).mkdir(parents=True) (root / "devices" / name / "cpus").write_text(lst + "\n") @@ -54,6 +54,15 @@ class LinuxCores(unittest.TestCase): self.assertEqual(self.classes(caps, pmu=("0-7", "8-23")), (8, 16)) # by the PMU lists self.assertEqual(setup.hybrid_pool_workers((8, 16)), 15) + def test_lp_e_cores_are_efficiency_cores(self): + """#3262: Core Ultra 7 255H (6P + 8E + 2 LP-E): the LP-E cores are in /sys/devices/cpu_lowpower/cpus, in neither + of the other two lists; they were counted as P-cores (8P + 8E, so the engine's every-core default of 15 stayed).""" + caps = [1024] * 6 + [774] * 8 + [312] * 2 + self.assertEqual(self.classes(caps, pmu=("0-5", "6-13", "14-15")), (6, 10)) + self.assertEqual(self.classes(caps), (6, 10)) # no PMU lists: capacity says the same + self.assertEqual(setup.hybrid_pool_workers((6, 10)), 10) # not None (8, 8 gave every core: 15) + self.assertEqual(self.classes(caps, pmu=("0-5", "6-13")), (6, 10)) # a kernel without the third list: capacity + def test_plain_capacities_and_plain_cpus(self): self.assertEqual(self.classes([1024] * 8 + [512] * 8), (8, 8)) self.assertIsNone(self.classes([1024] * 8)) # no E-cores