Look up read-ahead scans by key without touching another stream's scan

Each stream's scan is now found through a separate key table, so a
thread listing one folder never reads a scan another thread is freeing.
The read-ahead tests wait for the readers to finish instead of sleeping,
and check the thread count to prove nothing started.
This commit is contained in:
maxjivi05 committed 2026-11-26 00:55:33 -05:00
1 parent b78b19497d
commit ff7a2579d2
2 files changed
+59 -19

No files matched your search

+47 -11
View File
@@ -161,6 +161,18 @@ int statfs(const char *path, struct statfs *out) {
return r;
}
int closedir(DIR *d) {
static int (*next)(DIR *);
if (!next) next = dlsym(RTLD_NEXT, "closedir");
const char *log = prefetcher() ? getenv("PREFETCH_DONE") : NULL;
FILE *f = log ? fopen(log, "a") : NULL;
if (f) {
fprintf(f, "done\n");
fclose(f);
}
return next(d);
}
DIR *opendir(const char *path) {
static DIR *(*next)(const char *);
if (!next) next = dlsym(RTLD_NEXT, "opendir");
@@ -397,6 +409,25 @@ int main(int argc, char **argv) {
i += 2;
}
else if (!strcmp(op, "sleep")) { usleep(atoi(a) * 1000); i++; }
else if (!strcmp(op, "await")) {
const char *log = getenv("PREFETCH_DONE");
for (int tries = 0; tries < 2000; tries++) {
FILE *f = log ? fopen(log, "r") : NULL;
int lines = 0, c;
while (f && (c = fgetc(f)) != EOF) lines += c == '\n';
if (f) fclose(f);
if (lines >= atoi(a)) break;
usleep(5000);
}
i++;
}
else if (!strcmp(op, "threads")) {
FILE *f = fopen("/proc/self/status", "r");
char line[256];
while (f && fgets(line, sizeof(line), f))
if (!strncmp(line, "Threads:", 8)) printf("%d\n", atoi(line + 8));
if (f) fclose(f);
}
else if (!strcmp(op, "thread")) { pthread_t t; pthread_create(&t, NULL, idle, NULL); }
else if (!strcmp(op, "stats")) printf("%d\n", stats ? *stats : -1);
else if (!strcmp(op, "accesses")) printf("%d\n", accesses ? *accesses : -1);
@@ -953,15 +984,17 @@ class DirCachePreloadTest(unittest.TestCase):
return top.resolve()
def prefetched(self, *args, program=None, env=None):
log = Path(tempfile.mkdtemp(dir=self.dir)) / "prefetch.log"
out = self.run_program("thread", *args, "sleep", 1000, program=program,
env=dict({"SLOW": "40", "PREFETCH_LOG": str(log)}, **(env or {})))
logs = Path(tempfile.mkdtemp(dir=self.dir))
log = logs / "prefetch.log"
out = self.run_program("thread", *args, program=program,
env=dict({"SLOW": "40", "PREFETCH_LOG": str(log),
"PREFETCH_DONE": str(logs / "done.log")}, **(env or {})))
return out, sorted(log.read_text().splitlines()) if log.exists() else []
def test_slow_listings_on_slow_storage_prefetch_the_walk_ahead(self):
for program, root in ((self.other, self.lib.parent), (self.steam, self.lib)):
top = self.cold_tree(root)
out, fetched = self.prefetched("list", top, "sleep", 300, "list", top / "a", program=program)
out, fetched = self.prefetched("list", top, "await", 3, "list", top / "a", "await", 4, program=program)
self.assertEqual(["a,b,c,f.txt", "deep"], out)
self.assertEqual(sorted(str(top / d) for d in ("a", "a/deep", "b", "c")), fetched)
@@ -970,23 +1003,26 @@ class DirCachePreloadTest(unittest.TestCase):
warm = self.lib.parent / "warm"
(warm / "child").mkdir(parents=True)
top = self.cold_tree(self.lib.parent)
out, fetched = self.prefetched("list", internal, "list", warm, "list", top, program=self.other)
out, fetched = self.prefetched("list", internal, "list", warm, "list", top, "await", 3, program=self.other)
self.assertEqual(["a,b,c,f.txt", "child", "a,b,c,f.txt"], out)
self.assertEqual(sorted(str(top / d) for d in ("a", "b", "c")), fetched)
def test_single_threaded_programs_stay_single_threaded(self):
top = self.cold_tree(self.lib.parent)
log = Path(tempfile.mkdtemp(dir=self.dir)) / "prefetch.log"
env = {"SLOW": "40", "PREFETCH_LOG": str(log)}
self.run_program("list", top, "sleep", 1000, program=self.other, env=env)
logs = Path(tempfile.mkdtemp(dir=self.dir))
log = logs / "prefetch.log"
env = {"SLOW": "40", "PREFETCH_LOG": str(log), "PREFETCH_DONE": str(logs / "done.log")}
out = self.run_program("list", top, "threads", program=self.other, env=env)
self.assertEqual(["a,b,c,f.txt", "1"], out)
self.assertFalse(log.exists())
self.run_program("thread", "list", top, "sleep", 1000, program=self.other, env=env)
self.run_program("thread", "list", top, "await", 3, program=self.other, env=env)
self.assertEqual(sorted(str(top / d) for d in ("a", "b", "c")), sorted(log.read_text().splitlines()))
def test_prefetching_can_be_turned_off(self):
top = self.cold_tree(self.lib.parent)
_, off = self.prefetched("list", top, program=self.other, env={"DD_NO_DIR_PREFETCH": "1"})
_, on = self.prefetched("list", top, program=self.other)
out, off = self.prefetched("list", top, "threads", program=self.other, env={"DD_NO_DIR_PREFETCH": "1"})
_, on = self.prefetched("list", top, "await", 3, program=self.other)
self.assertEqual(["a,b,c,f.txt", "2"], out)
self.assertEqual([], off)
self.assertEqual(sorted(str(top / d) for d in ("a", "b", "c")), on)