#![cfg(feature = "metrics")]
#![allow(unsafe_code, reason = "an allocator that counts what the system's hands out")]
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::SystemTime;
use tinystore::metrics::Series;
use tinystore::{Options, Store};
struct Counting;
static HELD: AtomicU64 = AtomicU64::new(0);
static MOST: AtomicU64 = AtomicU64::new(0);
unsafe impl GlobalAlloc for Counting {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let held = HELD.fetch_add(layout.size() as u64, Ordering::Relaxed) + layout.size() as u64;
MOST.fetch_max(held, Ordering::Relaxed);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
HELD.fetch_sub(layout.size() as u64, Ordering::Relaxed);
unsafe { System.dealloc(ptr, layout) }
}
}
#[global_allocator]
static COUNTING: Counting = Counting;
static MEASURING: Mutex<()> = Mutex::new(());
fn measured(store: &Store, call: impl FnOnce()) -> (u64, u64) {
let memory = Arc::clone(store.memory());
let (done, reserved) = (Arc::new(AtomicBool::new(false)), Arc::new(AtomicU64::new(0)));
let reserved_before = memory.used();
let watching = {
let (done, reserved) = (Arc::clone(&done), Arc::clone(&reserved));
std::thread::spawn(move || {
while !done.load(Ordering::SeqCst) {
reserved.fetch_max(memory.used(), Ordering::SeqCst);
std::hint::spin_loop();
}
})
};
let before = HELD.load(Ordering::SeqCst);
MOST.store(before, Ordering::SeqCst);
call();
let most = MOST.load(Ordering::SeqCst);
done.store(true, Ordering::SeqCst);
watching.join().unwrap();
(most - before, reserved.load(Ordering::SeqCst).saturating_sub(reserved_before))
}
fn text(lines: usize, line: impl Fn(usize) -> String) -> String {
(0..lines).map(line).collect()
}
fn histogram(family: usize) -> String {
let mut lines = format!(
"# HELP rpc_{family}_seconds How long a call of method {family} took, from its first byte to its last.\n\
# UNIT rpc_{family}_seconds seconds\n# TYPE rpc_{family}_seconds histogram\n"
);
for (count, le) in ["0.005", "0.01", "0.05", "0.1", "0.5", "1", "5", "10", "+Inf"].iter().enumerate() {
lines += &format!("rpc_{family}_seconds_bucket{{code=\"200\",le=\"{le}\"}} {count}\n");
}
lines + &format!("rpc_{family}_seconds_sum{{code=\"200\"}} 4.2\nrpc_{family}_seconds_count{{code=\"200\"}} 9\n")
}
fn texts() -> Vec<(&'static str, String)> {
let ten_labels = |line: usize| {
let labels: Vec<String> = (0..10).map(|label| format!("label_{label}=\"value-{label}-{line:08}\"")).collect();
format!("requests_total{{{}}} {line}\n", labels.join(","))
};
vec![
("one name, a label a line", text(20_000, |line| format!("up{{shard=\"{line}\"}} 1\n"))),
("ten labels a line", text(3_000, ten_labels)),
("a name a line", text(10_000, |line| format!("gauge_{line} 1\n"))),
("a described name a line", text(5_000, |line| format!("# HELP load_{line} Busy.\nload_{line} 1\n"))),
("histograms", text(300, histogram)),
("quoted names", text(5_000, |line| format!("{{\"http.server.{line}\", \"url.scheme\"=\"https\"}} 1\n"))),
]
}
fn store() -> (tempfile::TempDir, Store) {
let dir = tempfile::tempdir().unwrap();
let options = Options { background: false, memory: Some(8 << 30), ..Options::default() };
let store = Store::open(dir.path(), options).unwrap();
(dir, store)
}
#[test]
fn an_exposition_reserves_the_heap_its_reading_and_its_write_hold() {
let _alone = MEASURING.lock().unwrap_or_else(PoisonError::into_inner);
for (shape, text) in texts() {
let (_dir, store) = store();
let metrics = store.metrics().unwrap();
for scrape in ["first", "second"] {
let web = metrics.prometheus().label("instance", "web-1");
let (heap, reserved) = measured(&store, || web.ingest(&text).unwrap());
println!("{shape}, {scrape} scrape: heap {heap}, reserved {reserved}");
assert!(heap <= reserved, "{shape}, {scrape} scrape: held {heap} bytes, reserved {reserved}");
}
}
}
#[test]
fn an_ingest_reserves_the_heap_its_write_holds() {
let _alone = MEASURING.lock().unwrap_or_else(PoisonError::into_inner);
let (_dir, store) = store();
let metrics = store.metrics().unwrap();
let series: Vec<Series> = (0..10_000)
.map(|line| Series::gauge(format!("disk_{}", line % 100)).label("host", format!("web-{line}")))
.map(|series| series.sample(SystemTime::now(), 1.0))
.collect();
for write in ["first", "second"] {
let (heap, reserved) = measured(&store, || metrics.ingest(&series).unwrap());
println!("series, {write} write: heap {heap}, reserved {reserved}");
assert!(heap <= reserved, "series, {write} write: held {heap} bytes, reserved {reserved}");
}
}