//! Local 9P controls, serviced on the frame thread: no SDL or state races. const std = @import("std"); const c9 = @import("cloud9"); const c = @import("c"); const Host = @import("Host.zig"); const Presentation = @import("Presentation.zig"); const fs = c9.fs; const transport = c9.transport; const Backend = struct { pub const Req = fs.Req; pub const Reply = fs.Reply; }; const Server = fs.Server(Backend, .{ .fid_capacity = 32, .slot_capacity = 4 }); const msize = 16384; const names = [_][]const u8{ "/", "ctl", "status", "log", "screen.png", "profile", "profile-trace", "timing-trace", "presentation-trace" }; const Connection = struct { fd: c_int = -1, server: Server = undefined, input: [msize]u8 = undefined, output: [msize * 2]u8 = undefined, incoming: [msize]u8 = undefined, used: usize = 0, snapshots: [32]?[]u8 = @splat(null), fn close(self: *Connection, a: std.mem.Allocator) void { if (self.fd < 0) return; transport.close(self.fd); for (&self.snapshots) |*snapshot| if (snapshot.*) |bytes| { a.free(bytes); snapshot.* = null; }; self.fd = -1; } }; pub const Control = struct { allocator: std.mem.Allocator, path: [:0]u8, listener: c_int, connections: [4]Connection = @splat(.{}), png: []u8 = &.{}, capture_pending: bool = false, generation: u64 = 0, capture_error: ?anyerror = null, pub fn init(a: std.mem.Allocator, path: []const u8) !*Control { const self = try a.create(Control); errdefer a.destroy(self); const z = try a.dupeSentinel(u8, path, 0); errdefer a.free(z); // Refuse an existing path; never unlink another process's socket. const fd = try transport.listenFd(.{ .unix = z }, 4); errdefer { transport.close(fd); _ = std.c.unlink(z); } if (std.c.chmod(z, 0o600) != 0) return error.SocketPermissions; self.* = .{ .allocator = a, .path = z, .listener = fd }; return self; } pub fn deinit(self: *Control) void { for (&self.connections) |*conn| conn.close(self.allocator); transport.close(self.listener); _ = std.c.unlink(self.path); self.allocator.free(self.path); self.allocator.free(self.png); self.allocator.destroy(self); } pub fn poll(self: *Control, host: *Host) void { if (transport.acceptFd(self.listener, false) catch null) |fd| { var accepted = false; for (&self.connections) |*conn| if (conn.fd < 0) { conn.fd = fd; conn.used = 0; conn.server = .init(.{ .in = &conn.input, .out = &conn.output, .root = 1 }); accepted = true; break; }; if (!accepted) transport.close(fd); } for (&self.connections) |*conn| { if (conn.fd < 0) continue; self.service(host, conn) catch conn.close(self.allocator); } } fn service(self: *Control, host: *Host, conn: *Connection) !void { // Bound work per frame, including clients which continually write. for (0..32) |_| { var progressed = false; if (conn.used != 0) { const consumed = conn.server.push(conn.incoming[0..conn.used]); std.mem.copyForwards(u8, &conn.incoming, conn.incoming[consumed..conn.used]); conn.used -= consumed; progressed = consumed != 0; } while (conn.server.next()) |req| self.answer(host, conn, req); if (conn.server.protocol.dead) return error.Closed; if (conn.server.output().len > 0) { if (try transport.write(conn.fd, conn.server.output())) |n| { if (n == 0) return error.Closed; conn.server.wrote(n); progressed = true; } } if (conn.used < conn.incoming.len) { if (try transport.read(conn.fd, conn.incoming[conn.used..])) |n| { if (n == 0) return error.Closed; conn.used += n; progressed = true; } } if (!progressed) break; } } fn answer(self: *Control, host: *Host, conn: *Connection, req: fs.Req) void { var reply: fs.Reply = .{ .tag = req.tag }; var buffer: [msize]u8 = undefined; const bytes = self.respond(host, conn, req, &reply, &buffer) catch |err| blk: { reply = .{ .tag = req.tag, .status = .err, .errno = fs.E.INVAL, .ename = @errorName(err) }; break :blk &.{}; }; conn.server.reply(&reply, bytes); } fn respond(self: *Control, host: *Host, conn: *Connection, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 { if (req.node < 1 or req.node > names.len) return error.UnknownFile; switch (req.op) { .lookup => { if (req.node != 1) return error.NotDirectory; for (names[1..], 2..) |name, node| if (std.mem.eql(u8, name, req.data)) { reply.attr = self.attr(node); return &.{}; }; return error.UnknownFile; }, .getattr => reply.attr = self.attr(req.node), .open => { if (req.node == 5 or req.node == 7 or req.node == 8 or req.node == 9) { if (req.node == 5) { if (self.capture_pending) return error.CapturePending; if (self.capture_error) |err| return err; if (self.png.len == 0) return error.CaptureScreenshotFirst; } for (&conn.snapshots, 1..) |*slot, handle| if (slot.* == null) { slot.* = switch (req.node) { 5 => try self.allocator.dupe(u8, self.png), 7 => try self.trace(host), 8 => try self.timingTrace(host), 9 => try self.presentationTrace(host), else => unreachable, }; reply.handle = @intCast(handle); return &.{}; }; return error.TooManySnapshots; } }, .release => if (req.opened and req.handle > 0 and req.handle <= conn.snapshots.len) { const slot = &conn.snapshots[req.handle - 1]; if (slot.*) |bytes| self.allocator.free(bytes); slot.* = null; }, .readdir => { if (req.node != 1) return error.NotDirectory; var used: usize = 0; for (names[1..], 0..) |name, index| { if (index < req.off) continue; std.mem.writeInt(u64, buffer[used..][0..8], index + 2, .little); buffer[used + 8] = 0; buffer[used + 9] = @intCast(name.len); @memcpy(buffer[used + 10 ..][0..name.len], name); used += 10 + name.len; } return buffer[0..used]; }, .read => { var writer: std.Io.Writer = .fixed(buffer); const bytes: []const u8 = switch (req.node) { 2 => "Write one console command per line. screenshot captures the next frame.\n", 3 => blk: { try writer.print("name {s}\nsize {d} {d}\nmode {t}\ngpu {s}\nscreenshot_pending {}\nscreenshot_generation {d}\nscreenshot_error {s}\n", .{ host.name, host.canvas[0], host.canvas[1], host.timeline.mode, if (host.gpu != null) "yes" else "no", self.capture_pending, self.generation, if (self.capture_error) |err| @errorName(err) else "none" }); break :blk writer.buffered(); }, 4 => blk: { for (host.console.lines) |line| { try writer.writeAll(line.bytes[0..line.len]); try writer.writeByte('\n'); } break :blk writer.buffered(); }, 5, 7, 8, 9 => if (req.handle > 0 and req.handle <= conn.snapshots.len) conn.snapshots[req.handle - 1] orelse return error.InvalidHandle else return error.InvalidHandle, 6 => blk: { const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; try writer.print("# CPU wall milliseconds including GPU submission and fence wait; not hardware timestamps\n# frames {d}\nlabel\tcount\tmean_ms\tworst_ms\tp95_ms\tp99_ms\n", .{gpu.frame - profile.start_frame}); var entries = profile.entries.iterator(); while (entries.next()) |entry| { const value = entry.value_ptr; const percentiles = value.percentiles(); const count: f64 = @floatFromInt(@max(value.count, 1)); try writer.print("{s}\t{d}\t{d:.6}\t{d:.6}\t{d:.6}\t{d:.6}\n", .{ entry.key_ptr.*, value.count, @as(f64, @floatFromInt(value.total)) / count / std.time.ns_per_ms, @as(f64, @floatFromInt(value.worst)) / std.time.ns_per_ms, @as(f64, @floatFromInt(percentiles[0])) / std.time.ns_per_ms, @as(f64, @floatFromInt(percentiles[1])) / std.time.ns_per_ms }); } break :blk writer.buffered(); }, else => return error.NotFile, }; return window(bytes, req.off, req.size); }, .write => { if (req.node != 2) return error.ReadOnly; if (req.data.len > 4096 or std.mem.indexOfScalar(u8, req.data, 0) != null) return error.InvalidCommand; var lines = std.mem.tokenizeScalar(u8, req.data, '\n'); while (lines.next()) |raw| { const line = std.mem.trim(u8, raw, " \t\r"); if (std.mem.eql(u8, line, "screenshot")) { self.capture_pending = true; self.capture_error = null; } else if (std.mem.eql(u8, line, "profile-reset")) { const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; for (profile.entries.values()) |*entry| entry.* = .{}; profile.start_frame = gpu.frame; } else try host.runLine(line, c.SDL_GetTicksNS()); } reply.written = @intCast(req.data.len); }, .setattr => { if (req.node != 2 or !req.truncate) return error.ReadOnly; reply.attr = self.attr(req.node); }, } return &.{}; } fn trace(self: *Control, host: *Host) ![]u8 { const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; const buffer = try self.allocator.alloc(u8, 256 << 10); defer self.allocator.free(buffer); var writer: std.Io.Writer = .fixed(buffer); try writer.writeAll("label,sample,ns\n"); var entries = profile.entries.iterator(); while (entries.next()) |entry| { const value = entry.value_ptr; for (0..value.retained()) |i| { try writer.writeByte('"'); for (entry.key_ptr.*) |ch| { if (ch == '"') try writer.writeByte('"'); try writer.writeByte(ch); } try writer.print("\",{d},{d}\n", .{ value.count - value.retained() + i + 1, value.sample(i) }); } } return self.allocator.dupe(u8, writer.buffered()); } fn timingTrace(self: *Control, host: *const Host) ![]u8 { var csv: std.ArrayList(u8) = .empty; errdefer csv.deinit(self.allocator); try csv.appendSlice(self.allocator, "index,frame_ns,work_ns,outside_work_ns\n"); for (0..host.timing.count) |i| { const sample = host.timing.sampleAt(i); try csv.print(self.allocator, "{d},{d},{d},{d}\n", .{ i, sample.frame, sample.work, sample.frame -| sample.work }); } return csv.toOwnedSlice(self.allocator); } fn presentationTrace(self: *Control, host: *const Host) ![]u8 { var csv: std.Io.Writer.Allocating = .init(self.allocator); defer csv.deinit(); try Presentation.csv(host.presentation, &csv.writer); if (csv.written().len > 2 << 20) return error.TraceTooLarge; return csv.toOwnedSlice(); } fn attr(self: *Control, node: u64) fs.Attr { return .{ .name = names[@intCast(node - 1)], .node = node, .dir = node == 1, .mode = if (node == 1) 0o700 else if (node == 2) 0o600 else 0o400, .size = if (node == 5) self.png.len else 0, .version = if (node == 5) @truncate(self.generation) else 0 }; } pub fn capture(self: *Control, surface: *c.SDL_Surface) void { self.capture_pending = false; self.save(surface) catch |err| { self.capture_error = err; }; } fn save(self: *Control, surface: *c.SDL_Surface) !void { const stream = c.SDL_IOFromDynamicMem() orelse return error.CaptureStream; defer _ = c.SDL_CloseIO(stream); if (!c.SDL_SavePNG_IO(surface, stream, false)) return error.EncodePng; const size = c.SDL_GetIOSize(stream); if (size <= 0) return error.EncodePng; const pointer = c.SDL_GetPointerProperty(c.SDL_GetIOProperties(stream), c.SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, null) orelse return error.EncodePng; const png = try self.allocator.dupe(u8, @as([*]const u8, @ptrCast(pointer))[0..@intCast(size)]); self.allocator.free(self.png); self.png = png; self.generation += 1; } }; fn window(bytes: []const u8, offset: u64, count: u32) []const u8 { if (offset >= bytes.len) return &.{}; const start: usize = @intCast(offset); return bytes[start..][0..@min(count, bytes.len - start)]; } test "9P reads respect offsets, counts and EOF" { try std.testing.expectEqualStrings("cd", window("abcdef", 2, 2)); try std.testing.expectEqualStrings("ef", window("abcdef", 4, 200)); try std.testing.expectEqualStrings("", window("abcdef", std.math.maxInt(u64), 10)); } test "control writes validate and screenshot opens pin an immutable snapshot" { const a = std.testing.allocator; var control: Control = .{ .allocator = a, .path = undefined, .listener = -1 }; var host: Host = undefined; // These backend operations do not access the host. var conn: Connection = .{}; var reply: fs.Reply = .{ .tag = 1 }; var buf: [256]u8 = undefined; _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .setattr, .node = 2, .truncate = true }, &reply, &buf); try std.testing.expectError(error.ReadOnly, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 3, .data = "screenshot" }, &reply, &buf)); try std.testing.expectError(error.InvalidCommand, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 2, .data = "screenshot\x00" }, &reply, &buf)); _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 2, .data = "screenshot\n" }, &reply, &buf); try std.testing.expect(control.capture_pending); try std.testing.expectError(error.CapturePending, control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 5 }, &reply, &buf)); control.capture_pending = false; control.png = try a.dupe(u8, "first image"); _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 5 }, &reply, &buf); const handle = reply.handle; a.free(control.png); control.png = try a.dupe(u8, "second image"); defer a.free(control.png); const bytes = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 5, .handle = handle, .size = 100 }, &reply, &buf); try std.testing.expectEqualStrings("first image", bytes); _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 5, .handle = handle, .opened = true }, &reply, &buf); try std.testing.expect(conn.snapshots[handle - 1] == null); } test "control writes reset profiling counters while retaining labels" { const a = std.testing.allocator; var control: Control = .{ .allocator = a, .path = undefined, .listener = -1 }; var host: Host = undefined; host.passes = null; var conn: Connection = .{}; var reply: fs.Reply = .{ .tag = 1 }; var buf: [512]u8 = undefined; const reset: fs.Req = .{ .tag = 1, .op = .write, .node = 2, .data = "profile-reset\n" }; try std.testing.expectError(error.ProfilingDisabled, control.respond(&host, &conn, reset, &reply, &buf)); host.passes = @as(@import("gpu"), undefined); const gpu = &host.passes.?; gpu.profile = .init(a); defer gpu.profile.?.deinit(); const profile = &gpu.profile.?; try profile.entries.put(profile.arena.allocator(), "scene", .{}); profile.entries.values()[0].add(400_000); profile.entries.values()[0].add(600_000); gpu.frame = 420; const read: fs.Req = .{ .tag = 1, .op = .read, .node = 6, .size = buf.len }; const before = try control.respond(&host, &conn, read, &reply, &buf); try std.testing.expect(std.mem.indexOf(u8, before, "scene\t2\t0.500000\t0.600000") != null); _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 7 }, &reply, &buf); const trace_handle = reply.handle; _ = try control.respond(&host, &conn, reset, &reply, &buf); const trace = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 7, .handle = trace_handle, .size = 100 }, &reply, &buf); try std.testing.expectEqualStrings("label,sample,ns\n\"scene\",1,400000\n\"scene\",2,600000\n", trace); _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 7, .handle = trace_handle, .opened = true }, &reply, &buf); try std.testing.expect(conn.snapshots[trace_handle - 1] == null); try std.testing.expectEqual(@as(usize, 1), profile.entries.count()); try std.testing.expectEqual(@as(u64, 0), profile.entries.values()[0].total); try std.testing.expectEqual(@as(u64, 420), profile.start_frame); const after = try control.respond(&host, &conn, read, &reply, &buf); try std.testing.expect(std.mem.indexOf(u8, after, "# frames 0") != null); try std.testing.expect(std.mem.indexOf(u8, after, "scene\t0\t0.000000\t0.000000") != null); } test "control writes profiling ring retains latest samples and nearest rank percentiles" { var entry: @import("gpu").Profile.Entry = .{}; for (1..4100) |ns| entry.add(ns); try std.testing.expectEqual(@as(u64, 4099), entry.count); try std.testing.expectEqual(@as(usize, 2048), entry.retained()); try std.testing.expectEqual(@as(u64, 2052), entry.sample(0)); try std.testing.expectEqual(@as(u64, 4099), entry.sample(2047)); try std.testing.expectEqual([2]u64{ 3997, 4079 }, entry.percentiles()); entry = .{}; try std.testing.expectEqual(@as(usize, 0), entry.retained()); try std.testing.expectEqual([2]u64{ 0, 0 }, entry.percentiles()); entry.add(20); try std.testing.expectEqual([2]u64{ 20, 20 }, entry.percentiles()); } test "control writes reject timing trace mutations and opens pin chronological samples" { const a = std.testing.allocator; var control: Control = .{ .allocator = a, .path = undefined, .listener = -1 }; var host: Host = undefined; host.timing = .{}; for (0..@import("Timing.zig").capacity + 2) |i| host.timing.add(.{ .frame = i + 1, .work = i }); var conn: Connection = .{}; var reply: fs.Reply = .{ .tag = 1 }; var buf: [256]u8 = undefined; _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .lookup, .node = 1, .data = "timing-trace" }, &reply, &buf); try std.testing.expectEqual(@as(u64, 8), reply.attr.node); try std.testing.expectEqual(@as(u16, 0o400), reply.attr.mode); try std.testing.expectError(error.ReadOnly, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 8, .data = "reset" }, &reply, &buf)); _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 8 }, &reply, &buf); const first = reply.handle; host.timing = .{}; host.timing.add(.{ .frame = 4, .work = 7 }); const original = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 8, .handle = first, .size = 100 }, &reply, &buf); try std.testing.expect(std.mem.startsWith(u8, original, "index,frame_ns,work_ns,outside_work_ns\n0,3,2,1\n1,4,3,1\n")); _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 8 }, &reply, &buf); const second = reply.handle; const latest = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 8, .handle = second, .size = 100 }, &reply, &buf); try std.testing.expectEqualStrings("index,frame_ns,work_ns,outside_work_ns\n0,4,7,0\n", latest); for ([_]u32{ first, second }) |handle| { _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 8, .handle = handle, .opened = true }, &reply, &buf); try std.testing.expect(conn.snapshots[handle - 1] == null); } } test "control writes reject presentation trace mutations and opens own independent snapshots" { const a = std.testing.allocator; var control: Control = .{ .allocator = a, .path = undefined, .listener = -1 }; var host: Host = undefined; host.presentation = null; var conn: Connection = .{}; var reply: fs.Reply = .{ .tag = 1 }; var buf: [512]u8 = undefined; _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .lookup, .node = 1, .data = "presentation-trace" }, &reply, &buf); try std.testing.expectEqual(@as(u64, 9), reply.attr.node); try std.testing.expectEqual(@as(u16, 0o400), reply.attr.mode); try std.testing.expectError(error.ReadOnly, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 9, .data = "reset" }, &reply, &buf)); _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 9 }, &reply, &buf); const first = reply.handle; _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 9 }, &reply, &buf); const second = reply.handle; try std.testing.expect(conn.snapshots[first - 1].?.ptr != conn.snapshots[second - 1].?.ptr); const original = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 9, .handle = first, .size = 4096 }, &reply, &buf); try std.testing.expect(std.mem.startsWith(u8, original, "# status ")); try std.testing.expect(std.mem.indexOf(u8, original, "disabled_or_allocation_failed") != null); try std.testing.expect(std.mem.endsWith(u8, original, "frame_id,sample_ms,commit_ns,result,presented_ns,refresh_ns,sequence,flags\n")); try std.testing.expect(std.mem.indexOfScalar(u8, original, 0) == null); const expected = try a.dupe(u8, original); defer a.free(expected); _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 9, .handle = first, .opened = true }, &reply, &buf); const retained = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 9, .handle = second, .size = 4096 }, &reply, &buf); try std.testing.expectEqualStrings(expected, retained); _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 9, .handle = second, .opened = true }, &reply, &buf); try std.testing.expect(conn.snapshots[first - 1] == null and conn.snapshots[second - 1] == null); }