diff options
Diffstat (limited to 'src/host/memory.zig')
| -rw-r--r-- | src/host/memory.zig | 102 |
1 files changed, 76 insertions, 26 deletions
diff --git a/src/host/memory.zig b/src/host/memory.zig index 11ace5e..044fc3b 100644 --- a/src/host/memory.zig +++ b/src/host/memory.zig @@ -1,7 +1,7 @@ //! Sketch memory by lifetime. //! -//! `StateFile`: one `MAP_SHARED` file holding the live state and its snapshot -//! slots, plus a header describing the layout, so the state outlives restarts, +//! `StateFile`: one `MAP_SHARED` file holding the live state, plus a header +//! describing the layout, so the state outlives restarts, //! a probe can read it from another process, and a reload with a changed //! layout can carry the old values over field by field. //! @@ -17,13 +17,16 @@ const abi = @import("abi"); const field_value = @import("fields.zig"); pub const page = std.heap.page_size_min; -/// Slot 0 is live; 1..7 are snapshots. -pub const slot_count = 8; pub const header_len = 64 * 1024; -/// Layouts with element leaves (`list[].x`, `StoredField` with `dims`). -pub const file_magic = "SKETCH02".*; +/// One slot, the live state: snapshots are marks in the sessions database. +pub const file_magic = "SKETCH03".*; +/// Before sessions: slot 0 live and 1..7 snapshots (`old_slot_count`), which +/// the host imports as marks and then drops (`dropSnapshots`). +pub const file_magic_v2 = "SKETCH02".*; +pub const old_slot_count = 8; /// What hosts before element leaves wrote: arrays are whole leaves -/// (`StoredFieldV1`). Read, and rewritten as `file_magic` on adopt. +/// (`StoredFieldV1`), and eight slots. Read, and rewritten as +/// `file_magic_v2` on adopt. pub const file_magic_v1 = "SKETCH01".*; pub const Header = extern struct { @@ -31,7 +34,8 @@ pub const Header = extern struct { size: u64, alignment: u64, hash: u64, - /// Distance between slots, page aligned. + /// The slot's size, page aligned (and the distance between an old + /// file's slots). stride: u64, /// Bit `i`: slot `i` holds state. used: u32, @@ -260,9 +264,14 @@ pub const Diff = struct { /// Compares two copies of state leaf by leaf, so padding (a Debug /// `@Vector`'s spare lane, the gaps between fields) never differs. pub fn compare(layout: Layout, a: []const u8, b: []const u8) Diff { + return compareKept(layout, layout, a, b); +} + +/// `compare` over the leaves `migrate` from `from` keeps (`keeps`). +pub fn compareKept(from: Layout, layout: Layout, a: []const u8, b: []const u8) Diff { var diff: Diff = .{}; for (layout.fields) |leaf| { - if (leaf.whole()) continue; + if (leaf.whole() or (from.hash != layout.hash and !keeps(from, leaf))) continue; var it = leaf.elements(); while (it.next()) |offset| { if (std.mem.eql(u8, a[offset..][0..leaf.size], b[offset..][0..leaf.size])) continue; @@ -285,6 +294,8 @@ fn remapEnum(from: Layout.Leaf, old: []const u8, to: Layout.Leaf, new: []u8) boo pub const StateFile = struct { fd: posix.fd_t, map: []align(page) u8, + /// 1, or `old_slot_count` in a file from before sessions. + slots: usize = 1, /// Layout of the bytes in the file, owned by `arena`. layout: Layout, arena: std.heap.ArenaAllocator, @@ -329,7 +340,7 @@ pub const StateFile = struct { } pub fn slot(self: *StateFile, index: usize) []align(page) u8 { - std.debug.assert(index < slot_count); + std.debug.assert(index < self.slots); const h = self.header(); const start = header_len + index * h.stride; return @alignCast(self.map[start..][0..h.size]); @@ -361,7 +372,7 @@ pub const StateFile = struct { self.arena = .init(gpa); self.layout = try Layout.copy(self.arena.allocator(), new); try writeLayout(self.map[0..header_len], self.layout); - self.header().magic = file_magic; + self.header().magic = file_magic_v2; } return .unchanged; } @@ -371,8 +382,8 @@ pub const StateFile = struct { var old_arena: std.heap.ArenaAllocator = .init(gpa); defer old_arena.deinit(); const old_layout = self.layout; - var old_slots: [slot_count]?[]u8 = @splat(null); - for (&old_slots, 0..) |*copy, i| { + var old_slots: [old_slot_count]?[]u8 = @splat(null); + for (old_slots[0..self.slots], 0..) |*copy, i| { if (self.used(i)) copy.* = try old_arena.allocator().dupe(u8, self.slot(i)); } const old_used = self.header().used; @@ -385,7 +396,7 @@ pub const StateFile = struct { const stride = std.mem.alignForward(usize, @max(new.size, 1), page); try self.resize(stride); const h = self.header(); - h.* = .{ .magic = file_magic, .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = stride, .used = old_used, .field_count = 0, .strings_len = 0 }; + h.* = .{ .magic = if (self.slots == 1) file_magic else file_magic_v2, .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = stride, .used = old_used, .field_count = 0, .strings_len = 0 }; try writeLayout(self.map[0..header_len], self.layout); var total: Migration = .{}; @@ -405,14 +416,29 @@ pub const StateFile = struct { return .{ .migrated = total }; } + /// Makes a file from before sessions one of a single slot, its + /// snapshots (imported as marks by now) dropped. + pub fn dropSnapshots(self: *StateFile) !void { + if (self.slots == 1) return; + const stride = self.header().stride; + self.slots = 1; + try self.resize(stride); + self.header().magic = file_magic; + self.header().used &= 1; + } + fn check(self: *StateFile) !void { const h = self.header(); - if (!std.mem.eql(u8, &h.magic, &file_magic) and !std.mem.eql(u8, &h.magic, &file_magic_v1)) return error.NotAStateFile; - if (self.map.len < header_len + slot_count * h.stride or h.size > h.stride) return error.CorruptStateFile; + if (std.mem.eql(u8, &h.magic, &file_magic)) { + self.slots = 1; + } else if (std.mem.eql(u8, &h.magic, &file_magic_v2) or std.mem.eql(u8, &h.magic, &file_magic_v1)) { + self.slots = old_slot_count; + } else return error.NotAStateFile; + if (self.map.len < header_len + self.slots * h.stride or h.size > h.stride) return error.CorruptStateFile; } fn resize(self: *StateFile, stride: usize) !void { - const len = header_len + slot_count * stride; + const len = header_len + self.slots * stride; if (self.map.len != 0) posix.munmap(self.map); self.map = &.{}; switch (posix.errno(posix.system.ftruncate(self.fd, @intCast(len)))) { @@ -423,6 +449,36 @@ pub const StateFile = struct { } }; +/// Fills `live` (laid out as `to`) from `image`, state kept in another +/// layout, described by `layout_bytes` (a header as `StateFile.layoutBytes` +/// gives it): `defaults`, then every leaf `migrate` keeps. Returns the +/// image's layout, `arena`'s. +pub fn restore(arena: std.mem.Allocator, layout_bytes: []const u8, image: []const u8, to: Layout, live: []u8, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !Layout { + const from = try layoutOf(arena, layout_bytes); + if (image.len != from.size) return error.CorruptKeyframe; + @memset(live, 0); + defaults(.state, live.ptr); + _ = migrate(from, image, to, live); + return from; +} + +/// The layout in a header as `StateFile.layoutBytes` gives it, `arena`'s. +pub fn layoutOf(arena: std.mem.Allocator, layout_bytes: []const u8) !Layout { + if (layout_bytes.len < @sizeOf(Header) or layout_bytes.len > header_len) return error.CorruptKeyframe; + const region = try arena.alignedAlloc(u8, .of(Header), header_len); + @memset(region, 0); + @memcpy(region[0..layout_bytes.len], layout_bytes); + return try readLayout(arena, region) orelse .{}; +} + +/// Whether `migrate` from `from` keeps `leaf`'s value: compared on these +/// only, state restored across a layout change differs nowhere it was reset. +pub fn keeps(from: Layout, leaf: Layout.Leaf) bool { + const before = from.pattern(leaf.path) orelse return false; + if (before.dims.len != leaf.dims.len) return false; + return (before.hash == leaf.hash and before.size == leaf.size) or (before.kind == .@"enum" and leaf.kind == .@"enum"); +} + /// Writes `layout`'s leaves after the `Header` at the start of `region`, /// in the current format (`StoredField`); the caller sets the magic. fn writeLayout(region: []u8, layout: Layout) !void { @@ -865,7 +921,7 @@ test "migration keeps unchanged fields and defaults the rest" { try std.testing.expectEqual(@as(usize, 1), m.reset); } -test "state file migrates live state and snapshots, and a reopen reads the layout back" { +test "state file migrates live state, and a reopen reads the layout back" { const V1 = extern struct { score: u32 = 0, name: [4]u8 = "none".* }; const V2 = extern struct { level: u16 = 1, score: u32 = 0 }; const D = struct { @@ -887,19 +943,13 @@ test "state file migrates live state and snapshots, and a reopen reads the layou try std.testing.expectEqual(StateFile.Adoption.created, try file.adopt(gpa, abi.layout.of(V1, .state), D.v1)); file.markUsed(0, true); @as(*V1, @ptrCast(file.slot(0).ptr)).score = 99; - @memcpy(file.slot(3), file.slot(0)); - file.markUsed(3, true); } var file = try StateFile.open(gpa, path); defer file.close(); try std.testing.expectEqual(abi.layout.hash(V1), file.layout.hash); const adoption = try file.adopt(gpa, abi.layout.of(V2, .state), D.v2); try std.testing.expectEqual(@as(usize, 1), adoption.migrated.reset); - for ([_]usize{ 0, 3 }) |i| { - const s: *V2 = @ptrCast(file.slot(i).ptr); - try std.testing.expectEqual(V2{ .level = 1, .score = 99 }, s.*); - } - try std.testing.expect(!file.used(1)); + try std.testing.expectEqual(V2{ .level = 1, .score = 99 }, @as(*V2, @ptrCast(file.slot(0).ptr)).*); } test "a store keeps its root and what it points to across a reopen, and migrates by field" { @@ -1090,7 +1140,7 @@ test "a state file from before element leaves (drat-meout's) keeps every value" var file = try StateFile.open(gpa, path); defer file.close(); try std.testing.expectEqual(StateFile.Adoption.unchanged, try file.adopt(gpa, same, NoDefaults.f)); - try std.testing.expectEqualStrings(&file_magic, &file.header().magic); + try std.testing.expectEqualStrings(&file_magic_v2, &file.header().magic); } var file = try StateFile.open(gpa, path); defer file.close(); |
