From 7ce8b37824ee423e9b052f97c55c5147eb8b31ba Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Tue, 17 Nov 2026 16:20:32 -0500 Subject: [PATCH] fix(zram): back off ineffective idle compression (#2347) --- docs/development/low-memory.md | 9 ++- .../overlay/usr/local/bin/droiddeck-session | 24 +++++-- tools/tests/test_memory_pressure.py | 71 ++++++++++++++++++- 3 files changed, 95 insertions(+), 9 deletions(-) diff --git a/docs/development/low-memory.md b/docs/development/low-memory.md index ba7d5b87fa..48fd915cf3 100644 --- a/docs/development/low-memory.md +++ b/docs/development/low-memory.md @@ -75,9 +75,12 @@ finds different processes. The session request targets processes that loaded Requests are at least 30 seconds apart and need the same swap reserve as launcher requests. When the session has read back more than half of what the previous request compressed, that memory is in use: the request is skipped and the wait doubles, -up to five minutes. A request whose memory stayed compressed resets it to 30 seconds. Below half -the headroom the wait is ignored and only the 30-second spacing applies. The state resets when -the game exits. +up to five minutes. A completed request must also raise `MemAvailable` by at least one +sixteenth of the tier headroom, with a 16 MiB minimum; otherwise the same wait doubles because +the pass did not provide meaningful system headroom. A request whose memory stayed compressed or +whose available memory recovers resets the wait to 30 seconds. Below half the headroom the wait +is ignored and only the 30-second spacing applies, so critical pressure can still trigger a +request. The state resets when the game exits. Measured on a Retroid Pocket Duo (7.5 GiB, kernel 6.6, 6 GiB lzo-rle zram plus a 4 GiB swap file) entering the world in World of Warcraft: Forever, one run each: diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index 8d169120b4..faf46d9142 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -975,15 +975,16 @@ case $mode in } bl_session_pressure() { bl_memory_read + bl_memory_critical || _bl_tier_backoff=30 _bl_tier_nogain=0 [ "$_bl_game" = 1 ] && bl_memory_critical || return 0 [ "${BL_ZRAM_IDLE:-1}" = 1 ] && bl_game_focused 0 || return 0 - local _wait=$_bl_tier_backoff _before _pids _targets + local _wait=$_bl_tier_backoff _before _available_before _pids _targets _gain _min_gain [ "$_bl_available" -ge $((_bl_headroom / 2)) ] || [ "$_bl_tier_pushed" -lt 0 ] || _wait=30 [ "$_bl_tier_at" -lt 0 ] || [ $((SECONDS - _bl_tier_at)) -ge "$_wait" ] || return 0 if [ "$_bl_tier_at" -ge 0 ] && [ "$_bl_tier_pushed" -gt 0 ]; then bl_tier_readback if [ "$_bl_tier_back" -le $((_bl_tier_pushed / 2)) ]; then - _bl_tier_backoff=30 + [ "${_bl_tier_nogain:-0}" = 1 ] || _bl_tier_backoff=30 else _bl_tier_backoff=$((_bl_tier_backoff * 2 > 300 ? 300 : _bl_tier_backoff * 2)) _bl_tier_at=$SECONDS _bl_tier_pushed=-1 @@ -999,11 +1000,24 @@ case $mode in step "zRAM: low-memory page-out skipped, swap headroom too small ($((_bl_swap_free / 1024)) MB free)" return 0 fi - _targets=("${_pids[@]}") _before=$_bl_swap + _targets=("${_pids[@]}") _before=$_bl_swap _available_before=$_bl_available bl_zram_send 1 "low memory (available $((_bl_real_available / 1024)) MB), compressing idle game memory" session bl_zram_usage "${_targets[@]}" > /dev/null _bl_tier_pushed=$((_bl_swap - _before)) bl_tier_note "${_targets[@]}" + if [ "${_bl_pageout_complete:-0}" = 1 ]; then + bl_memory_read + _gain=$((_bl_available - _available_before)) + _min_gain=$((_bl_tier_headroom / 16)) + [ "$_min_gain" -ge $((16 * 1024)) ] || _min_gain=$((16 * 1024)) + if [ "$_gain" -lt "$_min_gain" ]; then + _bl_tier_backoff=$((_bl_tier_backoff * 2 > 300 ? 300 : _bl_tier_backoff * 2)) + _bl_tier_at=$SECONDS _bl_tier_nogain=1 + step "zRAM: low-memory page-out gained only $((_gain / 1024)) MB available, next try in ${_bl_tier_backoff} s" + else + _bl_tier_nogain=0 + fi + fi } bl_proc_start() { local _s= @@ -1125,7 +1139,7 @@ case $mode in [ "$_bl_ram_cap" -gt 0 ] && { [ "$_bl_measured" -lt 0 ] || [ $((SECONDS - _bl_measured)) -ge 30 ]; } } _bl_since=-1 _bl_since_apps= _bl_packed=-1 _bl_measured=-1 _bl_unpack=0 _bl_capped=-1 _bl_backoff=30 - _bl_pressure_at=-1 _bl_pressure_backoff=30 _bl_tier_at=-1 _bl_tier_backoff=30 _bl_tier_pushed=0 _bl_tier_back=0 _bl_tier_targets=() _bl_tier_swaps=() _bl_tier_starts=() + _bl_pressure_at=-1 _bl_pressure_backoff=30 _bl_tier_at=-1 _bl_tier_backoff=30 _bl_tier_nogain=0 _bl_tier_pushed=0 _bl_tier_back=0 _bl_tier_targets=() _bl_tier_swaps=() _bl_tier_starts=() _bl_client_pending=0 _bl_client_reserve=0 _bl_client_targets=() _bl_away= _bl_sample_at= _bl_sample_available=0 _bl_sample_gpu=0 _bl_sample_unevictable=0 _bl_sample_shmem=0 while kill -0 $$ 2>/dev/null; do @@ -1176,7 +1190,7 @@ case $mode in if [ "$_bl_packed" -ge 0 ]; then _bl_unpack=1 fi - _bl_since=-1 _bl_packed=-1 _bl_tier_at=-1 _bl_tier_backoff=30 _bl_tier_pushed=0 _bl_tier_targets=() _bl_tier_swaps=() _bl_tier_starts=() + _bl_since=-1 _bl_packed=-1 _bl_tier_at=-1 _bl_tier_backoff=30 _bl_tier_nogain=0 _bl_tier_pushed=0 _bl_tier_targets=() _bl_tier_swaps=() _bl_tier_starts=() else _bl_since=-1 if [ "$_bl_focus" = 1 ]; then diff --git a/tools/tests/test_memory_pressure.py b/tools/tests/test_memory_pressure.py index 20b6b484ca..1005ebe4dd 100644 --- a/tools/tests/test_memory_pressure.py +++ b/tools/tests/test_memory_pressure.py @@ -170,12 +170,13 @@ _bl_game=1 _bl_tier_at=-1 _bl_tier_backoff=30 _bl_tier_pushed=0 _bl_tier_back=0 _bl_pressure_at=-1 _bl_pressure_backoff=30 _bl_packed=-1 rss=$((1536 * 1024)) swap=0 pushed=$((1024 * 1024)) alive="999 321 654" started=100 complete=1 step() { :; } +meminfo=''' + repr(str(self.root / 'meminfo')) + ''' bl_game_focused() { [ "${focused:-1}" = 1 ]; } bl_proc_start() { _bl_start=; case " $alive " in *" $1 "*) _bl_start=$started; return 0 ;; esac; return 1; } nap() { echo "nap:$1"; } bl_session_others() { _bl_session=(321 654); } bl_zram_usage() { _bl_rss=$rss _bl_anon=${anon:-$rss} _bl_swap=$swap; } -bl_zram_send() { echo "send:$1:$3"; rss=$((rss - pushed)); swap=$((swap + pushed)); _bl_pageout_complete=$complete; } +bl_zram_send() { echo "send:$1:$3"; rss=$((rss - pushed)); swap=$((swap + pushed)); next_available=${available_after:-$(( $(awk '/^MemAvailable:/ { print $2 }' "$meminfo") / 1024 + 128 ))}; sed -i "s/^MemAvailable:.*/MemAvailable: $((next_available * 1024)) kB/" "$meminfo"; _bl_pageout_complete=$complete; } ''' + functions + '\nbl_memory_read\n' + body result = run_bash('NOW=0\n' + harness.replace('SECONDS', 'NOW')) self.assertEqual(0, result.returncode, result.stderr) @@ -318,6 +319,57 @@ echo "backoff:$_bl_tier_backoff" _bl_tier_backoff=240 _bl_tier_at=0 _bl_tier_targets=(999) _bl_tier_swaps=($((1024 * 1024))) _bl_tier_starts=(100) _bl_tier_pushed=$((1024 * 1024)) swap=0 SECONDS=240; bl_session_pressure echo "backoff:$_bl_tier_backoff" +''')) + + def test_no_gain_pageout_doubles_the_tier_wait(self): + self.assertEqual(['send:1:session', 'backoff:60', 'send:1:session', 'backoff:120'], self.run_policy(''' +available_after=1024 +SECONDS=0; bl_session_pressure +echo "backoff:$_bl_tier_backoff" +SECONDS=30; bl_session_pressure +SECONDS=60; bl_session_pressure +echo "backoff:$_bl_tier_backoff" +''')) + + def test_meaningful_available_gain_keeps_the_tier_wait(self): + self.assertEqual(['send:1:session', 'backoff:30', 'send:1:session', 'backoff:30'], self.run_policy(''' +available_after=1300 +SECONDS=0; bl_session_pressure +echo "backoff:$_bl_tier_backoff" +available_after=1600 +SECONDS=30; bl_session_pressure +echo "backoff:$_bl_tier_backoff" +''')) + + def test_recovered_available_memory_resets_the_tier_wait(self): + self.assertEqual(['send:1:session', 'backoff:60', 'backoff:30', 'send:1:session'], self.run_policy(''' +available_after=1024 +SECONDS=0; bl_session_pressure +echo "backoff:$_bl_tier_backoff" +available_after=2000 +sed -i "s/^MemAvailable:.*/MemAvailable: 2048000 kB/" "$meminfo" +SECONDS=30; bl_session_pressure +echo "backoff:$_bl_tier_backoff" +available_after=1024 +sed -i "s/^MemAvailable:.*/MemAvailable: 1048576 kB/" "$meminfo" +SECONDS=60; bl_session_pressure +''')) + + def test_critical_memory_still_compresses_after_a_no_gain_pass(self): + self.memory(available=512) + self.assertEqual(['send:1:session', 'send:1:session'], self.run_policy(''' +available_after=512 +SECONDS=0; bl_session_pressure +SECONDS=30; bl_session_pressure +''')) + + def test_a_no_gain_pass_still_skips_memory_the_game_read_back(self): + self.memory(available=512) + self.assertEqual(['send:1:session'], self.run_policy(''' +available_after=512 +SECONDS=0; bl_session_pressure +swap=0 +SECONDS=30; bl_session_pressure ''')) def test_nearly_out_of_memory_ignores_the_backoff_but_keeps_30_seconds(self): @@ -426,6 +478,23 @@ echo "targets:${_bl_tier_targets[*]}" alive="999 1234" SECONDS=30; bl_session_pressure echo "backoff:$_bl_tier_backoff" +''')) + + def test_read_back_wait_holds_above_the_critical_level(self): + self.assertEqual(['send:1:session', 't:60', 'send:1:session'], self.run_policy(''' +SECONDS=0; bl_session_pressure +swap=0 +SECONDS=30; bl_session_pressure +SECONDS=60; bl_session_pressure; echo t:60 +SECONDS=90; bl_session_pressure +''')) + + def test_a_no_gain_pageout_preserves_client_timestamps(self): + self.assertEqual(['send:1:session', 'backoff:60', 'client:-1:-1'], self.run_policy(''' +available_after=1024 +SECONDS=0; bl_session_pressure +echo "backoff:$_bl_tier_backoff" +echo "client:$_bl_pressure_at:$_bl_packed" ''')) def test_an_incomplete_pageout_does_not_restart_the_launcher_wait(self):