Skip to content

Commit 5737ac4

Browse files
committed
fixup: share code between perf and analysis instrument
1 parent 364f982 commit 5737ac4

File tree

4 files changed

+66
-104
lines changed

4 files changed

+66
-104
lines changed

src/instruments/analysis.zig

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,6 @@
1-
const std = @import("std");
2-
const runner_fifo = @import("../runner_fifo.zig");
31
const shared = @import("../shared.zig");
2+
const helper = @import("helper.zig");
43

5-
pub const AnalysisInstrument = struct {
6-
fifo: runner_fifo.RunnerFifo,
4+
const AnalysisError = error{ModeError};
75

8-
const Self = @This();
9-
10-
pub fn init(allocator: std.mem.Allocator) !Self {
11-
var fifo = try runner_fifo.RunnerFifo.init(allocator);
12-
13-
// Get the instrumentation mode from the runner
14-
const mode = fifo.get_integration_mode() catch |err| {
15-
fifo.deinit();
16-
return err;
17-
};
18-
19-
// Only accept if the runner is in Analysis mode
20-
if (mode != .Analysis) {
21-
fifo.deinit();
22-
return error.NotAnalysisMode;
23-
}
24-
25-
return Self{ .fifo = fifo };
26-
}
27-
28-
pub fn deinit(self: *Self) void {
29-
self.fifo.deinit();
30-
}
31-
32-
pub fn send_version(self: *Self, protocol_version: u64) !void {
33-
try self.fifo.send_version(protocol_version);
34-
}
35-
36-
pub fn start_benchmark(self: *Self) !void {
37-
try self.fifo.start_benchmark();
38-
}
39-
40-
pub fn stop_benchmark(self: *Self) !void {
41-
try self.fifo.stop_benchmark();
42-
}
43-
44-
pub fn set_executed_benchmark(self: *Self, pid: u32, uri: [*c]const u8) !void {
45-
try self.fifo.set_executed_benchmark(pid, uri);
46-
}
47-
48-
pub fn set_integration(self: *Self, name: [*c]const u8, version: [*c]const u8) !void {
49-
try self.fifo.set_integration(name, version);
50-
}
51-
52-
pub fn add_marker(self: *Self, pid: u32, marker: shared.MarkerType) !void {
53-
try self.fifo.add_marker(pid, marker);
54-
}
55-
};
6+
pub const AnalysisInstrument = helper.FifoInstrument(.Analysis, AnalysisError);

src/instruments/helper.zig

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const std = @import("std");
2+
const runner_fifo = @import("../runner_fifo.zig");
3+
const shared = @import("../shared.zig");
4+
5+
/// Creates a complete FIFO-based instrument struct that validates a specific integration mode
6+
pub fn FifoInstrument(comptime mode: shared.IntegrationMode, comptime error_type: anytype) type {
7+
return struct {
8+
fifo: runner_fifo.RunnerFifo,
9+
10+
const Self = @This();
11+
12+
pub fn init(allocator: std.mem.Allocator) !Self {
13+
var fifo = try runner_fifo.RunnerFifo.init(allocator);
14+
15+
// Get the instrumentation mode from the runner
16+
const detected_mode = fifo.get_integration_mode() catch |err| {
17+
fifo.deinit();
18+
return err;
19+
};
20+
21+
// Only accept if the runner is in the correct mode
22+
if (detected_mode != mode) {
23+
fifo.deinit();
24+
return error_type.ModeError;
25+
}
26+
27+
return Self{ .fifo = fifo };
28+
}
29+
30+
pub fn deinit(self: *Self) void {
31+
self.fifo.deinit();
32+
}
33+
34+
pub fn send_version(self: *Self, protocol_version: u64) !void {
35+
try self.fifo.send_version(protocol_version);
36+
}
37+
38+
pub fn start_benchmark(self: *Self) !void {
39+
try self.fifo.start_benchmark();
40+
}
41+
42+
pub fn stop_benchmark(self: *Self) !void {
43+
try self.fifo.stop_benchmark();
44+
}
45+
46+
pub fn set_executed_benchmark(self: *Self, pid: u32, uri: [*c]const u8) !void {
47+
try self.fifo.set_executed_benchmark(pid, uri);
48+
}
49+
50+
pub fn set_integration(self: *Self, name: [*c]const u8, version: [*c]const u8) !void {
51+
try self.fifo.set_integration(name, version);
52+
}
53+
54+
pub fn add_marker(self: *Self, pid: u32, marker: shared.MarkerType) !void {
55+
try self.fifo.add_marker(pid, marker);
56+
}
57+
};
58+
}

src/instruments/perf.zig

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,6 @@
1-
const std = @import("std");
2-
const runner_fifo = @import("../runner_fifo.zig");
31
const shared = @import("../shared.zig");
2+
const helper = @import("helper.zig");
43

5-
pub const PerfInstrument = struct {
6-
fifo: runner_fifo.RunnerFifo,
4+
const PerfError = error{ModeError};
75

8-
const Self = @This();
9-
10-
pub fn init(allocator: std.mem.Allocator) !Self {
11-
var fifo = try runner_fifo.RunnerFifo.init(allocator);
12-
13-
// Get the instrumentation mode from the runner
14-
const mode = fifo.get_integration_mode() catch |err| {
15-
fifo.deinit();
16-
return err;
17-
};
18-
19-
// Only accept if the runner is in Perf mode
20-
if (mode != .Perf) {
21-
fifo.deinit();
22-
return error.NotPerfMode;
23-
}
24-
25-
return Self{ .fifo = fifo };
26-
}
27-
28-
pub fn deinit(self: *Self) void {
29-
self.fifo.deinit();
30-
}
31-
32-
pub fn send_version(self: *Self, protocol_version: u64) !void {
33-
try self.fifo.send_version(protocol_version);
34-
}
35-
36-
pub fn start_benchmark(self: *Self) !void {
37-
try self.fifo.start_benchmark();
38-
}
39-
40-
pub fn stop_benchmark(self: *Self) !void {
41-
try self.fifo.stop_benchmark();
42-
}
43-
44-
pub fn set_executed_benchmark(self: *Self, pid: u32, uri: [*c]const u8) !void {
45-
try self.fifo.set_executed_benchmark(pid, uri);
46-
}
47-
48-
pub fn set_integration(self: *Self, name: [*c]const u8, version: [*c]const u8) !void {
49-
try self.fifo.set_integration(name, version);
50-
}
51-
52-
pub fn add_marker(self: *Self, pid: u32, marker: shared.MarkerType) !void {
53-
try self.fifo.add_marker(pid, marker);
54-
}
55-
};
6+
pub const PerfInstrument = helper.FifoInstrument(.Perf, PerfError);

src/instruments/root.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const ValgrindInstrument = valgrind.ValgrindInstrument;
77
const PerfInstrument = perf.PerfInstrument;
88
const AnalysisInstrument = analysis.AnalysisInstrument;
99

10+
// TODO: Should we merge perf and analysis? -> Could just be a FifoInstrument
11+
1012
pub const InstrumentHooks = union(enum) {
1113
valgrind: ValgrindInstrument,
1214
perf: PerfInstrument,

0 commit comments

Comments
 (0)