diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 23:04:05 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 8ce27cfdbab6d576db36403afbcef9659cb6dbdc (patch) | |
| tree | d9c05dac053b17761e0d139cf14a192d49b4a46d /src/host/library.zig | |
| parent | 1b59d3e8eca0037d5c9fcb412e7c63cbe508f4ac (diff) | |
| download | sketchbook-8ce27cfdbab6d576db36403afbcef9659cb6dbdc.tar.gz sketchbook-8ce27cfdbab6d576db36403afbcef9659cb6dbdc.zip | |
Load sketch libraries through a symlink and identify builds by stat
A load copied the whole library to $XDG_RUNTIME_DIR (a read/write loop
across filesystems) and then hashed all of it for the build id: 40 ms for
a 10 MB Debug library, 200 ms for drat-meout's 58 MB. dlopen only needs a
fresh name, so the host links one to an fd open on the library, which also
pins the file a build may rename over meanwhile. The build id recordings
carry is a hash of the file's inode, size and modification time: Zig's own
linker writes no build-id note in Debug.
Diffstat (limited to 'src/host/library.zig')
| -rw-r--r-- | src/host/library.zig | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/src/host/library.zig b/src/host/library.zig index f8bc4e8..91b2296 100644 --- a/src/host/library.zig +++ b/src/host/library.zig @@ -1,9 +1,12 @@ //! Loading sketch libraries, and noticing when a build replaces one. //! //! `dlopen` returns the handle it already has for a path it loaded before, so -//! every load goes through a private copy with a fresh name. The copy is -//! deleted right after loading: the mapping keeps the code alive, and nothing -//! is left behind if the host dies. +//! every load goes through a symlink with a fresh name to an fd open on the +//! library: what loads is the file that was opened, whatever a build renames +//! over it meanwhile. The link is deleted right after loading: the mapping +//! keeps the code alive, and nothing is left behind if the host dies. A +//! library rewritten in place instead (same inode) would not load again, and +//! would change the code under the running host. const std = @import("std"); const linux = std.os.linux; const c = @import("c"); @@ -40,14 +43,18 @@ pub const Library = struct { return .{ .object = object, .sketch = sketch }; } - /// Loads `source` through a uniquely named copy in `scratch_dir`. - pub fn openCopy(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library { - var buf: [std.fs.max_path_bytes]u8 = undefined; - const copy = try std.mem.printSentinel(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); + /// Loads `source` through a uniquely named symlink in `scratch_dir`. + pub fn openFresh(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library { const cwd = std.Io.Dir.cwd(); - try cwd.copyFile(source, cwd, copy, io, .{}); - defer cwd.deleteFile(io, copy) catch {}; - return open(copy, source); + const file = try cwd.openFile(io, source, .{}); + defer file.close(io); + var target_buf: [32]u8 = undefined; + const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{file.handle}); + var buf: [std.fs.max_path_bytes]u8 = undefined; + const link = try std.mem.printSentinel(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); + try cwd.symLink(io, target, link, .{}); + defer cwd.deleteFile(io, link) catch {}; + return open(link, source); } pub fn close(self: *Library) void { |
