lib/accy/src/executable/phase.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const sys = @import("sys");
2
3 pub const FragmentPhase = enum {
4 run_contract_pipeline,
5 run_tensor_pipeline,
6 run_dispatch_pipeline,
7 run_memory_pipeline,
8 run_kernel_pipeline,
9 run_target_pipeline,
10 plan_create_backend_artifacts,
11 compile_fragment,
12 load_backend_artifacts,
13 import_launch_tuning,
14
15 pub fn name(self: FragmentPhase) []const u8 {
16 return switch (self) {
17 .run_contract_pipeline => "run_contract_pipeline",
18 .run_tensor_pipeline => "run_tensor_pipeline",
19 .run_dispatch_pipeline => "run_dispatch_pipeline",
20 .run_memory_pipeline => "run_memory_pipeline",
21 .run_kernel_pipeline => "run_kernel_pipeline",
22 .run_target_pipeline => "run_target_pipeline",
23 .plan_create_backend_artifacts => "plan_create_backend_artifacts",
24 .compile_fragment => "compile_fragment",
25 .load_backend_artifacts => "load_backend_artifacts",
26 .import_launch_tuning => "import_launch_tuning",
27 };
28 }
29 };
30
31 pub const FragmentPhaseObserver = *const fn (?*anyopaque, []const u8, u64) anyerror!void;
32
33 pub const FragmentInstrumentation = struct {
34 context: ?*anyopaque = null,
35 observe: ?FragmentPhaseObserver = null,
36
37 pub fn record(self: FragmentInstrumentation, phase_value: FragmentPhase, start_ns: i128) !void {
38 const observe = self.observe orelse return;
39 try observe(self.context, phase_value.name(), elapsedNs(start_ns));
40 }
41
42 pub fn recordElapsed(self: FragmentInstrumentation, phase_value: FragmentPhase, elapsed_ns: u64) !void {
43 const observe = self.observe orelse return;
44 try observe(self.context, phase_value.name(), elapsed_ns);
45 }
46 };
47
48 fn elapsedNs(start: i128) u64 {
49 const end = sys.time.nanoTimestamp();
50 const delta = if (end >= start) end - start else 0;
51 return @intCast(delta);
52 }