From 4ef6b53eb09f9cd5150f288be49c5c740e400e48 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Tue, 27 Oct 2026 19:07:59 -0400 Subject: [PATCH] fix(proton): Windows CPU 0 is the fastest core (#1697) Wine numbers the Windows CPUs like the host, so Windows CPU 0 was host core 0, a small core on most Android chips. Games that pin their main thread to CPU 0 ran it there: GTA IV 32 fps on the AYANEO Pocket FIT, 61 fps once CPU 0 is the fastest core. droiddeck-game-env hands Wine the game's cores fastest first, from each core's cpu_capacity (or its top clock). Equal cores keep their order. Cores picked globally or in a game profile stay the only cores the game sees, now fastest first; a pick only counts when it narrows the cores. Games on Proton's own default_cpu_limit list keep their count on the fastest cores (quoted and unquoted counts); other values such as "P" are left to Proton. A typed WINE_CPU_TOPOLOGY / PROTON_CPU_TOPOLOGY still wins, an empty one turns it off for a game, and DROIDDECK_FASTEST_CORES=0 turns it off. Steam's own Proton runs in compatdata/0 are left as before. Builds on maxjivi05's "Windows games see only the cores they are pinned to" (9d85af75). --- .../overlay/usr/local/bin/droiddeck-game-env | 63 ++++++++++++++++-- tools/tests/test_game_environment.py | 66 +++++++++++++++++++ 2 files changed, 125 insertions(+), 4 deletions(-) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env index 288c8d57c2..6593537d2e 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env @@ -357,21 +357,76 @@ def game_cores(env): print("droiddeck-game-env: could not use game cores %r (%s)" % (text, error), file=sys.stderr) -def cpu_topology(env): +def core_speeds(cores, root="/sys/devices/system/cpu"): + """Each core's capacity, else its top clock; None when some core reports neither.""" + for name in ("cpu_capacity", "cpufreq/cpuinfo_max_freq"): + speeds = {} + for core in cores: + try: + with open("%s/cpu%d/%s" % (root, core, name)) as f: + speeds[core] = int(f.read().strip()) + except (OSError, ValueError): + break + else: + return speeds + return None + + +def fastest_first(cores): + """The cores, fastest first; cores of equal or unknown speed keep their numeric order.""" + cores = sorted(cores) + speeds = core_speeds(cores) + return sorted(cores, key=lambda core: -speeds[core]) if speeds else cores + + +def proton_cpu_limit(command, env): + """This game's entry in Proton's own default_cpu_limit list: a core count, another value such as "P" as text, or None.""" + app_id = env.get("SteamGameId", "") + if not command or not app_id.isdigit(): + return None + try: + if os.path.getsize(command[0]) > 4 * 1024 * 1024: + return None + with open(command[0], encoding="utf-8", errors="replace") as f: + text = f.read() + except OSError: + return None + table = re.search(r"^default_cpu_limit = \{(.*?)^\}", text, re.M | re.S) + match = table and re.search(r'^\s*"%s"\s*:\s*"?(\w+)' % app_id, table.group(1), re.M) + if not match: + return None + value = match.group(1) + return int(value) if value.isdigit() else value + + +def cpu_topology(env, command=()): + """Windows sees the game's cores fastest first, so a thread a game pins to CPU 0 runs on the fastest core.""" if "PROTON_CPU_TOPOLOGY" in env: return if "WINE_CPU_TOPOLOGY" in env: if env["WINE_CPU_TOPOLOGY"]: env["PROTON_CPU_TOPOLOGY"] = env["WINE_CPU_TOPOLOGY"] return - if not (env.get("DROIDDECK_GAME_CORES", "").strip() or env.get("BL_GAME_CPUS", "").strip()): + picked = bool(env.get("DROIDDECK_GAME_CORES", "").strip() or env.get("BL_GAME_CPUS", "").strip()) + fastest = env.get("DROIDDECK_FASTEST_CORES", "").strip() != "0" and game_id(env) is not None + if not (picked or fastest): return try: cores = sorted(os.sched_getaffinity(0)) except OSError: return online = online_cores() - if not cores or set(cores) >= online or cores[-1] >= len(online): + if not cores or cores[-1] >= len(online): + return + picked = picked and not set(cores) >= online + if fastest: + limit = None if picked else proton_cpu_limit(command, env) + if isinstance(limit, str): + return + cores = fastest_first(cores)[:limit or None] + elif set(cores) >= online: + return + if cores == sorted(online): return env["WINE_CPU_TOPOLOGY"] = env["PROTON_CPU_TOPOLOGY"] = "%d:%s" % (len(cores), ",".join(map(str, cores))) print("droiddeck-game-env: Windows sees %d cores (%s)" % (len(cores), env["WINE_CPU_TOPOLOGY"]), file=sys.stderr) @@ -653,7 +708,7 @@ def main(): env = vram_cap(env) env = force_ssbs(command, env) game_cores(env) - cpu_topology(env) + cpu_topology(env, command) try: xbox = runpy.run_path(str(Path(__file__).with_name("droiddeck-xbox"))) except Exception as error: diff --git a/tools/tests/test_game_environment.py b/tools/tests/test_game_environment.py index 0b1bee1753..13341d4280 100644 --- a/tools/tests/test_game_environment.py +++ b/tools/tests/test_game_environment.py @@ -464,12 +464,78 @@ class GameEnvironmentTest(unittest.TestCase): ({"BL_GAME_CPUS": "9"}, {9}, None, None), ] for env, affinity, expected, proton in cases: + env["DROIDDECK_FASTEST_CORES"] = "0" with mock.patch.dict(MODULE["cpu_topology"].__globals__, online), \ mock.patch.object(os_mod, "sched_getaffinity", return_value=affinity, create=True): MODULE["cpu_topology"](env) self.assertEqual(env.get("WINE_CPU_TOPOLOGY"), expected, env) self.assertEqual(env.get("PROTON_CPU_TOPOLOGY"), proton, env) + def test_windows_cpu_0_is_the_fastest_core(self): + os_mod = MODULE["os"] + pocket_fit = {0: 379, 1: 379, 2: 923, 3: 923, 4: 923, 5: 867, 6: 867, 7: 1024} + every = set(range(8)) + with tempfile.TemporaryDirectory() as tmp: + proton = Path(tmp) / "proton" + proton.write_text('default_cpu_limit = {\n "19900" : "16", # Far Cry 2\n' + ' "2229830" : "1", # Command & Conquer\n "11440" : "4", # DiRT\n' + ' "1038250" : "P", # DIRT 5\n "618970" : 4, # Outcast, written as Proton 10 does\n' + '}\n\nother = {\n "620" : "2",\n}\n') + command = [str(proton), "waitforexitandrun", "/game/game.exe"] + cases = [ + ({}, every, pocket_fit, "8:7,2,3,4,5,6,0,1", "8:7,2,3,4,5,6,0,1"), + ({"BL_GAME_CPUS": "6,7"}, {6, 7}, pocket_fit, "2:7,6", "2:7,6"), + ({"DROIDDECK_GAME_CORES": "0-2"}, {0, 1, 2}, pocket_fit, "3:2,0,1", "3:2,0,1"), + ({"BL_GAME_CPUS": "0-7"}, every, pocket_fit, "8:7,2,3,4,5,6,0,1", "8:7,2,3,4,5,6,0,1"), + ({"WINE_CPU_TOPOLOGY": "2:0,1"}, every, pocket_fit, "2:0,1", "2:0,1"), + ({"WINE_CPU_TOPOLOGY": ""}, every, pocket_fit, "", None), + ({"PROTON_CPU_TOPOLOGY": "1:0"}, every, pocket_fit, None, "1:0"), + ({"DROIDDECK_FASTEST_CORES": "0"}, every, pocket_fit, None, None), + ({"SteamGameId": "2229830"}, every, pocket_fit, "1:7", "1:7"), + ({"SteamGameId": "11440"}, every, pocket_fit, "4:7,2,3,4", "4:7,2,3,4"), + ({"SteamGameId": "19900"}, every, pocket_fit, "8:7,2,3,4,5,6,0,1", "8:7,2,3,4,5,6,0,1"), + ({"SteamGameId": "1038250"}, every, pocket_fit, None, None), + ({"SteamGameId": "618970"}, every, pocket_fit, "4:7,2,3,4", "4:7,2,3,4"), + ({"SteamGameId": "620"}, every, pocket_fit, "8:7,2,3,4,5,6,0,1", "8:7,2,3,4,5,6,0,1"), + ({"SteamGameId": "2229830", "BL_GAME_CPUS": "0,1"}, {0, 1}, pocket_fit, "2:0,1", "2:0,1"), + ({"SteamGameId": "2229830", "DROIDDECK_GAME_CORES": "12-13"}, every, pocket_fit, "1:7", "1:7"), + ({"SteamGameId": "11440", "BL_GAME_CPUS": "0-7"}, every, pocket_fit, "4:7,2,3,4", "4:7,2,3,4"), + ({}, every, {core: 100 for core in every}, None, None), + ({}, every, None, None, None), + ({"SteamGameId": "11440"}, every, None, "4:0,1,2,3", "4:0,1,2,3"), + ({"BL_GAME_CPUS": "9"}, {9}, pocket_fit, None, None), + ] + cases += [ + ({"STEAM_COMPAT_DATA_PATH": "/steam/compatdata/0"}, every, pocket_fit, None, None), + ({"STEAM_COMPAT_DATA_PATH": ""}, every, pocket_fit, None, None), + ({"STEAM_COMPAT_DATA_PATH": "/steam/compatdata/0", "BL_GAME_CPUS": "6,7"}, {6, 7}, pocket_fit, "2:6,7", "2:6,7"), + ] + for env, affinity, speeds, expected, proton_topology in cases: + env.setdefault("STEAM_COMPAT_DATA_PATH", "/steam/compatdata/3608888694") + with mock.patch.dict(MODULE["cpu_topology"].__globals__, {"online_cores": lambda: every}), \ + mock.patch.dict(MODULE["fastest_first"].__globals__, {"core_speeds": lambda cores, speeds=speeds: speeds}), \ + mock.patch.object(os_mod, "sched_getaffinity", return_value=affinity, create=True): + MODULE["cpu_topology"](env, command) + self.assertEqual(env.get("WINE_CPU_TOPOLOGY"), expected, env) + self.assertEqual(env.get("PROTON_CPU_TOPOLOGY"), proton_topology, env) + self.assertIsNone(MODULE["proton_cpu_limit"](["/missing/proton"], {"SteamGameId": "11440"})) + self.assertIsNone(MODULE["proton_cpu_limit"](command, {})) + self.assertEqual(MODULE["proton_cpu_limit"](command, {"SteamGameId": "1038250"}), "P") + self.assertEqual(MODULE["proton_cpu_limit"](command, {"SteamGameId": "618970"}), 4) + + def test_core_speeds_read_capacity_then_top_clock(self): + with tempfile.TemporaryDirectory() as tmp: + for core, capacity, clock in ((0, "379", "2265600"), (1, None, "3302400")): + folder = Path(tmp) / ("cpu%d" % core) / "cpufreq" + folder.mkdir(parents=True) + (folder / "cpuinfo_max_freq").write_text(clock + "\n") + if capacity: + (folder.parent / "cpu_capacity").write_text(capacity + "\n") + speeds = MODULE["core_speeds"] + self.assertEqual(speeds([0], tmp), {0: 379}) + self.assertEqual(speeds([0, 1], tmp), {0: 2265600, 1: 3302400}) + self.assertIsNone(speeds([0, 2], tmp)) + def test_reskate_runs_at_feature_level_12_1(self): with tempfile.TemporaryDirectory() as tmp: reskate = Path(tmp) / "ReSkate"