| 1 |
const std = @import("std"); |
| 2 |
const valgrind = @import("../helpers/valgrind.zig"); |
| 3 |
const features = @import("../features.zig"); |
| 4 |
|
| 5 |
pub const ValgrindInstrument = struct { |
| 6 |
allocator: std.mem.Allocator, |
| 7 |
const Self = @This(); |
| 8 |
|
| 9 |
pub fn init(allocator: std.mem.Allocator) !Self { |
| 10 |
if (!ValgrindInstrument.is_instrumented()) { |
| 11 |
return error.NotInstrumented; |
| 12 |
} |
| 13 |
|
| 14 |
return Self{ |
| 15 |
.allocator = allocator, |
| 16 |
}; |
| 17 |
} |
| 18 |
|
| 19 |
pub inline fn is_instrumented() bool { |
| 20 |
return valgrind.running_on_valgrind() > 0; |
| 21 |
} |
| 22 |
|
| 23 |
pub inline fn set_integration(self: Self, name: [*c]const u8, version: [*c]const u8) !void { |
| 24 |
const metadata = try std.fmt.allocPrintZ( |
| 25 |
self.allocator, |
| 26 |
"Metadata: {s} {s}", |
| 27 |
.{ name, version }, |
| 28 |
); |
| 29 |
defer self.allocator.free(metadata); |
| 30 |
|
| 31 |
valgrind.callgrind_dump_stats_at(metadata.ptr); |
| 32 |
} |
| 33 |
|
| 34 |
pub inline fn start_benchmark() void { |
| 35 |
if (!features.is_feature_enabled(.disable_callgrind_markers)) { |
| 36 |
valgrind.callgrind_zero_stats(); |
| 37 |
valgrind.callgrind_start_instrumentation(); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
pub inline fn stop_benchmark() void { |
| 42 |
if (!features.is_feature_enabled(.disable_callgrind_markers)) { |
| 43 |
valgrind.callgrind_stop_instrumentation(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
pub inline fn set_executed_benchmark(pid: i32, uri: [*c]const u8) void { |
| 48 |
_ = pid; |
| 49 |
valgrind.callgrind_dump_stats_at(uri); |
| 50 |
} |
| 51 |
}; |