1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Task-scoped compile-id plumbing for the `ZCCACHE_INNER_TRACE` sub-phase
//! trace (issue #940).
//!
//! `EmbeddedDaemon::compile` (the `ZccacheService::compile` entry) generates
//! a per-compile id and calls the pipeline through [`scope`]. Deep inside the
//! pipeline — at the hash/verify seam, the miss-path store, and the cached-hit
//! materialize — the timing seams call [`record_ns`] with a named sub-phase.
//! `record_ns` reads the current task's compile-id via the task-local and
//! forwards to [`crate::daemon_core::compile_trace::record`], which is itself a no-op unless
//! `ZCCACHE_INNER_TRACE` points at a writable file.
//!
//! ## Why a task-local instead of threading a `compile_id` argument
//!
//! The sub-phase seams live 3–4 calls deep (`handle_compile_ephemeral` →
//! `handle_compile` → `handle_compile_request` → `store_outcome`/`cached_hit`)
//! and are shared by both the embedded path and the IPC wrapper path. Threading
//! a `compile_id: &str` through every signature would churn ~12 test call sites
//! and both production callers for a diagnostic-only feature. A task-local
//! scopes the id to exactly the embedded compile future without touching any
//! signature: the seams read it when present and emit nothing otherwise, so the
//! IPC path (which does not open a scope) stays silent by construction.
//!
//! The foreground seams all run in the same task as the [`scope`] wrapper — the
//! only `tokio::spawn` in the compile path is the *background* artifact persist,
//! which is enqueued after `cache_store` timing is already recorded — so the
//! task-local is always in scope where [`record_ns`] is called.
task_local!
/// Record a sub-phase duration (in **nanoseconds**, converted to the trace's
/// microsecond unit) against the current embedded compile's trace id.
///
/// No-op when called outside a [`scope`] future (e.g. the IPC wrapper path) or
/// when `ZCCACHE_INNER_TRACE` is unset. Never blocks, allocates on the disabled
/// path, or perturbs the compile it measures.
pub
/// Run `fut` with `id` installed as the current embedded compile's trace id so
/// sub-phase [`record_ns`] calls emitted within it attribute to `id`.
pub async