From 9d2580d502ba901a09909dcb2df5554e0f760a14 Mon Sep 17 00:00:00 2001
From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
Date: Thu, 5 Nov 2026 18:27:35 -0500
Subject: [PATCH] perf: reclaim launcher memory earlier under pressure (#2138)
* perf: reclaim launcher memory under system pressure
* docs: record Caravan save-load OOM with spare swap
* fix: wait for completed reclaim before increasing backoff
---
app/src/main/res/values/strings.xml | 2 +-
docs/development/low-memory.md | 86 ++++++++
tools/experiments/anonymous-memory-pager.c | 202 ++++++++++++++++++
.../overlay/usr/local/bin/droiddeck-session | 88 +++++++-
tools/tests/test_client_zram_focus.py | 27 ++-
tools/tests/test_memory_pressure.py | 141 ++++++++++++
6 files changed, 535 insertions(+), 11 deletions(-)
create mode 100644 docs/development/low-memory.md
create mode 100644 tools/experiments/anonymous-memory-pager.c
create mode 100644 tools/tests/test_memory_pressure.py
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index df5cf9ac28..f82841a888 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -627,7 +627,7 @@
Off
Driver default
Compress Steam while a game runs
- 20 s after a game starts, the Steam client is compressed into zRAM to leave the game more memory, and read back when the game closes. Turn this off if the session closes shortly after games start. A RAM cap above still compresses Steam when the session goes over it.
+ Compresses Steam while a game runs, starting sooner when memory is tight. Steam is read back after the game closes once enough RAM is available. Turn this off if the session closes shortly after games start. A RAM cap above still compresses Steam when the session goes over it.
RAM cap
Applies while the Steam client runs, in Steam mode or from the desktop. When the session uses more than this, Steam and then the session\'s other supported processes are compressed into zRAM. A soft cap: nothing is closed, but games may stutter while pages come back.
Needs zRAM swap, which this device does not have.
diff --git a/docs/development/low-memory.md b/docs/development/low-memory.md
new file mode 100644
index 0000000000..398dbf9132
--- /dev/null
+++ b/docs/development/low-memory.md
@@ -0,0 +1,86 @@
+# Low-memory reclaim and userspace compression
+
+The Steam session monitor uses the existing `libblsession.so` page-out request to reclaim
+private anonymous launcher memory. The default remains a request 20 seconds after game focus,
+with five minutes between requests. With client compression enabled, low system headroom can
+also trigger a request while a game is loading, before its first focused window.
+
+Every five seconds the monitor reads `MemAvailable`, swap headroom and memory PSI. Low
+headroom means less than one fifth of total RAM (bounded to 512–1536 MiB), or at least 1%
+`some avg10` memory stall with less than one third of RAM available. Pressure requests target
+only the launcher processes already recognized by the monitor, never the game. They require
+at least 128 MiB launcher RSS and enough free swap to cover its entire RSS while leaving
+one fifth of swap (at least 256 MiB) free. The regular automatic client request uses the
+same swap guard. An explicit session RAM cap retains its existing behavior.
+
+Pressure requests are at least 30 seconds apart. Reclaiming less than 16 MiB doubles the
+retry delay, up to five minutes. After game exit, speculative launcher readback is deferred
+until the session is under any explicit RAM cap, pressure has subsided, and available RAM
+can cover twice the launcher's swap while leaving the headroom reserve. Normal demand paging
+continues while readback is deferred; Steam does not have to wait for the monitor to recover.
+Missing memory counters disable automatic pressure reclaim and speculative readback. Missing
+PSI alone still permits the `MemAvailable` fallback. No root settings, swap sizes, compression
+algorithms, Android process priorities or game-specific settings are changed.
+
+A bounded live test of the pressure helper on the FIT during Caravan SandWitch loading
+requested page-out at 20:52:37 UTC, 18 seconds before the installed timed reclaimer. Aggregate
+launcher RSS fell from 608 to 283 MiB across that request; this is summed process RSS, not a
+measurement of net physical RAM saved. The game reached its main menu at 60 FPS. The test ran
+the candidate monitor functions alongside the old APK's monitor, not a newly built APK;
+the old monitor's later request is visible separately at 20:52:55 in the session log. This
+does not establish gameplay performance or resolve Stellar Blade's larger memory failure.
+
+Continuing an existing Caravan save reproduced a whole-session low-memory kill with the
+pressure helper running. Android recorded `ApplicationExitInfo` reason `LOW_MEMORY`, and
+`lmkd` killed the foreground app with `oom_score_adj=0` because the device was not responding.
+The last half-second sample before the kill still had 2,172,612 KiB swap free. Over the
+captured interval, system unevictable memory grew from 1,955,932 to 3,236,276 KiB and shmem
+from 1,690,276 to 2,965,900 KiB, while the game's private swap reached 1,916,136 KiB. The
+pressure helper found the launcher already largely reclaimed: its first request at Continue
+reduced aggregate launcher RSS only from 263 to 255 MiB and backed off. Thus launcher reclaim
+does not resolve this load failure, and this run did not exhaust swap. The unevictable/shmem
+growth needs allocation attribution before treating it as a driver or GPU-memory diagnosis.
+
+## CRAM and the bounded pager experiment
+
+[Meta's CRAM proposal](https://lpc.events/event/20/contributions/2424/) uses memory compression
+hardware to allow byte/cacheline reads without software decompression faults. A userspace
+software pager cannot reproduce that hardware property. This is separate from Valve's device
+memory cgroup prioritization work.
+
+`tools/experiments/anonymous-memory-pager.c` is an opt-in feasibility probe, not a preload or
+runtime component. Build it for the guest architecture with a Linux compiler:
+
+```sh
+cc -O2 -Wall -Wextra -Werror -pthread tools/experiments/anonymous-memory-pager.c -ldl -o /tmp/anonymous-memory-pager
+/tmp/anonymous-memory-pager 64
+```
+
+It requires `liblz4.so.1` and `UFFD_USER_MODE_ONLY`. The requested arena is bounded to
+1–256 MiB. Half its pages contain a repeating byte and half contain pseudorandom bytes;
+these are synthetic data, not a game-memory compression estimate. The probe registers only
+its own arena, packs compressed or incompressible pages into a separate store, discards the
+original physical pages, then restores them on demand with `UFFDIO_COPY`. It checks the full
+checksum and subsequent writes. A separate child checks kernel access to an evicted buffer.
+The compressed store is retained until the probe ends; this is one eviction/restore cycle,
+without hot/cold selection, concurrent eviction, lifecycle hooks or game integration.
+
+On the Pocket FIT Elite's Android 6.6.82 kernel, tested from DroidDeck's unprivileged guest:
+
+| Measurement | Result |
+| --- | --- |
+| unrestricted `userfaultfd` | `EPERM` |
+| userspace-only creation and API negotiation | available |
+| arena / compressed payload | 64 MiB / 33,767,424 bytes |
+| process RSS before / after eviction | 68,864 / 36,340 KiB |
+| restored pages / checksum / writes | 16,384 / intact / intact |
+| resident scan / compress and evict / faulted scan | 46.851 / 17.289 / 446.524 ms |
+| kernel pipe write from evicted buffer | `EFAULT` |
+
+These timings include the full checksum scan and are one synthetic run, not a comparison
+against zram or a game-performance result. The normal unrestricted pager interface was denied;
+`/dev/userfaultfd` was also inaccessible. The kernel-access result is a hard integration
+constraint: a transparent heap-wide pager cannot assume every access comes from userspace.
+Wine/FEX signal handling, syscall buffers, shared and GPU memory, mapping changes, concurrent
+writes and allocation overhead would all need a correctness design before enabling such a
+pager in a real game. The experiment is deliberately excluded from runtime builds.
diff --git a/tools/experiments/anonymous-memory-pager.c b/tools/experiments/anonymous-memory-pager.c
new file mode 100644
index 0000000000..f363e91d81
--- /dev/null
+++ b/tools/experiments/anonymous-memory-pager.c
@@ -0,0 +1,202 @@
+#define _GNU_SOURCE
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#ifndef UFFD_USER_MODE_ONLY
+#define UFFD_USER_MODE_ONLY 1
+#endif
+
+static unsigned char *arena, *store;
+static size_t page, size, *offsets;
+static int *lengths, uffd;
+static atomic_int finished;
+static unsigned faults;
+static int (*decode)(const char *, char *, int, int);
+
+static void fail(const char *name) {
+ perror(name);
+ exit(1);
+}
+
+static double milliseconds(void) {
+ struct timespec t;
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ return t.tv_sec * 1000.0 + t.tv_nsec / 1000000.0;
+}
+
+static long resident_kb(void) {
+ FILE *f = fopen("/proc/self/status", "r");
+ if (!f) fail("status");
+ char line[256];
+ long value = -1;
+ while (fgets(line, sizeof(line), f)) {
+ if (sscanf(line, "VmRSS: %ld", &value) == 1) break;
+ }
+ fclose(f);
+ return value;
+}
+
+static int open_pager(void) {
+ int fd = (int)syscall(SYS_userfaultfd, O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY);
+ if (fd < 0) fail("userfaultfd");
+ struct uffdio_api api = {.api = UFFD_API};
+ if (ioctl(fd, UFFDIO_API, &api)) fail("UFFDIO_API");
+ return fd;
+}
+
+static void register_range(int fd, void *address, size_t bytes) {
+ struct uffdio_register range = {
+ .range = {.start = (uintptr_t)address, .len = bytes},
+ .mode = UFFDIO_REGISTER_MODE_MISSING,
+ };
+ if (ioctl(fd, UFFDIO_REGISTER, &range)) fail("UFFDIO_REGISTER");
+}
+
+static void *serve(void *unused) {
+ (void)unused;
+ unsigned char *scratch = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (scratch == MAP_FAILED) fail("scratch");
+ struct pollfd p = {.fd = uffd, .events = POLLIN};
+ while (!atomic_load(&finished)) {
+ int ready = poll(&p, 1, 100);
+ if (ready < 0 && errno == EINTR) continue;
+ if (ready < 0 || (p.revents & (POLLERR | POLLHUP | POLLNVAL))) fail("pager poll");
+ if (!ready) continue;
+ struct uffd_msg msg;
+ ssize_t n = read(uffd, &msg, sizeof(msg));
+ if (n < 0 && errno == EAGAIN) continue;
+ if (n != sizeof(msg) || msg.event != UFFD_EVENT_PAGEFAULT) fail("pager event");
+ uintptr_t address = msg.arg.pagefault.address & ~(page - 1);
+ if (address < (uintptr_t)arena || address >= (uintptr_t)arena + size) fail("pager address");
+ size_t index = (address - (uintptr_t)arena) / page;
+ if (lengths[index] == 0) memcpy(scratch, store + offsets[index], page);
+ else if (decode((char *)store + offsets[index], (char *)scratch, lengths[index], (int)page) != (int)page)
+ fail("decompression");
+ struct uffdio_copy copy = {.dst = address, .src = (uintptr_t)scratch, .len = page};
+ if (ioctl(uffd, UFFDIO_COPY, ©)) fail("UFFDIO_COPY");
+ faults++;
+ }
+ munmap(scratch, page);
+ return NULL;
+}
+
+static uint64_t checksum(void) {
+ uint64_t sum = 0;
+ for (size_t i = 0; i < size; i++) sum = sum * 31 + arena[i];
+ return sum;
+}
+
+static void kernel_access_probe(void) {
+ pid_t child = fork();
+ if (child < 0) fail("fork");
+ if (!child) {
+ alarm(2);
+ int fd = open_pager(), pipefd[2];
+ unsigned char *buffer = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (buffer == MAP_FAILED || pipe(pipefd)) _exit(1);
+ memset(buffer, 0x5a, page);
+ register_range(fd, buffer, page);
+ if (madvise(buffer, page, MADV_DONTNEED)) _exit(1);
+ errno = 0;
+ ssize_t n = write(pipefd[1], buffer, page);
+ _exit(n < 0 && errno == EFAULT ? 42 : 1);
+ }
+ int status;
+ if (waitpid(child, &status, 0) < 0) fail("waitpid");
+ if (WIFSIGNALED(status)) printf("kernel_access_signal=%d\n", WTERMSIG(status));
+ else printf("kernel_access_exit=%d (42=EFAULT)\n", WEXITSTATUS(status));
+}
+
+int main(int argc, char **argv) {
+ if (argc > 2) return 2;
+ char *end = NULL;
+ long mb = argc == 2 ? strtol(argv[1], &end, 10) : 64;
+ if (mb < 1 || mb > 256 || (end && (*end || end == argv[1]))) return 2;
+ alarm(30);
+ page = (size_t)sysconf(_SC_PAGESIZE);
+ size = (size_t)mb << 20;
+ if (!page || size % page) return 2;
+ void *codec = dlopen("liblz4.so.1", RTLD_NOW | RTLD_LOCAL);
+ if (!codec) { fprintf(stderr, "%s\n", dlerror()); return 1; }
+ int (*encode)(const char *, char *, int, int) = dlsym(codec, "LZ4_compress_default");
+ int (*bound)(int) = dlsym(codec, "LZ4_compressBound");
+ decode = dlsym(codec, "LZ4_decompress_safe");
+ if (!encode || !decode || !bound) return 1;
+ arena = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ store = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (arena == MAP_FAILED || store == MAP_FAILED) fail("arena");
+ size_t count = size / page;
+ offsets = calloc(count, sizeof(*offsets));
+ lengths = calloc(count, sizeof(*lengths));
+ char *scratch = malloc((size_t)bound((int)page));
+ if (!offsets || !lengths || !scratch) fail("metadata");
+ uint32_t random = 123456789;
+ for (size_t i = 0; i < size; i++) {
+ if ((i / page) % 2) {
+ random ^= random << 13; random ^= random >> 17; random ^= random << 5;
+ arena[i] = (unsigned char)random;
+ } else arena[i] = (unsigned char)(i / page);
+ }
+ double start = milliseconds();
+ uint64_t before = checksum();
+ double baseline_ms = milliseconds() - start;
+ long before_kb = resident_kb();
+ uffd = open_pager();
+ register_range(uffd, arena, size);
+ start = milliseconds();
+ size_t used = 0;
+ for (size_t i = 0; i < count; i++) {
+ int n = encode((char *)arena + i * page, scratch, (int)page, bound((int)page));
+ if (n <= 0) fail("compression");
+ offsets[i] = used;
+ if ((size_t)n >= page) {
+ memcpy(store + used, arena + i * page, page);
+ used += page;
+ } else {
+ lengths[i] = n;
+ memcpy(store + used, scratch, (size_t)n);
+ used += (size_t)n;
+ }
+ }
+ if (madvise(arena, size, MADV_DONTNEED)) fail("eviction");
+ double compress_ms = milliseconds() - start;
+ long compressed_kb = resident_kb();
+ pthread_t pager;
+ if (pthread_create(&pager, NULL, serve, NULL)) fail("pthread_create");
+ start = milliseconds();
+ uint64_t after = checksum();
+ double restore_ms = milliseconds() - start;
+ int writable = 1;
+ for (size_t i = 0; i < size; i += page) {
+ unsigned char value = arena[i] ^ 0xff;
+ arena[i] = value;
+ if (*(volatile unsigned char *)(arena + i) != value) writable = 0;
+ }
+ atomic_store(&finished, 1);
+ pthread_join(pager, NULL);
+ printf("arena_mb=%ld compressed_bytes=%zu rss_before_kb=%ld rss_compressed_kb=%ld faults=%u intact=%d writable=%d\n",
+ mb, used, before_kb, compressed_kb, faults, before == after, writable);
+ printf("baseline_scan_ms=%.3f compress_ms=%.3f restore_scan_ms=%.3f\n", baseline_ms, compress_ms, restore_ms);
+ close(uffd);
+ munmap(arena, size); munmap(store, size);
+ free(offsets); free(lengths); free(scratch);
+ dlclose(codec);
+ kernel_access_probe();
+ return before == after && writable && faults == count ? 0 : 1;
+}
diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session
index 4ebfbc00ea..3edc114c8f 100755
--- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session
+++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session
@@ -707,8 +707,75 @@ case $mode in
done 2>/dev/null < "/proc/$_p/status"
done
_bl_rss=$_rss
+ _bl_swap=$_swap
echo "$# processes, resident $((_rss / 1024)) MB, in zram $((_swap / 1024)) MB"
}
+ bl_memory_read() {
+ local _k _v _u _kind _fields _avg
+ _bl_mem_total=0 _bl_available=-1 _bl_swap_total=0 _bl_swap_free=-1 _bl_psi=0
+ while read -r _k _v _u; do
+ case $_v in ''|*[!0-9]*) continue ;; esac
+ case $_k in
+ MemTotal:) _bl_mem_total=$_v ;;
+ MemAvailable:) _bl_available=$_v ;;
+ SwapTotal:) _bl_swap_total=$_v ;;
+ SwapFree:) _bl_swap_free=$_v ;;
+ esac
+ done 2>/dev/null < /proc/meminfo
+ if [ -r /proc/pressure/memory ]; then
+ while read -r _kind _fields; do
+ [ "$_kind" = some ] || continue
+ _avg=${_fields#avg10=} _avg=${_avg%% *} _avg=${_avg%%.*}
+ case $_avg in ''|*[!0-9]*) ;; *) _bl_psi=$((10#$_avg)) ;; esac
+ done 2>/dev/null < /proc/pressure/memory
+ fi
+ _bl_headroom=$((_bl_mem_total / 5))
+ [ "$_bl_headroom" -ge $((512 * 1024)) ] || _bl_headroom=$((512 * 1024))
+ [ "$_bl_headroom" -le $((1536 * 1024)) ] || _bl_headroom=$((1536 * 1024))
+ _bl_swap_reserve=$((_bl_swap_total / 5))
+ [ "$_bl_swap_reserve" -ge $((256 * 1024)) ] || _bl_swap_reserve=$((256 * 1024))
+ }
+ bl_memory_tight() {
+ [ "$_bl_mem_total" -gt 0 ] && [ "$_bl_available" -ge 0 ] &&
+ { [ "$_bl_available" -lt "$_bl_headroom" ] ||
+ { [ "$_bl_psi" -ge 1 ] && [ "$_bl_available" -lt $((_bl_mem_total / 3)) ]; }; }
+ }
+ bl_client_can_pageout() {
+ bl_memory_read
+ bl_zram_usage "${_bl_pids[@]}" > /dev/null
+ [ "$_bl_mem_total" -gt 0 ] && [ "$_bl_swap_total" -gt 0 ] &&
+ [ $((_bl_swap_free - _bl_rss)) -ge "$_bl_swap_reserve" ]
+ }
+ bl_client_can_readback() {
+ bl_memory_read
+ bl_zram_usage "${_bl_pids[@]}" > /dev/null
+ [ "$_bl_mem_total" -gt 0 ] && ! bl_memory_tight &&
+ [ "$_bl_available" -ge $((_bl_swap * 2)) ] &&
+ [ $((_bl_available - _bl_swap)) -ge "$_bl_headroom" ]
+ }
+ bl_client_pressure() {
+ [ "${BL_ZRAM_CLIENT:-1}" = 1 ] && [ "$_bl_game" = 1 ] &&
+ [ "${#_bl_pids[@]}" -gt 0 ] && bl_memory_tight || return 0
+ [ "$_bl_pressure_at" -lt 0 ] ||
+ [ $((SECONDS - _bl_pressure_at)) -ge "$_bl_pressure_backoff" ] || return 0
+ [ "$_bl_packed" -lt 0 ] || [ $((SECONDS - _bl_packed)) -ge 30 ] || return 0
+ _bl_pressure_at=$SECONDS
+ if ! bl_client_can_pageout; then
+ step "zRAM: pressure reclaim skipped, swap headroom too small ($((_bl_swap_free / 1024)) MB free)"
+ return 0
+ fi
+ [ "$_bl_rss" -ge $((128 * 1024)) ] || return 0
+ local _before=$_bl_rss
+ bl_zram_send 1 "memory pressure, compressing the client (available $((_bl_available / 1024)) MB, swap free $((_bl_swap_free / 1024)) MB)" client
+ _bl_packed=$SECONDS
+ [ "${_bl_pageout_complete:-0}" = 1 ] || return 0
+ bl_zram_usage "${_bl_pids[@]}" > /dev/null
+ if [ $((_before - _bl_rss)) -lt $((16 * 1024)) ]; then
+ _bl_pressure_backoff=$((_bl_pressure_backoff * 2 > 300 ? 300 : _bl_pressure_backoff * 2))
+ else
+ _bl_pressure_backoff=30
+ fi
+ }
bl_session_others() {
local _p
_bl_session=()
@@ -737,7 +804,11 @@ case $mode in
[ "$_left" = 1 ] || break
nap 0.2
done
- for _p in "$@"; do rm -f "/tmp/.bl-zram.ack.$_p"; done
+ _bl_pageout_complete=1
+ for _p in "$@"; do
+ if [ -d "/proc/$_p" ] && [ ! -s "/tmp/.bl-zram.ack.$_p" ]; then _bl_pageout_complete=0; fi
+ rm -f "/tmp/.bl-zram.ack.$_p"
+ done
}
bl_zram_send() {
local _mode=$1 _what=$2 _which=$3 _pids _p
@@ -775,8 +846,10 @@ 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
while kill -0 $$ 2>/dev/null; do
bl_zram_scan
+ bl_memory_read
if bl_cap_due; then
if bl_over_cap; then
if [ "$_bl_capped" -lt 0 ] || [ $((SECONDS - _bl_capped)) -ge "$_bl_backoff" ]; then
@@ -798,29 +871,30 @@ case $mode in
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
+ bl_client_pressure
if [ "$_bl_game" = 1 ] && bl_game_focused; then
if [ "$_bl_since" -lt 0 ] || [ "$_bl_since_apps" != "$_bl_apps" ]; then
_bl_since=$SECONDS _bl_since_apps=$_bl_apps
fi
if [ "${BL_ZRAM_CLIENT:-1}" = 1 ] && [ $((SECONDS - _bl_since)) -ge 20 ] && [ "${#_bl_pids[@]}" -gt 0 ] &&
- { [ "$_bl_packed" -lt 0 ] || [ $((SECONDS - _bl_packed)) -ge 300 ]; }; then
+ { [ "$_bl_packed" -lt 0 ] || [ $((SECONDS - _bl_packed)) -ge 300 ]; } && bl_client_can_pageout; then
bl_zram_send 1 "compressing the client" client
_bl_packed=$SECONDS
fi
elif [ "$_bl_game" != 1 ]; then
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
+ _bl_unpack=1
fi
_bl_since=-1 _bl_packed=-1
else
_bl_since=-1
fi
+ if [ "$_bl_unpack" = 1 ] && [ "$_bl_game" = 0 ] && ! bl_over_cap && bl_client_can_readback; then
+ bl_zram_send 0 "memory recovered, reading the client back" client
+ _bl_unpack=0
+ fi
nap 5
done
) &
diff --git a/tools/tests/test_client_zram_focus.py b/tools/tests/test_client_zram_focus.py
index dda4e5b6ee..62bb89d372 100644
--- a/tools/tests/test_client_zram_focus.py
+++ b/tools/tests/test_client_zram_focus.py
@@ -84,10 +84,12 @@ class ClientZramFocusTest(unittest.TestCase):
stop = source.index(' nap 5', start)
body = source[start:stop]
harness = '''
-_bl_since=-1 _bl_since_apps= _bl_packed=-1 _bl_apps=" 413150"
+_bl_since=-1 _bl_since_apps= _bl_packed=-1 _bl_unpack=0 _bl_apps=" 413150"
_bl_pids=(123)
bl_game_focused() { [ "$focused" = 1 ]; }
bl_over_cap() { return 1; }
+bl_client_can_pageout() { return 0; }
+bl_client_can_readback() { return 0; }
bl_zram_send() { echo "send:$1"; }
tick() {
SECONDS=$1 _bl_game=$2 focused=$3
@@ -109,7 +111,7 @@ tick 60 0 0
def monitor_harness(self):
source = SCRIPT.read_text()
- start = source.index(' bl_zram_scan\n if bl_cap_due; then')
+ start = source.index(' bl_zram_scan\n bl_memory_read\n')
stop = source.index(' nap 5', start)
return '''
_bl_since=-1 _bl_since_apps= _bl_packed=-1 _bl_measured=-1 _bl_unpack=0 _bl_capped=-1 _bl_backoff=30
@@ -117,6 +119,10 @@ _bl_apps=" 413150"
_bl_pids=(123)
step() { :; }
bl_zram_scan() { :; }
+bl_memory_read() { :; }
+bl_client_pressure() { :; }
+bl_client_can_pageout() { return 0; }
+bl_client_can_readback() { [ "${readback:-1}" = 1 ]; }
bl_game_focused() { [ "$focused" = 1 ]; }
bl_cap_due() { [ "$cap_due" = 1 ]; }
bl_over_cap() { [ "$over_cap" = 1 ]; }
@@ -169,13 +175,28 @@ tick 20 1 1 0 0
tick 25 1 0 0 0
tick 30 0 0 1 0
echo "deferred:$_bl_unpack:$_bl_packed"
-tick 35 0 0 0 1
+tick 35 0 0 0 0
echo "restored:$_bl_unpack"
''')
self.assertEqual(0, result.returncode, result.stderr)
self.assertEqual('', result.stderr)
self.assertEqual(['send:1:client', 'deferred:1:-1', 'send:0:client', 'restored:0'], result.stdout.splitlines())
+ def test_game_exit_defers_readback_under_pressure_even_without_a_ram_cap(self):
+ result = run_bash(self.monitor_harness() + """
+tick 0 1 1 0 0
+tick 20 1 1 0 0
+readback=0
+tick 25 0 0 0 0
+echo "deferred:$_bl_unpack"
+readback=1
+tick 30 0 0 0 0
+echo "restored:$_bl_unpack"
+""")
+ self.assertEqual(0, result.returncode, result.stderr)
+ self.assertEqual('', result.stderr)
+ self.assertEqual(['send:1:client', 'deferred:1', 'send:0:client', 'restored:0'], result.stdout.splitlines())
+
def test_scan_keeps_the_running_game_and_ignores_steams_own_reaper(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
diff --git a/tools/tests/test_memory_pressure.py b/tools/tests/test_memory_pressure.py
new file mode 100644
index 0000000000..96c16ec158
--- /dev/null
+++ b/tools/tests/test_memory_pressure.py
@@ -0,0 +1,141 @@
+from pathlib import Path
+import tempfile
+import unittest
+
+from tools.tests.test_client_zram_focus import function_source, run_bash
+
+
+class MemoryPressureTest(unittest.TestCase):
+ def setUp(self):
+ self.directory = tempfile.TemporaryDirectory()
+ self.addCleanup(self.directory.cleanup)
+ self.root = Path(self.directory.name)
+ self.memory()
+
+ def memory(self, available=1024, swap_free=4096, total=7168, swap_total=6144, psi='0.00'):
+ (self.root / 'meminfo').write_text(
+ f'MemTotal: {total * 1024} kB\nMemAvailable: {available * 1024} kB\n'
+ f'SwapTotal: {swap_total * 1024} kB\nSwapFree: {swap_free * 1024} kB\n')
+ (self.root / 'pressure').write_text(f'some avg10={psi} avg60=0.00 avg300=0.00 total=1234\n')
+
+ def run_policy(self, body):
+ functions = '\n'.join(function_source(name) for name in (
+ 'bl_memory_read', 'bl_memory_tight', 'bl_client_can_pageout',
+ 'bl_client_can_readback', 'bl_client_pressure'))
+ functions = functions.replace('/proc/meminfo', str(self.root / 'meminfo'))
+ functions = functions.replace('/proc/pressure/memory', str(self.root / 'pressure'))
+ harness = '''
+set -u
+_bl_pids=(123) _bl_game=1 _bl_packed=-1 _bl_pressure_at=-1 _bl_pressure_backoff=30
+rss=$((384 * 1024)) client_swap=$((512 * 1024)) reclaimed=$((128 * 1024))
+step() { :; }
+bl_zram_usage() { _bl_rss=$rss _bl_swap=$client_swap; }
+bl_zram_send() { echo "send:$1:$3"; rss=$((rss - reclaimed)); _bl_pageout_complete=${pageout_complete:-1}; }
+''' + functions + '\nbl_memory_read\n' + body
+ result = run_bash(harness)
+ self.assertEqual(0, result.returncode, result.stderr)
+ self.assertEqual('', result.stderr)
+ return result.stdout.splitlines()
+
+ def test_pressure_reclaims_before_the_focus_delay_and_respects_cooldown(self):
+ self.assertEqual(['send:1:client', 'send:1:client'], self.run_policy('''
+SECONDS=0; bl_client_pressure
+SECONDS=5; bl_client_pressure
+SECONDS=29; bl_client_pressure
+SECONDS=30; bl_client_pressure
+'''))
+
+ def test_no_game_or_disabled_client_compression_does_not_reclaim(self):
+ self.assertEqual([], self.run_policy('''
+_bl_game=0; bl_client_pressure
+_bl_game=1 BL_ZRAM_CLIENT=0; bl_client_pressure
+'''))
+
+ def test_high_available_memory_does_not_reclaim_even_with_stalls(self):
+ self.memory(available=4096, psi='12.34')
+ self.assertEqual([], self.run_policy('bl_client_pressure'))
+
+ def test_psi_reclaims_with_moderate_headroom(self):
+ self.memory(available=2048, psi='01.23')
+ self.assertEqual(['send:1:client'], self.run_policy('bl_client_pressure'))
+
+ def test_missing_psi_still_uses_memory_headroom(self):
+ (self.root / 'pressure').unlink()
+ self.assertEqual(['send:1:client'], self.run_policy('bl_client_pressure'))
+
+ def test_missing_meminfo_never_reclaims_or_prefetches(self):
+ (self.root / 'meminfo').write_text('SwapTotal: 6291456 kB\nSwapFree: 4194304 kB\n')
+ self.assertEqual(['no'], self.run_policy('''
+bl_client_pressure
+if bl_client_can_readback; then echo yes; else echo no; fi
+'''))
+
+ def test_missing_available_memory_does_not_trigger_pressure(self):
+ p = self.root / 'meminfo'
+ p.write_text('\n'.join(line for line in p.read_text().splitlines() if not line.startswith('MemAvailable:')))
+ self.assertEqual([], self.run_policy('bl_client_pressure'))
+
+ def test_swap_reserve_includes_the_clients_resident_memory(self):
+ self.memory(swap_free=1536)
+ self.assertEqual(['no'], self.run_policy('''
+bl_client_pressure
+if bl_client_can_pageout; then echo yes; else echo no; fi
+'''))
+
+ def test_without_swap_automatic_pageout_is_disabled(self):
+ self.memory(swap_total=0, swap_free=0)
+ self.assertEqual([], self.run_policy('bl_client_pressure'))
+
+ def test_recent_periodic_pageout_prevents_an_immediate_pressure_repeat(self):
+ self.assertEqual([], self.run_policy('_bl_packed=$SECONDS; bl_client_pressure'))
+
+ def test_ineffective_reclaim_backs_off_and_success_resets_it(self):
+ self.assertEqual(['send:1:client', 'backoff:60', 'send:1:client', 'backoff:30'], self.run_policy('''
+reclaimed=0
+SECONDS=0; bl_client_pressure
+echo "backoff:$_bl_pressure_backoff"
+SECONDS=30; bl_client_pressure
+reclaimed=$((128 * 1024))
+SECONDS=60; bl_client_pressure
+echo "backoff:$_bl_pressure_backoff"
+'''))
+
+ def test_readback_requires_room_for_the_swapped_client_plus_headroom(self):
+ self.memory(available=2048)
+ self.assertEqual(['yes', 'no'], self.run_policy('''
+if bl_client_can_readback; then echo yes; else echo no; fi
+client_swap=$((1024 * 1024))
+if bl_client_can_readback; then echo yes; else echo no; fi
+'''))
+
+ def test_unfinished_pageout_does_not_increase_the_ineffective_reclaim_backoff(self):
+ self.assertEqual(['send:1:client', 'backoff:30'], self.run_policy('''
+reclaimed=0 pageout_complete=0
+SECONDS=0; bl_client_pressure
+echo "backoff:$_bl_pressure_backoff"
+'''))
+
+ def test_pageout_completion_requires_a_finished_ack_from_every_live_target(self):
+ proc = self.root / 'proc'
+ proc.mkdir()
+ for pid in (123, 456):
+ (proc / str(pid)).mkdir()
+ source = function_source('bl_zram_wait').replace('/proc/', str(proc) + '/')
+ source = source.replace('/tmp/.bl-zram.ack.', str(self.root / 'ack.'))
+ for pending in ('done', 'running', 'not_started'):
+ with self.subTest(pending=pending):
+ (self.root / 'ack.123').write_text('1')
+ if pending != 'not_started':
+ (self.root / 'ack.456').write_text('1' if pending == 'done' else '')
+ result = run_bash('nap() { SECONDS=$((SECONDS + 1)); }\n' + source +
+ '\nSECONDS=0; bl_zram_wait 123 456; echo "$_bl_pageout_complete"')
+ self.assertEqual(0, result.returncode, result.stderr)
+ self.assertEqual('1' if pending == 'done' else '0', result.stdout.strip())
+
+ def test_pressure_prevents_prefetch_even_if_the_client_would_fit(self):
+ self.memory(available=2048, psi='2.00')
+ self.assertEqual(['no'], self.run_policy('if bl_client_can_readback; then echo yes; else echo no; fi'))
+
+
+if __name__ == '__main__':
+ unittest.main()