| 1 |
const std = @import("std"); |
| 2 |
const fifo = @import("fifo/root.zig"); |
| 3 |
const shared = @import("shared.zig"); |
| 4 |
const logger = @import("logger.zig"); |
| 5 |
|
| 6 |
// v1: Initial release |
| 7 |
// v2: Added GetIntegrationMode |
| 8 |
pub const PROTOCOL_VERSION: u64 = 2; |
| 9 |
|
| 10 |
pub const RunnerFifo = struct { |
| 11 |
allocator: std.mem.Allocator, |
| 12 |
writer: fifo.Pipe.Writer, |
| 13 |
reader: fifo.Pipe.Reader, |
| 14 |
|
| 15 |
const Self = @This(); |
| 16 |
|
| 17 |
pub fn init(allocator: std.mem.Allocator) !Self { |
| 18 |
return .{ |
| 19 |
.allocator = allocator, |
| 20 |
.writer = try fifo.Pipe.openWrite(allocator, shared.RUNNER_CTL_FIFO), |
| 21 |
.reader = try fifo.Pipe.openRead(allocator, shared.RUNNER_ACK_FIFO), |
| 22 |
}; |
| 23 |
} |
| 24 |
|
| 25 |
pub fn validate_protocol_version(self: *Self) !void { |
| 26 |
self.send_version(PROTOCOL_VERSION) catch |err| { |
| 27 |
switch (err) { |
| 28 |
// No runner present - silently continue as NOP |
| 29 |
error.AckTimeout => return, |
| 30 |
|
| 31 |
// Runner explicitly rejected version |
| 32 |
error.UnexpectedError => { |
| 33 |
logger.err("instrument-hooks: CodSpeed runner rejected protocol version {}\n", .{PROTOCOL_VERSION}); |
| 34 |
logger.err("instrument-hooks: please update the CodSpeed action to the latest version\n", .{}); |
| 35 |
std.posix.exit(1); |
| 36 |
}, |
| 37 |
|
| 38 |
// All other errors - log and exit |
| 39 |
else => { |
| 40 |
logger.err("instrument-hooks: error {s} during version check\n", .{@errorName(err)}); |
| 41 |
std.posix.exit(1); |
| 42 |
}, |
| 43 |
} |
| 44 |
}; |
| 45 |
} |
| 46 |
|
| 47 |
pub fn deinit(self: *Self) void { |
| 48 |
self.writer.deinit(); |
| 49 |
self.reader.deinit(); |
| 50 |
} |
| 51 |
|
| 52 |
pub fn send_cmd(self: *Self, cmd: fifo.Command) !void { |
| 53 |
try self.writer.sendCmd(cmd); |
| 54 |
try self.reader.waitForAck(null); |
| 55 |
} |
| 56 |
|
| 57 |
pub fn ping_profiler(self: *Self) bool { |
| 58 |
self.send_cmd(fifo.Command.PingProfiler) catch { |
| 59 |
return false; |
| 60 |
}; |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
pub noinline fn start_benchmark(self: *Self) !void { |
| 66 |
@branchHint(.cold); // Prevent inline |
| 67 |
|
| 68 |
try self.writer.sendCmd(fifo.Command.StartBenchmark); |
| 69 |
try self.reader.waitForAck(null); |
| 70 |
} |
| 71 |
|
| 72 |
pub noinline fn stop_benchmark(self: *Self) !void { |
| 73 |
@branchHint(.cold); // Prevent inline |
| 74 |
|
| 75 |
try self.writer.sendCmd(fifo.Command.StopBenchmark); |
| 76 |
try self.reader.waitForAck(null); |
| 77 |
} |
| 78 |
|
| 79 |
pub fn set_executed_benchmark(self: *Self, pid: i32, uri: [*c]const u8) !void { |
| 80 |
try self.writer.sendCmd(fifo.Command{ .ExecutedBenchmark = .{ |
| 81 |
.pid = pid, |
| 82 |
.uri = std.mem.span(uri), |
| 83 |
} }); |
| 84 |
try self.reader.waitForAck(null); |
| 85 |
} |
| 86 |
|
| 87 |
pub fn set_integration(self: *Self, name: [*c]const u8, version: [*c]const u8) !void { |
| 88 |
try self.writer.sendCmd(fifo.Command{ .SetIntegration = .{ |
| 89 |
.name = std.mem.span(name), |
| 90 |
.version = std.mem.span(version), |
| 91 |
} }); |
| 92 |
try self.reader.waitForAck(null); |
| 93 |
} |
| 94 |
|
| 95 |
pub fn add_marker(self: *Self, pid: i32, marker: shared.MarkerType) !void { |
| 96 |
try self.writer.sendCmd(fifo.Command{ .AddMarker = .{ |
| 97 |
.pid = pid, |
| 98 |
.marker = marker, |
| 99 |
} }); |
| 100 |
try self.reader.waitForAck(null); |
| 101 |
} |
| 102 |
|
| 103 |
pub fn send_version(self: *Self, protocol_version: u64) !void { |
| 104 |
try self.writer.sendCmd(fifo.Command{ .SetVersion = protocol_version }); |
| 105 |
try self.reader.waitForAck(null); |
| 106 |
} |
| 107 |
|
| 108 |
pub fn get_integration_mode(self: *Self) !shared.IntegrationMode { |
| 109 |
// NOTE: Other messages send data to the runner, and expect an ACK response (see `sendCmd`). This |
| 110 |
// command expects the runner to respond with data, so have to write and read directly. |
| 111 |
try self.writer.sendCmd(fifo.Command.GetIntegrationMode); |
| 112 |
const response = try self.reader.waitForResponse(null); |
| 113 |
defer response.deinit(self.allocator); |
| 114 |
|
| 115 |
if (response == .IntegrationModeResponse) { |
| 116 |
return response.IntegrationModeResponse; |
| 117 |
} |
| 118 |
return error.UnexpectedResponse; |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
test "test runner fifo" { |
| 123 |
const allocator = std.testing.allocator; |
| 124 |
|
| 125 |
try fifo.Pipe.create(shared.RUNNER_ACK_FIFO); |
| 126 |
try fifo.Pipe.create(shared.RUNNER_CTL_FIFO); |
| 127 |
|
| 128 |
var ctl_fifo = try fifo.Pipe.openRead(allocator, shared.RUNNER_CTL_FIFO); |
| 129 |
defer ctl_fifo.deinit(); |
| 130 |
|
| 131 |
var ack_fifo = try fifo.Pipe.openWrite(allocator, shared.RUNNER_ACK_FIFO); |
| 132 |
defer ack_fifo.deinit(); |
| 133 |
|
| 134 |
const FifoTester = struct { |
| 135 |
allocator: std.mem.Allocator, |
| 136 |
ctl_pipe: *fifo.Pipe.Reader, |
| 137 |
ack_pipe: *fifo.Pipe.Writer, |
| 138 |
|
| 139 |
received_cmd: ?fifo.Command = null, |
| 140 |
error_occurred: bool = false, |
| 141 |
|
| 142 |
pub fn func(ctx: *@This()) void { |
| 143 |
const received_cmd = ctx.ctl_pipe.waitForResponse(null) catch |err| { |
| 144 |
std.debug.print("Failed to receive command: {}\n", .{err}); |
| 145 |
ctx.error_occurred = true; |
| 146 |
return; |
| 147 |
}; |
| 148 |
ctx.received_cmd = received_cmd; |
| 149 |
|
| 150 |
ctx.ack_pipe.sendCmd(fifo.Command.Ack) catch |err| { |
| 151 |
std.debug.print("Failed to send ACK: {}\n", .{err}); |
| 152 |
ctx.error_occurred = true; |
| 153 |
}; |
| 154 |
} |
| 155 |
|
| 156 |
pub fn send(self: *@This(), comptime f: anytype, args: anytype) !fifo.Command { |
| 157 |
// 1. Create the thread which handles the command |
| 158 |
// 2. Execute the callback |
| 159 |
// 3. Wait for the thread to finish |
| 160 |
// |
| 161 |
const receiver_thread = try std.Thread.spawn(.{}, @This().func, .{self}); |
| 162 |
try @call(.auto, f, args); |
| 163 |
receiver_thread.join(); |
| 164 |
|
| 165 |
if (self.error_occurred) { |
| 166 |
return error.IntegrationError; |
| 167 |
} |
| 168 |
self.error_occurred = false; |
| 169 |
|
| 170 |
return self.received_cmd.?; |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
var tester = FifoTester{ |
| 175 |
.allocator = allocator, |
| 176 |
.ctl_pipe = &ctl_fifo, |
| 177 |
.ack_pipe = &ack_fifo, |
| 178 |
}; |
| 179 |
|
| 180 |
var runner_fifo = try RunnerFifo.init(allocator); |
| 181 |
defer runner_fifo.deinit(); |
| 182 |
|
| 183 |
const si_result = try tester.send(RunnerFifo.set_integration, .{ &runner_fifo, "zig", "0.10.0" }); |
| 184 |
try std.testing.expect(si_result.equal(fifo.Command{ .SetIntegration = .{ .name = "zig", .version = "0.10.0" } })); |
| 185 |
si_result.deinit(allocator); |
| 186 |
|
| 187 |
const cb_result = try tester.send(RunnerFifo.set_executed_benchmark, .{ &runner_fifo, 42, "foo" }); |
| 188 |
try std.testing.expect(cb_result.equal(fifo.Command{ .ExecutedBenchmark = .{ .pid = 42, .uri = "foo" } })); |
| 189 |
cb_result.deinit(allocator); |
| 190 |
|
| 191 |
const start_result = try tester.send(RunnerFifo.start_benchmark, .{&runner_fifo}); |
| 192 |
try std.testing.expect(start_result.equal(fifo.Command.StartBenchmark)); |
| 193 |
start_result.deinit(allocator); |
| 194 |
|
| 195 |
const stop_result = try tester.send(RunnerFifo.stop_benchmark, .{&runner_fifo}); |
| 196 |
try std.testing.expect(stop_result.equal(fifo.Command.StopBenchmark)); |
| 197 |
stop_result.deinit(allocator); |
| 198 |
} |