summaryrefslogtreecommitdiff
path: root/src/trace/store.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 13:36:32 -0300
committerGabriel Schneider <[email protected]>2026-11-10 14:12:08 -0300
commit02aa51a6f54235cf18e627dbbfe21f21e502eb0b (patch)
treea53416bdf0e5bb0ce80da63176fe13393c502a2c /src/trace/store.zig
parent16895292becf82f493aaced2b10f889bad436156 (diff)
downloadcomputeranim-02aa51a6f54235cf18e627dbbfe21f21e502eb0b.tar.gz
computeranim-02aa51a6f54235cf18e627dbbfe21f21e502eb0b.zip
context: disassembly, telescopes and names in every stop; strings as one table
For a viewer to show what pwndbg's context shows, the debugger records it while the memory is there: each register and the top of the stack followed as pointers three deep (--telescope), every code address met named, eight instructions from the pc disassembled by capstone (built from source for x86, into the command only; pinned as in 03-codenomicom). zt.telescope reads a chain back with the kind of memory each link points into, and ztrace show prints the context, coloured on a terminal as pwndbg colours. The thirty thousand strings of a small run showed that a string map in each keyframe costs 700 bytes of tree a string: strings are now one table for the recording, appended a batch and read whole at open. fib(12) by instruction with everything on: 9 MB, 71 000 moments/s.
Diffstat (limited to 'src/trace/store.zig')
-rw-r--r--src/trace/store.zig65
1 files changed, 51 insertions, 14 deletions
diff --git a/src/trace/store.zig b/src/trace/store.zig
index c8c32e7..0b7e8fe 100644
--- a/src/trace/store.zig
+++ b/src/trace/store.zig
@@ -4,6 +4,7 @@
//! root = map {
//! moments list of bytes: a moment's events, encoded (`Codec`)
//! keys list of keyframe, one a batch of moments (4096)
+//! strings list of bytes: the strings defined in each batch, (id, length, text)*
//! }
//! keyframe = map {
//! moment the last moment it holds
@@ -15,9 +16,12 @@
//! heap sorted map, big-endian ptr -> Allocation: the live allocations
//! vars sorted map, big-endian addr -> Variable: the live named variables
//! maps sorted map, big-endian addr -> Mapping, path: the mapped regions
-//! strings map id -> text
//! }
//!
+//! Strings are names, not state: one table for the whole recording, read
+//! whole when it is opened. (As a map in each keyframe, the thirty thousand
+//! instructions and names of a small run cost twenty megabytes of tree.)
+//!
//! A keyframe shares what did not change with the one before, so its cost
//! is what changed since, once, however often it changed. The state at any
//! moment is the keyframe before it with the moments since replayed over
@@ -206,7 +210,8 @@ const State = struct {
const t: ?*Thread = if (ev.tid != 0) try s.thread(gpa, ev.tid) else null;
switch (ev.kind) {
.mem_write => return s.write(gpa, a[0], payload),
- .string => return s.strings.put(gpa, a[0], try s.arena.allocator().dupe(u8, payload)),
+ // A reader has every string from the recording's table already.
+ .string => return if (s.mem_dirty != null) s.strings.put(gpa, a[0], try s.arena.allocator().dupe(u8, payload)),
.alloc => try s.heap.put(gpa, a[0], .{ .ptr = a[0], .size = a[1], .pc = a[2], .born = s.count }),
// Freeing what was never seen allocated removes nothing.
.free => try s.heap.put(gpa, a[0], null),
@@ -536,8 +541,14 @@ pub const Recorder = struct {
}
}
if (s.strings.count() > 0) {
- const strings = try Map.init(try k.putCursor(key(.strings)));
- for (s.strings.keys(), s.strings.values()) |id, text| try strings.put(id, .{ .bytes = text });
+ var table: std.ArrayList(u8) = .empty;
+ defer table.deinit(r.gpa);
+ for (s.strings.keys(), s.strings.values()) |id, text| {
+ try table.appendSlice(r.gpa, std.mem.asBytes(&id));
+ try table.appendSlice(r.gpa, std.mem.asBytes(&@as(u32, @intCast(text.len))));
+ try table.appendSlice(r.gpa, text);
+ }
+ try (try List.init(try root.putCursor(key(.strings)))).append(.{ .bytes = table.items });
}
}
};
@@ -553,6 +564,9 @@ pub const Trace = struct {
len: u64,
/// The last moment of each keyframe.
key_moments: []u64,
+ /// Every string the recording defines, by id.
+ strings: std.AutoHashMapUnmanaged(u64, []const u8) = .empty,
+ strings_arena: std.heap.ArenaAllocator,
cache: *Cache,
const Map = ReadDB.HashMap(.read_only);
@@ -586,7 +600,24 @@ pub const Trace = struct {
errdefer gpa.destroy(db);
db.* = try ReadDB.init(io, .{ .file = file, .fsync = false });
- var t: Trace = .{ .gpa = gpa, .io = io, .file = file, .db = db, .len = 0, .key_moments = &.{}, .cache = undefined };
+ var t: Trace = .{ .gpa = gpa, .io = io, .file = file, .db = db, .len = 0, .key_moments = &.{}, .strings_arena = .init(gpa), .cache = undefined };
+ errdefer {
+ t.strings.deinit(gpa);
+ t.strings_arena.deinit();
+ }
+ if (try root0(db).getCursor(key(.strings))) |cursor| {
+ var it = try (try List.init(cursor)).iterator();
+ while (try it.next()) |blob| {
+ const table = try blob.readBytesAlloc(t.strings_arena.allocator(), null);
+ var pos: usize = 0;
+ while (pos < table.len) {
+ const id = std.mem.readInt(u64, table[pos..][0..8], .little);
+ const len = std.mem.readInt(u32, table[pos + 8 ..][0..4], .little);
+ try t.strings.put(gpa, id, table[pos + 12 ..][0..len]);
+ pos += 12 + len;
+ }
+ }
+ }
const root = try Map.init(db.rootCursor().readOnly());
if (try root.getCursor(key(.moments))) |cursor| t.len = try (try List.init(cursor)).count();
var key_moments: std.ArrayList(u64) = .empty;
@@ -603,7 +634,14 @@ pub const Trace = struct {
return t;
}
+ fn root0(db: *ReadDB) Map {
+ // An empty file has no root map yet: a map that holds nothing.
+ return Map.init(db.rootCursor().readOnly()) catch unreachable;
+ }
+
pub fn close(t: *Trace) void {
+ t.strings.deinit(t.gpa);
+ t.strings_arena.deinit();
for (&t.cache.slots) |*slot| {
slot.state.deinit(t.gpa);
slot.events.deinit(t.gpa);
@@ -869,16 +907,15 @@ pub const Moment = struct {
return found.mapping;
}
+ /// The string with this id, copied into `buf`: a file's name, a
+ /// function's, a type's description, an instruction (`event.codeId`).
+ /// Strings are the recording's, not a moment's: one defined later is
+ /// found too.
pub fn string(m: Moment, id: u64, buf: []u8) !?[]const u8 {
- const s = try m.state();
- if (s.strings.get(id)) |text| {
- const n = @min(buf.len, text.len);
- @memcpy(buf[0..n], text[0..n]);
- return buf[0..n];
- }
- const base = s.base orelse return null;
- const all = try Trace.Map.init((try base.getCursor(key(.strings))) orelse return null);
- return try ((try all.getCursor(id)) orelse return null).readBytes(buf);
+ const text = m.trace.strings.get(id) orelse return null;
+ const n = @min(buf.len, text.len);
+ @memcpy(buf[0..n], text[0..n]);
+ return buf[0..n];
}
/// The events that led from the moment before to this one. Free with