Skip to content
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
186 commits
Select commit Hold shift + click to select a range
3688f3c
CUDA keyed graph cache and device arena, both off until a family inst…
jayleaton Oct 10, 2026
b7be793
A failed CUDA kernel launch names its kernel and launch configuration…
jayleaton Oct 10, 2026
faf5901
fix(cuda): correct RDMA VMM capability attribute
jrb-ai Oct 10, 2026
3cc0397
Graph cache: a capture is freed until the map holds it, the keep floo…
jayleaton Oct 11, 2026
c58d83c
CUDA arena: the address maps are rehashed before tombstones fill them
jayleaton Oct 11, 2026
25c02d3
Tensor parallelism across CUDA processes: bootstrap, NCCL, mailbox an…
jayleaton Oct 10, 2026
6b5404d
TP tests: a drain on rank 0 then its clean stop ends rank 1's loop at…
jayleaton Oct 10, 2026
06c4e75
TP RoCE: with no TF_TP_ROCE_HCA, each rank picks the ports on an IPv4…
jayleaton Oct 11, 2026
062af8b
TP README: when -Dtp-nvcc, TF_TP_ROCE_HCA, NCCL_IB_HCA, NCCL_SOCKET_I…
jayleaton Oct 11, 2026
d7510ef
TP test runner: a binary built without the mailbox kernel refuses the…
jayleaton Oct 11, 2026
e16e331
TP RoCE: every RDMA post is bounds-checked before the verb, and peers…
jayleaton Oct 11, 2026
649e788
TP RoCE: no RDMA write past 1 MiB, and boot refuses any setting that …
jayleaton Oct 11, 2026
664a8e2
TP RoCE: TF_TP_ROCE_CHUNKED_KB gathers strides past the slot in slot-…
jayleaton Oct 11, 2026
4ad5029
TP RoCE: a failed transport halts its QPs, drains them and keeps its …
jayleaton Oct 11, 2026
9bbc21a
TP RoCE: tf-tp-test failfast V transport checks the failed-transport …
jayleaton Oct 11, 2026
cf19042
Fix RoCE test smaps lookup for merged mappings
jayleaton Oct 11, 2026
0ba7d30
Sessions: the page map is rehashed before tombstones fill it
jayleaton Oct 11, 2026
0918809
Let a family's round planner drive the lane host with prompt pieces
jayleaton Oct 10, 2026
361f063
Lane host halt: a stop's drain deadline ends the rounds at the next b…
jayleaton Oct 10, 2026
e9f9210
Round planner test: both prompts queue before the host starts, and it…
jayleaton Oct 10, 2026
96ae865
Server hooks for a model family's own chat rules.
jayleaton Oct 10, 2026
df480e0
Server: a stop logs its signal and the open connections when the acce…
jayleaton Oct 10, 2026
2c4d837
Server: SIGTERM / SIGINT drains before stopping.
jayleaton Oct 10, 2026
55bf0a7
Server tests: a stop that lands between a request's count and its dra…
jayleaton Oct 10, 2026
9fbc2b3
Server: a stream that has not sent its first token gets keepalives ev…
jayleaton Oct 10, 2026
bb53ff4
Server tests: the drain test's reply in progress is held until its dr…
jayleaton Oct 10, 2026
9eb765d
DeepSeek-V4.1 host side: release config, EXL3 pack headers, a rank's …
jayleaton Oct 11, 2026
865335e
DeepSeek-V4.1 weight forms built at load, and prepared weight folders…
jayleaton Oct 11, 2026
7ccfc5c
DeepSeek-V4.1 Engram host side: table layout, n-gram hashes of a wind…
jayleaton Oct 11, 2026
b775815
DeepSeek-V4.1 phase timers: where a serving round's time goes, by nam…
jayleaton Oct 11, 2026
def4859
DeepSeek-V4.1 Engram rows from packed per-rank shards on local NVMe, …
jayleaton Oct 11, 2026
c7d29ea
DeepSeek-V4.1 CUDA kernel sources: CSA2 attention and top-k, mHC, rou…
jayleaton Oct 11, 2026
b17df97
DeepSeek-V4.1 kernel tooling: nvcc in a container for any checkout la…
jayleaton Oct 11, 2026
73be75f
DeepSeek-V4.1 kernel launch bindings and their loader: each kernel's …
jayleaton Oct 11, 2026
e687ad5
DeepSeek-V4.1 device kernel tests: tf-dsv41-test, bit-exact against t…
jayleaton Oct 11, 2026
a7674d2
DeepSeek-V4.1 kernel prototypes: attention merge and phase timing, x3…
jayleaton Oct 11, 2026
c0605f0
DeepSeek-V4.1 device-side load: named weights in device buffers, run-…
jayleaton Oct 11, 2026
46bff5e
DeepSeek-V4.1 block emitter: a decode window as calls (attention, the…
jayleaton Oct 11, 2026
6d44dab
CUDA runtime: prioritized streams, external event records, 2D async c…
jayleaton Oct 11, 2026
63e708f
CUDA AOT sets: aot.json specs parsed and matched without cubins, and …
jayleaton Oct 11, 2026
2eeabf7
CUDA memory: a driver allocation count, the arena's first failed chun…
jayleaton Oct 11, 2026
a89bd4f
Graph cache: named caches publish their counts, the floor's state is …
jayleaton Oct 11, 2026
05977fa
Session store: historyOf returns an entry's ids from RAM or from the …
jayleaton Oct 11, 2026
063e855
Graph cache registry: slots claimed, published, released and read und…
jayleaton Oct 11, 2026
d800c62
DeepSeek-V4.1 KV memory: the family's pages, split KV's exchange, the…
jayleaton Oct 11, 2026
bf5a8e8
DeepSeek-V4.1 forward: decode windows over the block emitter, split K…
jayleaton Oct 11, 2026
cd1e3d8
DeepSeek-V4.1 prefill: prompt segments over the prefill emitter, CED …
jayleaton Oct 11, 2026
4b5a078
DeepSeek-V4.1 keyed sampling: the exact host rule, the ranks' pipelin…
jayleaton Oct 11, 2026
1b1e565
DeepSeek-V4.1 drafting, host side: DSpark's chain, cost-derived depth…
jayleaton Oct 11, 2026
18121a3
DeepSeek-V4.1 DSpark on the GPU: the drafting pass as calls, candidat…
jayleaton Oct 11, 2026
f9dab0b
DeepSeek-V4.1 batched rows: several slots in one round, the GPU targe…
jayleaton Oct 11, 2026
f5db10b
DeepSeek-V4.1 serving side: release tokenizer, chat encoding, DSML to…
jayleaton Oct 11, 2026
59af121
DeepSeek-V4.1 served engine and tool: the model at boot, the engine b…
jayleaton Oct 11, 2026
3c93b75
DeepSeek-V4.1 performance extras, host side: side streams, the Engram…
jayleaton Oct 11, 2026
989ca7e
TP RoCE: run_hosts.sh gives NCCL the RoCE picker's ports when neither…
jayleaton Oct 11, 2026
05c5213
Session memory for paged-KV families: page pool, prefix trie, RAM res…
jayleaton Oct 10, 2026
a3303c7
CUDA keyed graph cache and device arena, both off until a family inst…
jayleaton Oct 10, 2026
69e8a65
A failed CUDA kernel launch names its kernel and launch configuration…
jayleaton Oct 10, 2026
d7e89df
fix(cuda): correct RDMA VMM capability attribute
jrb-ai Oct 10, 2026
5826f89
Graph cache: a capture is freed until the map holds it, the keep floo…
jayleaton Oct 11, 2026
5806e55
CUDA arena: the address maps are rehashed before tombstones fill them
jayleaton Oct 11, 2026
d335520
Graph cache: TF_GRAPH_CACHE=0 or off runs every step eagerly in any f…
jayleaton Oct 14, 2026
7ff83d8
Tensor parallelism across CUDA processes: bootstrap, NCCL, mailbox an…
jayleaton Oct 10, 2026
9af2ea2
TP tests: a drain on rank 0 then its clean stop ends rank 1's loop at…
jayleaton Oct 10, 2026
7c9a26e
TP RoCE: with no TF_TP_ROCE_HCA, each rank picks the ports on an IPv4…
jayleaton Oct 11, 2026
8ebb0be
TP README: when -Dtp-nvcc, TF_TP_ROCE_HCA, NCCL_IB_HCA, NCCL_SOCKET_I…
jayleaton Oct 11, 2026
e9e0056
TP test runner: a binary built without the mailbox kernel refuses the…
jayleaton Oct 11, 2026
0472cbf
TP RoCE: every RDMA post is bounds-checked before the verb, and peers…
jayleaton Oct 11, 2026
9688398
TP RoCE: no RDMA write past 1 MiB, and boot refuses any setting that …
jayleaton Oct 11, 2026
72132f0
TP RoCE: TF_TP_ROCE_CHUNKED_KB gathers strides past the slot in slot-…
jayleaton Oct 11, 2026
e65583c
Server hooks for a model family's own chat rules.
jayleaton Oct 10, 2026
bf19238
TP RoCE: a failed transport halts its QPs, drains them and keeps its …
jayleaton Oct 11, 2026
9a486bb
Server: a stop logs its signal and the open connections when the acce…
jayleaton Oct 10, 2026
beae7e8
TP RoCE: tf-tp-test failfast V transport checks the failed-transport …
jayleaton Oct 11, 2026
467504a
Server: SIGTERM / SIGINT drains before stopping.
jayleaton Oct 10, 2026
602aad5
Fix RoCE test smaps lookup for merged mappings
jayleaton Oct 11, 2026
23bf7d8
Server tests: a stop that lands between a request's count and its dra…
jayleaton Oct 10, 2026
890dfc6
TP RoCE: run_hosts.sh gives NCCL the RoCE picker's ports when neither…
jayleaton Oct 11, 2026
5410eac
Session memory for paged-KV families: page pool, prefix trie, RAM res…
jayleaton Oct 10, 2026
a5b8b35
Server: a stream that has not sent its first token gets keepalives ev…
jayleaton Oct 10, 2026
a3e2fb1
TP NCCL: all_reduce's doc says NCCL adds in its ring's order, so its …
jayleaton Oct 14, 2026
0d0bdb2
Sessions: the page map is rehashed before tombstones fill it
jayleaton Oct 11, 2026
2b3cccf
Server tests: the drain test's reply in progress is held until its dr…
jayleaton Oct 10, 2026
62a75ae
TP: keep Linux-only transport host tests off macOS builds
jayleaton Oct 15, 2026
2cf61d3
Sessions: honor zero residency budgets and reserve uncapped requests
jayleaton Oct 15, 2026
9367784
server: keep shared halt and drain on the existing template path
jayleaton Oct 15, 2026
864fe25
Graph cache: effective settings have one digest for ranks to compare …
jayleaton Oct 16, 2026
e2504f6
TP: bootstrap compares the graph cache's effective settings, so a ran…
jayleaton Oct 16, 2026
dcdc30c
Keep the Linux NVMe lane test out of portable host tests
cwschroeder Oct 16, 2026
368a0d3
Keep the Linux NVMe lane test out of portable host tests
cwschroeder Oct 16, 2026
7a6b32f
server: main's drain stop closes the engine without the closed lane-h…
jayleaton Oct 16, 2026
abcad7d
Merge ashhart/main into stack/02-graph-cache-arena
jayleaton Oct 16, 2026
02d455e
Merge stack/02-graph-cache-arena (with ashhart/main) into stack/03-tp…
jayleaton Oct 16, 2026
9266f3f
Merge stack/03-tp-fabric (with ashhart/main) into stack/04-sessions
jayleaton Oct 16, 2026
001ac2e
Merge stack/04-sessions (with ashhart/main) into stack/07-server
jayleaton Oct 16, 2026
33c0ef6
Merge stack/07-server (sessions, TP, graph cache, current main) into …
jayleaton Oct 16, 2026
af0bc53
Server tests: the drain test imports the named json module
jayleaton Oct 16, 2026
7051a3c
Merge stack/07-server's drain-test json import fix into stack/08a-config
jayleaton Oct 16, 2026
e6df8ad
merge origin/main: the round planner and halt on main's split lane host
jayleaton Oct 16, 2026
4da2618
Merge shared/lane-host-core (#861, on current main) into stack/08a-co…
jayleaton Oct 16, 2026
6b9bede
Merge stack/08a-config (current main, shared layers, #861) into stack…
jayleaton Oct 16, 2026
f17db02
Merge stack/08b-engram (current main, shared layers, #861) into stack…
jayleaton Oct 16, 2026
0137ac2
Merge stack/08c-kernels (current main, shared layers, #861) into stac…
jayleaton Oct 16, 2026
cf649ff
Merge stack/08e-block (current main, shared layers, #861) into stack/…
jayleaton Oct 16, 2026
4a02606
Merge stack/08r-runtime (current main, shared layers, #861) into stac…
jayleaton Oct 16, 2026
968a4f2
Merge stack/09a-kv (current main, shared layers, #861) into stack/09b…
jayleaton Oct 16, 2026
1d0b2d7
Merge stack/09b-forward (current main, shared layers, #861) into stac…
jayleaton Oct 16, 2026
ed4239b
Merge stack/10-prefill (current main, shared layers, #861) into stack…
jayleaton Oct 16, 2026
a6bc6c4
Merge stack/11a-sampling (current main, shared layers, #861) into sta…
jayleaton Oct 16, 2026
999755d
Merge stack/11b-draft (current main, shared layers, #861) into stack/…
jayleaton Oct 16, 2026
850f6b2
Merge stack/11c-device-drafts (current main, shared layers, #861) int…
jayleaton Oct 16, 2026
a0d8181
Merge stack/11d-batched-rows (current main, shared layers, #861) into…
jayleaton Oct 16, 2026
d624aa1
Merge stack/12a-serving-host (current main, shared layers, #861, fami…
jayleaton Oct 16, 2026
d2f0223
Merge stack/12b-serving-engine (current main, shared layers, #861, fa…
jayleaton Oct 16, 2026
95f6b4c
DeepSeek-V4.1 sessions: an unset idle residency stays unlimited
jayleaton Oct 16, 2026
3f392a6
Merge stack/11d-batched-rows (unset session residency stays unlimited…
jayleaton Oct 16, 2026
75eae6c
Merge stack/12a-serving-host (unset session residency stays unlimited…
jayleaton Oct 16, 2026
503eeb3
Merge stack/12b-serving-engine (unset session residency stays unlimit…
jayleaton Oct 16, 2026
8900c7c
Sessions: a failed prefix insert leaves the index as it was
jayleaton Oct 17, 2026
3aeebb9
CUDA arena: a reuse whose address-map insertion fails keeps its range…
jayleaton Oct 17, 2026
46161ac
Merge stack/02-graph-cache-arena (the #635 arena fix) into stack/03-t…
jayleaton Oct 17, 2026
78ce706
Merge stack/03-tp-fabric (the #635 arena and #637 prefix fixes) into …
jayleaton Oct 17, 2026
9c5bd60
Merge stack/04-sessions (the #635 arena and #637 prefix fixes) into s…
jayleaton Oct 17, 2026
eaa7953
Merge stack/07-server (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
4370f91
Merge stack/08a-config (the #635 arena and #637 prefix fixes) into st…
jayleaton Oct 17, 2026
a0f9598
Merge stack/08b-engram (the #635 arena and #637 prefix fixes) into st…
jayleaton Oct 17, 2026
98a1695
Merge stack/08c-kernels (the #635 arena and #637 prefix fixes) into s…
jayleaton Oct 17, 2026
28e33cc
Merge stack/08e-block (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
f5a0b8b
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
3b7681f
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
334ce65
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
6b97211
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
fc25bef
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
c58f30c
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
d46405c
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
87d7912
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
ada4b74
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
9002037
Merge the layer below (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
88418d8
test(tp): reject malformed runner commands before configuration
cwschroeder Oct 17, 2026
4c69b0d
fix(tp): parse runner commands before opening sessions
cwschroeder Oct 17, 2026
4df28ce
test(tp): retain verbs load failures in HCA diagnostics
cwschroeder Oct 17, 2026
0a80da0
fix(tp): distinguish verbs load errors from absent HCA ports
cwschroeder Oct 17, 2026
999cb03
Merge stack/03-tp-fabric (adopted #636 runner CLI validation and HCA …
jayleaton Oct 18, 2026
5f14a64
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
749240a
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
7122563
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
6390cf1
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
829c4c9
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
b81382d
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
2be028e
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
e47ed0e
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
e548a60
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
2fc04e3
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
92ff736
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
043a4c8
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
5ebfcdd
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
956febe
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
d9df51c
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
4a5263d
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
cdde1a4
Merge ashhart/main (1.1.0, 76aafcf2) into stack/02-graph-cache-arena
jayleaton Oct 18, 2026
adbdb8a
CUDA launches: a not-ready launch returns NotReady unlogged, as Drive…
jayleaton Oct 18, 2026
3bd6a6a
Merge stack/02-graph-cache-arena (adbdb8ab, main 76aafcf2) into stack…
jayleaton Oct 18, 2026
54141a1
Merge stack/03-tp-fabric (3bd6a6ab, main 76aafcf2) into stack/04-sess…
jayleaton Oct 18, 2026
80d4ed5
Merge stack/04-sessions (54141a1e, main 76aafcf2) into stack/07-server
jayleaton Oct 18, 2026
aebceae
Merge stack/07-server (80d4ed5c, main 76aafcf2) into stack/08a-config
jayleaton Oct 18, 2026
1003f08
Build: DeepSeek-V4.1's host tests on hosts other than Windows, whose …
jayleaton Oct 18, 2026
aa96782
Merge stack/08a-config (1003f08f, main 76aafcf2) into stack/08b-engram
jayleaton Oct 18, 2026
3f758dc
Merge stack/08b-engram (aa967828, main 76aafcf2) into stack/08c-kernels
jayleaton Oct 18, 2026
ea54d42
Merge stack/08c-kernels (3f758dcf, main 76aafcf2) into stack/08e-block
jayleaton Oct 18, 2026
d6190dc
Merge stack/08e-block (ea54d42d, main 76aafcf2) into stack/08r-runtime
jayleaton Oct 18, 2026
6742872
Merge stack/08r-runtime (d6190dcf, main 76aafcf2) into stack/09a-kv
jayleaton Oct 18, 2026
a88da5d
Build: DeepSeek-V4.1's KV host tests also skip Windows, like the fami…
jayleaton Oct 18, 2026
696e58a
Merge stack/09a-kv (a88da5de, main 76aafcf2) into stack/09b-forward
jayleaton Oct 18, 2026
4ac0c37
Merge stack/09b-forward (696e58a7, main 76aafcf2) into stack/10-prefill
jayleaton Oct 18, 2026
a4ca6d3
Merge stack/10-prefill (4ac0c37d, main 76aafcf2) into stack/11a-sampling
jayleaton Oct 18, 2026
5e6e8c6
Merge stack/11a-sampling (a4ca6d39, main 76aafcf2) into stack/11b-draft
jayleaton Oct 18, 2026
3f0fc40
Merge stack/11b-draft (5e6e8c68, main 76aafcf2) into stack/11c-device…
jayleaton Oct 18, 2026
5faa948
Merge stack/11c-device-drafts (3f0fc404, main 76aafcf2) into stack/11…
jayleaton Oct 18, 2026
c24b124
Merge stack/11d-batched-rows (5faa9489, main 76aafcf2) into stack/12a…
jayleaton Oct 18, 2026
c14a40a
Merge stack/12a-serving-host (c24b124d, main 76aafcf2) into stack/12b…
jayleaton Oct 18, 2026
7169718
Merge stack/12b-serving-engine (c14a40a0, main 76aafcf2) into stack/1…
jayleaton Oct 18, 2026
ab2fa63
DeepSeek-V4.1 serving tests: TF_DSV41_MODEL and TF_DSV41_GOLDEN throu…
jayleaton Oct 18, 2026
b04302a
Merge stack/12a-serving-host (ab2fa636: DeepSeek-V4.1 serving tests r…
jayleaton Oct 18, 2026
232f69e
Merge stack/12b-serving-engine (b04302a7: DeepSeek-V4.1 serving tests…
jayleaton Oct 18, 2026
ba8856a
Build: tensorfold-dsv41's checkpoint CLI imports core, as main's chec…
jayleaton Oct 18, 2026
45d5b13
Merge stack/12b-serving-engine (ba8856a7: tensorfold-dsv41's checkpoi…
jayleaton Oct 18, 2026
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
35 changes: 33 additions & 2 deletions zig/src/cuda/arena.zig
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ pub const Arena = struct {
return p;
}
const n: u64 = roundUp(len, a.o.alignment);
try a.live.ensureUnusedCapacity(a.gpa, 1); // before a range is taken: a failure leaves free list and cursor alone
if (a.free_small.getPtr(n)) |l| if (l.pop()) |p| {
try a.live.put(a.gpa, p, n);
a.live.putAssumeCapacityNoClobber(p, n);
a.stats.reused += 1;
return p;
};
Expand All @@ -105,7 +106,7 @@ pub const Arena = struct {
a.stats.chunk_bytes += cl;
}
const p = a.cursor;
try a.live.put(a.gpa, p, n);
a.live.putAssumeCapacityNoClobber(p, n);
a.cursor += n;
a.stats.small += 1;
return p;
Expand Down Expand Up @@ -447,3 +448,33 @@ test "arena: VMM keeps the first failed driver call and its code, later failures
try t.expectEqual(@as(?u64, null), be.chunk(be.ctx, 8 << 20));
try t.expectEqualStrings("the reserved range", v.failed.?.call);
}

test "arena: a reuse whose address-map insertion fails keeps its range free, and the retry gets it" {
const t = std.testing;
var f: Fake = .{};
var fa = t.FailingAllocator.init(t.allocator, .{});
{
var a = Arena.init(fa.allocator(), f.backend(2 << 20), .{ .small_max = 1 << 20, .small_chunk = 4 << 20 });
defer a.deinit(); // testing.allocator underneath reports any leaked host bytes
const p = a.alloc(4096).?;
try t.expect(a.free(p));
// other sizes take back the room the free gave: inserting the reused range must grow the map
while (a.live.available > 0) _ = a.alloc(512).?;
const live = a.live.count();
const cursor = a.cursor;
const chunks = f.chunks;
fa.fail_index = fa.alloc_index; // the next allocation fails, and every one after it until healed
try t.expectEqual(@as(?u64, null), a.alloc(4096));
try t.expectEqual(live, a.live.count());
try t.expectEqual(cursor, a.cursor);
try t.expectEqual(chunks, f.chunks);
try t.expectEqualSlices(u64, &.{p}, a.free_small.get(4096).?.items);
try t.expectEqual(@as(u64, 0), a.stats.reused);
fa.fail_index = std.math.maxInt(usize);
try t.expectEqual(p, a.alloc(4096).?);
try t.expectEqual(@as(u64, 1), a.stats.reused);
try t.expectEqual(@as(usize, 0), a.free_small.get(4096).?.items.len);
try t.expect(a.free(p));
}
try t.expectEqual(f.chunks, f.released); // every chunk went back to the backend
}
1 change: 1 addition & 0 deletions zig/src/sessions/portable_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ test {
_ = @import("pool.zig");
_ = @import("pagestore.zig");
_ = @import("prefix.zig");
_ = @import("prefix_fail_test.zig");
_ = @import("format.zig");
}
50 changes: 38 additions & 12 deletions zig/src/sessions/prefix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,19 @@ pub fn Index(comptime Extra: type) type {
return x.by_key.get(key);
}

/// Reserves first, so a failure changes nothing; free_nodes keeps room for every slot (prune never allocates).
fn newNode(x: *Self, digest: Digest, parent: u32, depth: u32) !u32 {
const id: u32 = if (x.free_nodes.pop()) |i| i else blk: {
try x.nodes.append(x.gpa, undefined);
try x.by_digest.ensureUnusedCapacity(x.gpa, 1);
if (x.free_nodes.items.len == 0) {
try x.nodes.ensureUnusedCapacity(x.gpa, 1);
try x.free_nodes.ensureTotalCapacity(x.gpa, x.nodes.items.len + 1);
}
const id: u32 = x.free_nodes.pop() orelse blk: {
x.nodes.appendAssumeCapacity(undefined);
break :blk @intCast(x.nodes.items.len - 1);
};
x.nodes.items[id] = .{ .digest = digest, .parent = parent, .depth = depth };
try x.by_digest.put(x.gpa, digest, id);
x.by_digest.putAssumeCapacityNoClobber(digest, id);
if (parent != none) {
const p = &x.nodes.items[parent];
x.nodes.items[id].next_sib = p.first_child;
Expand All @@ -143,18 +149,22 @@ pub fn Index(comptime Extra: type) type {
return x.gpa.alloc(i32, x.page);
}

/// Adds an entry for ids (chain: its full pages' digests), or returns the one with the same key.
/// Adds an entry for ids (chain: its full pages' digests) or returns the one with its key; an error adds none.
pub fn insert(x: *Self, tag: u32, ids: []const i32, digests: []const Digest, ram: bool, extra: Extra) !Placed {
const full: u32 = @intCast(ids.len / x.page);
std.debug.assert(digests.len >= full);
const r = root(tag);
var n = x.by_digest.get(r) orelse try x.newNode(r, none, 0);
errdefer x.prune(n); // the path's new nodes, which no entry holds yet
for (0..full) |j| {
const d = digests[j];
n = x.by_digest.get(d) orelse try x.newNode(d, n, @intCast(j + 1));
}
const r2 = try x.place(tag, n, ids.len, ids[full * x.page ..], extra);
if (r2.new and ram) try x.setRam(r2.id, true, ids);
if (r2.new and ram) x.setRam(r2.id, true, ids) catch |err| {
_ = x.drop(r2.id); // the errdefer above prunes its path
return err;
};
return r2;
}

Expand All @@ -164,6 +174,7 @@ pub fn Index(comptime Extra: type) type {
const r = root(tag);
break :blk x.by_digest.get(r) orelse try x.newNode(r, none, 0);
};
errdefer x.prune(n); // the path's new nodes, which no entry holds yet
const d0 = x.nodes.items[n].depth;
const full: u32 = @intCast(suffix.len / x.page);
for (0..full) |j| {
Expand All @@ -183,13 +194,17 @@ pub fn Index(comptime Extra: type) type {
fn place(x: *Self, tag: u32, n: u32, pos: u64, tail_ids: []const i32, extra: Extra) !Placed {
const key = entryKey(x.nodes.items[n].digest, pos, tail_ids);
if (x.by_key.get(key)) |e| return .{ .id = e, .new = false };
const tail = try x.gpa.dupe(i32, tail_ids);
errdefer x.gpa.free(tail);
const id: u32 = if (x.free_entries.pop()) |i| i else blk: {
try x.entries.append(x.gpa, undefined);
try x.by_key.ensureUnusedCapacity(x.gpa, 1);
if (x.free_entries.items.len == 0) {
try x.entries.ensureUnusedCapacity(x.gpa, 1);
try x.free_entries.ensureTotalCapacity(x.gpa, x.entries.items.len + 1);
}
const tail = try x.gpa.dupe(i32, tail_ids); // the last step that can fail
const id: u32 = x.free_entries.pop() orelse blk: {
x.entries.appendAssumeCapacity(undefined);
break :blk @intCast(x.entries.items.len - 1);
};
try x.by_key.put(x.gpa, key, id);
x.by_key.putAssumeCapacityNoClobber(key, id);
const nd = &x.nodes.items[n];
x.entries.items[id] = .{ .key = key, .tag = tag, .pos = pos, .node = n, .tail = tail, .ram = false, .next_at = nd.first_entry, .extra = extra };
if (nd.first_entry != none) x.entries.items[nd.first_entry].prev_at = id;
Expand All @@ -214,6 +229,7 @@ pub fn Index(comptime Extra: type) type {
if (e.ram == ram) return;
if (ram) {
const all = ids orelse return error.IdsNeeded;
errdefer x.dropTokens(e.node); // the blocks this call took: tokens stay only where sub_ram > 0
var n = e.node;
while (n != none and x.nodes.items[n].depth > 0) : (n = x.nodes.items[n].parent) {
const nd = &x.nodes.items[n];
Expand Down Expand Up @@ -243,6 +259,11 @@ pub fn Index(comptime Extra: type) type {

/// Removes an entry and the nodes no entry needs any more.
pub fn remove(x: *Self, id: u32) void {
x.prune(x.drop(id));
}

/// Unlinks an entry and frees its slot; returns its node, whose path may now hold no entry.
fn drop(x: *Self, id: u32) u32 {
x.setRam(id, false, null) catch unreachable;
const e = &x.entries.items[id];
const n = e.node;
Expand All @@ -251,8 +272,13 @@ pub fn Index(comptime Extra: type) type {
_ = x.by_key.remove(e.key);
x.gpa.free(e.tail);
e.live = false;
x.free_entries.append(x.gpa, id) catch {};
x.free_entries.appendAssumeCapacity(id); // place keeps room for every slot
x.addPath(n, -1, 0);
return n;
}

/// Frees the nodes from n up that no entry holds (sub_all 0).
fn prune(x: *Self, n: u32) void {
var m = n;
while (m != none and x.nodes.items[m].sub_all == 0) {
const nd = x.nodes.items[m];
Expand All @@ -261,7 +287,7 @@ pub fn Index(comptime Extra: type) type {
if (nd.next_sib != none) x.nodes.items[nd.next_sib].prev_sib = nd.prev_sib;
}
if (x.by_digest.remove(nd.digest)) x.dropped();
x.free_nodes.append(x.gpa, m) catch {};
x.free_nodes.appendAssumeCapacity(m); // newNode keeps room for every slot
m = nd.parent;
}
}
Expand Down
127 changes: 127 additions & 0 deletions zig/src/sessions/prefix_fail_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//! prefix.Index under allocation failure: an insert that fails changes nothing, and the same insert then succeeds.

const std = @import("std");
const prefix = @import("prefix.zig");
const testing = std.testing;
const T = prefix.Index(u64);
const Digest = prefix.Digest;
const page = 16;

/// What a failed insert must leave as it was; spare token blocks may stay in the reuse pool.
const Shape = struct { keys: u32, digests: u32, ram: u32, held: u32, sub_all: u32, sub_ram: u32 };

fn shape(x: *T, tag: u32) Shape {
var s: Shape = .{ .keys = x.by_key.count(), .digests = x.by_digest.count(), .ram = 0, .held = 0, .sub_all = 0, .sub_ram = 0 };
var e = x.by_key.valueIterator();
while (e.next()) |id| s.ram += @intFromBool(x.get(id.*).ram);
var n = x.by_digest.valueIterator();
while (n.next()) |id| s.held += @intFromBool(x.node(id.*).tokens != null);
if (x.by_digest.get(prefix.root(tag))) |r| {
s.sub_all = x.node(r).sub_all;
s.sub_ram = x.node(r).sub_ram;
}
return s;
}

/// Every slot is live in its map or on its free list, and a node keeps its page's tokens exactly while sub_ram > 0.
fn consistent(x: *T) !void {
try testing.expectEqual(x.entries.items.len - x.free_entries.items.len, x.by_key.count());
try testing.expectEqual(x.nodes.items.len - x.free_nodes.items.len, x.by_digest.count());
var n = x.by_digest.valueIterator();
while (n.next()) |id| {
const nd = x.node(id.*);
if (nd.depth > 0) try testing.expectEqual(nd.sub_ram > 0, nd.tokens != null);
}
}

fn seq(gpa: std.mem.Allocator, n: usize, salt: i32) ![]i32 {
const v = try gpa.alloc(i32, n);
for (v, 0..) |*t, i| t.* = @as(i32, @intCast(i % 997)) * 3 + salt;
return v;
}

fn add(x: *T, tag: u32, ids: []const i32, ram: bool, used: u64) !u32 {
var d: [64]Digest = undefined;
prefix.chain(tag, ids, page, d[0 .. ids.len / page]);
return (try x.insert(tag, ids, d[0 .. ids.len / page], ram, used)).id;
}

/// The (k + 1)-th allocation from now fails, and every one after it until `heal`.
fn failAfter(fa: *testing.FailingAllocator, k: usize) void {
fa.fail_index = fa.alloc_index + k;
}

fn heal(fa: *testing.FailingAllocator) void {
fa.fail_index = std.math.maxInt(usize);
}

test "a RAM insert failing at any allocation leaves the index as it was, and the same insert then succeeds" {
const chat = try seq(testing.allocator, 160, 0);
defer testing.allocator.free(chat);
var d: [16]Digest = undefined;
prefix.chain(7, chat[0..150], page, d[0..9]);
var k: usize = 0;
while (k < 256) : (k += 1) {
var fa = testing.FailingAllocator.init(testing.allocator, .{});
var x = T.init(fa.allocator(), page);
defer x.deinit();
// what the insert meets: entries sharing its first pages, a disk entry, another tag, and free slots to reuse
_ = try add(&x, 7, chat[0..40], true, 1);
_ = try add(&x, 7, chat[0..72], false, 2);
_ = try add(&x, 8, chat[0..33], true, 3);
x.remove(try add(&x, 9, chat[0..48], true, 4));
try consistent(&x);
const before = shape(&x, 7);
failAfter(&fa, k);
const got = x.insert(7, chat[0..150], d[0..9], true, 9) catch |err| {
try testing.expectEqual(error.OutOfMemory, err);
try consistent(&x);
try testing.expectEqual(before, shape(&x, 7));
heal(&fa);
const again = try x.insert(7, chat[0..150], d[0..9], true, 9);
try testing.expect(again.new);
try consistent(&x);
var out: [150]i32 = undefined;
try x.idsOf(again.id, &out);
try testing.expectEqualSlices(i32, chat[0..150], &out);
var d10: [10]Digest = undefined;
prefix.chain(7, chat[0..160], page, &d10);
try testing.expectEqual(@as(?u32, again.id), x.find(7, chat[0..160], &d10, .ram));
x.remove(again.id);
try consistent(&x);
try testing.expectEqual(before, shape(&x, 7));
continue;
};
// no allocation left to fail: every failure point before this one was injected and rolled back
try testing.expect(got.new and k > 0);
return;
}
return error.TestUnexpectedResult;
}

test "a disk insert into an empty index failing at any allocation leaves it empty, and the same insert then succeeds" {
const chat = try seq(testing.allocator, 90, 0);
defer testing.allocator.free(chat);
var k: usize = 0;
while (k < 256) : (k += 1) {
var fa = testing.FailingAllocator.init(testing.allocator, .{});
var x = T.init(fa.allocator(), page);
defer x.deinit();
failAfter(&fa, k);
const got = x.insertFrom(7, prefix.none, chat, 5) catch |err| {
try testing.expectEqual(error.OutOfMemory, err);
try consistent(&x);
try testing.expectEqual(@as(u32, 0), x.by_digest.count()); // the tag's root and pages made before the failure
try testing.expectEqual(@as(u32, 0), x.by_key.count());
heal(&fa);
const again = try x.insertFrom(7, prefix.none, chat, 5);
try testing.expect(again.new);
try testing.expectEqual(@as(u32, 1 + 5), x.by_digest.count());
try consistent(&x);
continue;
};
try testing.expect(got.new and k > 0);
return;
}
return error.TestUnexpectedResult;
}
1 change: 1 addition & 0 deletions zig/src/sessions/sessions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test {
_ = disk;
_ = jobs;
_ = store;
_ = @import("prefix_fail_test.zig");
_ = @import("disk_test.zig");
_ = @import("store_test.zig");
}
Loading