| 1 |
const instrument_hooks = @import("./instrument_hooks.zig"); |
| 2 |
const InstrumentHooks = instrument_hooks.InstrumentHooks; |
| 3 |
const builtin = @import("builtin"); |
| 4 |
const features = @import("./features.zig"); |
| 5 |
const shared = @import("./shared.zig"); |
| 6 |
const std = @import("std"); |
| 7 |
const utils = @import("./utils.zig"); |
| 8 |
|
| 9 |
pub const panic = if (builtin.is_test) std.debug.FullPanic(std.debug.defaultPanic) else std.debug.no_panic; |
| 10 |
const allocator = if (builtin.is_test) std.testing.allocator else std.heap.c_allocator; |
| 11 |
|
| 12 |
/// Cast a C char pointer to a u8 pointer for internal Zig usage. |
| 13 |
fn toU8(ptr: [*c]const c_char) [*c]const u8 { |
| 14 |
return @ptrCast(ptr); |
| 15 |
} |
| 16 |
|
| 17 |
pub export fn instrument_hooks_set_feature(feature: u64, enabled: bool) void { |
| 18 |
const feature_enum = @as(features.Feature, @enumFromInt(feature)); |
| 19 |
features.set_feature(feature_enum, enabled); |
| 20 |
} |
| 21 |
|
| 22 |
pub export fn instrument_hooks_init() ?*InstrumentHooks { |
| 23 |
const hooks = allocator.create(InstrumentHooks) catch { |
| 24 |
return null; |
| 25 |
}; |
| 26 |
|
| 27 |
hooks.* = InstrumentHooks.init(allocator) catch { |
| 28 |
allocator.destroy(hooks); |
| 29 |
return null; |
| 30 |
}; |
| 31 |
|
| 32 |
return hooks; |
| 33 |
} |
| 34 |
|
| 35 |
pub export fn instrument_hooks_deinit(hooks: ?*InstrumentHooks) void { |
| 36 |
if (hooks) |h| { |
| 37 |
h.deinit(); |
| 38 |
allocator.destroy(h); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
pub export fn instrument_hooks_is_instrumented(hooks: ?*InstrumentHooks) bool { |
| 43 |
if (hooks) |h| { |
| 44 |
return h.instrument.is_instrumented(); |
| 45 |
} |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
pub export fn instrument_hooks_start_benchmark(hooks: ?*InstrumentHooks) u8 { |
| 50 |
if (hooks) |h| { |
| 51 |
h.instrument.start_benchmark() catch { |
| 52 |
return 1; |
| 53 |
}; |
| 54 |
} |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
pub export fn instrument_hooks_stop_benchmark(hooks: ?*InstrumentHooks) u8 { |
| 59 |
if (hooks) |h| { |
| 60 |
h.instrument.stop_benchmark() catch { |
| 61 |
return 1; |
| 62 |
}; |
| 63 |
} |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
pub export fn instrument_hooks_set_executed_benchmark(hooks: ?*InstrumentHooks, pid: i32, uri: [*c]const c_char) u8 { |
| 68 |
if (hooks) |h| { |
| 69 |
h.instrument.set_executed_benchmark(pid, toU8(uri)) catch { |
| 70 |
return 1; |
| 71 |
}; |
| 72 |
} |
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
// Deprecated: use instrument_hooks_set_executed_benchmark instead |
| 77 |
pub export fn instrument_hooks_executed_benchmark(hooks: ?*InstrumentHooks, pid: i32, uri: [*c]const c_char) u8 { |
| 78 |
return instrument_hooks_set_executed_benchmark(hooks, pid, uri); |
| 79 |
} |
| 80 |
|
| 81 |
pub export fn instrument_hooks_set_integration(hooks: ?*InstrumentHooks, name: [*c]const c_char, version: [*c]const c_char) u8 { |
| 82 |
if (hooks) |h| { |
| 83 |
h.instrument.set_integration(toU8(name), toU8(version)) catch { |
| 84 |
return 1; |
| 85 |
}; |
| 86 |
} |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
pub const MARKER_TYPE_SAMPLE_START: u8 = 0; |
| 91 |
pub const MARKER_TYPE_SAMPLE_END: u8 = 1; |
| 92 |
pub const MARKER_TYPE_BENCHMARK_START: u8 = 2; |
| 93 |
pub const MARKER_TYPE_BENCHMARK_END: u8 = 3; |
| 94 |
|
| 95 |
pub export fn instrument_hooks_add_marker(hooks: ?*InstrumentHooks, pid: i32, marker_type: u8, timestamp: u64) u8 { |
| 96 |
if (hooks) |h| { |
| 97 |
const marker_enum = switch (marker_type) { |
| 98 |
MARKER_TYPE_SAMPLE_START => shared.MarkerType{ .SampleStart = timestamp }, |
| 99 |
MARKER_TYPE_SAMPLE_END => shared.MarkerType{ .SampleEnd = timestamp }, |
| 100 |
MARKER_TYPE_BENCHMARK_START => shared.MarkerType{ .BenchmarkStart = timestamp }, |
| 101 |
MARKER_TYPE_BENCHMARK_END => shared.MarkerType{ .BenchmarkEnd = timestamp }, |
| 102 |
else => return 2, // Invalid marker type |
| 103 |
}; |
| 104 |
h.instrument.add_marker(pid, marker_enum) catch { |
| 105 |
return 1; |
| 106 |
}; |
| 107 |
} |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
// macOS uses Mach absolute time with timebase scaling (mirrors scripts/stub.c). |
| 112 |
const MachTimebaseInfo = extern struct { numer: u32, denom: u32 }; |
| 113 |
extern "c" fn mach_absolute_time() u64; |
| 114 |
extern "c" fn mach_timebase_info(info: *MachTimebaseInfo) c_int; |
| 115 |
|
| 116 |
// Returns monotonic time since boot in nanoseconds. |
| 117 |
// |
| 118 |
// NOTE: Maximum representable timestamp is u64::MAX nanoseconds = 18,446,744,073,709,551,615 ns |
| 119 |
// which equals ~584.94 years from epoch. Since CLOCK_MONOTONIC measures time since boot, |
| 120 |
// a system would need to run for ~585 years continuously to overflow this value. |
| 121 |
pub export fn instrument_hooks_current_timestamp() u64 { |
| 122 |
return switch (builtin.os.tag) { |
| 123 |
.linux => blk: { |
| 124 |
const ts = std.posix.clock_gettime(std.posix.clockid_t.MONOTONIC) catch unreachable; |
| 125 |
const s = @as(u64, @intCast(ts.sec)) * std.time.ns_per_s; |
| 126 |
const nsec: u64 = @intCast(ts.nsec); |
| 127 |
break :blk s + nsec; |
| 128 |
}, |
| 129 |
.macos => blk: { |
| 130 |
const S = struct { |
| 131 |
var cached: MachTimebaseInfo = .{ .numer = 0, .denom = 0 }; |
| 132 |
var once = std.once(init); |
| 133 |
fn init() void { |
| 134 |
var info: MachTimebaseInfo = undefined; |
| 135 |
if (mach_timebase_info(&info) != 0 or info.denom == 0) { |
| 136 |
info = .{ .numer = 1, .denom = 1 }; |
| 137 |
} |
| 138 |
cached = info; |
| 139 |
} |
| 140 |
}; |
| 141 |
S.once.call(); |
| 142 |
break :blk mach_absolute_time() * S.cached.numer / S.cached.denom; |
| 143 |
}, |
| 144 |
.windows => 0, |
| 145 |
else => @compileError("unsupported OS for instrument_hooks_current_timestamp"), |
| 146 |
}; |
| 147 |
} |
| 148 |
|
| 149 |
pub export fn instrument_hooks_set_environment( |
| 150 |
hooks: ?*InstrumentHooks, |
| 151 |
section_name: [*c]const c_char, |
| 152 |
key: [*c]const c_char, |
| 153 |
value: [*c]const c_char, |
| 154 |
) u8 { |
| 155 |
if (section_name == null or key == null or value == null) return 1; |
| 156 |
if (hooks) |h| { |
| 157 |
h.environment.setIntegrationEnvironment(std.mem.span(toU8(section_name)), std.mem.span(toU8(key)), std.mem.span(toU8(value))) catch return 1; |
| 158 |
return 0; |
| 159 |
} |
| 160 |
return 1; |
| 161 |
} |
| 162 |
|
| 163 |
pub export fn instrument_hooks_set_environment_list( |
| 164 |
hooks: ?*InstrumentHooks, |
| 165 |
section_name: [*c]const c_char, |
| 166 |
key: [*c]const c_char, |
| 167 |
values: [*c]const [*c]const c_char, |
| 168 |
count: u32, |
| 169 |
) u8 { |
| 170 |
if (section_name == null or key == null or values == null) return 1; |
| 171 |
if (hooks) |h| { |
| 172 |
const slices = allocator.alloc([]const u8, count) catch return 1; |
| 173 |
defer allocator.free(slices); |
| 174 |
const u8_values: [*c]const [*c]const u8 = @ptrCast(values); |
| 175 |
for (0..count) |i| { |
| 176 |
if (u8_values[i] == null) return 1; |
| 177 |
slices[i] = std.mem.span(u8_values[i]); |
| 178 |
} |
| 179 |
h.environment.setIntegrationEnvironmentList(std.mem.span(toU8(section_name)), std.mem.span(toU8(key)), slices) catch return 1; |
| 180 |
return 0; |
| 181 |
} |
| 182 |
return 1; |
| 183 |
} |
| 184 |
|
| 185 |
pub export fn instrument_hooks_write_environment(hooks: ?*InstrumentHooks, pid: i32) u8 { |
| 186 |
if (hooks) |h| { |
| 187 |
return h.environment.writeEnvironment(pid); |
| 188 |
} |
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
test "no crash when not instrumented" { |
| 193 |
const instance = instrument_hooks_init(); |
| 194 |
defer instrument_hooks_deinit(instance); |
| 195 |
|
| 196 |
_ = instrument_hooks_is_instrumented(instance); |
| 197 |
_ = instrument_hooks_set_feature(0, true); |
| 198 |
try std.testing.expectEqual(0, instrument_hooks_start_benchmark(instance)); |
| 199 |
try std.testing.expectEqual(0, instrument_hooks_stop_benchmark(instance)); |
| 200 |
try std.testing.expectEqual(0, instrument_hooks_executed_benchmark(instance, 0, @ptrCast("test"))); |
| 201 |
try std.testing.expectEqual(0, instrument_hooks_set_integration(instance, @ptrCast("pytest-codspeed"), @ptrCast("1.0"))); |
| 202 |
} |