| 1 |
const std = @import("std"); |
| 2 |
const builtin = @import("builtin"); |
| 3 |
const walltime = @import("walltime.zig"); |
| 4 |
const analysis = @import("analysis.zig"); |
| 5 |
const valgrind = @import("valgrind.zig"); |
| 6 |
const shared = @import("../shared.zig"); |
| 7 |
const ValgrindInstrument = valgrind.ValgrindInstrument; |
| 8 |
const WalltimeInstrument = walltime.WalltimeInstrument; |
| 9 |
const AnalysisInstrument = analysis.AnalysisInstrument; |
| 10 |
|
| 11 |
pub const Instrument = union(enum) { |
| 12 |
valgrind: ValgrindInstrument, |
| 13 |
walltime: WalltimeInstrument, |
| 14 |
analysis: AnalysisInstrument, |
| 15 |
none: void, |
| 16 |
|
| 17 |
const Self = @This(); |
| 18 |
|
| 19 |
pub fn init(allocator: std.mem.Allocator) !Self { |
| 20 |
// Valgrind/Callgrind client requests only work on Linux. |
| 21 |
if (comptime builtin.os.tag == .linux) { |
| 22 |
if (ValgrindInstrument.init(allocator)) |valgrind_inst| { |
| 23 |
return Self{ .valgrind = valgrind_inst }; |
| 24 |
} else |_| {} |
| 25 |
} |
| 26 |
|
| 27 |
if (AnalysisInstrument.init(allocator)) |analysis_inst| { |
| 28 |
return Self{ .analysis = analysis_inst }; |
| 29 |
} else |_| {} |
| 30 |
|
| 31 |
if (WalltimeInstrument.init(allocator)) |walltime_inst| { |
| 32 |
return Self{ .walltime = walltime_inst }; |
| 33 |
} else |_| {} |
| 34 |
|
| 35 |
return Self{ .none = {} }; |
| 36 |
} |
| 37 |
|
| 38 |
pub inline fn deinit(self: *Self) void { |
| 39 |
switch (self.*) { |
| 40 |
.valgrind => {}, |
| 41 |
.walltime => self.walltime.deinit(), |
| 42 |
.analysis => self.analysis.deinit(), |
| 43 |
.none => {}, |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
pub inline fn is_instrumented(self: *Self) bool { |
| 48 |
return switch (self.*) { |
| 49 |
.valgrind => ValgrindInstrument.is_instrumented(), |
| 50 |
.walltime => true, |
| 51 |
.analysis => true, |
| 52 |
.none => false, |
| 53 |
}; |
| 54 |
} |
| 55 |
|
| 56 |
pub inline fn start_benchmark(self: *Self) !void { |
| 57 |
if (self.* == .walltime) { |
| 58 |
return self.walltime.start_benchmark(); |
| 59 |
} else if (self.* == .valgrind) { |
| 60 |
return ValgrindInstrument.start_benchmark(); |
| 61 |
} else if (self.* == .analysis) { |
| 62 |
return self.analysis.start_benchmark(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
pub inline fn stop_benchmark(self: *Self) !void { |
| 67 |
if (self.* == .valgrind) { |
| 68 |
return ValgrindInstrument.stop_benchmark(); |
| 69 |
} else if (self.* == .walltime) { |
| 70 |
return self.walltime.stop_benchmark(); |
| 71 |
} else if (self.* == .analysis) { |
| 72 |
return self.analysis.stop_benchmark(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
pub inline fn set_executed_benchmark(self: *Self, pid: i32, uri: [*c]const u8) !void { |
| 77 |
switch (self.*) { |
| 78 |
.valgrind => ValgrindInstrument.set_executed_benchmark(pid, uri), |
| 79 |
.walltime => try self.walltime.set_executed_benchmark(pid, uri), |
| 80 |
.analysis => try self.analysis.set_executed_benchmark(pid, uri), |
| 81 |
.none => {}, |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
pub inline fn set_integration(self: *Self, name: [*c]const u8, version: [*c]const u8) !void { |
| 86 |
switch (self.*) { |
| 87 |
.valgrind => try self.valgrind.set_integration(name, version), |
| 88 |
.walltime => try self.walltime.set_integration(name, version), |
| 89 |
.analysis => try self.analysis.set_integration(name, version), |
| 90 |
.none => {}, |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
pub inline fn add_marker(self: *Self, pid: i32, marker: shared.MarkerType) !void { |
| 95 |
if (self.* == .walltime) { |
| 96 |
return self.walltime.add_marker(pid, marker); |
| 97 |
} else if (self.* == .analysis) { |
| 98 |
return self.analysis.add_marker(pid, marker); |
| 99 |
} |
| 100 |
} |
| 101 |
}; |