mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 23:36:04 +00:00
perf(epic): keep the hidden launcher from loading its store and Fab pages, and compress it while games run (#1764)
* perf(epic): keep the hidden launcher from loading its store and Fab pages, and compress it while games run The hidden Epic Games Launcher that stays up for sign-in while a game runs pre-warmed its store and Fab pages on a display nobody sees. Under FEX each page used about one core and grew past a gigabyte, crashing and reloading in a loop. The hidden instance now skips the store pre-warm, gets a blank page in place of Fab, and is paged out to zRAM with the Steam client once a game has focus. The visible launcher is unchanged. * test(zram): keep the scan fixture PIDs above the kernel PID range
This commit is contained in:
1 parent
29c83976d0
commit
9f7f6d821d
7 files changed
+184
-8
No files matched your search
@@ -45,7 +45,7 @@ lib=$out/x86_64/libwebkitinput.so
|
||||
gcc -m64 -shared -fPIC -O2 -Wall -Wextra -nostdlib -fno-stack-protector -fno-builtin -fvisibility=hidden \
|
||||
-o "$lib" tools/linuxfs/fex/webkitinput.c
|
||||
strip --strip-unneeded "$lib"
|
||||
if objdump -p "$lib" | grep -q NEEDED || [ "$(nm -D --undefined-only "$lib" | awk '{print $1 $2}' | LC_ALL=C sort | tr '\n' ' ')" != "wdlsym wg_object_get_data wg_object_set wg_object_set_data wg_type_check_instance_is_a wgtk_container_forall wgtk_container_get_type wgtk_key_snooper_install wgtk_widget_get_toplevel wwebkit_user_script_new wwebkit_user_script_unref wwebkit_web_view_get_type wwebkit_web_view_get_uri wwebkit_web_view_run_javascript " ]; then
|
||||
if objdump -p "$lib" | grep -q NEEDED || [ "$(nm -D --undefined-only "$lib" | awk '{print $1 $2}' | LC_ALL=C sort | tr '\n' ' ')" != "wdlsym wg_getenv wg_object_get_data wg_object_set wg_object_set_data wg_type_check_instance_is_a wgtk_container_forall wgtk_container_get_type wgtk_key_snooper_install wgtk_widget_get_toplevel wwebkit_user_script_new wwebkit_user_script_unref wwebkit_web_view_get_type wwebkit_web_view_get_uri wwebkit_web_view_run_javascript " ]; then
|
||||
echo "$lib must need nothing but weak GLib, GTK and WebKit calls" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -22,6 +22,7 @@ extern void webkit_web_view_run_javascript(void *view, const char *script, void
|
||||
extern WebKitUserScript *webkit_user_script_new(const char *source, int frames, int time,
|
||||
const char *const *allow, const char *const *block) __attribute__((weak));
|
||||
extern void webkit_user_script_unref(WebKitUserScript *script) __attribute__((weak));
|
||||
extern const char *g_getenv(const char *name) __attribute__((weak));
|
||||
|
||||
static const char NAV_SCRIPT[] =
|
||||
"(()=>{let open=-1e9,arrowAt=-1e9,ringed=null,ringOutline='',ringOffset='',settleTimer=0,settling=false,redirecting=false;"
|
||||
@@ -196,3 +197,45 @@ __attribute__((visibility("default"))) void webkit_user_content_manager_add_scri
|
||||
}
|
||||
next(manager, script);
|
||||
}
|
||||
|
||||
static int starts(const char *text, const char *prefix)
|
||||
{
|
||||
while (*prefix)
|
||||
if (*text++ != *prefix++)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int fab_uri(const char *uri)
|
||||
{
|
||||
if (!uri)
|
||||
return 0;
|
||||
if (starts(uri, "https://"))
|
||||
uri += 8;
|
||||
else if (starts(uri, "http://"))
|
||||
uri += 7;
|
||||
else
|
||||
return 0;
|
||||
if (starts(uri, "www."))
|
||||
uri += 4;
|
||||
if (!starts(uri, "fab.com"))
|
||||
return 0;
|
||||
uri += 7;
|
||||
return !*uri || *uri == '/' || *uri == '?' || *uri == '#' || *uri == ':';
|
||||
}
|
||||
|
||||
static int hidden_launcher(void)
|
||||
{
|
||||
const char *hidden = g_getenv ? g_getenv("DROIDDECK_EPIC_HIDDEN") : 0;
|
||||
return hidden && hidden[0] == '1' && !hidden[1];
|
||||
}
|
||||
|
||||
__attribute__((visibility("default"))) void webkit_web_view_load_uri(void *view, const char *uri)
|
||||
{
|
||||
static void (*next)(void *, const char *);
|
||||
if (!next && dlsym)
|
||||
next = (void (*)(void *, const char *))dlsym((void *)-1l, "webkit_web_view_load_uri");
|
||||
if (!next)
|
||||
return;
|
||||
next(view, hidden_launcher() && fab_uri(uri) ? "about:blank" : uri);
|
||||
}
|
||||
@@ -100,6 +100,10 @@ CLIENT_ENV = {
|
||||
"JSC_useDFGJIT": "0",
|
||||
"JSC_useFTLJIT": "0",
|
||||
}
|
||||
HIDDEN_ENV = {
|
||||
"DROIDDECK_EPIC_HIDDEN": "1",
|
||||
"EGL_DISABLE_STORE_PREWARM": "1",
|
||||
}
|
||||
KEPT_ENV = ("HOME", "USER", "LOGNAME", "LANG", "LANGUAGE", "LC_ALL", "LC_MESSAGES", "TZ", "XDG_RUNTIME_DIR", "BL_LAUNCH_DIR")
|
||||
LAUNCH_VERBS = ("waitforexitandrun", "run", "runinprefix")
|
||||
EPIC_ARGS = ("-auth_", "-epic")
|
||||
@@ -725,7 +729,7 @@ def fex_environment(fex, files, env):
|
||||
return result
|
||||
|
||||
|
||||
def client_environment(base, fex, files, display, bus, broker, a11y=None):
|
||||
def client_environment(base, fex, files, display, bus, broker, a11y=None, hidden=False):
|
||||
env = {name: base[name] for name in KEPT_ENV if base.get(name)}
|
||||
env.setdefault("HOME", HOME)
|
||||
env.setdefault("LANG", "C.UTF-8")
|
||||
@@ -734,6 +738,8 @@ def client_environment(base, fex, files, display, bus, broker, a11y=None):
|
||||
env["DBUS_SESSION_BUS_ADDRESS"] = bus
|
||||
env[BROKER_ENV] = broker
|
||||
env.update(CLIENT_ENV)
|
||||
if hidden:
|
||||
env.update(HIDDEN_ENV)
|
||||
if a11y:
|
||||
for name in A11Y_OFF:
|
||||
env.pop(name, None)
|
||||
@@ -1248,7 +1254,7 @@ class Session:
|
||||
path = broker_path()
|
||||
self.broker = Broker(path, self.mode, base)
|
||||
write_json(instance_file(), {"pid": os.getpid(), "mode": self.mode, "bus": bus, "broker": path})
|
||||
env = client_environment(base, fex, files, display, bus, path, self.a11y)
|
||||
env = client_environment(base, fex, files, display, bus, path, self.a11y, hidden=self.mode == "hidden")
|
||||
self.command = [fex["bin"], os.path.join(app_dir, "AppRun")]
|
||||
self.env = env
|
||||
say("starting the Epic Games Launcher %s (%s, display %s)" % (current.get("app"), self.mode, display))
|
||||
|
||||
@@ -511,7 +511,7 @@ case $mode in
|
||||
fi
|
||||
if [ "${BL_ZRAM:-0}" = 1 ]; then
|
||||
step "zRAM compression: on"
|
||||
[ "${BL_ZRAM_CLIENT:-1}" = 1 ] || step "zRAM: Steam is not compressed while games run (turned off in Performance)"
|
||||
[ "${BL_ZRAM_CLIENT:-1}" = 1 ] || step "zRAM: Steam and the hidden Epic Games Launcher are not compressed while games run (turned off in Performance)"
|
||||
_bl_ram_cap=0
|
||||
case ${BL_RAM_CAP_MB:-} in
|
||||
''|0) ;;
|
||||
@@ -521,16 +521,16 @@ case $mode in
|
||||
(
|
||||
bl_zram_scan() {
|
||||
_bl_game=0 _bl_pids=() _bl_others=() _bl_apps=
|
||||
local _d _c _args _name _a _id
|
||||
local _d _c _args _name _a _id _fex
|
||||
for _d in /proc/[0-9]*; do
|
||||
[ "${_d##*/}" != "$BASHPID" ] || continue
|
||||
_args=()
|
||||
# Under FEX the program is FEX's first argument. AppId= is its own argument.
|
||||
while IFS= read -r -d '' _c; do _args+=("$_c"); done 2>/dev/null < "$_d/cmdline" || true
|
||||
[ "${#_args[@]}" -gt 0 ] || continue
|
||||
_name=${_args[0]}
|
||||
_name=${_args[0]} _fex=0
|
||||
if [ "${#_args[@]}" -gt 1 ]; then
|
||||
case ${_name##*/} in FEX|FEXInterpreter) _name=${_args[1]} ;; esac
|
||||
case ${_name##*/} in FEX|FEXInterpreter) _name=${_args[1]} _fex=1 ;; esac
|
||||
fi
|
||||
case ${_name##*/} in
|
||||
reaper)
|
||||
@@ -545,7 +545,13 @@ case $mode in
|
||||
done
|
||||
;;
|
||||
steam|steamwebhelper) _bl_pids+=("${_d##*/}") ;;
|
||||
*) _bl_others+=("${_d##*/}") ;;
|
||||
*)
|
||||
if [ "$_fex" = 1 ] && grep -qz '^DROIDDECK_EPIC_HIDDEN=1$' "$_d/environ" 2>/dev/null; then
|
||||
_bl_pids+=("${_d##*/}")
|
||||
else
|
||||
_bl_others+=("${_d##*/}")
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
@@ -19,6 +19,30 @@ def run_bash(script, env=None):
|
||||
|
||||
|
||||
class ClientZramFocusTest(unittest.TestCase):
|
||||
def test_the_hidden_epic_launcher_is_compressed_with_the_client(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
proc = Path(tmp)
|
||||
|
||||
def task(pid, argv, env=()):
|
||||
folder = proc / str(pid)
|
||||
folder.mkdir()
|
||||
(folder / 'cmdline').write_bytes(b''.join(arg.encode() + b'\0' for arg in argv))
|
||||
(folder / 'environ').write_bytes(b''.join(item.encode() + b'\0' for item in env))
|
||||
|
||||
fex = '/s/FEX-Emu/usr/bin/FEX'
|
||||
task(5000010, ['/s/steamrtarm64/steam', '-gamepadui'])
|
||||
task(5000011, [fex, '/e/usr/bin/EpicGamesLauncher'], ['HOME=/root', 'DROIDDECK_EPIC_HIDDEN=1'])
|
||||
task(5000012, [fex, './/lib/webkit2gtk-4.1/WebKitWebProcess', '4'], ['DROIDDECK_EPIC_HIDDEN=1', 'LANG=C'])
|
||||
task(5000013, [fex, '/e/usr/bin/EpicGamesLauncher'], ['HOME=/root'])
|
||||
task(5000014, ['/e/WebKitWebProcess', '4'], ['DROIDDECK_EPIC_HIDDEN=1'])
|
||||
task(5000015, [fex, '/e/usr/bin/EpicGamesLauncher'], ['DROIDDECK_EPIC_HIDDEN=10'])
|
||||
task(5000016, ['/s/reaper', 'SteamLaunch', 'AppId=413150', '--', 'game'])
|
||||
source = function_source('bl_zram_scan').replace('/proc/[0-9]*', tmp + '/[0-9]*')
|
||||
result = run_bash(source + '\nbl_zram_scan\necho "${_bl_pids[*]}|${_bl_others[*]}|$_bl_game|$_bl_apps"')
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
self.assertEqual('', result.stderr)
|
||||
self.assertEqual('5000010 5000011 5000012|5000013 5000014 5000015|1| 413150', result.stdout.strip())
|
||||
|
||||
def test_refocus_restarts_the_delay_and_game_exit_restores_a_packed_client(self):
|
||||
source = SCRIPT.read_text()
|
||||
start = source.index(' if [ "$_bl_game" = 1 ] && bl_game_focused; then')
|
||||
|
||||
@@ -326,6 +326,15 @@ class SignInHandoffTest(EpicTestCase):
|
||||
self.assertEqual((None, None, "unix:abstract=a11y"),
|
||||
(carried.get("NO_AT_BRIDGE"), carried.get("GTK_A11Y"), carried.get("AT_SPI_BUS_ADDRESS")))
|
||||
|
||||
def test_only_the_hidden_launcher_skips_the_store_and_fab_pages(self):
|
||||
with mock.patch.dict(G, {"fex_environment": lambda fex, files, base: {}}):
|
||||
visible = G["client_environment"]({}, None, None, ":0", "bus", "broker")
|
||||
hidden = G["client_environment"]({}, None, None, ":2", "bus", "broker", hidden=True)
|
||||
self.assertNotIn("DROIDDECK_EPIC_HIDDEN", visible)
|
||||
self.assertNotIn("EGL_DISABLE_STORE_PREWARM", visible)
|
||||
self.assertEqual(("1", "1"), (hidden.get("DROIDDECK_EPIC_HIDDEN"), hidden.get("EGL_DISABLE_STORE_PREWARM")))
|
||||
self.assertEqual(visible["WEBKIT_SKIA_ENABLE_CPU_RENDERING"], hidden["WEBKIT_SKIA_ENABLE_CPU_RENDERING"])
|
||||
|
||||
def session(self):
|
||||
session = G["Session"]("visible")
|
||||
session.carried = True
|
||||
@@ -906,6 +915,20 @@ class BrokerTest(EpicTestCase):
|
||||
self.assertNotIn(G["STEAM_PRELOAD"], envs[0])
|
||||
self.assertNotIn("LD_PRELOAD", envs[1])
|
||||
|
||||
|
||||
def test_a_game_started_by_the_hidden_launcher_does_not_inherit_its_page_switches(self):
|
||||
folder = self.tmp / "Fig"
|
||||
folder.mkdir()
|
||||
envs = []
|
||||
|
||||
def popen(command, env=None, cwd=None, start_new_session=False):
|
||||
envs.append(env)
|
||||
return mock.Mock(wait=mock.Mock(return_value=0))
|
||||
|
||||
with mock.patch.object(subprocess, "Popen", popen):
|
||||
G["run_game"]({"exe": str(folder / "Fig.exe"), "args": []}, dict(G["HIDDEN_ENV"], PATH="/usr/bin"))
|
||||
self.assertFalse(set(G["HIDDEN_ENV"]) & set(envs[0]))
|
||||
self.assertEqual("/usr/bin", envs[0]["PATH"])
|
||||
def test_an_owner_gone_ends_a_game_the_open_launcher_handed_to_steam(self):
|
||||
broker, _ = self.broker("visible")
|
||||
session = G["Session"]("visible")
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import os
|
||||
import platform
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
SOURCE = ROOT / "tools/linuxfs/fex/webkitinput.c"
|
||||
|
||||
WEBKIT = r"""
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
const char *g_getenv(const char *name) { return getenv(name); }
|
||||
void webkit_web_view_load_uri(void *view, const char *uri) { (void)view; printf("%s\n", uri); }
|
||||
"""
|
||||
|
||||
LOADER = r"""
|
||||
void webkit_web_view_load_uri(void *view, const char *uri);
|
||||
int main(int argc, char **argv) {
|
||||
for (int i = 1; i < argc; i++) webkit_web_view_load_uri(0, argv[i]);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
URIS = [
|
||||
"https://www.fab.com",
|
||||
"https://fab.com/listings/1",
|
||||
"http://www.fab.com?x=1",
|
||||
"https://www.fab.com:443/",
|
||||
"https://store.epicgames.com/en-US/",
|
||||
"https://www.fab.community/",
|
||||
"https://notfab.com/",
|
||||
"tauri://localhost",
|
||||
]
|
||||
|
||||
|
||||
@unittest.skipUnless(platform.machine() == "x86_64" and shutil.which("gcc"), "webkitinput is built for x86_64 guests")
|
||||
class WebKitInputTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.tmp = Path(tempfile.mkdtemp())
|
||||
(cls.tmp / "webkit.c").write_text(WEBKIT)
|
||||
(cls.tmp / "loader.c").write_text(LOADER)
|
||||
subprocess.run(["gcc", "-m64", "-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-nostdlib", "-fno-stack-protector",
|
||||
"-fno-builtin", "-fvisibility=hidden", "-o", str(cls.tmp / "libwebkitinput.so"), str(SOURCE)], check=True)
|
||||
subprocess.run(["gcc", "-shared", "-fPIC", "-o", str(cls.tmp / "libfakewebkit.so"), str(cls.tmp / "webkit.c")], check=True)
|
||||
subprocess.run(["gcc", "-o", str(cls.tmp / "loader"), str(cls.tmp / "loader.c"), "-L" + str(cls.tmp),
|
||||
"-Wl,--no-as-needed", "-lwebkitinput", "-lfakewebkit", "-ldl", "-Wl,-rpath," + str(cls.tmp)], check=True)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
shutil.rmtree(cls.tmp, ignore_errors=True)
|
||||
|
||||
def load(self, hidden):
|
||||
env = {k: v for k, v in os.environ.items() if k != "DROIDDECK_EPIC_HIDDEN"}
|
||||
if hidden is not None:
|
||||
env["DROIDDECK_EPIC_HIDDEN"] = hidden
|
||||
result = subprocess.run([str(self.tmp / "loader")] + URIS, env=env, capture_output=True, text=True, timeout=10)
|
||||
self.assertEqual(0, result.returncode, result.stderr)
|
||||
return result.stdout.splitlines()
|
||||
|
||||
def test_the_hidden_launcher_loads_a_blank_page_for_fab(self):
|
||||
self.assertEqual(["about:blank"] * 4 + URIS[4:], self.load("1"))
|
||||
|
||||
def test_the_visible_launcher_loads_every_page(self):
|
||||
self.assertEqual(URIS, self.load(None))
|
||||
self.assertEqual(URIS, self.load("0"))
|
||||
self.assertEqual(URIS, self.load("10"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user