ifc_lite_processing/result_cache/report.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! What a [`ResultCache::process`](super::ResultCache::process) call did.
6//!
7//! The work counters are the CURRENT call's cost (hash, read, validate,
8//! decode, or the live pass and publication). A replayed result's
9//! `ProcessingStats` durations are the producer's, kept verbatim; they are not
10//! this call's work.
11
12use std::time::Duration;
13
14/// How the result was obtained.
15#[derive(Debug, Clone, PartialEq, Eq)]
16#[non_exhaustive]
17pub enum CacheOutcome {
18 /// The cache was off: the pipeline ran, nothing was hashed or read.
19 Off,
20 /// This request cannot be replayed (or the cache is unusable); the
21 /// pipeline ran and nothing was stored.
22 Bypass(BypassReason),
23 /// A stored result was replayed; the pipeline did not run.
24 Hit,
25 /// No usable entry; the pipeline ran (and, read-write, published).
26 Miss(MissReason),
27}
28
29#[derive(Debug, Clone, PartialEq, Eq)]
30#[non_exhaustive]
31pub enum BypassReason {
32 /// A cancellable pass may return a partial result.
33 CancellationArmed,
34 /// An injected entity index is not verified against the bytes.
35 InjectedEntityIndex,
36 /// `retain_meshes == false`: nothing worth replaying.
37 MeshesNotRetained,
38 /// The calling thread is inside an execution account, source stop or
39 /// terminal domain, whose allowance or cancellation can refuse work.
40 AmbientResourceScope,
41 /// The cache directory could not be opened or is not private.
42 Unavailable(String),
43}
44
45#[derive(Debug, Clone, PartialEq, Eq)]
46#[non_exhaustive]
47pub enum MissReason {
48 /// No entry for this key.
49 Absent,
50 /// An entry failed validation or decoding.
51 Corrupt(String),
52 /// An entry written in another format version.
53 Unsupported(u32),
54 /// An entry under this key describes a different source, request or engine.
55 IdentityMismatch,
56 /// An entry over the per-entry limit; not read.
57 Oversized { bytes: u64, limit: u64 },
58 /// The entry could not be opened or read (permissions, symlink, I/O).
59 ReadFailed(String),
60 /// An output-policy setter ran while the entry was being read.
61 PolicyChangedDuringLookup,
62}
63
64/// What happened to the live result afterwards.
65#[derive(Debug, Clone, PartialEq, Eq, Default)]
66#[non_exhaustive]
67pub enum PublishOutcome {
68 /// Read-only, off, bypassed or a hit.
69 #[default]
70 NotAttempted,
71 Published { bytes: u64 },
72 /// An identical entry was already stored.
73 AlreadyPresent,
74 SkippedOversized { bytes: u64, limit: u64 },
75 /// Another process holds this key's writer lock and is storing it.
76 SkippedConcurrentWriter,
77 /// Less than the write plus 1 GiB of headroom is free.
78 SkippedLowDiskSpace,
79 /// An output-policy setter ran while the pass ran (even one undone before
80 /// it ended), so the result may mix policies and is not this key's.
81 SkippedPolicyChanged,
82 /// A job of this pass ran under resource state it did not install (a
83 /// stopped terminal domain, another caller's account), or a product
84 /// somewhere in the process was stopped while it ran: the result may be
85 /// incomplete.
86 SkippedResourceStop,
87 /// The pass reported refused input through a process-wide channel a replay
88 /// would not repeat: a scan diagnostic through core's report sink
89 /// (refused records, malformed file) or a refused texture binary
90 /// (`ifc_lite_geometry::binary_textures_refused`).
91 SkippedRefusalReported,
92 /// The result breaks a structural invariant the decoder enforces.
93 Unencodable(String),
94 Failed(String),
95}
96
97/// The current call's work. Durations are wall time on this machine; byte
98/// counts are exact.
99#[derive(Debug, Clone, Default)]
100pub struct CacheWork {
101 /// Source bytes hashed for the key (0 when off or bypassed).
102 pub bytes_hashed: u64,
103 pub hash: Duration,
104 /// Entry bytes read from disk.
105 pub entry_bytes_read: u64,
106 pub read: Duration,
107 /// Header, directory and every section checksum.
108 pub validate: Duration,
109 /// Section payload bytes decoded into the result (hits only).
110 pub bytes_decoded: u64,
111 pub decode: Duration,
112 /// The live pipeline (misses, bypasses, off).
113 pub process: Duration,
114 pub encode: Duration,
115 pub write: Duration,
116 pub entry_bytes_written: u64,
117}
118
119#[derive(Debug, Clone)]
120pub struct CacheReport {
121 pub outcome: CacheOutcome,
122 pub publish: PublishOutcome,
123 pub work: CacheWork,
124 /// Hex cache key, once one was computed.
125 pub key: Option<String>,
126 /// Bounded warnings (also sent to `tracing`).
127 pub warnings: Vec<String>,
128}
129
130const MAX_WARNINGS: usize = 8;
131
132impl CacheReport {
133 pub(super) fn new(outcome: CacheOutcome) -> Self {
134 Self { outcome, publish: PublishOutcome::NotAttempted, work: CacheWork::default(), key: None, warnings: Vec::new() }
135 }
136
137 pub(super) fn warn(&mut self, message: String) {
138 tracing::warn!(target: "ifc_lite_processing::result_cache", "{message}");
139 if self.warnings.len() < MAX_WARNINGS {
140 self.warnings.push(message);
141 }
142 }
143
144 /// Short stable label for logs and probes.
145 pub fn outcome_label(&self) -> &'static str {
146 match self.outcome {
147 CacheOutcome::Off => "off",
148 CacheOutcome::Bypass(_) => "bypass",
149 CacheOutcome::Hit => "hit",
150 CacheOutcome::Miss(_) => "miss",
151 }
152 }
153}