diff options
Diffstat (limited to 'src/host/control.zig')
| -rw-r--r-- | src/host/control.zig | 94 |
1 files changed, 90 insertions, 4 deletions
diff --git a/src/host/control.zig b/src/host/control.zig index 58b41b3..8cc8296 100644 --- a/src/host/control.zig +++ b/src/host/control.zig @@ -3,6 +3,7 @@ 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 { @@ -11,7 +12,7 @@ const Backend = struct { }; 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" }; +const names = [_][]const u8{ "/", "ctl", "status", "log", "screen.png", "profile", "profile-trace", "timing-trace", "presentation-trace" }; const Connection = struct { fd: c_int = -1, @@ -139,14 +140,20 @@ pub const Control = struct { }, .getattr => reply.attr = self.attr(req.node), .open => { - if (req.node == 5 or req.node == 7) { + 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.* = if (req.node == 5) try self.allocator.dupe(u8, self.png) else try self.trace(host); + 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 &.{}; }; @@ -186,7 +193,7 @@ pub const Control = struct { } break :blk writer.buffered(); }, - 5, 7 => if (req.handle > 0 and req.handle <= conn.snapshots.len) conn.snapshots[req.handle - 1] orelse return error.InvalidHandle else return error.InvalidHandle, + 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; @@ -252,6 +259,25 @@ pub const Control = struct { 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 }; } @@ -366,3 +392,63 @@ test "control writes profiling ring retains latest samples and nearest rank perc 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); +} |
