mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 13:55:58 +00:00
feat: pick a game's CPU cores in its game profile (#1566)
* feat: pick a game's CPU cores in its game profile * fix: match per-game cores against the online CPU list
This commit is contained in:
1 parent
ee3f32d31e
commit
8d858b4334
4 files changed
+63
No files matched your search
@@ -299,6 +299,46 @@ def vram_cap(env):
|
||||
return result
|
||||
|
||||
|
||||
def parse_cores(text):
|
||||
"""CPU numbers from a list such as "0-3,6"; None when the text is not one."""
|
||||
cores = set()
|
||||
for part in text.replace(" ", "").split(","):
|
||||
match = re.fullmatch(r"(\d{1,3})(?:-(\d{1,3}))?", part)
|
||||
if not match:
|
||||
return None
|
||||
first, last = int(match.group(1)), int(match.group(2) or match.group(1))
|
||||
if last < first:
|
||||
return None
|
||||
cores.update(range(first, last + 1))
|
||||
return cores or None
|
||||
|
||||
|
||||
def online_cores():
|
||||
try:
|
||||
with open("/sys/devices/system/cpu/online") as f:
|
||||
cores = parse_cores(f.read().strip())
|
||||
except OSError:
|
||||
cores = None
|
||||
return cores or os.sched_getaffinity(0) | set(range(os.cpu_count() or 0))
|
||||
|
||||
|
||||
def game_cores(env):
|
||||
"""Pins the game to the cores its profile names, in place of the session's game cores."""
|
||||
text = env.get("DROIDDECK_GAME_CORES", "").strip()
|
||||
if not text:
|
||||
return
|
||||
cores = parse_cores(text)
|
||||
chosen = cores & online_cores() if cores else None
|
||||
if not chosen:
|
||||
print("droiddeck-game-env: ignoring game cores %r" % text, file=sys.stderr)
|
||||
return
|
||||
try:
|
||||
os.sched_setaffinity(0, chosen)
|
||||
print("droiddeck-game-env: game cores %s" % ",".join(map(str, sorted(chosen))), file=sys.stderr)
|
||||
except OSError as error:
|
||||
print("droiddeck-game-env: could not use game cores %r (%s)" % (text, error), file=sys.stderr)
|
||||
|
||||
|
||||
def apply_config(env, config, app_id, fixes=None):
|
||||
if config.get("version") != 1:
|
||||
raise ValueError("unsupported environment configuration")
|
||||
@@ -574,6 +614,7 @@ def main():
|
||||
env["BANNER_AUDIO_DIRECT_MIC"] = "0"
|
||||
env = vram_cap(env)
|
||||
env = force_ssbs(command, env)
|
||||
game_cores(env)
|
||||
try:
|
||||
xbox = runpy.run_path(str(Path(__file__).with_name("droiddeck-xbox")))
|
||||
except Exception as error:
|
||||
|
||||
@@ -8,6 +8,7 @@ import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
from unittest import mock
|
||||
import unittest
|
||||
|
||||
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
|
||||
@@ -372,6 +373,25 @@ class GameEnvironmentTest(unittest.TestCase):
|
||||
self.assertTrue(notes)
|
||||
self.assertEqual(MODULE["engine_fixes"]("/game/Game/DarkSoulsII.exe", []), ({}, [], []))
|
||||
|
||||
def test_game_cores_are_parsed_from_lists_and_ranges(self):
|
||||
parse = MODULE["parse_cores"]
|
||||
self.assertEqual(parse("0-3,6"), {0, 1, 2, 3, 6})
|
||||
self.assertEqual(parse(" 4, 5 "), {4, 5})
|
||||
for bad in ("", "a", "3-1", "1,,2", "-1", "1-"):
|
||||
self.assertIsNone(parse(bad), bad)
|
||||
|
||||
def test_a_profiles_game_cores_pin_the_game_within_the_online_cores(self):
|
||||
calls = []
|
||||
os_mod = MODULE["os"]
|
||||
with mock.patch.dict(MODULE["game_cores"].__globals__, {"online_cores": lambda: {0, 1, 2, 3, 4, 5, 6, 7}}), \
|
||||
mock.patch.object(os_mod, "sched_setaffinity", side_effect=lambda pid, cores: calls.append(set(cores)), create=True):
|
||||
MODULE["game_cores"]({"DROIDDECK_GAME_CORES": "0-2,9"})
|
||||
MODULE["game_cores"]({"DROIDDECK_GAME_CORES": "junk"})
|
||||
MODULE["game_cores"]({"DROIDDECK_GAME_CORES": "12-13"})
|
||||
MODULE["game_cores"]({})
|
||||
self.assertEqual(calls, [{0, 1, 2}])
|
||||
self.assertEqual(MODULE["parse_cores"]("0,2-7"), {0, 2, 3, 4, 5, 6, 7})
|
||||
|
||||
def test_caravan_sandwitch_robocop_and_cyberpunk_run_at_feature_level_12_1(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
caravan = Path(tmp) / "Caravan SandWitch"
|
||||
|
||||
Reference in new issue
Block a user