use std::cell::Cell;
use web_time::Instant;
thread_local! {
pub(crate) static HOP_EXPANDS: Cell<u64> = const { Cell::new(0) };
pub(crate) static NEIGHBOR_WALKS: Cell<u64> = const { Cell::new(0) };
static STORE_PROFILING: Cell<bool> = const { Cell::new(cfg!(test)) };
}
pub(crate) fn arm_store_profiling() -> bool {
STORE_PROFILING.with(|c| c.replace(true))
}
pub(crate) fn restore_store_profiling(prior: bool) {
STORE_PROFILING.with(|c| c.set(prior));
}
#[cfg(test)]
pub(crate) fn disarm_store_profiling() {
STORE_PROFILING.with(|c| c.set(false));
}
pub(crate) fn count_hop_expands(count: u64) {
if STORE_PROFILING.with(Cell::get) {
HOP_EXPANDS.with(|c| c.set(c.get() + count));
}
}
pub(crate) fn count_neighbor_walks(count: u64) {
if STORE_PROFILING.with(Cell::get) {
NEIGHBOR_WALKS.with(|c| c.set(c.get() + count));
}
}
#[derive(Debug, Default)]
pub(crate) struct WalkTallies {
hops: Cell<u64>,
walks: Cell<u64>,
}
impl WalkTallies {
pub(crate) fn hop(&self) {
self.hops.set(self.hops.get() + 1);
}
pub(crate) fn expansion_hop(&self, idx: usize) {
if idx > 0 {
self.hop();
}
}
pub(crate) fn walk(&self) {
self.walks.set(self.walks.get() + 1);
}
fn flush(&self) {
count_hop_expands(self.hops.replace(0));
count_neighbor_walks(self.walks.replace(0));
}
}
impl Drop for WalkTallies {
fn drop(&mut self) {
self.flush();
}
}
pub(crate) fn store_reads() -> crate::metrics::StoreReads {
crate::metrics::StoreReads {
hops: HOP_EXPANDS.with(Cell::get),
neighbor_walks: NEIGHBOR_WALKS.with(Cell::get),
node_reads: crate::ctx::node_reads(),
}
}
thread_local! {
static ZONE_SKIPS: Cell<u64> = const { Cell::new(0) };
}
pub(crate) fn count_zone_skip() {
ZONE_SKIPS.with(|c| c.set(c.get() + 1));
}
pub(crate) fn zone_skips() -> u64 {
ZONE_SKIPS.with(Cell::get)
}
thread_local! {
static FRONTIER: Cell<crate::metrics::FrontierCounters> = const {
Cell::new(crate::metrics::FrontierCounters {
edge_visits: 0,
levels: 0,
endpoints: 0,
cells: 0,
rearrival_work: 0,
memo_hits: 0,
memo_misses: 0,
representative_proofs: 0,
search_proven: 0,
search_disproven: 0,
declined_exhausted: 0,
})
};
}
pub(crate) fn count_frontier(run: crate::metrics::FrontierCounters) {
FRONTIER.with(|c| {
let mut counters = c.get();
counters += run;
c.set(counters);
});
}
pub(crate) fn frontier_counters() -> crate::metrics::FrontierCounters {
FRONTIER.with(Cell::get)
}
thread_local! {
static MEMO: Cell<crate::metrics::MemoCounters> = const {
Cell::new(crate::metrics::MemoCounters {
probes: 0,
hits: 0,
inserts: 0,
refused: 0,
bytes: 0,
disabled_count: 0,
})
};
}
pub(crate) fn record_memos(read: impl FnOnce() -> crate::metrics::MemoCounters) {
if STORE_PROFILING.with(Cell::get) {
let run = read();
MEMO.with(|c| {
let mut total = c.get();
total += run;
c.set(total);
});
}
}
pub(crate) fn memo_counters() -> crate::metrics::MemoCounters {
MEMO.with(Cell::get)
}
thread_local! {
static WALK_START: Cell<Option<crate::metrics::WalkStart>> = const { Cell::new(None) };
}
pub(crate) fn record_walk_start(start: crate::metrics::WalkStart) {
WALK_START.with(|slot| {
if slot.get().is_none() {
slot.set(Some(start));
}
});
}
pub(crate) fn take_walk_start() -> Option<crate::metrics::WalkStart> {
WALK_START.with(|slot| slot.take())
}
fn put_walk_start(start: Option<crate::metrics::WalkStart>) {
WALK_START.with(|slot| slot.set(start));
}
pub(crate) struct WalkStartSwap(Option<crate::metrics::WalkStart>);
impl WalkStartSwap {
pub(crate) fn take() -> Self {
Self(take_walk_start())
}
}
impl Drop for WalkStartSwap {
fn drop(&mut self) {
put_walk_start(self.0.take());
}
}
#[cfg(test)]
pub(crate) use crate::ctx::take_node_reads;
#[cfg(test)]
pub(crate) fn take_hop_expands() -> u64 {
HOP_EXPANDS.with(|c| c.replace(0))
}
#[cfg(test)]
pub(crate) fn take_neighbor_walks() -> u64 {
NEIGHBOR_WALKS.with(|c| c.replace(0))
}
#[cfg(test)]
pub(crate) fn take_zone_skips() -> u64 {
ZONE_SKIPS.with(|c| c.replace(0))
}
#[cfg(test)]
pub(crate) fn take_frontier_counters() -> crate::metrics::FrontierCounters {
FRONTIER.with(|c| c.replace(crate::metrics::FrontierCounters::default()))
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct ZoneSkips(u64);
impl ZoneSkips {
pub(crate) fn now() -> Self {
Self(zone_skips())
}
pub(crate) fn since(self, start: Self) -> Self {
Self(
self.0
.checked_sub(start.0)
.expect("the counters only ever increase"),
)
}
pub(crate) fn get(self) -> u64 {
self.0
}
}
impl std::ops::AddAssign for ZoneSkips {
fn add_assign(&mut self, window: Self) {
self.0 = self
.0
.checked_add(window.0)
.expect("the counters only ever accumulate");
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct ExpressionBytes(u64);
impl ExpressionBytes {
pub(crate) fn now(graph: &crate::overlay::ExecGraph<'_>) -> Self {
Self(graph.expression_built_bytes())
}
pub(crate) fn since(self, start: Self) -> Self {
Self(self.0.saturating_sub(start.0))
}
pub(crate) fn get(self) -> u64 {
self.0
}
}
impl std::ops::AddAssign for ExpressionBytes {
fn add_assign(&mut self, window: Self) {
self.0 = self.0.saturating_add(window.0);
}
}
macro_rules! profile_family {
(
$(#[$doc:meta])*
$snapshot:ident / $delta:ident { $($field:ident : $ty:ty,)+ }
) => {
$(#[$doc])*
#[derive(Clone, Copy)]
pub(crate) struct $snapshot {
t0: Instant,
$($field: $ty,)+
}
impl $snapshot {
pub(crate) fn since(self, start: Self) -> $delta {
$delta {
time_ns: start.t0.elapsed().as_nanos(),
$($field: self.$field.since(start.$field),)+
}
}
}
#[derive(Clone, Copy, Default)]
pub(crate) struct $delta {
pub(super) time_ns: u128,
$(pub(super) $field: $ty,)+
}
impl $delta {
pub(crate) fn minus(self, downstream: Self) -> Self {
Self {
time_ns: self.time_ns.saturating_sub(downstream.time_ns),
$($field: self.$field.since(downstream.$field),)+
}
}
pub(crate) fn accumulate(&mut self, window: Self) {
self.time_ns += window.time_ns;
$(self.$field += window.$field;)+
}
}
};
}
profile_family! {
DeltaSnapshot / StageDelta {
reads: crate::metrics::StoreReads,
phases: crate::metrics::PhaseTimes,
zone_skips: ZoneSkips,
frontier: crate::metrics::FrontierCounters,
memo: crate::metrics::MemoCounters,
expression_built: ExpressionBytes,
}
}
impl DeltaSnapshot {
pub(crate) fn now(graph: &crate::overlay::ExecGraph<'_>) -> Self {
Self {
t0: Instant::now(),
reads: store_reads(),
phases: crate::ctx::phase_times(),
zone_skips: ZoneSkips::now(),
frontier: frontier_counters(),
memo: memo_counters(),
expression_built: ExpressionBytes::now(graph),
}
}
}