|
| 1 | +const std = @import("std"); |
| 2 | +const fifo = @import("fifo.zig"); |
| 3 | +const shared = @import("shared.zig"); |
| 4 | + |
| 5 | +pub const PROTOCOL_VERSION: u64 = 1; |
| 6 | + |
| 7 | +// Note: Using printf to avoid the extra code from std.log/std.debug. Those won't |
| 8 | +// compile because they are internally using syscalls (for Mutexes) which aren't cross-platform. |
| 9 | +extern "c" fn printf(format: [*c]const c_char, ...) c_int; |
| 10 | + |
| 11 | +pub const RunnerFifo = struct { |
| 12 | + allocator: std.mem.Allocator, |
| 13 | + writer: fifo.UnixPipe.Writer, |
| 14 | + reader: fifo.UnixPipe.Reader, |
| 15 | + |
| 16 | + const Self = @This(); |
| 17 | + |
| 18 | + pub fn init(allocator: std.mem.Allocator) !Self { |
| 19 | + return .{ |
| 20 | + .allocator = allocator, |
| 21 | + .writer = try fifo.UnixPipe.openWrite(allocator, shared.RUNNER_CTL_FIFO), |
| 22 | + .reader = try fifo.UnixPipe.openRead(allocator, shared.RUNNER_ACK_FIFO), |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + pub fn validate_protocol_version(self: *Self) !void { |
| 27 | + self.send_version(PROTOCOL_VERSION) catch { |
| 28 | + _ = printf(@as([*c]const c_char, @ptrCast("[ERROR] instrument-hooks: failed to communicate with CodSpeed runner\n"))); |
| 29 | + _ = printf(@as([*c]const c_char, @ptrCast("[ERROR] instrument-hooks: please update the CodSpeed action to the latest version\n"))); |
| 30 | + std.posix.exit(1); |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + pub fn deinit(self: *Self) void { |
| 35 | + self.writer.deinit(); |
| 36 | + self.reader.deinit(); |
| 37 | + } |
| 38 | + |
| 39 | + pub fn send_cmd(self: *Self, cmd: fifo.Command) !void { |
| 40 | + try self.writer.sendCmd(cmd); |
| 41 | + try self.reader.waitForAck(null); |
| 42 | + } |
| 43 | + |
| 44 | + pub fn ping_perf(self: *Self) bool { |
| 45 | + self.send_cmd(fifo.Command.PingPerf) catch { |
| 46 | + return false; |
| 47 | + }; |
| 48 | + |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + pub noinline fn start_benchmark(self: *Self) !void { |
| 53 | + @branchHint(.cold); // Prevent inline |
| 54 | + |
| 55 | + try self.writer.sendCmd(fifo.Command.StartBenchmark); |
| 56 | + try self.reader.waitForAck(null); |
| 57 | + } |
| 58 | + |
| 59 | + pub noinline fn stop_benchmark(self: *Self) !void { |
| 60 | + @branchHint(.cold); // Prevent inline |
| 61 | + |
| 62 | + try self.writer.sendCmd(fifo.Command.StopBenchmark); |
| 63 | + try self.reader.waitForAck(null); |
| 64 | + } |
| 65 | + |
| 66 | + pub fn set_executed_benchmark(self: *Self, pid: u32, uri: [*c]const u8) !void { |
| 67 | + try self.writer.sendCmd(fifo.Command{ .ExecutedBenchmark = .{ |
| 68 | + .pid = pid, |
| 69 | + .uri = std.mem.span(uri), |
| 70 | + } }); |
| 71 | + try self.reader.waitForAck(null); |
| 72 | + } |
| 73 | + |
| 74 | + pub fn set_integration(self: *Self, name: [*c]const u8, version: [*c]const u8) !void { |
| 75 | + try self.writer.sendCmd(fifo.Command{ .SetIntegration = .{ |
| 76 | + .name = std.mem.span(name), |
| 77 | + .version = std.mem.span(version), |
| 78 | + } }); |
| 79 | + try self.reader.waitForAck(null); |
| 80 | + } |
| 81 | + |
| 82 | + pub fn add_marker(self: *Self, pid: u32, marker: shared.MarkerType) !void { |
| 83 | + try self.writer.sendCmd(fifo.Command{ .AddMarker = .{ |
| 84 | + .pid = pid, |
| 85 | + .marker = marker, |
| 86 | + } }); |
| 87 | + try self.reader.waitForAck(null); |
| 88 | + } |
| 89 | + |
| 90 | + pub fn send_version(self: *Self, protocol_version: u64) !void { |
| 91 | + try self.writer.sendCmd(fifo.Command{ .SetVersion = protocol_version }); |
| 92 | + try self.reader.waitForAck(null); |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +test "test runner fifo" { |
| 97 | + const allocator = std.testing.allocator; |
| 98 | + |
| 99 | + try fifo.UnixPipe.create(shared.RUNNER_ACK_FIFO); |
| 100 | + try fifo.UnixPipe.create(shared.RUNNER_CTL_FIFO); |
| 101 | + |
| 102 | + var ctl_fifo = try fifo.UnixPipe.openRead(allocator, shared.RUNNER_CTL_FIFO); |
| 103 | + defer ctl_fifo.deinit(); |
| 104 | + |
| 105 | + var ack_fifo = try fifo.UnixPipe.openWrite(allocator, shared.RUNNER_ACK_FIFO); |
| 106 | + defer ack_fifo.deinit(); |
| 107 | + |
| 108 | + const FifoTester = struct { |
| 109 | + allocator: std.mem.Allocator, |
| 110 | + ctl_pipe: *fifo.UnixPipe.Reader, |
| 111 | + ack_pipe: *fifo.UnixPipe.Writer, |
| 112 | + |
| 113 | + received_cmd: ?fifo.Command = null, |
| 114 | + error_occurred: bool = false, |
| 115 | + |
| 116 | + pub fn func(ctx: *@This()) void { |
| 117 | + const received_cmd = ctx.ctl_pipe.recvCmd() catch |err| { |
| 118 | + std.debug.print("Failed to receive command: {}\n", .{err}); |
| 119 | + ctx.error_occurred = true; |
| 120 | + return; |
| 121 | + }; |
| 122 | + ctx.received_cmd = received_cmd; |
| 123 | + |
| 124 | + ctx.ack_pipe.sendCmd(fifo.Command.Ack) catch |err| { |
| 125 | + std.debug.print("Failed to send ACK: {}\n", .{err}); |
| 126 | + ctx.error_occurred = true; |
| 127 | + }; |
| 128 | + } |
| 129 | + |
| 130 | + pub fn send(self: *@This(), comptime f: anytype, args: anytype) !fifo.Command { |
| 131 | + // 1. Create the thread which handles the command |
| 132 | + // 2. Execute the callback |
| 133 | + // 3. Wait for the thread to finish |
| 134 | + // |
| 135 | + const receiver_thread = try std.Thread.spawn(.{}, @This().func, .{self}); |
| 136 | + try @call(.auto, f, args); |
| 137 | + receiver_thread.join(); |
| 138 | + |
| 139 | + if (self.error_occurred) { |
| 140 | + return error.IntegrationError; |
| 141 | + } |
| 142 | + self.error_occurred = false; |
| 143 | + |
| 144 | + return self.received_cmd.?; |
| 145 | + } |
| 146 | + }; |
| 147 | + |
| 148 | + var tester = FifoTester{ |
| 149 | + .allocator = allocator, |
| 150 | + .ctl_pipe = &ctl_fifo, |
| 151 | + .ack_pipe = &ack_fifo, |
| 152 | + }; |
| 153 | + |
| 154 | + var runner_fifo = try RunnerFifo.init(allocator); |
| 155 | + defer runner_fifo.deinit(); |
| 156 | + |
| 157 | + const si_result = try tester.send(RunnerFifo.set_integration, .{ &runner_fifo, "zig", "0.10.0" }); |
| 158 | + try std.testing.expect(si_result.equal(fifo.Command{ .SetIntegration = .{ .name = "zig", .version = "0.10.0" } })); |
| 159 | + si_result.deinit(allocator); |
| 160 | + |
| 161 | + const cb_result = try tester.send(RunnerFifo.set_executed_benchmark, .{ &runner_fifo, 42, "foo" }); |
| 162 | + try std.testing.expect(cb_result.equal(fifo.Command{ .ExecutedBenchmark = .{ .pid = 42, .uri = "foo" } })); |
| 163 | + cb_result.deinit(allocator); |
| 164 | + |
| 165 | + const start_result = try tester.send(RunnerFifo.start_benchmark, .{&runner_fifo}); |
| 166 | + try std.testing.expect(start_result.equal(fifo.Command.StartBenchmark)); |
| 167 | + start_result.deinit(allocator); |
| 168 | + |
| 169 | + const stop_result = try tester.send(RunnerFifo.stop_benchmark, .{&runner_fifo}); |
| 170 | + try std.testing.expect(stop_result.equal(fifo.Command.StopBenchmark)); |
| 171 | + stop_result.deinit(allocator); |
| 172 | +} |
0 commit comments