Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@
All notable changes to the AVA1 crates and C library. The wire format is version 1 throughout;
see [README](README.md#versions).

## 1.0.9

Read-back while the file arrives. Nothing changes on the wire format.

C library:

- The read-back now runs while a large file arrives: each part is read back as soon as it is safely on
the drive, and only what is left (usually the last few MiB) is read after the last byte. It used to
read every file again from the start after its last byte, which added about 30% to a large transfer
(19.5 s on a 4 GiB file on a PS5 Pro). It runs on 6 threads of its own, so a busy upload does not
hold it back, and it backs off to one read at a time while the incoming data waits to be written.
- The read-back now reads from the drive, not from memory. Read right after it was written, a file is
still in the system's cache, and a check against that copy cannot find a fault on the drive. Each
part is now dropped from the cache before it is read, and read with direct I/O where the system
allows. The log says once per job which method is in use, or that the read may come from the cache
(macOS has no way to drop it).
- A mismatch is still handled when the file is finished, as before. The read after the last byte uses
8 threads of 8 MiB reads. The end-of-job line says how much was read back while files arrived (with
how many ranges and reads that took), how much after, and how long the threads spent reading and
hashing.
- Measured on a PS5 Pro, 4 GiB to the internal drive: 60.4 s with the check, against 83.5 s for the
1.0.8 check and about 62 s with no check at all.

## 1.0.8

Read-back verification. Nothing changes on the wire format: the receiver advertises a new capability bit, and a sender that sets the new job flag gets the check.
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ resolver = "3"
[workspace.package]
edition = "2021"
license = "MIT OR Apache-2.0"
version = "1.0.8"
version = "1.0.9"
# File::lock (1.89). CI builds the published crates with exactly this version.
rust-version = "1.89"
repository = "https://github.com/phantomptr/ava1"
Expand Down
542 changes: 469 additions & 73 deletions c/ava1_apply.c

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion c/ava1_apply.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,22 @@ void ava1_apply_finish_landed(ava1_job_t *j);
#define AVA1_HOOK_SWEPT 11 /* a JnlSweep was appended (the files are durable in place) */
#define AVA1_HOOK_BATCH_ALLOC 13 /* a sync batch is about to allocate its work lists: a nonzero ava1_apply_fault is "out of memory" (tests) */
#define AVA1_HOOK_PREALLOC 9 /* a part file is about to be preallocated (job mutex NOT held) */
#define AVA1_HOOK_READBACK 14 /* JF_READBACK: a large file is about to be read back before its commit */
#define AVA1_HOOK_READBACK 14 /* JF_READBACK: a large file is about to be read back before its commit
* (what was not already read back while it arrived) */
/* Read-backs that find a file wrong before the job fails (each one first resends the file). */
#define AVA1_READBACK_TRIES 3u
/* Tests: every pwrite of file data writes at most this many bytes (0, the default: no cap). */
extern size_t ava1_test_pwrite_max;
/* Tests: every chunk's write first waits this many ms (0, the default: none). */
extern uint32_t ava1_test_chunk_delay_ms;
extern void (*ava1_apply_hook)(ava1_job_t *j, int point, uint32_t id);
/* Tests only (NULL in the payload): an errno to inject at a point instead of doing the work.
* Consulted at AVA1_HOOK_DIR_SYNCED of the staged tree's rename. */
extern int (*ava1_apply_fault)(ava1_job_t *j, int point, uint32_t id);
/* Tests only (NULL in the payload): JF_READBACK is about to read bytes [off, off + len) of file `id` back
* through `fd` (its own descriptor, opened for writing too), `live` while the file still arrives, else in
* the commit's pass. Called with no lock held, from any worker or read-back thread. */
extern void (*ava1_apply_rb_hook)(ava1_job_t *j, uint32_t id, uint64_t off, uint64_t len, int fd, int live);
/* Tests only (0 in the payload): while nonzero, the job thread starts no sync batch. */
extern int ava1_apply_hold_batches;

Expand Down
7 changes: 7 additions & 0 deletions c/ava1_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ static ava1_job_t *create(const uint8_t id[16], const uint8_t owner[32], const u
pthread_mutex_init(&j->jnl_mu, NULL);
pthread_mutex_init(&j->pack_mu, NULL);
pthread_cond_init(&j->cv, NULL);
pthread_cond_init(&j->rb_cv, NULL);
pthread_mutex_init(&j->cmu, NULL);
pthread_cond_init(&j->ccv, NULL);
pthread_mutex_lock(&T.mu);
Expand Down Expand Up @@ -158,6 +159,7 @@ static ava1_job_t *create(const uint8_t id[16], const uint8_t owner[32], const u
pthread_mutex_destroy(&j->jnl_mu);
pthread_mutex_destroy(&j->pack_mu);
pthread_cond_destroy(&j->cv);
pthread_cond_destroy(&j->rb_cv);
pthread_mutex_destroy(&j->cmu);
pthread_cond_destroy(&j->ccv);
free(j);
Expand Down Expand Up @@ -237,6 +239,7 @@ static void job_destroy(ava1_job_t *j) {
pthread_mutex_lock(&j->mu);
j->stopping = 1;
pthread_cond_broadcast(&j->cv);
pthread_cond_broadcast(&j->rb_cv);
pthread_mutex_unlock(&j->mu);
if (j->feeder_started) { /* first: it may be handing work to the threads below */
pthread_mutex_lock(&j->cmu);
Expand All @@ -247,6 +250,7 @@ static void job_destroy(ava1_job_t *j) {
}
if (j->thread_started) pthread_join(j->thread, NULL);
for (i = 0; i < j->nworkers; i++) pthread_join(j->workers[i], NULL);
for (i = 0; i < j->rb_th_n; i++) pthread_join(j->rb_th[i], NULL); /* the job thread started them: joined above */
/* The role's reader is the only thread left that can enqueue work (a copy's reader
* applies what it reads): join it before the queue below is drained, or an
* enqueue concurrent with the drain would append to a freed item. */
Expand Down Expand Up @@ -295,6 +299,8 @@ static void job_destroy(ava1_job_t *j) {
if (j->lf[i]->held) ava1_lf_release(j->lf[i]->held); /* its slots in the large-file budget */
ava1_rset_clear(&j->lf[i]->written);
ava1_rset_clear(&j->lf[i]->durable);
ava1_rset_clear(&j->lf[i]->rb_todo);
ava1_rset_clear(&j->lf[i]->rb_done);
free(j->lf[i]);
}
free(j->lf);
Expand All @@ -312,6 +318,7 @@ static void job_destroy(ava1_job_t *j) {
pthread_mutex_destroy(&j->jnl_mu);
pthread_mutex_destroy(&j->pack_mu);
pthread_cond_destroy(&j->cv);
pthread_cond_destroy(&j->rb_cv);
pthread_mutex_destroy(&j->cmu);
pthread_cond_destroy(&j->ccv);
free(j);
Expand Down
28 changes: 28 additions & 0 deletions c/ava1_job.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
/* JnlOpen.staged: bit 0 = staged under <root>.ava-part; bit 1 = the receiver created the
* empty <root> as its lock (SPEC.md §11.6), so an empty <root> on resume is its own. */
#define AVA1_STAGED_HELD 2
/* JF_READBACK: threads per job that read large files back while they arrive (SPEC.md §13.5). An 8 MiB
* read from a PS5's drive (not its cache) runs at ~26-30 MiB/s per thread (8 threads: ~211 MiB/s), so six
* give ~150 MiB/s, twice a 65 MiB/s upload, with the drive also taking the writes. */
#define AVA1_READBACK_THREADS 6

typedef struct ava1_job ava1_job_t;
/* Where a job's outgoing messages go: a session (network jobs) or a recorder (local, tests).
Expand Down Expand Up @@ -52,6 +56,16 @@ typedef struct { /* one large file being assembled */
int writers; /* write_chunk calls that took their own dups and have not yet recorded the range */
uint8_t held; /* descriptors counted against the large-file budget (0, 1 or 2) */
uint8_t rb_bad; /* read-backs (JF_READBACK) that found the drive's bytes wrong; the file fails at AVA1_READBACK_TRIES */
/* JF_READBACK while the file arrives (SPEC.md §13.5; ava1_apply.c, "read-back"), under j->mu.
* rb_todo: durable bytes nobody has read back yet (a sync batch adds what it made durable, a write
* takes its range out again). rb_done: read back and right since they were last made durable.
* rb_gen: bumped by a reset and by a write over durable bytes, so a read-back in flight across
* either counts for nothing. rb_busy: read-backs of this file in flight. rb_found: one found the
* drive's bytes wrong at group rb_found_g; the commit acts on it (unless that group is written again). */
ava1_rset_t rb_todo, rb_done;
uint32_t rb_gen;
uint8_t rb_busy, rb_found;
uint64_t rb_found_g;
} ava1_lfile_t;

/* Durable-by-log (SPEC.md §15.7). A small file's bytes go to `pack.<n>` in the job directory and
Expand Down Expand Up @@ -208,6 +222,20 @@ struct ava1_job {
int readback;
uint64_t rb_files, rb_bytes, rb_us, rb_mismatches;
uint64_t rb_read_us, rb_hash_us; /* summed over read-back threads: where the time went */
uint64_t rb_live_bytes, rb_after_bytes; /* read back while files arrived / after their last byte (rb_us is the latter's wall time) */
/* The live read-back's own threads (ava1_apply.c, "read-back"): started by the job thread at the first
* durable range of a large file, joined by job_destroy. They wait on rb_cv, which each batch signals. */
pthread_t rb_th[AVA1_READBACK_THREADS];
uint32_t rb_th_n;
pthread_cond_t rb_cv;
uint32_t rb_live_run; /* live reads in flight (under mu): ava1_apply_quiesce waits for none */
uint64_t rb_todo_ranges, rb_live_reads, rb_live_crowded; /* atomics, for the end-of-job line: ranges batches handed
* over, live reads done, of those with chunks waiting */
/* Reading from the drive, not the page cache (atomics): reads whose pages were dropped from the cache
* first (asked / done), reads made with O_DIRECT; the one line per job that says how. */
uint64_t rb_drops, rb_drops_ok, rb_direct_reads;
int rb_how_logged, rb_direct_err; /* ... and why O_DIRECT was refused, the last time it was */
uint32_t rb_bufs; /* read-back buffers in use, live and at commit (under mu; at most READBACK_STRIPES) */
int resumed; /* decided at open (journal replay) or at an attach that finds durable work; never per re-arm */
uint64_t prog_gen; /* the attach generation `resumed` was last decided under */
uint32_t tune_ticks, tune_busy; /* queue occupancy since the last tuning step */
Expand Down
8 changes: 8 additions & 0 deletions c/ava1_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@ void ava1_platform_sleep_ms(unsigned ms);
int ava1_platform_preallocate(int fd, uint64_t len);
/* Set a file's mtime (seconds). The PS5 uses utimensat on the path (verified in backup.c). */
void ava1_platform_set_mtime(int fd, const char *path, uint64_t mtime);
/* Best effort: drops the cached pages of bytes [off, off + len) of the file open at `fd` (written and
* synced, so clean), so that the next read of them comes from the drive (JF_READBACK, SPEC.md §13.5).
* `off` is page-aligned. 0 when the OS was told to, else an errno (ENOTSUP: this OS has no way; ENOSYS:
* its kernel lacks the call). *how names the method, set either way, for a log line. */
int ava1_platform_drop_cache(int fd, uint64_t off, uint64_t len, const char **how);
/* Opens `path` read-only for direct I/O (O_DIRECT: past the page cache; reads in whole, aligned blocks).
* A descriptor, or -1 with errno (ENOTSUP: no direct I/O on this OS; EINVAL: not on this filesystem). */
int ava1_platform_open_direct(const char *path);

#endif
37 changes: 37 additions & 0 deletions c/ava1_ranges.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,43 @@ int ava1_rset_covers(const ava1_rset_t *r, uint64_t start, uint64_t end) {
return 0;
}

int ava1_rset_overlaps(const ava1_rset_t *r, uint64_t start, uint64_t end) {
size_t i;
for (i = 0; start < end && i < r->n; i++)
if (r->v[2 * i] < end && start < r->v[2 * i + 1]) return 1;
return 0;
}

int ava1_rset_remove(ava1_rset_t *r, uint64_t start, uint64_t end) {
size_t i = 0, j;
if (start >= end) return 0;
while (i < r->n && r->v[2 * i + 1] <= start) i++; /* the first pair that ends past start */
if (i == r->n || r->v[2 * i] >= end) return 0; /* none overlaps */
if (r->v[2 * i] < start && end < r->v[2 * i + 1]) { /* inside one pair: it becomes two */
if (r->n == r->cap) {
size_t cap = r->cap ? r->cap * 2 : 4;
uint64_t *v = realloc(r->v, cap * 2 * sizeof *v);
if (!v) return -1;
r->v = v;
r->cap = cap;
}
memmove(&r->v[2 * (i + 1)], &r->v[2 * i], (r->n - i) * 2 * sizeof *r->v);
r->n++;
r->v[2 * i + 1] = start;
r->v[2 * (i + 1)] = end;
return 1;
}
if (r->v[2 * i] < start) r->v[2 * i++ + 1] = start; /* keep the head of the first pair */
j = i;
while (j < r->n && r->v[2 * j + 1] <= end) j++; /* pairs i..j-1 lie inside: they go */
if (j < r->n && r->v[2 * j] < end) r->v[2 * j] = end; /* keep the tail of the last pair */
if (j > i) {
memmove(&r->v[2 * i], &r->v[2 * j], (r->n - j) * 2 * sizeof *r->v);
r->n -= j - i;
}
return 1;
}

void ava1_rset_clear(ava1_rset_t *r) {
free(r->v);
memset(r, 0, sizeof *r);
Expand Down
5 changes: 5 additions & 0 deletions c/ava1_ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ typedef struct {
int ava1_rset_add(ava1_rset_t *r, uint64_t start, uint64_t end); /* 0, or -1 out of memory */
uint64_t ava1_rset_covered(const ava1_rset_t *r);
int ava1_rset_covers(const ava1_rset_t *r, uint64_t start, uint64_t end);
/* 1 when some byte of [start, end) is in the set (never for an empty range). */
int ava1_rset_overlaps(const ava1_rset_t *r, uint64_t start, uint64_t end);
/* Takes [start, end) out of the set: 1 when that removed something, 0 when nothing was there, -1 out of
* memory (cutting one range in two needs a pair more; the set is then unchanged). */
int ava1_rset_remove(ava1_rset_t *r, uint64_t start, uint64_t end);
void ava1_rset_clear(ava1_rset_t *r); /* frees */

/* A bitset of file ids; ava1_bits_init sets every field, ava1_bits_free releases. */
Expand Down
4 changes: 4 additions & 0 deletions c/ava1_recv.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ static void forget_file(ava1_job_t *j, uint32_t i) {
ava1_bits_clear(&j->done, i);
if (j->lf[i]) {
ava1_rset_clear(&j->lf[i]->durable);
ava1_rset_clear(&j->lf[i]->rb_todo);
ava1_rset_clear(&j->lf[i]->rb_done);
j->lf[i]->has_root = j->lf[i]->root_journaled = 0;
}
}
Expand Down Expand Up @@ -278,6 +280,8 @@ static void free_lf(ava1_job_t *j, uint32_t id, ava1_lfile_t *lf) {
ava1_lf_close_fds(j, id, lf); /* the descriptors and their slots in the large-file budget */
ava1_rset_clear(&lf->written);
ava1_rset_clear(&lf->durable);
ava1_rset_clear(&lf->rb_todo);
ava1_rset_clear(&lf->rb_done);
free(lf);
}

Expand Down
46 changes: 46 additions & 0 deletions c/platform_posix.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#if defined(__linux__) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE /* glibc declares O_DIRECT only with it */
#endif
#include "ava1_platform.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
Expand Down Expand Up @@ -37,3 +41,45 @@ void ava1_platform_set_mtime(int fd, const char *path, uint64_t mtime) {
ts[1] = ts[0];
(void)futimens(fd, ts);
}

#if defined(__FreeBSD__)
/* FreeBSD's POSIX_FADV_DONTNEED releases the clean buffers but only deactivates their pages, which stay
* valid: a read right after is still answered from memory. msync(MS_INVALIDATE) then takes the clean,
* now unwired pages out of the file's object. */
static int invalidate_mapped(int fd, uint64_t off, uint64_t len) {
void *p = mmap(NULL, (size_t)len, PROT_READ, MAP_SHARED, fd, (off_t)off);
int rc;
if (p == MAP_FAILED) return errno;
rc = msync(p, (size_t)len, MS_INVALIDATE) == 0 ? 0 : errno;
(void)munmap(p, (size_t)len);
return rc;
}
#endif

int ava1_platform_drop_cache(int fd, uint64_t off, uint64_t len, const char **how) {
#if defined(__linux__)
*how = "posix_fadvise(DONTNEED)";
return posix_fadvise(fd, (off_t)off, (off_t)len, POSIX_FADV_DONTNEED);
#elif defined(__FreeBSD__)
int rc;
*how = "posix_fadvise(DONTNEED) + msync(MS_INVALIDATE)";
if ((rc = posix_fadvise(fd, (off_t)off, (off_t)len, POSIX_FADV_DONTNEED)) != 0) return rc;
return invalidate_mapped(fd, off, len);
#else
(void)fd;
(void)off;
(void)len;
*how = "no call to drop cached pages on this OS";
return ENOTSUP;
#endif
}

int ava1_platform_open_direct(const char *path) {
#ifdef O_DIRECT
return open(path, O_RDONLY | O_DIRECT | O_NOFOLLOW);
#else
(void)path;
errno = ENOTSUP;
return -1;
#endif
}
Loading
Loading