Skip to main content

relate_executor/exec/
plan_grade.rs

1//! The plan-grade probe (ADR-0687 decision 6): one execution's measured store
2//! reads — the thread-local counters behind the PROFILE `Db Hits` metric, read
3//! once at the statement's end and never through the PROFILE sink. The
4//! `relate` crate arms it around a cache-served statement's dispatch and reads
5//! it after, so the cache entry's grade moves outside every cache lock.
6//!
7//! A statement entry drives its plan from exactly one root row (the one
8//! statement-entry operation mints it), so the per-execution read total is
9//! the whole measurement — no per-input-row normalisation is recorded
10//! anywhere, and a route that never reaches the executor reads a zero delta.
11
12use super::profiling::store_reads;
13
14/// One execution's plan-grade probe (ADR-0687 decision 6): the store-read
15/// counters armed and snapshotted at [`Self::begin`], the reading taken at
16/// [`Self::finish`]. Dropping an unfinished probe restores the counters' arm
17/// flag and nothing else — an execution that never finished grades nothing,
18/// and the counters it left armed never extend past the run.
19pub struct GradeProbe {
20    /// The counters' prior arm flag, `None` once restored (the drop after a
21    /// `finish` restores nothing a second time).
22    prior: Option<bool>,
23    /// The store-read total at `begin`: the reading is the delta across the
24    /// execution.
25    reads: u64,
26}
27
28impl GradeProbe {
29    /// Arm the store-read counters and snapshot them for one execution.
30    pub fn begin() -> Self {
31        Self {
32            prior: Some(super::arm_store_profiling()),
33            reads: store_reads().total(),
34        }
35    }
36
37    /// The run's reading — the store reads the execution performed — and the
38    /// arm flag's restore. Called once, after the execution returned.
39    pub fn finish(mut self) -> u64 {
40        let reads = store_reads()
41            .total()
42            .checked_sub(self.reads)
43            .expect("the store-read counters only ever increase");
44        self.restore();
45        reads
46    }
47
48    /// Restore the arm flag `begin` saved — idempotent, so `finish` and the
49    /// drop never restore twice.
50    fn restore(&mut self) {
51        if let Some(prior) = self.prior.take() {
52            super::restore_store_profiling(prior);
53        }
54    }
55}
56
57impl Drop for GradeProbe {
58    fn drop(&mut self) {
59        self.restore();
60    }
61}