summaryrefslogtreecommitdiff
path: root/src/host/memory.zig
blob: dd9f6320c68de6817182cc850f9fe7315f3fa7fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! 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,
//! a probe can read it from another process, and a reload with a changed
//! layout can carry the old values over field by field.
const std = @import("std");
const posix = std.posix;
const linux = std.os.linux;
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;
pub const file_magic = "SKETCH01".*;

pub const Header = extern struct {
    magic: [8]u8 = file_magic,
    size: u64,
    alignment: u64,
    hash: u64,
    /// Distance between slots, page aligned.
    stride: u64,
    /// Bit `i`: slot `i` holds state.
    used: u32,
    field_count: u32,
    strings_len: u32,
    reserved: u32 = 0,
};

/// A leaf as stored in the file: strings are offsets into the string table.
pub const StoredField = extern struct {
    offset: u32,
    size: u32,
    hash: u64,
    path: u32,
    path_len: u16,
    tags_len: u16,
    tags: u32,
    kind: abi.Field.Kind,
    reserved: [3]u8 = @splat(0),
};

/// A layout the host owns: copied out of a sketch, or read from a file.
pub const Layout = struct {
    size: usize = 0,
    alignment: usize = 1,
    hash: u64 = 0,
    fields: []const Leaf = &.{},

    pub const Leaf = struct {
        path: []const u8,
        offset: u32,
        size: u32,
        kind: abi.Field.Kind,
        hash: u64,
        tags: []const u8,

        pub fn bytes(leaf: Leaf, memory: []const u8) []const u8 {
            return memory[leaf.offset..][0..leaf.size];
        }
    };

    pub fn copy(arena: std.mem.Allocator, l: abi.Layout) !Layout {
        const fields = try arena.alloc(Leaf, l.fields.len);
        for (fields, l.fields) |*to, from| to.* = .{
            .path = try arena.dupe(u8, std.mem.span(from.path)),
            .offset = from.offset,
            .size = from.size,
            .kind = from.kind,
            .hash = from.hash,
            .tags = try arena.dupe(u8, std.mem.span(from.tags)),
        };
        return .{ .size = l.size, .alignment = l.alignment, .hash = l.hash, .fields = fields };
    }

    pub fn find(self: Layout, path: []const u8) ?Leaf {
        for (self.fields) |leaf| if (std.mem.eql(u8, leaf.path, path)) return leaf;
        return null;
    }
};

pub const Migration = struct { kept: usize = 0, reset: usize = 0 };

/// Copies every leaf of `from` whose path and type are unchanged into `to`,
/// which already holds the new defaults.
pub fn migrate(from: Layout, old: []const u8, to: Layout, new: []u8) Migration {
    var result: Migration = .{};
    for (to.fields) |leaf| {
        const before = from.find(leaf.path) orelse {
            result.reset += 1;
            continue;
        };
        if (before.hash == leaf.hash and before.size == leaf.size) {
            @memcpy(new[leaf.offset..][0..leaf.size], before.bytes(old));
            result.kept += 1;
        } else if (before.kind == .@"enum" and leaf.kind == .@"enum" and remapEnum(before, old, leaf, new)) {
            result.kept += 1;
        } else result.reset += 1;
    }
    return result;
}

/// Carries an enum over by tag name; false when its name no longer exists.
fn remapEnum(from: Layout.Leaf, old: []const u8, to: Layout.Leaf, new: []u8) bool {
    const name = field_value.tagName(from.tags, field_value.readInt(i64, from.bytes(old))) orelse return false;
    const value = field_value.tagValue(to.tags, name) orelse return false;
    field_value.writeInt(new[to.offset..][0..to.size], value);
    return true;
}

