mod codec;
mod index;
pub(crate) mod log;
mod memory;
mod persist;
mod stats;
mod stored;
mod tier;
pub use codec::STORE_FORMAT;
pub use memory::{BYTES_PER_FLOP, Counters, DEFAULT_CACHE_BYTES, DEFAULT_MARK_EVERY};
pub(crate) use memory::{Facts, Known, Memory, Offered};
pub use persist::{Backend, DEFAULT_STORE_BYTES, INDEX_NAME, Persisted, Store};
pub(crate) use stats::Recording;
pub use stats::{CacheStats, Lookup, Outcome};
pub use stored::Stored;
pub(crate) use stored::node_key;
pub use sva_formula::Hash;
pub(crate) use tier::now;
pub use tier::{FETCH_READS, Nothing, Tier};
use std::collections::BTreeMap;
use std::sync::Arc;
use sva_samples::{Buffer, Frames, Label, MachineState};
#[derive(Clone, Debug, PartialEq)]
pub enum Payload {
Segments(Vec<Arc<Buffer>>),
Frames(Arc<Frames>),
Run(Arc<Run>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PayloadKind {
Segments,
Frames,
Run,
}
#[derive(Clone)]
pub struct Run {
pub samples: Arc<Buffer>,
pub marks: BTreeMap<i64, MachineState>,
pub parent: Option<Hash>,
}
impl Run {
pub fn end(&self) -> i64 {
self.samples.extent().end
}
pub fn bytes(&self) -> usize {
let marks: usize = self.marks.values().map(MachineState::bytes).sum();
self.samples.len() * self.samples.width * size_of::<f64>() + marks
}
}
impl PartialEq for Run {
fn eq(&self, other: &Run) -> bool {
self.samples == other.samples
}
}
impl std::fmt::Debug for Run {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Run")
.field("samples", &self.samples.extent())
.finish()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Expected {
Segments { rate: u32, width: usize },
Frames,
Run { rate: u32, width: usize },
}
#[derive(Clone, Debug, PartialEq)]
pub struct Entry {
pub payload: Payload,
pub label: Option<Label>,
}
impl Payload {
pub fn run(self) -> Option<Arc<Run>> {
match self {
Payload::Run(run) => Some(run),
_ => None,
}
}
pub fn bytes(&self) -> usize {
match self {
Payload::Segments(parts) => parts
.iter()
.map(|b| b.len() * b.width * size_of::<f64>())
.sum(),
Payload::Frames(f) => f.width * f.frames * f.bins * 2 * size_of::<f64>(),
Payload::Run(run) => run.bytes(),
}
}
pub fn answers(&self, expected: Expected) -> bool {
match (self, expected) {
(Payload::Segments(parts), Expected::Segments { rate, width }) => {
parts.iter().all(|b| b.rate == rate && b.width == width)
}
(Payload::Frames(_), Expected::Frames) => true,
(Payload::Run(run), Expected::Run { rate, width }) => {
run.samples.rate == rate && run.samples.width == width
}
_ => false,
}
}
}
pub fn frames_key(value: Hash, window: usize, hop: usize) -> Hash {
mixed(
value,
&[window as u64, hop as u64, 0x66_72_61_6d_65_73_00_01],
)
}
pub fn value_key(
identity: Hash,
step: (i128, i128),
rate: u32,
width: usize,
profile: &sva_samples::Profile,
) -> Hash {
let placed = [step.0 as u64, step.1 as u64, u64::from(rate), width as u64];
let tag = [0x76_61_6c_75_65_00_00_01];
mixed(identity, &[&placed[..], &profile.deciding(), &tag].concat())
}
const ADDRESS_ROTATE: u32 = 17;
pub(crate) fn mixed(seed: Hash, parts: &[u64]) -> Hash {
let mut lanes = sva_formula::Lanes::<ADDRESS_ROTATE>::from(seed);
for part in parts {
lanes.word(*part);
}
lanes.finish()
}
pub(crate) fn joined(parts: &mut Vec<Arc<Buffer>>, more: Vec<Arc<Buffer>>) {
for part in more {
parts.push(part);
}
parts.sort_by_key(|b| b.start);
let mut out: Vec<Arc<Buffer>> = Vec::with_capacity(parts.len());
for part in parts.drain(..) {
match out.last_mut() {
Some(last) if last.extent().end >= part.start => {
let from = (last.extent().end - part.start) as usize;
if from < part.len() {
let last = Arc::make_mut(last);
for (held, more) in last.planes.iter_mut().zip(&part.planes) {
held.extend_from_slice(&more[from.min(more.len())..]);
}
}
}
_ => out.push(part),
}
}
*parts = out;
}