pub mod ring;
use std::{
collections::BTreeMap,
fs,
hash::{DefaultHasher, Hash, Hasher},
path::PathBuf,
sync::{
Arc,
atomic::{AtomicBool, AtomicU64, Ordering},
},
thread::{self, JoinHandle},
time::{Duration, Instant},
};
use serde::{Deserialize, Serialize};
use crate::{Fire, Observer, Want};
const CLK_TCK: f64 = 100.0;
const SAMPLE: Duration = Duration::from_millis(100);
pub struct Probe {
began: Instant,
cpu0: u64,
stop: Arc<AtomicBool>,
sampler: JoinHandle<Vec<f64>>,
}
impl Probe {
pub fn start() -> Self {
let stop = Arc::new(AtomicBool::new(false));
let flag = stop.clone();
let sampler = thread::spawn(move || {
let mut out = Vec::new();
while !flag.load(Ordering::Relaxed) {
thread::sleep(SAMPLE);
out.push(rss_mb());
}
out
});
Self {
began: Instant::now(),
cpu0: cpu_ticks(),
stop,
sampler,
}
}
pub fn stop(self) -> (f64, f64, [f64; 2]) {
let wall = self.began.elapsed().as_secs_f64();
let cpu = (cpu_ticks() - self.cpu0) as f64 / CLK_TCK;
self.stop.store(true, Ordering::Relaxed);
let mut rss = self.sampler.join().expect("the sampler only reads procfs");
rss.sort_by(f64::total_cmp);
assert!(!rss.is_empty(), "a timed section shorter than one {SAMPLE:?} window has no footprint to report");
let at = |q: f64| rss[((rss.len() as f64 * q) as usize).min(rss.len() - 1)];
(wall, cpu, [at(0.68), at(0.95)])
}
}
pub struct Counters {
pub trades: AtomicU64,
pub deltas: AtomicU64,
pub bars_closed: AtomicU64,
pub screener: AtomicU64,
pub classify: AtomicU64,
pub decision: AtomicU64,
pub deprecator: AtomicU64,
}
impl Counters {
fn snapshot(&self) -> BTreeMap<String, u64> {
[
("trades", &self.trades),
("deltas", &self.deltas),
("bars_closed", &self.bars_closed),
("screener", &self.screener),
("classify", &self.classify),
("decision", &self.decision),
("deprecator", &self.deprecator),
]
.into_iter()
.map(|(k, v)| (k.to_string(), v.load(Ordering::Relaxed)))
.collect()
}
}
pub static COUNTERS: Counters = Counters {
trades: AtomicU64::new(0),
deltas: AtomicU64::new(0),
bars_closed: AtomicU64::new(0),
screener: AtomicU64::new(0),
classify: AtomicU64::new(0),
decision: AtomicU64::new(0),
deprecator: AtomicU64::new(0),
};
#[derive(Default)]
pub struct Digest {
hasher: DefaultHasher,
pub count: u64,
}
impl Digest {
pub fn feed(&mut self, item: impl Hash) {
self.count += 1;
item.hash(&mut self.hasher);
}
}
#[derive(Deserialize, Serialize)]
pub struct Row {
pub name: String,
pub ticks: u64,
pub intents: u64,
pub digest: u64,
pub total_s: f64,
pub feed_s: f64,
pub cpu_s: f64,
pub cores_avail: usize,
pub cpus: String,
pub rss_mb: [f64; 2],
pub counters: BTreeMap<String, u64>,
pub notes: Vec<String>,
}
impl Row {
pub fn new(name: &str, ticks: u64, digest: &Digest, total: (f64, f64, [f64; 2]), feed_s: f64, notes: Vec<String>) -> Self {
Self {
name: name.to_string(),
ticks,
intents: digest.count,
digest: digest.hasher.finish(),
total_s: total.0,
feed_s,
cpu_s: total.1,
cores_avail: thread::available_parallelism().expect("a thread count is always known on linux").get(),
cpus: status_field("Cpus_allowed_list:"),
rss_mb: total.2,
counters: COUNTERS.snapshot(),
notes,
}
}
}
pub fn publish(row: Row) {
let dir = PathBuf::from(concat!(env!("CARGO_MANIFEST_DIR"), "/../tmp/bench"));
fs::create_dir_all(&dir).expect("tmp/ is writable");
fs::write(dir.join(format!("{}.json", row.name)), serde_json::to_vec_pretty(&row).expect("a Row serialises")).expect("tmp/bench is writable");
let mut rows: Vec<Row> = fs::read_dir(&dir)
.expect("just created")
.map(|e| e.expect("a readable dir entry").path())
.filter(|p| p.extension().is_some_and(|e| e == "json"))
.map(|p| serde_json::from_slice(&fs::read(&p).expect("just listed")).unwrap_or_else(|e| panic!("stale row at {}: {e} — delete tmp/bench and rerun", p.display())))
.collect();
rows.sort_by(|a, b| a.name.cmp(&b.name));
println!(
"\n{:<14} {:>9} {:>9} {:>9} {:>9} {:>7} {:>11} {:>11} {:>9} {:>18}",
"bench", "total s", "feed s", "compute", "cpu s", "cores", "rss p68 MB", "rss p95 MB", "intents", "digest"
);
for r in &rows {
println!(
"{:<14} {:>9.2} {:>9.2} {:>9.2} {:>9.2} {:>7.2} {:>11.0} {:>11.0} {:>9} {:>18x}",
r.name,
r.total_s,
r.feed_s,
r.total_s - r.feed_s,
r.cpu_s,
r.cpu_s / r.total_s,
r.rss_mb[0],
r.rss_mb[1],
r.intents,
r.digest
);
}
for r in &rows {
println!("{}: cpus {} ({} of them)", r.name, r.cpus, r.cores_avail);
}
let keys: Vec<&String> = rows.first().map(|r| r.counters.keys().collect()).unwrap_or_default();
if !keys.is_empty() {
print!("\n{:<14}", "passes");
for k in &keys {
print!(" {k:>12}");
}
println!();
for r in &rows {
print!("{:<14}", r.name);
for k in &keys {
print!(" {:>12}", r.counters[*k]);
}
println!();
}
}
match rows.windows(2).all(|w| w[0].digest == w[1].digest) {
true if rows.len() > 1 => println!("\nintent streams identical across all {} rows", rows.len()),
true => (),
false => println!("\nDIGESTS DIFFER — the rows above are timing different work, see each row's notes"),
}
for r in &rows {
for n in &r.notes {
println!(" {}: {n}", r.name);
}
}
}
#[derive(Default)]
pub struct Census {
seen: Vec<Seen>,
}
struct Seen {
name: &'static str,
deps: &'static [&'static str],
fired: u64,
skipped: u64,
}
impl Observer for Census {
fn want(&self, _: &'static str) -> Want {
Want::Vals
}
fn on(&mut self, node: &'static str, deps: &'static [&'static str], _: &'static [bool], fire: Fire<'_>) {
let i = self.seen.iter().position(|s| s.name == node).unwrap_or_else(|| {
self.seen.push(Seen {
name: node,
deps,
fired: 0,
skipped: 0,
});
self.seen.len() - 1
});
match fire.vals.is_some() {
true => self.seen[i].fired += 1,
false => self.seen[i].skipped += 1,
}
}
}
impl core::fmt::Display for Census {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
writeln!(f, "{:<44} {:>10} {:>10} {:>7} deps", "node (step = topo order)", "fired", "skipped", "hit")?;
for s in &self.seen {
let steps = s.fired + s.skipped;
writeln!(
f,
"{:<44} {:>10} {:>10} {:>6.1}% {}",
s.name,
s.fired,
s.skipped,
100.0 * s.fired as f64 / steps as f64,
s.deps.join(", ")
)?;
}
let mut roots: Vec<&str> = self.seen.iter().flat_map(|s| s.deps).filter(|d| !self.seen.iter().any(|s| s.name == **d)).copied().collect();
roots.sort_unstable();
roots.dedup();
write!(f, "roots (dep names never stepped): {}", roots.join(", "))
}
}
fn cpu_ticks() -> u64 {
let stat = fs::read_to_string("/proc/self/stat").expect("procfs is mounted");
let rest = &stat[stat.rfind(')').expect("stat has a comm field") + 1..];
let mut f = rest.split_whitespace().skip(11);
let utime: u64 = f.next().expect("utime").parse().expect("utime is a number");
let stime: u64 = f.next().expect("stime").parse().expect("stime is a number");
utime + stime
}
fn status_field(key: &str) -> String {
let status = fs::read_to_string("/proc/self/status").expect("procfs is mounted");
let line = status
.lines()
.find(|l| l.starts_with(key))
.unwrap_or_else(|| panic!("/proc/self/status reports {key} for any live process"));
line[key.len()..].trim().to_string()
}
fn rss_mb() -> f64 {
status_field("VmRSS:")
.split_whitespace()
.next()
.expect("VmRSS has a value")
.parse::<f64>()
.expect("VmRSS is a number")
/ 1024.0
}