pub const StateFile = struct {
    fd: posix.fd_t,
    map: []align(page) u8,
    /// Layout of the bytes in the file, owned by `arena`.
    layout: Layout,
    arena: std.heap.ArenaAllocator,

    /// Opens or creates `path`. A missing or foreign file has no layout and no
    /// used slots: the first `adopt` fills it.
    pub fn open(gpa: std.mem.Allocator, path: []const u8) !StateFile {
        const fd = try posix.openat(posix.AT.FDCWD, path, .{ .ACCMODE = .RDWR, .CREAT = true }, 0o600);
        errdefer _ = posix.system.close(fd);
        var self: StateFile = .{ .fd = fd, .map = &.{}, .layout = .{}, .arena = .init(gpa) };
        errdefer self.arena.deinit();
        const len = try fileSize(fd);
        if (len >= header_len) {
            self.map = try mapFile(fd, @intCast(len), .{ .READ = true, .WRITE = true });
            if (self.valid()) {
                self.layout = try readLayout(self.arena.allocator(), self.map) orelse .{};
                return self;
            }
            posix.munmap(self.map);
            self.map = &.{};
        }
        try self.resize(0);
        self.header().* = .{ .size = 0, .alignment = 1, .hash = 0, .stride = 0, .used = 0, .field_count = 0, .strings_len = 0 };
        return self;
    }

    pub fn close(self: *StateFile) void {
        if (self.map.len != 0) posix.munmap(self.map);
        _ = posix.system.close(self.fd);
        self.arena.deinit();
    }

    pub fn header(self: *StateFile) *Header {
        return @ptrCast(self.map.ptr);
    }

    pub fn slot(self: *StateFile, index: usize) []align(page) u8 {
        std.debug.assert(index < slot_count);
        const h = self.header();
        const start = header_len + index * h.stride;
        return @alignCast(self.map[start..][0..h.size]);
    }

    pub fn used(self: *StateFile, index: usize) bool {
        return self.header().used & (@as(u32, 1) << @intCast(index)) != 0;
    }

    pub fn markUsed(self: *StateFile, index: usize, value: bool) void {
        const bit = @as(u32, 1) << @intCast(index);
        if (value) self.header().used |= bit else self.header().used &= ~bit;
    }

    pub const Adoption = union(enum) {
        unchanged,
        created,
        migrated: Migration,
    };

    /// Makes the file hold `new`'s layout. Used slots are migrated: `defaults`
    /// writes the new default values, then unchanged fields are copied over.
    pub fn adopt(self: *StateFile, gpa: std.mem.Allocator, new: abi.Layout, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !Adoption {
        if (self.layout.hash == new.hash and self.layout.size == new.size and self.header().used & 1 != 0) return .unchanged;
        const had_state = self.header().used & 1 != 0;

        // Keep the old bytes and layout while the file is rebuilt.
        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| {
            if (self.used(i)) copy.* = try old_arena.allocator().dupe(u8, self.slot(i));
        }
        const old_used = self.header().used;
        // `old_layout` points into `self.arena`: move it aside before resetting.
        var kept = self.arena;
        defer kept.deinit();
        self.arena = .init(gpa);

        self.layout = try Layout.copy(self.arena.allocator(), new);
        const stride = std.mem.alignForward(usize, @max(new.size, 1), page);
        try self.resize(stride);
        const h = self.header();
        h.* = .{ .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = stride, .used = old_used, .field_count = 0, .strings_len = 0 };
        try self.writeLayout();

        var total: Migration = .{};
        for (old_slots, 0..) |maybe_old, i| {
            const old = maybe_old orelse continue;
            const bytes = self.slot(i);
            @memset(bytes, 0);
            defaults(.state, bytes.ptr);
            const m = migrate(old_layout, old, self.layout, bytes);
            total.kept += m.kept;
            if (i == 0) total.reset = m.reset;
        }
        if (!had_state) {
            self.markUsed(0, false);
            return .created;
        }
        return .{ .migrated = total };
    }

    fn valid(self: *StateFile) bool {
        const h = self.header();
        if (!std.mem.eql(u8, &h.magic, &file_magic)) return false;
        return self.map.len >= header_len + slot_count * h.stride and h.size <= h.stride;
    }

    fn resize(self: *StateFile, stride: usize) !void {
        const len = header_len + slot_count * stride;
        if (self.map.len != 0) posix.munmap(self.map);
        self.map = &.{};
        switch (posix.errno(posix.system.ftruncate(self.fd, @intCast(len)))) {
            .SUCCESS => {},
            else => |err| return posix.unexpectedErrno(err),
        }
        self.map = try mapFile(self.fd, len, .{ .READ = true, .WRITE = true });
    }

    fn writeLayout(self: *StateFile) !void {
        const h = self.header();
        const fields: [*]StoredField = @ptrCast(@alignCast(self.map.ptr + @sizeOf(Header)));
        const strings_start = @sizeOf(Header) + self.layout.fields.len * @sizeOf(StoredField);
        var strings: usize = 0;
        for (self.layout.fields, 0..) |leaf, i| {
            if (strings_start + strings + leaf.path.len + leaf.tags.len > header_len) return error.LayoutTooLarge;
            const base = strings_start + strings;
            @memcpy(self.map[base..][0..leaf.path.len], leaf.path);
            @memcpy(self.map[base + leaf.path.len ..][0..leaf.tags.len], leaf.tags);
            fields[i] = .{
                .offset = leaf.offset,
                .size = leaf.size,
                .hash = leaf.hash,
                .kind = leaf.kind,
                .path = @intCast(strings),
                .path_len = @intCast(leaf.path.len),
                .tags = @intCast(strings + leaf.path.len),
                .tags_len = @intCast(leaf.tags.len),
            };
            strings += leaf.path.len + leaf.tags.len;
        }
        h.field_count = @intCast(self.layout.fields.len);
        h.strings_len = @intCast(strings);
    }
};

/// The layout stored in a state file's header, or null if it has none.
pub fn readLayout(arena: std.mem.Allocator, map: []const u8) !?Layout {
    const h: *const Header = @ptrCast(@alignCast(map.ptr));
    if (h.hash == 0 and h.field_count == 0) return null;
    const stored: [*]const StoredField = @ptrCast(@alignCast(map.ptr + @sizeOf(Header)));
    const strings_start = @sizeOf(Header) + h.field_count * @sizeOf(StoredField);
    if (strings_start + h.strings_len > header_len) return error.CorruptHeader;
    const strings = map[strings_start..][0..h.strings_len];
    const leaves = try arena.alloc(Layout.Leaf, h.field_count);
    for (leaves, stored[0..h.field_count]) |*leaf, f| {
        if (f.path + f.path_len > strings.len or f.tags + f.tags_len > strings.len) return error.CorruptHeader;
        // Copied: the mapping moves when the file is resized.
        leaf.* = .{
            .path = try arena.dupe(u8, strings[f.path..][0..f.path_len]),
            .offset = f.offset,
            .size = f.size,
            .kind = f.kind,
            .hash = f.hash,
            .tags = try arena.dupe(u8, strings[f.tags..][0..f.tags_len]),
        };
    }
    return .{ .size = h.size, .alignment = h.alignment, .hash = h.hash, .fields = leaves };
}

pub fn mapFile(fd: posix.fd_t, len: usize, prot: posix.PROT) ![]align(page) u8 {
    return posix.mmap(null, len, prot, .{ .TYPE = .SHARED }, fd, 0);
}

pub fn fileSize(fd: posix.fd_t) !u64 {
    var stat: linux.Statx = undefined;
    switch (linux.errno(linux.statx(fd, "", linux.AT.EMPTY_PATH, .{ .SIZE = true }, &stat))) {
        .SUCCESS => {},
        else => |err| return posix.unexpectedErrno(err),
    }
    return stat.size;
}

/// Maps a whole file read-only; empty when missing or empty.
pub fn mapReadOnly(path: []const u8) []align(page) const u8 {
    const fd = posix.openat(posix.AT.FDCWD, path, .{ .ACCMODE = .RDONLY }, 0) catch return &.{};
    defer _ = posix.system.close(fd);
    const len = fileSize(fd) catch return &.{};
    if (len == 0) return &.{};
    return posix.mmap(null, @intCast(len), .{ .READ = true }, .{ .TYPE = .SHARED }, fd, 0) catch &.{};
}

/// Session memory: survives reloads while its layout hash is unchanged.
/// `heap` is released with it.
pub const Session = struct {
    bytes: []align(16) u8 = &.{},
    hash: u64 = 0,
    heap: std.heap.ArenaAllocator,

    pub fn init(gpa: std.mem.Allocator) Session {
        return .{ .heap = .init(gpa) };
    }

    pub fn deinit(self: *Session, gpa: std.mem.Allocator) void {
        gpa.free(self.bytes);
        self.heap.deinit();
    }

    /// True when the session was reset to `layout`'s defaults.
    pub fn adopt(self: *Session, gpa: std.mem.Allocator, layout: abi.Layout, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !bool {
        if (self.hash == layout.hash and self.bytes.len == @max(layout.size, 1)) return false;
        if (layout.alignment > 16) return error.SessionOveraligned;
        gpa.free(self.bytes);
        self.bytes = &.{};
        _ = self.heap.reset(.free_all);
        self.bytes = try gpa.alignedAlloc(u8, .@"16", @max(layout.size, 1));
        @memset(self.bytes, 0);
        defaults(.session, self.bytes.ptr);
        self.hash = layout.hash;
        return true;
    }
};

test "enums migrate by tag name" {
    const V1 = struct { mood: enum(u8) { calm, lost, angry } = .calm, gone: enum(u8) { a, b } = .b };
    const V2 = struct { mood: enum(u16) { angry = 7, calm, sleepy } = .sleepy, gone: enum(u8) { c } = .c };
    var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
    defer arena.deinit();
    const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .forbidden));
    const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .forbidden));
    for ([_]struct { @FieldType(V1, "mood"), @FieldType(V2, "mood") }{ .{ .calm, .calm }, .{ .angry, .angry }, .{ .lost, .sleepy } }) |case| {
        const old: V1 = .{ .mood = case[0] };
        var new: V2 = .{};
        _ = migrate(l1, std.mem.asBytes(&old), l2, std.mem.asBytes(&new));
        try std.testing.expectEqual(case[1], new.mood);
        try std.testing.expectEqual(.c, new.gone);
    }
}

