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

+12 -8
View File
@@ -694,7 +694,7 @@ static void finish(struct handle *h) {
#define NTFS3_MAGIC 0x7366746e
#define SDCARDFS_MAGIC 0x5dca2df5
struct scan { DIR *key; char *path; char *names[PREFETCH_BATCH]; int n; };
struct scan { char *path; char *names[PREFETCH_BATCH]; int n; };
static pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t qwake = PTHREAD_COND_INITIALIZER;
@@ -702,6 +702,7 @@ static char *queue[PREFETCH_QUEUE];
static int qtop, qcount;
static int workers;
static struct scan *scans[PREFETCH_SCANS];
static DIR *scan_keys[PREFETCH_SCANS];
static struct { dev_t dev; ino_t ino; } seen[PREFETCH_SEEN];
static int nseen, seen_at;
@@ -719,7 +720,10 @@ static void prefetch_fork(void) {
pthread_cond_init(&qwake, NULL);
for (int i = 0; i < PREFETCH_QUEUE; i++) queue[i] = NULL;
qtop = qcount = workers = 0;
for (int i = 0; i < PREFETCH_SCANS; i++) scans[i] = NULL;
for (int i = 0; i < PREFETCH_SCANS; i++) {
scans[i] = NULL;
scan_keys[i] = NULL;
}
nseen = seen_at = 0;
}
@@ -841,8 +845,7 @@ static void flush_scan(struct scan *s) {
static int scan_slot(DIR *key) {
for (int i = 0; i < PREFETCH_SCANS; i++) {
struct scan *s = __atomic_load_n(&scans[i], __ATOMIC_ACQUIRE);
if (s && s->key == key) return i;
if (__atomic_load_n(&scan_keys[i], __ATOMIC_ACQUIRE) == key) return i;
}
return -1;
}
@@ -851,7 +854,8 @@ static void end_scan(DIR *key) {
int i = scan_slot(key);
if (i < 0) return;
struct scan *s = scans[i];
__atomic_store_n(&scans[i], NULL, __ATOMIC_RELEASE);
scans[i] = NULL;
__atomic_store_n(&scan_keys[i], NULL, __ATOMIC_RELEASE);
flush_scan(s);
free(s->path);
free(s);
@@ -868,11 +872,11 @@ static void begin_scan(DIR *key, int fd) {
free(s);
return;
}
s->key = key;
pthread_mutex_lock(&qlock);
for (int i = 0; i < PREFETCH_SCANS; i++) {
if (scans[i] == NULL) {
__atomic_store_n(&scans[i], s, __ATOMIC_RELEASE);
if (__atomic_load_n(&scan_keys[i], __ATOMIC_ACQUIRE) == NULL) {
scans[i] = s;
__atomic_store_n(&scan_keys[i], key, __ATOMIC_RELEASE);
s = NULL;
break;
}
+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)