Debian Codegraph @

debian/rust-vendor/codspeed/instrument-hooks/src/shared.zig

raw
1 const std = @import("std");
2
3 // WARNING: Has to be in sync with `runner`
4
5 pub const RUNNER_CTL_FIFO = "/tmp/runner.ctl.fifo";
6 pub const RUNNER_ACK_FIFO = "/tmp/runner.ack.fifo";
7
8 // The different markers that can be set in the perf.data.
9 //
10 // `SampleStart/End`: Marks the start and end of a sampling period. This is used to differentiate between benchmarks.
11 // `BenchmarkStart/End`: Marks the start and end of a benchmark. This is used to measure the duration of a benchmark, without the benchmark harness code.
12 pub const MarkerType = union(enum) {
13 SampleStart: u64,
14 SampleEnd: u64,
15 BenchmarkStart: u64,
16 BenchmarkEnd: u64,
17
18 pub fn format(
19 self: MarkerType,
20 comptime fmt: []const u8,
21 options: std.fmt.FormatOptions,
22 writer: anytype,
23 ) !void {
24 _ = fmt;
25 _ = options;
26 switch (self) {
27 .SampleStart => |ts| try writer.print("SampleStart({d})", .{ts}),
28 .SampleEnd => |ts| try writer.print("SampleEnd({d})", .{ts}),
29 .BenchmarkStart => |ts| try writer.print("BenchmarkStart({d})", .{ts}),
30 .BenchmarkEnd => |ts| try writer.print("BenchmarkEnd({d})", .{ts}),
31 }
32 }
33
34 pub fn equal(self: MarkerType, other: MarkerType) bool {
35 return std.meta.eql(self, other);
36 }
37 };
38
39 pub const IntegrationMode = enum {
40 // Walltime measurement under any profiler (perf, instruments, etc.).
41 Walltime,
42 Simulation,
43 Analysis,
44 };
45
46 pub const Command = union(enum) {
47 ExecutedBenchmark: struct {
48 pid: i32,
49 uri: []const u8,
50 },
51 StartBenchmark,
52 StopBenchmark,
53 Ack,
54 PingProfiler,
55 SetIntegration: struct {
56 name: []const u8,
57 version: []const u8,
58 },
59 Err,
60 AddMarker: struct {
61 pid: i32,
62 marker: MarkerType,
63 },
64 SetVersion: u64,
65 GetIntegrationMode,
66 IntegrationModeResponse: IntegrationMode,
67
68 pub fn deinit(self: Command, allocator: std.mem.Allocator) void {
69 switch (self) {
70 .SetIntegration => |data| {
71 allocator.free(data.name);
72 allocator.free(data.version);
73 },
74 .ExecutedBenchmark => |data| allocator.free(data.uri),
75 .SetVersion => {},
76 .GetIntegrationMode => {},
77 .IntegrationModeResponse => {},
78 else => {},
79 }
80 }
81
82 pub fn format(
83 self: Command,
84 comptime fmt: []const u8,
85 options: std.fmt.FormatOptions,
86 writer: anytype,
87 ) !void {
88 _ = fmt;
89 _ = options;
90 switch (self) {
91 .ExecutedBenchmark => |data| try writer.print("ExecutedBenchmark {{ pid: {d}, uri: {s} }}", .{ data.pid, data.uri }),
92 .StartBenchmark => try writer.writeAll("StartBenchmark"),
93 .StopBenchmark => try writer.writeAll("StopBenchmark"),
94 .Ack => try writer.writeAll("Ack"),
95 .PingProfiler => try writer.writeAll("PingProfiler"),
96 .SetIntegration => |data| try writer.print("SetIntegration {{ name: {s}, version: {s} }}", .{ data.name, data.version }),
97 .Err => try writer.writeAll("Err"),
98 .AddMarker => |data| try writer.print("AddMarker {{ pid: {d}, marker: {} }}", .{ data.pid, data.marker }),
99 .SetVersion => |data| try writer.print("SetVersion {{ protocol_version: {d} }}", .{data}),
100 .GetIntegrationMode => try writer.writeAll("GetIntegrationMode"),
101 .IntegrationModeResponse => |mode| try writer.print("IntegrationModeResponse {}", .{mode}),
102 }
103 }
104
105 pub fn equal(self: Command, other: Command) bool {
106 return switch (self) {
107 .ExecutedBenchmark => |self_data| switch (other) {
108 .ExecutedBenchmark => |other_data| self_data.pid == other_data.pid and
109 std.mem.eql(u8, self_data.uri, other_data.uri),
110 else => false,
111 },
112 .StartBenchmark => switch (other) {
113 .StartBenchmark => true,
114 else => false,
115 },
116 .StopBenchmark => switch (other) {
117 .StopBenchmark => true,
118 else => false,
119 },
120 .Ack => switch (other) {
121 .Ack => true,
122 else => false,
123 },
124 .PingProfiler => switch (other) {
125 .PingProfiler => true,
126 else => false,
127 },
128 .SetIntegration => |self_data| switch (other) {
129 .SetIntegration => |other_data| std.mem.eql(u8, self_data.name, other_data.name) and
130 std.mem.eql(u8, self_data.version, other_data.version),
131 else => false,
132 },
133 .Err => switch (other) {
134 .Err => true,
135 else => false,
136 },
137 .AddMarker => |self_data| switch (other) {
138 .AddMarker => |other_data| self_data.pid == other_data.pid and self_data.marker.equal(other_data.marker),
139 else => false,
140 },
141 .SetVersion => |self_data| switch (other) {
142 .SetVersion => |other_data| self_data == other_data,
143 else => false,
144 },
145 .GetIntegrationMode => switch (other) {
146 .GetIntegrationMode => true,
147 else => false,
148 },
149 .IntegrationModeResponse => |self_mode| switch (other) {
150 .IntegrationModeResponse => |other_mode| self_mode == other_mode,
151 else => false,
152 },
153 };
154 }
155 };