test "migration keeps unchanged fields and defaults the rest" {
    const V1 = extern struct { a: u32 = 1, b: f32 = 2, gone: u8 = 3 };
    const V2 = extern struct { b: f32 = 20, a: u32 = 10, b2: [2]u8 = .{ 7, 7 } };
    var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
    defer arena.deinit();
    const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .forbidden));
    const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .forbidden));
    const old: V1 = .{ .a = 42, .b = 0.5 };
    var new: V2 = .{};
    const m = migrate(l1, std.mem.asBytes(&old), l2, std.mem.asBytes(&new));
    try std.testing.expectEqual(V2{ .a = 42, .b = 0.5 }, new);
    try std.testing.expectEqual(@as(usize, 2), m.kept);
    try std.testing.expectEqual(@as(usize, 1), m.reset);
}

test "state file migrates live state and snapshots, 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 {
        fn v1(_: abi.Memory, p: [*]u8) callconv(.c) void {
            @as(*V1, @ptrCast(@alignCast(p))).* = .{};
        }
        fn v2(_: abi.Memory, p: [*]u8) callconv(.c) void {
            @as(*V2, @ptrCast(@alignCast(p))).* = .{};
        }
    };
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var buf: [std.fs.max_path_bytes]u8 = undefined;
    const path = try std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/test.state", .{&tmp.sub_path});
    const gpa = std.testing.allocator;
    {
        var file = try StateFile.open(gpa, path);
        defer file.close();
        try std.testing.expectEqual(StateFile.Adoption.created, try file.adopt(gpa, abi.layout.of(V1, .forbidden), 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, .forbidden), 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));
}