feat: adjustable RAM and VRAM caps for the container (#1537)

* feat: adjustable RAM and VRAM caps for the container

Performance > Memory gets two sliders:

- RAM cap (soft, Steam sessions): when the session is resident above the
  cap, the session watcher compresses the Steam client into zRAM, waits for
  its page-out acknowledgment, then compresses the rest of the session
  (processes that loaded libblsession) if still over. Nothing is closed.
  Measured at most every 30 s; the client is read back once usage drops
  below the cap. Invalid values leave the cap off. Needs zRAM swap.
- VRAM cap: sets dxgi.maxDeviceMemory ahead of the game's own DXVK_CONFIG,
  so Direct3D games budget for less video memory. A game profile still
  wins. Applies from the next session.

* fix: back off the RAM cap when paging out cannot get under it

A cap below the game's working set was never met, so the watcher paged out
the client and the whole session every 30 s for the rest of the session.
Usage is still measured every 30 s, but after a page-out that leaves the
session over the cap the next one waits twice as long (60 s up to 10 min),
resetting once usage is under the cap. The client step is skipped while the
client is already under 512 MB resident.

* fix: RAM cap covers desktop Steam, signals each group once, no idle churn

- Steam opened from the desktop runs the same session watcher, so the cap
  is exported whenever the Steam client runs, not only in Steam mode.
- The second page-out goes to the rest of the session only; Steam was
  signalled again even when its own step had been skipped as already small.
- A cap page-out while no game runs stays paged out. Reading the client back
  right away pushed it over the cap again and repeated every minute; only a
  game closing while over the cap still defers a read-back.

* fix: RAM cap marks the client packed only when it compressed it

When the cap skipped the client as already small during a game, it still
recorded the client as packed, so the regular in-game client page-out
waited up to 300 s for a page-out that never happened.

---------

Co-authored-by: jazzyjames <182448902+jazzyjamesgames@users.noreply.github.com>
This commit is contained in:
Jamison Prineandjazzyjames authored and GitHub committed 2026-10-25 12:06:45 -04:00
1 parent dfe8227f49
commit 2be08bc67d
10 files changed
+180 -13

No files matched your search

@@ -287,6 +287,18 @@ def game_gpu(env):
return result
def vram_cap(env):
"""Puts the session's VRAM cap ahead of the game's own DXVK settings, so a game profile can still raise it."""
value = env.get("BL_VRAM_MB", "")
if not re.fullmatch(r"[1-9][0-9]{0,6}", value):
return env
result = env.copy()
cap = "dxgi.maxDeviceMemory = %d" % int(value)
own = result.get("DXVK_CONFIG", "").strip().strip(";").strip()
result["DXVK_CONFIG"] = cap + "; " + own if own else cap
return result
def apply_config(env, config, app_id, fixes=None):
if config.get("version") != 1:
raise ValueError("unsupported environment configuration")
@@ -560,6 +572,7 @@ def main():
command = offline_without_anticheat(command, env)
if mic_enabled == "0":
env["BANNER_AUDIO_DIRECT_MIC"] = "0"
env = vram_cap(env)
env = force_ssbs(command, env)
try:
xbox = runpy.run_path(str(Path(__file__).with_name("droiddeck-xbox")))
@@ -446,48 +446,134 @@ case $mode in
fi
if [ "${BL_ZRAM:-0}" = 1 ]; then
step "zRAM compression: on"
_bl_ram_cap=0
case ${BL_RAM_CAP_MB:-} in
''|0) ;;
*[!0-9]*|0*|???????*) step "RAM cap: ignored invalid value '$BL_RAM_CAP_MB'" ;;
*) _bl_ram_cap=$((BL_RAM_CAP_MB * 1024)); step "RAM cap: ${BL_RAM_CAP_MB} MB" ;;
esac
(
bl_zram_scan() {
_bl_game=0 _bl_pids=()
_bl_game=0 _bl_pids=() _bl_others=()
for _d in /proc/[0-9]*; do
[ "${_d#/proc/}" != "$BASHPID" ] || continue
# Under FEX the program is FEX's first argument.
{ IFS= read -r -d '' _c && case ${_c##*/} in FEX|FEXInterpreter) IFS= read -r -d '' _c ;; esac; } \
2>/dev/null < "$_d/cmdline" || continue
case ${_c##*/} in
reaper) _bl_game=1 ;;
steam|steamwebhelper) _bl_pids+=("${_d#/proc/}") ;;
*) _bl_others+=("${_d#/proc/}") ;;
esac
done
}
bl_zram_usage() {
local _p _k _v _u _rss=0 _swap=0
for _p in "${_bl_pids[@]}"; do
for _p in "$@"; do
while read -r _k _v _u; do
case $_k in VmRSS:) _rss=$((_rss + _v)) ;; VmSwap:) _swap=$((_swap + _v)) ;; esac
done 2>/dev/null < "/proc/$_p/status"
done
echo "${#_bl_pids[@]} processes, resident $((_rss / 1024)) MB, in zram $((_swap / 1024)) MB"
_bl_rss=$_rss
echo "$# processes, resident $((_rss / 1024)) MB, in zram $((_swap / 1024)) MB"
}
# Only processes that loaded libblsession are the session's own and answer SIGURG.
bl_session_others() {
local _p
_bl_session=()
for _p in "${_bl_others[@]}"; do
grep -q -m1 '/libblsession\.so$' "/proc/$_p/maps" 2>/dev/null && _bl_session+=("$_p")
done
}
bl_zram_pick() {
if [ "$1" = session ]; then
bl_session_others
_pids=("${_bl_session[@]}")
else
_pids=("${_bl_pids[@]}")
fi
}
# Same protocol as SessionSuspendController: an empty ack when the page-out starts, a byte when it is done.
# A process whose SIGURG handler was replaced never acks, so it is not waited for after the first 2 s.
bl_zram_wait() {
local _p _left _start=$SECONDS
while [ $((SECONDS - _start)) -lt 12 ]; do
_left=0
for _p in "$@"; do
[ -s "/tmp/.bl-zram.ack.$_p" ] && continue
[ -d "/proc/$_p" ] || continue
[ $((SECONDS - _start)) -ge 2 ] && [ ! -e "/tmp/.bl-zram.ack.$_p" ] && continue
_left=1
done
[ "$_left" = 1 ] || break
nap 0.2
done
for _p in "$@"; do rm -f "/tmp/.bl-zram.ack.$_p"; done
}
bl_zram_send() {
step "zRAM: $2 ($(bl_zram_usage))"
printf '%s' "$1" > /tmp/.bl-zram
[ "${#_bl_pids[@]}" = 0 ] || kill -URG "${_bl_pids[@]}" 2>/dev/null
nap 5
local _mode=$1 _what=$2 _which=$3 _pids _p
bl_zram_pick "$_which"
step "zRAM: $_what ($(bl_zram_usage "${_pids[@]}"))"
for _p in "${_pids[@]}"; do rm -f "/tmp/.bl-zram.ack.$_p"; done
printf '%s' "$_mode" > /tmp/.bl-zram
[ "${#_pids[@]}" = 0 ] || kill -URG "${_pids[@]}" 2>/dev/null
if [ "$_mode" = 1 ]; then bl_zram_wait "${_pids[@]}"; else nap 5; fi
bl_zram_scan
step "zRAM: done ($(bl_zram_usage))"
bl_zram_pick "$_which"
step "zRAM: done ($(bl_zram_usage "${_pids[@]}"))"
}
_bl_since=-1 _bl_packed=-1
bl_over_cap() {
[ "$_bl_ram_cap" -gt 0 ] || return 1
_bl_measured=$SECONDS
bl_session_others
bl_zram_usage "${_bl_pids[@]}" "${_bl_session[@]}" > /dev/null
[ "$_bl_rss" -gt "$_bl_ram_cap" ]
}
bl_cap_due() {
[ "$_bl_ram_cap" -gt 0 ] && { [ "$_bl_measured" -lt 0 ] || [ $((SECONDS - _bl_measured)) -ge 30 ]; }
}
_bl_since=-1 _bl_packed=-1 _bl_measured=-1 _bl_unpack=0 _bl_capped=-1 _bl_backoff=30
while kill -0 $$ 2>/dev/null; do
bl_zram_scan
if bl_cap_due; then
if bl_over_cap; then
# A cap below the game's working set cannot be met; paging out again every 30 s only churns.
if [ "$_bl_capped" -lt 0 ] || [ $((SECONDS - _bl_capped)) -ge "$_bl_backoff" ]; then
bl_zram_usage "${_bl_pids[@]}" > /dev/null
if [ "$_bl_rss" -ge $((512 * 1024)) ]; then
bl_zram_send 1 "over the ${BL_RAM_CAP_MB} MB RAM cap, compressing the client" client
[ "$_bl_game" = 1 ] && _bl_packed=$SECONDS
else
step "RAM cap: over, client already small ($((_bl_rss / 1024)) MB resident), skipping it"
fi
bl_over_cap && bl_zram_send 1 "still over the RAM cap, compressing the rest of the session" session
if bl_over_cap; then
_bl_backoff=$((_bl_backoff * 2 > 600 ? 600 : _bl_backoff * 2))
step "RAM cap: still over after paging out, next try in ${_bl_backoff} s"
else
_bl_backoff=30
fi
_bl_capped=$SECONDS
fi
else
_bl_backoff=30
if [ "$_bl_unpack" = 1 ] && [ "$_bl_game" = 0 ]; then
bl_zram_send 0 "back under the RAM cap, reading the client back" client
_bl_unpack=0
fi
fi
fi
if [ "$_bl_game" = 1 ]; then
[ "$_bl_since" -ge 0 ] || _bl_since=$SECONDS
if [ $((SECONDS - _bl_since)) -ge 20 ] && [ "${#_bl_pids[@]}" -gt 0 ] &&
{ [ "$_bl_packed" -lt 0 ] || [ $((SECONDS - _bl_packed)) -ge 300 ]; }; then
bl_zram_send 1 "compressing the client"
bl_zram_send 1 "compressing the client" client
_bl_packed=$SECONDS
fi
elif [ "$_bl_since" -ge 0 ]; then
[ "$_bl_packed" -lt 0 ] || bl_zram_send 0 "game closed, reading the client back"
if [ "$_bl_packed" -ge 0 ]; then
if bl_over_cap; then _bl_unpack=1; else bl_zram_send 0 "game closed, reading the client back" client; _bl_unpack=0; fi
fi
_bl_since=-1 _bl_packed=-1
fi
nap 5