From 4fc21622033249aa288368f7df57a8173bb21f12 Mon Sep 17 00:00:00 2001 From: xXJsonDeruloXx Date: Sun, 4 Oct 2026 16:36:01 -0400 Subject: [PATCH] perf(storage): stream directory snapshots without blocking other readers --- tools/linuxfs/preload/readdir_cache.c | 172 ++++++++++++++------------ tools/tests/test_readdir_cache.py | 166 +++++++++++++++++++++++++ 2 files changed, 258 insertions(+), 80 deletions(-) create mode 100644 tools/tests/test_readdir_cache.py diff --git a/tools/linuxfs/preload/readdir_cache.c b/tools/linuxfs/preload/readdir_cache.c index 5838d19a3..0bc6725ab 100644 --- a/tools/linuxfs/preload/readdir_cache.c +++ b/tools/linuxfs/preload/readdir_cache.c @@ -30,6 +30,10 @@ struct reader { DIR *dir; struct listing *listing; size_t pos; + size_t capacity; + int cached; + int capture_failed; + struct listing capture; struct reader *next; }; @@ -85,7 +89,8 @@ static void release_reader(DIR *dir) { while (*link) { if ((*link)->dir == dir) { struct reader *reader = *link; - reader->listing->readers--; + if (reader->cached) reader->listing->readers--; + else free(reader->capture.entries); *link = reader->next; free(reader); return; @@ -104,10 +109,9 @@ static struct listing *available_listing(void) { return oldest; } -/* Capture a directory only for Steam's FUSE-backed library. The short lifetime is a fallback - * for filesystems that fail to update directory timestamps; normal create/remove operations - * invalidate the snapshot as soon as fstat observes their new mtime or ctime. */ -static struct listing *listing_for(DIR *dir) { +/* Capture only what the caller reads. Completing a stable listing makes it reusable, without + * forcing callers that stop early to scan the whole directory or holding a global lock over I/O. */ +static struct reader *reader_for(DIR *dir) { int fd = dirfd(dir); struct statfs fs; struct stat before; @@ -115,69 +119,72 @@ static struct listing *listing_for(DIR *dir) { fstatfs(fd, &fs) != 0 || fs.f_type != FUSE_SUPER_MAGIC || fstat(fd, &before) != 0) return NULL; + struct reader *reader = calloc(1, sizeof(*reader)); + if (!reader) return NULL; + reader->dir = dir; + reader->capture = (struct listing){ + .dev = before.st_dev, .ino = before.st_ino, + .mtime = before.st_mtim, .ctime = before.st_ctim, + }; + reader->listing = &reader->capture; uint64_t now = monotonic_ns(); - for (size_t i = 0; i < CACHE_SLOTS; i++) - if (same_directory(&listings[i], &before, now)) return &listings[i]; - - struct listing *slot = available_listing(); - if (!slot) return NULL; - - size_t capacity = 64; - struct dirent64 *entries = malloc(capacity * sizeof(*entries)); - if (!entries) return NULL; - size_t count = 0; - int error = 0; - for (;;) { - if (count == capacity) { - if (capacity > SIZE_MAX / 2 / sizeof(*entries)) { - error = ENOMEM; - break; - } - size_t next_capacity = capacity * 2; - struct dirent64 *grown = realloc(entries, next_capacity * sizeof(*entries)); - if (!grown) { - error = ENOMEM; - break; - } - entries = grown; - capacity = next_capacity; - } - errno = 0; - struct dirent64 *entry = real_readdir64(dir); - if (!entry) { - error = errno; + pthread_mutex_lock(&cache_lock); + for (size_t i = 0; i < CACHE_SLOTS; i++) { + if (same_directory(&listings[i], &before, now)) { + reader->listing = &listings[i]; + reader->listing->readers++; + reader->cached = 1; break; } - size_t bytes = entry->d_reclen < sizeof(*entry) ? entry->d_reclen : sizeof(*entry); - memset(&entries[count], 0, sizeof(*entries)); - memcpy(&entries[count++], entry, bytes); } - real_rewinddir(dir); + reader->next = readers; + readers = reader; + pthread_mutex_unlock(&cache_lock); + return reader; +} +static void capture_entry(struct reader *reader, const struct dirent64 *entry) { + struct listing *listing = &reader->capture; + if (listing->count == reader->capacity) { + size_t capacity = reader->capacity ? reader->capacity * 2 : 64; + if (capacity < reader->capacity || capacity > SIZE_MAX / sizeof(*entry)) { + reader->capture_failed = 1; + return; + } + struct dirent64 *grown = realloc(listing->entries, capacity * sizeof(*entry)); + if (!grown) { reader->capture_failed = 1; return; } + listing->entries = grown; + reader->capacity = capacity; + } + size_t bytes = entry->d_reclen < sizeof(*entry) ? entry->d_reclen : sizeof(*entry); + memset(&listing->entries[listing->count], 0, sizeof(*entry)); + memcpy(&listing->entries[listing->count++], entry, bytes); +} + +static void publish_listing(struct reader *reader) { struct stat after; - if (error || fstat(fd, &after) != 0 || - before.st_dev != after.st_dev || before.st_ino != after.st_ino || - before.st_mtim.tv_sec != after.st_mtim.tv_sec || - before.st_mtim.tv_nsec != after.st_mtim.tv_nsec || - before.st_ctim.tv_sec != after.st_ctim.tv_sec || - before.st_ctim.tv_nsec != after.st_ctim.tv_nsec) { - free(entries); - return NULL; - } + struct listing *listing = &reader->capture; + if (fstat(dirfd(reader->dir), &after) != 0 || + listing->dev != after.st_dev || listing->ino != after.st_ino || + listing->mtime.tv_sec != after.st_mtim.tv_sec || + listing->mtime.tv_nsec != after.st_mtim.tv_nsec || + listing->ctime.tv_sec != after.st_ctim.tv_sec || + listing->ctime.tv_nsec != after.st_ctim.tv_nsec) + return; - struct dirent64 *small = realloc(entries, (count ? count : 1) * sizeof(*entries)); - if (small) entries = small; - free(slot->entries); - *slot = (struct listing){ - .dev = before.st_dev, - .ino = before.st_ino, - .mtime = before.st_mtim, - .ctime = before.st_ctim, - .expires = monotonic_ns() + CACHE_TTL_NS, - .count = count, - .entries = entries, - }; - return slot; + pthread_mutex_lock(&cache_lock); + struct listing *slot = available_listing(); + if (slot) { + free(slot->entries); + *slot = *listing; + slot->expires = monotonic_ns() + CACHE_TTL_NS; + slot->readers = 1; + reader->listing = slot; + reader->pos = slot->count; + reader->cached = 1; + listing->entries = NULL; + } + pthread_mutex_unlock(&cache_lock); } struct dirent64 *readdir64(DIR *dir) { @@ -188,24 +195,25 @@ struct dirent64 *readdir64(DIR *dir) { int saved_errno = errno; pthread_mutex_lock(&cache_lock); struct reader *reader = find_reader(dir); - if (!reader) { - struct listing *listing = listing_for(dir); - if (listing && (reader = malloc(sizeof(*reader)))) { - *reader = (struct reader){dir, listing, 0, readers}; - readers = reader; - listing->readers++; + pthread_mutex_unlock(&cache_lock); + if (!reader) reader = reader_for(dir); + if (reader && reader->cached) { + errno = saved_errno; + return reader->pos < reader->listing->count ? + &reader->listing->entries[reader->pos++] : NULL; + } + errno = 0; + struct dirent64 *entry = real_readdir64(dir); + int error = errno; + if (reader && !reader->capture_failed) { + if (entry) capture_entry(reader, entry); + else { + if (!error) publish_listing(reader); + reader->capture_failed = 1; } } - struct dirent64 *entry = NULL; - if (reader && reader->pos < reader->listing->count) - entry = &reader->listing->entries[reader->pos++]; - pthread_mutex_unlock(&cache_lock); - - if (reader) { - errno = saved_errno; - return entry; - } - return real_readdir64(dir); + errno = error ? error : saved_errno; + return entry; } /* 32- and 64-bit dirent layouts are identical in the supported 64-bit runtimes. */ @@ -241,10 +249,10 @@ long telldir(DIR *dir) { int saved_errno = errno; pthread_mutex_lock(&cache_lock); struct reader *reader = find_reader(dir); - long offset = reader ? (reader->pos ? reader->listing->entries[reader->pos - 1].d_off : 0) : -1; + long offset = reader && reader->cached ? (reader->pos ? reader->listing->entries[reader->pos - 1].d_off : 0) : -1; pthread_mutex_unlock(&cache_lock); - if (!reader && real_telldir) return real_telldir(dir); - if (!reader) { errno = ENOSYS; return -1; } + if ((!reader || !reader->cached) && real_telldir) return real_telldir(dir); + if (!reader || !reader->cached) { errno = ENOSYS; return -1; } errno = saved_errno; return offset; } @@ -254,6 +262,10 @@ void seekdir(DIR *dir, long offset) { int saved_errno = errno; pthread_mutex_lock(&cache_lock); struct reader *reader = find_reader(dir); + if (reader && !reader->cached) { + release_reader(dir); + reader = NULL; + } if (reader) { size_t pos = 0; if (offset) { diff --git a/tools/tests/test_readdir_cache.py b/tools/tests/test_readdir_cache.py new file mode 100644 index 000000000..657bfacaa --- /dev/null +++ b/tools/tests/test_readdir_cache.py @@ -0,0 +1,166 @@ +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest + +SOURCE = Path(__file__).resolve().parents[1] / "linuxfs/preload/readdir_cache.c" + +# Mark test directories as FUSE and count reads below the cache. Blocking one stream verifies +# that its disk I/O does not hold up a different stream; the harness has a deadlock timeout. +SHIM = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +static int calls, blocked_fd = -1, entered; +static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t condition = PTHREAD_COND_INITIALIZER; +int fstatfs(int fd, struct statfs *st) { + int (*real)(int, struct statfs *) = dlsym(RTLD_NEXT, "fstatfs"); + int result = real(fd, st); + if (!result) st->f_type = 0x65735546; + return result; +} +int probe_calls(void) { return __atomic_load_n(&calls, __ATOMIC_RELAXED); } +void probe_block(int fd) { blocked_fd = fd; entered = 0; } +void probe_wait(void) { + pthread_mutex_lock(&lock); + while (!entered) pthread_cond_wait(&condition, &lock); + pthread_mutex_unlock(&lock); +} +void probe_release(void) { + pthread_mutex_lock(&lock); + blocked_fd = -1; + pthread_cond_broadcast(&condition); + pthread_mutex_unlock(&lock); +} +struct dirent64 *readdir64(DIR *dir) { + struct dirent64 *(*real)(DIR *) = dlsym(RTLD_NEXT, "readdir64"); + __atomic_add_fetch(&calls, 1, __ATOMIC_RELAXED); + pthread_mutex_lock(&lock); + if (dirfd(dir) == blocked_fd) { + entered = 1; + pthread_cond_broadcast(&condition); + while (blocked_fd >= 0) pthread_cond_wait(&condition, &lock); + } + pthread_mutex_unlock(&lock); + return real(dir); +} +""" + +PROGRAM = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define CHECK(c) do { if (!(c)) { fprintf(stderr, "failed line %d\n", __LINE__); exit(1); } } while (0) +static int count(const char *path, const char *wanted, int *found) { + DIR *dir = opendir(path); + CHECK(dir); + int n = 0; + struct dirent *entry; + errno = 73; + while ((entry = readdir(dir))) { + n++; + if (wanted && !strcmp(entry->d_name, wanted)) *found = 1; + } + CHECK(errno == 73); + CHECK(closedir(dir) == 0); + return n; +} +static void *read_one(void *dir) { CHECK(readdir(dir)); return NULL; } +int main(int argc, char **argv) { + CHECK(argc == 3); + alarm(10); + int (*calls)(void) = dlsym(RTLD_DEFAULT, "probe_calls"); + void (*block)(int) = dlsym(RTLD_DEFAULT, "probe_block"); + void (*wait)(void) = dlsym(RTLD_DEFAULT, "probe_wait"); + void (*release)(void) = dlsym(RTLD_DEFAULT, "probe_release"); + DIR *dir = opendir(argv[1]); + CHECK(dir && readdir(dir)); + CHECK(calls() == 1); /* An early lookup must not read all 5,000 files. */ + long offset = telldir(dir); + char expected[256]; + struct dirent *entry = readdir(dir); + CHECK(entry); + strcpy(expected, entry->d_name); + seekdir(dir, offset); + CHECK((entry = readdir(dir)) && !strcmp(entry->d_name, expected)); + CHECK(closedir(dir) == 0); + int before = calls(); + CHECK(count(argv[1], NULL, NULL) == 5002 && calls() > before); + before = calls(); + CHECK(count(argv[1], NULL, NULL) == 5002 && calls() == before); + dir = opendir(argv[1]); + CHECK(readdir(dir)); + offset = telldir(dir); + CHECK((entry = readdir(dir))); + strcpy(expected, entry->d_name); + seekdir(dir, offset); + CHECK((entry = readdir(dir)) && !strcmp(entry->d_name, expected)); + char path[4096]; + snprintf(path, sizeof(path), "%s/created", argv[1]); + int fd = open(path, O_CREAT | O_WRONLY | O_EXCL, 0600); + CHECK(fd >= 0 && close(fd) == 0); + rewinddir(dir); + int found = 0, n = 0; + errno = 73; + while ((entry = readdir(dir))) { n++; if (!strcmp(entry->d_name, "created")) found = 1; } + CHECK(n == 5003 && found && errno == 73 && closedir(dir) == 0); + CHECK(unlink(path) == 0); + found = 0; + CHECK(count(argv[1], "created", &found) == 5002 && !found); + dir = opendir(argv[2]); + CHECK(dir); + block(dirfd(dir)); + pthread_t worker; + CHECK(pthread_create(&worker, NULL, read_one, dir) == 0); + wait(); + CHECK(count(argv[1], NULL, NULL) == 5002); /* Must complete while the other stream is blocked. */ + release(); + CHECK(pthread_join(worker, NULL) == 0 && closedir(dir) == 0); + puts("lazy reads, complete large listings, seek, mutation and concurrent streams passed"); + return 0; +} +""" + + +@unittest.skipUnless(sys.platform.startswith("linux") and shutil.which("cc"), "Linux and a C compiler are needed") +class ReaddirCacheTest(unittest.TestCase): + def test_streaming_cache_preserves_directory_behavior(self): + with tempfile.TemporaryDirectory() as temporary: + base = Path(temporary) + exe = base / "Steam/steamrtarm64/steam" + exe.parent.mkdir(parents=True) + large, other = base / "large", base / "other" + large.mkdir() + other.mkdir() + for i in range(5000): + (large / str(i)).touch() + for name, source in (("shim", SHIM), ("probe", PROGRAM)): + (base / f"{name}.c").write_text(source) + cache, shim = base / "cache.so", base / "shim.so" + for target, source in ((cache, SOURCE), (shim, base / "shim.c")): + subprocess.run(["cc", "-O2", "-Wall", "-Wextra", "-Werror", "-fPIC", "-shared", "-pthread", + "-o", str(target), str(source), "-ldl"], check=True) + subprocess.run(["cc", "-O2", "-Wall", "-Wextra", "-Werror", "-pthread", "-o", str(exe), + str(base / "probe.c"), "-ldl"], check=True) + result = subprocess.run([str(exe), str(large), str(other)], + env=dict(os.environ, LD_PRELOAD=f"{cache}:{shim}"), + capture_output=True, text=True, timeout=15) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + + +if __name__ == "__main__": + unittest.main()