summaryrefslogtreecommitdiff
path: root/core/ninep/tree.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-20 07:33:55 -0300
committerGabriel Schneider <[email protected]>2026-11-20 14:27:36 -0300
commit5671a8275203334594b6f3b548e6a6012ef9fae6 (patch)
treeee18ce296eea19620efdca231ee0de30b66c33f9 /core/ninep/tree.zig
parent90ba0ef908dd65b322676f7a0f6edc5b7b976242 (diff)
downloadpardes-5671a8275203334594b6f3b548e6a6012ef9fae6.tar.gz
pardes-5671a8275203334594b6f3b548e6a6012ef9fae6.zip
ninep: a client that joins a session with 9P work under way has the 9P state the session has: what a step reads that the requests made is the core's state like any other (P30, P35), so the walk sees it: fs leaves Pardes's and Pane's authority lists and the 9P server's own part says so in its types (fs.Namespace: the sockets, mounts, log, the request being served's flags and the rest; pane.State: the event and pty readers' queues and counts, winsize, unannounced; Open: the listener's held read and a stream's last answer; LogOpen whole), so the open records, the writes held to go in as one edit (fs.batch) and each pane's addr, limit, dot truncation, wrsel, joined, nomark, noscroll, lock, run, unmarked, tag truncation and chord are in every dump and in the digest; the digest is cheap by type: a pane's 9P state adds nothing for a pane no script has touched and its fields as one run of bytes for the rest (pane.State.digest), the namespace the opens in use and held writes by their length (Namespace.digest: a 9P body append hashed its whole batch, +0.14% on body-appends), so the frame and perf gate's 121 per-operation counts are within 0.1% of tree2 (largest client-key-down +0.054%; the plain walk of the same state cost client-key-down +0.73%); an open's union is named (Open.What) and digests what a step reads of it (its kind, a run, an event reader's flag, a pager's serial, a port's index), never the text its reader is answered with, which each process makes of its own state (walk.hash takes a tagged union's own digest as it takes a struct's); the opens are taken as far as fs.opens_len (an idle session's none; the scans that read records bound by it, openOf too, and an open lets go of what a rollback left in its record past the length); a session revived from a dump (Restore, an Upgrade, a start from one: dump.loadAndRevive; a closed pane brought back: loadPane) drops the opens and each pane's 9P state, as today, its connections gone (fs.Namespace.forget): a client's join loads alone and keeps them; not the join's dump alone: the tape's dump round trip at a replay's end wants the image to carry what the digest covers; pty.Run's answer cut at its length; the wire version 24, so the semantics word moves 64e89d6b25a84796 -> 18636a639ac075de (it did not move with the walk alone: a base client would have digested other fields than its session; test/daemon.py's WIRE with it, and its upgrade case takes a client of the other side's protocol saying `run the same pardes at both ends`, as its third terminal already did); the eleven tests of the change before pass; a join's dump (the probe, Debug): idle 63258 -> 64612 bytes, 50 opens held 63270 -> 66744; the image idle 233426 -> 237001, 50 opens 233738 -> 295089; tui core-test 867/1022 (155 skipped), shell-test 1062/1065, behaviour-test 275/275 and --replay 275/275, replicas --clients 5 diverged 0/0/0, unequal 0, unlisted stops 0, corpus 22 of 22, no leak (load 30-40, before the digests were made cheap; the whole gate on the change after)
Co-Authored-By: Claude Opus <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/ninep/tree.zig')
-rw-r--r--core/ninep/tree.zig95
1 files changed, 61 insertions, 34 deletions
diff --git a/core/ninep/tree.zig b/core/ninep/tree.zig
index 1eaaa8cb..107479f6 100644
--- a/core/ninep/tree.zig
+++ b/core/ninep/tree.zig
@@ -124,9 +124,45 @@ pub const out_reserve = 4 * 1024;
/// (editors/acme/dat.h:373-385). An open that holds something answers its
/// record's index plus one as the handle; one that holds nothing answers 0.
pub const Open = struct {
+ /// A client's join carries it and the digest covers it (fs.Namespace),
+ /// but what only its reader is answered with: the listener's held
+ /// read, the stream's last answer, the panes a look or exec touched.
+ pub const replica = .{ .authority = .{ "results", "held", "replay" } };
/// The node it was opened on, which every use checks; 0 is a free record.
node: u64 = 0,
- what: union(enum) {
+ what: What = .{ .snapshot = null },
+ /// What a write to a file of command lines left unfinished: a last line
+ /// with no newline yet, or an Edit block still open. A write through a
+ /// mount is cut at the 9P message size, so one `echo` of many lines may
+ /// come as several writes, cut anywhere; the rest waits for the open's
+ /// next write, or its release, and never runs as a fragment.
+ pending: std.ArrayList(u8) = .empty,
+ /// The panes this open's last write touched, which a read of look or
+ /// exec on it answers, as /net/tcp/clone answers on the open that
+ /// asked; a fresh open holds the session's last, from before it opened.
+ results: Results = .{},
+ /// A read that found nothing yet (`.again`), kept to be answered when
+ /// this open's file has something, the way factotum keeps its log's
+ /// waiting reads and answers them on append (security/auth/factotum/
+ /// log.c:4-52) and acme an event read (editors/acme/xfid.c:994). One at a
+ /// time, as acme's window keeps one `eventx`: a newer read takes the
+ /// place of the older. The listener fills it in and answers it
+ /// (runtime/9p_io.zig, `answerHeld`).
+ held: ?Held = null,
+ /// The last answer a stream open (a followed log, event, pty/data) gave,
+ /// and the offset it was read at. A read at an offset inside it is
+ /// answered from it again: bash's `read` takes a chunk, keeps a line and
+ /// lseeks back to just past it, and a stream has already moved on.
+ replay: ?Replay = null,
+ /// A `name` open has named its pane: one name an open, so a second line
+ /// on it (bash writes `printf 'a\nb\n'` a line at a time) is refused.
+ named: bool = false,
+ /// A line over 1 MiB was refused: what follows of it, through its
+ /// newline, is dropped rather than run as a line of its own.
+ discarding: bool = false,
+
+ /// What it is an open of, and what that holds.
+ pub const What = union(enum) {
/// /screen's frame from the open, a terminal body's history from its
/// first read.
snapshot: ?[]const u8,
@@ -160,36 +196,22 @@ pub const Open = struct {
/// An open reading a plumb port (Plumb.zig's ports, plumber(4)'s):
/// the messages sent to it since the open, packed, a read each.
port: struct { index: u8, queue: events.Queue = .{} },
- } = .{ .snapshot = null },
- /// What a write to a file of command lines left unfinished: a last line
- /// with no newline yet, or an Edit block still open. A write through a
- /// mount is cut at the 9P message size, so one `echo` of many lines may
- /// come as several writes, cut anywhere; the rest waits for the open's
- /// next write, or its release, and never runs as a fragment.
- pending: std.ArrayList(u8) = .empty,
- /// The panes this open's last write touched, which a read of look or
- /// exec on it answers, as /net/tcp/clone answers on the open that
- /// asked; a fresh open holds the session's last, from before it opened.
- results: Results = .{},
- /// A read that found nothing yet (`.again`), kept to be answered when
- /// this open's file has something, the way factotum keeps its log's
- /// waiting reads and answers them on append (security/auth/factotum/
- /// log.c:4-52) and acme an event read (editors/acme/xfid.c:994). One at a
- /// time, as acme's window keeps one `eventx`: a newer read takes the
- /// place of the older. The listener fills it in and answers it
- /// (runtime/9p_io.zig, `answerHeld`).
- held: ?Held = null,
- /// The last answer a stream open (a followed log, event, pty/data) gave,
- /// and the offset it was read at. A read at an offset inside it is
- /// answered from it again: bash's `read` takes a chunk, keeps a line and
- /// lseeks back to just past it, and a stream has already moved on.
- replay: ?Replay = null,
- /// A `name` open has named its pane: one name an open, so a second line
- /// on it (bash writes `printf 'a\nb\n'` a line at a time) is refused.
- named: bool = false,
- /// A line over 1 MiB was refused: what follows of it, through its
- /// newline, is dropped rather than run as a line of its own.
- discarding: bool = false,
+
+ /// What a step reads of it: not the text a reader is answered from
+ /// (a screen, a listing, the log, a port's messages), which each
+ /// process makes of its own state, and a replica never answers.
+ pub fn digest(w: *const What, h: *pardes.walk.Hasher) void {
+ const tag = std.meta.activeTag(w.*);
+ pardes.walk.hash(@TypeOf(tag), &tag, h);
+ switch (w.*) {
+ .run => |*run| pardes.walk.hash(pty.Run, run, h),
+ .event => |*reader| pardes.walk.hash(bool, reader, h),
+ .pager => |*serial| pardes.walk.hash(u32, serial, h),
+ .port => |*port| pardes.walk.hash(u8, &port.index, h),
+ .snapshot, .log, .frozen, .pty_data, .ctl, .lines, .writes, .plumb => {},
+ }
+ }
+ };
pub const Replay = struct { off: u64, bytes: []u8 };
@@ -230,7 +252,7 @@ pub const open_slots = 64;
/// The record a request's handle names, when it was opened on that node.
pub fn openOf(p: *Pardes, req: Req) ?*Open {
- if (req.handle == 0 or req.handle > p.fs.opens.len) return null;
+ if (req.handle == 0 or req.handle > p.fs.opens_len) return null;
const o = &p.fs.opens[req.handle - 1];
return if (o.node == req.node) o else null;
}
@@ -614,7 +636,7 @@ fn runAttr(p: *Pardes, req: Req, target: Target) ?Reply.Attr {
.top, .col, .port => return null,
};
if (t.file != .pty_run) return null;
- const run = if (req.handle != 0) openOf(p, req) else for (&p.fs.opens) |*o| {
+ const run = if (req.handle != 0) openOf(p, req) else for (p.fs.opens[0..p.fs.opens_len]) |*o| {
if (o.node == req.node and o.what == .run) break o;
} else null;
if ((run orelse return null).what != .run) return null;
@@ -895,7 +917,7 @@ fn open(p: *Pardes, req: Req, target: Target) Reply {
.ctl => if (linesOpen(target, req)) .lines else .{ .frozen = &.{} },
// One writer of the rules at a time, as plumber(4)'s.
.rules, .send => if (req.omode & 3 == 0) return .{ .tag = req.tag } else plumb: {
- if (f == .rules) for (p.fs.opens) |o| if (o.node == req.node and o.what == .plumb) return failText(req.tag, E.BUSY, e_in_use);
+ if (f == .rules) for (p.fs.opens[0..p.fs.opens_len]) |o| if (o.node == req.node and o.what == .plumb) return failText(req.tag, E.BUSY, e_in_use);
break :plumb .plumb;
},
else => if (linesOpen(target, req)) .lines else return .{ .tag = req.tag },
@@ -934,7 +956,11 @@ fn open(p: *Pardes, req: Req, target: Target) Reply {
// record would be another than the session's.
const i = p.tape.authority(freeOpen, .{p}) orelse return Reply.fail(req.tag, E.NFILE);
const o = &p.fs.opens[i];
+ // Past `opens_len` a replica's record may hold what a rollback did not
+ // put back (rollback.zig saves records as far as it says).
+ if (o.node != 0) o.deinit(p.gpa);
o.* = .{ .node = req.node, .what = what };
+ p.fs.opens_len = @max(p.fs.opens_len, @as(u8, @intCast(i + 1)));
if (resultsFile(target)) {
@memcpy(o.results.list[0..p.fs.results_len], p.fs.results[0..p.fs.results_len]);
o.results.len = p.fs.results_len;
@@ -1074,6 +1100,7 @@ fn releaseHandle(p: *Pardes, req: Req) void {
.snapshot, .log, .lines, .writes, .frozen, .pager, .plumb, .port => {},
};
o.deinit(p.gpa);
+ while (p.fs.opens_len > 0 and p.fs.opens[p.fs.opens_len - 1].node == 0) p.fs.opens_len -= 1;
}
fn setattr(p: *Pardes, req: Req, target: Target) Reply {