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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//! The streamed MATCH chain's PROFILE sidecar (REL8-1904, beside
//! `profile_tick.rs`'s per-clause tick and the parked generator's
//! `StreamedStats`): the chain streams under PROFILE too — a held path
//! would materialize the clause's whole output, changing the memory shape
//! of the very statement PROFILE reports — and each consumed stage
//! observes itself instead: one window per batch flush and one over the
//! drain, the counter deltas accumulated into the stage's facts, and one
//! record per consumed clause when the run drains, with the immediate
//! downstream stage's totals subtracted out. `match_chain.rs` keeps the
//! stage traversal; this module owns the accounting vocabulary.
use std::cell::{Cell, OnceCell};
use relate_core::Result;
use crate::metrics::{BatchFacts, ClauseMetricsRecord, InTxBatches, PlanMetricsSite, WalkStart};
use crate::overlay::ExecGraph;
use super::match_clause::{LadderFacts, MatchOperator};
use super::pattern_policies::RowFold;
use super::{DeltaSnapshot, StageDelta};
/// One clause's record — the one spelling every record site shares
/// (`ProfileTick::record` for a clause arm, `StageStats::report` for a
/// consumed stage, the parked generator's record in `profile_tick.rs`):
/// the family delta's own figures, the per-site rows, memory, start and
/// time, and the ladder's facts projected once, here. The sites differ
/// only in those.
pub(crate) fn clause_record(
site: &PlanMetricsSite<'_>,
clause: usize,
own: StageDelta,
rows: u64,
memory_bytes: u64,
start: Option<WalkStart>,
ladder: Option<LadderFacts>,
) -> ClauseMetricsRecord {
let (batched, kernel, frontier_decline) = match ladder {
None => (false, None, None),
Some(LadderFacts {
operator,
frontier_decline,
}) => {
let (batched, kernel) = match operator {
MatchOperator::BatchChain => (true, None),
MatchOperator::Rung(rung) => (false, Some(rung)),
};
(batched, kernel, frontier_decline)
}
};
ClauseMetricsRecord {
stage: site.stage,
part: site.part,
clause,
rows,
memory_bytes,
// `own.time_ns` is the record's time: the per-site adjustments
// (the tick's excluded producer time) applied before it arrives.
time_ns: own.time_ns,
expression_built_bytes: own.expression_built.get(),
reads: own.reads,
phases: own.phases,
start,
kernel,
batch: BatchFacts {
ran: batched,
zone_skips: own.zone_skips.get(),
},
frontier: own.frontier,
frontier_decline,
memo: own.memo,
// REL8-2047: the `IN TRANSACTIONS` batch facts are arm-local, not
// counter-snapshot deltas — the shared record zeroed here, and the
// arm's parked facts stamp its accumulated cell onto the record at
// the one reporting site that runs for that arm.
intx: InTxBatches::default(),
}
}
/// One stage's PROFILE facts — present only when the run observes itself
/// (`Stage::stats`), so every method here is already on the observing
/// path: an unprofiled run has no sidecar, no clock, no counter read, no
/// walk-start swap, `StreamedStats::timed_step`'s rule by construction.
pub(crate) struct StageStats {
/// Rows the stage emitted to its consumer, counted at the emission
/// boundary through [`CountingFold`]: a fold consumer takes rows as
/// they are offered, so the transform's returned vector holds none of
/// them and cannot be the count.
rows_out: Cell<u64>,
/// The accumulated window totals.
totals: Cell<StageDelta>,
/// The stage's first operator-ladder facts (LABS-1117).
facts: OnceCell<LadderFacts>,
/// The stage's first walk start (LABS-369), captured through the
/// walk-start swap while the stage's own ladder runs.
start: OnceCell<WalkStart>,
}
impl StageStats {
pub(crate) fn new() -> Self {
Self {
rows_out: Cell::new(0),
totals: Cell::new(StageDelta::default()),
facts: OnceCell::new(),
start: OnceCell::new(),
}
}
/// The stage's first ladder facts, when a ladder ran.
pub(crate) fn note_facts(&self, facts: LadderFacts) {
let _ = self.facts.set(facts);
}
/// The stage's own walk start, taken while its ladder held the
/// first-wins slot.
pub(crate) fn note_start(&self, start: Option<WalkStart>) {
if let Some(start) = start {
let _ = self.start.set(start);
}
}
/// Note rows the stage emitted to its consumer — the one write path
/// for the emission-boundary count, both `CountingFold` arms through
/// it.
fn note_emitted(&self, rows: u64) {
self.rows_out.set(self.rows_out.get() + rows);
}
/// Run `work` inside one PROFILE window: the elapsed time and the
/// counter deltas land in the stage's totals, the walk-start slot is
/// swapped for the duration (the stage's own first walk records; the
/// producer's start is handed back on every path, `Drop`'s rule), and
/// the result passes through.
pub(crate) fn window<T>(
&self,
graph: &ExecGraph<'_>,
work: impl FnOnce() -> Result<T>,
) -> Result<T> {
let _swap = super::WalkStartSwap::take();
let start = DeltaSnapshot::now(graph);
let out = work();
let mut totals = self.totals.get();
totals.accumulate(DeltaSnapshot::now(graph).since(start));
self.totals.set(totals);
out
}
/// Report this stage's own facts as one consumed clause's record:
/// its emitted rows, its own transform's time, reads, phases and
/// expression bytes (the immediate downstream stage's totals, which
/// its windows contain, subtracted out), its first ladder facts and
/// walk start, and `memory_bytes` — the retained output a terminal
/// stage's collector holds until the barrier consumes it (every other
/// stage's in-flight batch is bounded, so it reports zero). Returns
/// the stage's totals for the upstream stage's subtraction.
pub(crate) fn report(
&self,
site: &PlanMetricsSite<'_>,
clause: usize,
downstream: StageDelta,
memory_bytes: u64,
) -> StageDelta {
let own = self.totals.get().minus(downstream);
site.sink.record_clause(clause_record(
site,
clause,
own,
self.rows_out.get(),
memory_bytes,
self.start.get().copied(),
self.facts.get().copied(),
));
self.totals.get()
}
}
/// A stage's consumer that counts the rows offered to it before handing
/// each on — the emission boundary a fold consumer hides (it takes rows
/// as they are offered, so the transform's returned vector is empty).
/// Only an observing stage wraps its consumer in one.
pub(crate) struct CountingFold<'a> {
inner: &'a dyn RowFold,
stats: &'a StageStats,
}
impl<'a> CountingFold<'a> {
pub(crate) fn new(inner: &'a dyn RowFold, stats: &'a StageStats) -> Self {
Self { inner, stats }
}
}
impl RowFold for CountingFold<'_> {
fn offer(&self, graph: &ExecGraph<'_>, row: crate::row::Row) -> Result<()> {
self.stats.note_emitted(1);
self.inner.offer(graph, row)
}
fn offer_container(&self, graph: &ExecGraph<'_>, rows: Vec<crate::row::Row>) -> Result<()> {
self.stats.note_emitted(rows.len() as u64);
self.inner.offer_container(graph, rows)
}
fn is_id_ordered_window(&self, var: &relate_names::SmallName) -> bool {
self.inner.is_id_ordered_window(var)
}
fn id_order_saturated(&self, var: &relate_names::SmallName) -> bool {
self.inner.id_order_saturated(var)
}
}