use std::sync::atomic::{AtomicU64, Ordering};
pub const LARGE_READ_BYTES: u64 = 4 * 1024 * 1024;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Site {
GrepIndexed,
GrepCollect,
GrepFallback,
DegradedGrep,
Presence,
MatchingLine,
CandidateText,
SourceFile,
LexicalExactness,
MemoDigest,
ExternalSweep,
ExactCorpus,
DataFile,
NearestNames,
LineWindows,
SnippetLines,
ParseSource,
}
pub const SITES: [(Site, &str); 17] = [
(Site::GrepIndexed, "grep_indexed"),
(Site::GrepCollect, "grep_collect"),
(Site::GrepFallback, "grep_fallback"),
(Site::DegradedGrep, "degraded_grep"),
(Site::Presence, "presence"),
(Site::MatchingLine, "matching_line"),
(Site::CandidateText, "candidate_text"),
(Site::SourceFile, "source_file"),
(Site::LexicalExactness, "lexical_exactness"),
(Site::MemoDigest, "memo_digest"),
(Site::ExternalSweep, "external_sweep"),
(Site::ExactCorpus, "exact_corpus"),
(Site::DataFile, "data_file"),
(Site::NearestNames, "nearest_names"),
(Site::LineWindows, "line_windows"),
(Site::SnippetLines, "snippet_lines"),
(Site::ParseSource, "parse_source"),
];
pub struct Counter {
pub count: AtomicU64,
pub bytes: AtomicU64,
pub largest: AtomicU64,
pub whole_count: AtomicU64,
pub whole_bytes: AtomicU64,
pub skipped: AtomicU64,
}
#[allow(clippy::declare_interior_mutable_const)]
pub const ZERO: Counter = Counter {
count: AtomicU64::new(0),
bytes: AtomicU64::new(0),
largest: AtomicU64::new(0),
whole_count: AtomicU64::new(0),
whole_bytes: AtomicU64::new(0),
skipped: AtomicU64::new(0),
};
pub static COUNTERS: [Counter; SITES.len()] = [ZERO; SITES.len()];
#[cfg(any(test, feature = "test-support"))]
thread_local! {
pub static TEST_COUNTERS: std::cell::RefCell<Option<[Counter; SITES.len()]>> = const { std::cell::RefCell::new(None) };
}
pub fn with_counter<T>(site: Site, work: impl FnOnce(&Counter) -> T) -> T {
#[cfg(any(test, feature = "test-support"))]
let mut work = Some(work);
#[cfg(any(test, feature = "test-support"))]
if let Some(result) = TEST_COUNTERS.with(|counters| {
counters
.borrow()
.as_ref()
.map(|counters| work.take().unwrap()(&counters[site as usize]))
}) {
return result;
}
#[cfg(any(test, feature = "test-support"))]
let work = work.unwrap();
work(&COUNTERS[site as usize])
}
fn record_read(site: Site, size: u64, bytes: u64, whole: bool) {
if size <= LARGE_READ_BYTES && bytes <= LARGE_READ_BYTES {
return;
}
with_counter(site, |counter| {
counter.count.fetch_add(1, Ordering::Relaxed);
counter.bytes.fetch_add(bytes, Ordering::Relaxed);
counter.largest.fetch_max(bytes, Ordering::Relaxed);
if whole {
counter.whole_count.fetch_add(1, Ordering::Relaxed);
counter.whole_bytes.fetch_add(bytes, Ordering::Relaxed);
}
});
}
pub fn record(site: Site, size: u64, bytes: u64) {
record_read(site, size, bytes, false);
}
pub fn record_whole(site: Site, bytes: usize) {
record_read(site, bytes as u64, bytes as u64, true);
}
pub const WHOLE_READ_CEILING: u64 = 16 * 1024 * 1024;
pub const WHOLE_READ_SKIP_REASON: &str =
"Skipped: reading this file would exceed the 16 MiB in-memory read ceiling.";
pub fn allow_whole(site: Site, path: &std::path::Path, size: u64) -> std::io::Result<()> {
if size <= WHOLE_READ_CEILING {
return Ok(());
}
skip(site, path);
Err(std::io::Error::other(WHOLE_READ_SKIP_REASON))
}
pub fn skip(site: Site, path: &std::path::Path) {
with_counter(site, |counter| {
counter.skipped.fetch_add(1, Ordering::Relaxed);
});
crate::bounded_io::note_large_read_skip(path);
}
pub fn is_ceiling_error(error: &std::io::Error) -> bool {
error.to_string() == WHOLE_READ_SKIP_REASON
}