diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 00:24:06 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | f2829440b0f933422e1da5394870505d3c2d67d6 (patch) | |
| tree | 1404a30697f3b64de5d54b52a74007143d40e4e3 | |
| parent | 20bfc56562068ff963281d562cba17a65880ec2d (diff) | |
| parent | 2ddceb90430806905988ae9bef4b3d921f75515e (diff) | |
| download | sketchbook-f2829440b0f933422e1da5394870505d3c2d67d6.tar.gz sketchbook-f2829440b0f933422e1da5394870505d3c2d67d6.zip | |
Merge the audio work and the incremental reload loop
The mixer's knee, newSound, sfxr, voice rate and groups, the scope's public pieces, micscope and spheres as examples; with the loader workaround for Zig 0.17.1's incremental linker and the panic-trace link. The README's hot-reload bullet takes the incremental loop's paragraph; zig build test: 164/164 steps, 132/132 tests.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
| -rw-r--r-- | README.md | 13 | ||||
| -rw-r--r-- | build.zig | 3 | ||||
| -rw-r--r-- | src/host/library.zig | 91 |
3 files changed, 97 insertions, 10 deletions
@@ -748,7 +748,18 @@ a talk with a slider, a loop, a choice, two slides and `beside`. - **Hot reload.** It watches the library file and loads each new build through a symlink with a fresh name (a build must replace the file, as `zig build` - does, not rewrite it in place). + does, not rewrite it in place). `zig build lib --watch -fincremental + --debounce 0` makes a reload take tens of milliseconds instead of a second: + Zig recompiles only what the edit changed, and starts as soon as a file is + saved instead of 50 ms later. Its limits, with Zig 0.17.1: the incremental + linker is new, and a library of it that does not link libc loads only + through a host workaround that Zig 0.18 makes unnecessary; if a reload ever runs code other than what you + wrote, stop the watch and rebuild without `-fincremental`. The library is + larger (27 MB instead of 10 for a small sketch), the watching compilers + stay in memory (about 300 MB for a small sketch, 1.3 GB for a game with C + and shaders), and a save that touches several files may build more than + once. The cache grows with each start of the watch (65 MB for a game), + not with edits. - **State file.** `<name>.state` holds the live State, shared via `MAP_SHARED`, so the file is up to date as it changes. Its header carries the reflected layout, so a layout change migrates it field by field, with @@ -212,6 +212,9 @@ pub fn build(b: *Build) void { // field name, through the real dlopen path and the headless host. const v1 = sb.app(.{ .name = "counter", .root_source_file = b.path("test/counter_v1.zig") }).addLibrary(); const v2 = sb.app(.{ .name = "counter", .root_source_file = b.path("test/counter_v2.zig") }).addLibrary(); + // v2 through the linker `-fincremental` uses, whose 0.17.1 libraries + // glibc loads only after the host's fixup (`withoutVersym`). + v2.use_new_linker = true; // Command.shape on the native host: the parity cases, screenshotted // and checked pixel by pixel (tools/shapes_web.mjs compares the same // sketch in headless Chrome: `zig build test-web`). diff --git a/src/host/library.zig b/src/host/library.zig index 91b2296..6619d5b 100644 --- a/src/host/library.zig +++ b/src/host/library.zig @@ -3,10 +3,11 @@ //! `dlopen` returns the handle it already has for a path it loaded before, so //! 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. +//! over it meanwhile. Link and fd live as long as the library is loaded: the +//! name dlopen was given is where a panic's trace reads the sketch's debug +//! info, so a host that dies leaves its dangling links behind. 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"); @@ -15,6 +16,11 @@ const abi = @import("abi"); pub const Library = struct { object: *c.SDL_SharedObject, sketch: abi.Sketch, + /// What the link names (the library, or its fixed copy) and where the + /// link is, for `openFresh`'s libraries; -1 for `open`'s. + fd: i32 = -1, + scratch_dir: []const u8 = "", + serial: usize = 0, /// Loads the library at `path`; `source` names it in messages. pub fn open(path: [:0]const u8, source: []const u8) !Library { @@ -47,21 +53,88 @@ pub const Library = struct { pub fn openFresh(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library { const cwd = std.Io.Dir.cwd(); const file = try cwd.openFile(io, source, .{}); - defer file.close(io); + const fixed = withoutVersym(file.handle) catch |err| { + file.close(io); + return err; + }; + if (fixed != null) file.close(io); + const fd = fixed orelse file.handle; + errdefer _ = linux.close(fd); var target_buf: [32]u8 = undefined; - const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{file.handle}); + const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{fd}); 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); + const link = try linkName(&buf, scratch_dir, serial); try cwd.symLink(io, target, link, .{}); - defer cwd.deleteFile(io, link) catch {}; - return open(link, source); + errdefer cwd.deleteFile(io, link) catch {}; + var library = try open(link, source); + library.fd = fd; + library.scratch_dir = scratch_dir; + library.serial = serial; + return library; } pub fn close(self: *Library) void { c.SDL_UnloadObject(self.object); + if (self.fd < 0) return; + var buf: [std.fs.max_path_bytes]u8 = undefined; + _ = linux.unlink(linkName(&buf, self.scratch_dir, self.serial) catch unreachable); + _ = linux.close(self.fd); + } + + fn linkName(buf: []u8, scratch_dir: []const u8, serial: usize) ![:0]u8 { + return std.mem.printSentinel(buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); } }; +// ponytail: Zig 0.17.1's new ELF linker (which `-fincremental` selects) +// writes DT_VERSYM without DT_VERDEF or DT_VERNEED when nothing is +// versioned, and glibc's dlopen then reads a null version table and +// faults. Such a library has nothing versioned, so a copy without +// DT_VERSYM, in an anonymous memory file, loads instead (the build's own +// file is never written: the watch would see the write). Never fires on a +// correct library; dead once Zig has commit 6d27011edb (0.18): delete it. +fn withoutVersym(fd: i32) !?i32 { + const elf = std.elf; + var ehdr: elf.Elf64.Ehdr = undefined; + try pread(fd, std.mem.asBytes(&ehdr), 0); + if (!std.mem.eql(u8, &ehdr.ident.magic, elf.MAGIC) or ehdr.ident.class != .@"64" or ehdr.phnum > 32) return null; + var phdrs: [32]elf.Elf64.Phdr = undefined; + try pread(fd, std.mem.sliceAsBytes(phdrs[0..ehdr.phnum]), ehdr.phoff); + for (phdrs[0..ehdr.phnum]) |ph| { + if (ph.type != .DYNAMIC) continue; + var dyn: [64]elf.Elf64_Dyn = undefined; + const len = @min(dyn.len, ph.filesz / @sizeOf(elf.Elf64_Dyn)); + try pread(fd, std.mem.sliceAsBytes(dyn[0..len]), ph.offset); + var versym: ?usize = null; + var end: usize = 0; + while (end < len and dyn[end].d_tag != elf.DT_NULL) : (end += 1) switch (dyn[end].d_tag) { + elf.DT_VERSYM => versym = end, + elf.DT_VERDEF, elf.DT_VERNEED => return null, + else => {}, + }; + const at = versym orelse return null; + if (end == len) return null; + const copy: i32 = @intCast(@as(isize, @bitCast(linux.memfd_create("sketchbook", linux.MFD.CLOEXEC)))); + if (copy < 0) return error.MemfdFailed; + errdefer _ = linux.close(copy); + var offset: i64 = 0; + while (true) { + const rc = linux.sendfile(copy, fd, &offset, 1 << 30); + if (linux.errno(rc) != .SUCCESS) return error.CopyFailed; + if (rc == 0) break; + } + std.mem.copyForwards(elf.Elf64_Dyn, dyn[at..end], dyn[at + 1 .. end + 1]); + const bytes = std.mem.sliceAsBytes(dyn[at..end]); + if (linux.pwrite(copy, bytes.ptr, bytes.len, @intCast(ph.offset + at * @sizeOf(elf.Elf64_Dyn))) != bytes.len) return error.WriteFailed; + return copy; + } + return null; +} + +fn pread(fd: i32, buf: []u8, offset: u64) !void { + if (linux.pread(fd, buf.ptr, buf.len, @intCast(offset)) != buf.len) return error.ShortRead; +} + /// inotify on a library's directory, filtered to its file name. Builds /// replace files by renaming (`MOVED_TO`) or rewrite them (`CLOSE_WRITE`). pub const Watch = struct { |
