1use std::sync::atomic::{AtomicU64, Ordering};
4
5pub const LARGE_READ_BYTES: u64 = 4 * 1024 * 1024;
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum Site {
10 GrepIndexed,
12 GrepCollect,
14 GrepFallback,
16 DegradedGrep,
18 Presence,
20 MatchingLine,
22 CandidateText,
24 SourceFile,
26 LexicalExactness,
28 MemoDigest,
30 ExternalSweep,
32 ExactCorpus,
34 DataFile,
36 NearestNames,
38 LineWindows,
40 SnippetLines,
42 ParseSource,
44}
45
46pub const SITES: [(Site, &str); 17] = [
47 (Site::GrepIndexed, "grep_indexed"),
48 (Site::GrepCollect, "grep_collect"),
49 (Site::GrepFallback, "grep_fallback"),
50 (Site::DegradedGrep, "degraded_grep"),
51 (Site::Presence, "presence"),
52 (Site::MatchingLine, "matching_line"),
53 (Site::CandidateText, "candidate_text"),
54 (Site::SourceFile, "source_file"),
55 (Site::LexicalExactness, "lexical_exactness"),
56 (Site::MemoDigest, "memo_digest"),
57 (Site::ExternalSweep, "external_sweep"),
58 (Site::ExactCorpus, "exact_corpus"),
59 (Site::DataFile, "data_file"),
60 (Site::NearestNames, "nearest_names"),
61 (Site::LineWindows, "line_windows"),
62 (Site::SnippetLines, "snippet_lines"),
63 (Site::ParseSource, "parse_source"),
64];
65
66pub struct Counter {
67 pub count: AtomicU64,
68 pub bytes: AtomicU64,
69 pub largest: AtomicU64,
70 pub whole_count: AtomicU64,
71 pub whole_bytes: AtomicU64,
72 pub skipped: AtomicU64,
73}
74
75#[allow(clippy::declare_interior_mutable_const)]
76pub const ZERO: Counter = Counter {
77 count: AtomicU64::new(0),
78 bytes: AtomicU64::new(0),
79 largest: AtomicU64::new(0),
80 whole_count: AtomicU64::new(0),
81 whole_bytes: AtomicU64::new(0),
82 skipped: AtomicU64::new(0),
83};
84
85pub static COUNTERS: [Counter; SITES.len()] = [ZERO; SITES.len()];
86
87#[cfg(any(test, feature = "test-support"))]
88thread_local! {
89 pub static TEST_COUNTERS: std::cell::RefCell<Option<[Counter; SITES.len()]>> = const { std::cell::RefCell::new(None) };
92}
93
94pub fn with_counter<T>(site: Site, work: impl FnOnce(&Counter) -> T) -> T {
95 #[cfg(any(test, feature = "test-support"))]
96 let mut work = Some(work);
97 #[cfg(any(test, feature = "test-support"))]
98 if let Some(result) = TEST_COUNTERS.with(|counters| {
99 counters
100 .borrow()
101 .as_ref()
102 .map(|counters| work.take().unwrap()(&counters[site as usize]))
103 }) {
104 return result;
105 }
106 #[cfg(any(test, feature = "test-support"))]
107 let work = work.unwrap();
108 work(&COUNTERS[site as usize])
109}
110
111fn record_read(site: Site, size: u64, bytes: u64, whole: bool) {
112 if size <= LARGE_READ_BYTES && bytes <= LARGE_READ_BYTES {
113 return;
114 }
115 with_counter(site, |counter| {
116 counter.count.fetch_add(1, Ordering::Relaxed);
117 counter.bytes.fetch_add(bytes, Ordering::Relaxed);
118 counter.largest.fetch_max(bytes, Ordering::Relaxed);
119 if whole {
120 counter.whole_count.fetch_add(1, Ordering::Relaxed);
121 counter.whole_bytes.fetch_add(bytes, Ordering::Relaxed);
122 }
123 });
124}
125
126pub fn record(site: Site, size: u64, bytes: u64) {
130 record_read(site, size, bytes, false);
131}
132
133pub fn record_whole(site: Site, bytes: usize) {
135 record_read(site, bytes as u64, bytes as u64, true);
136}
137
138pub const WHOLE_READ_CEILING: u64 = 16 * 1024 * 1024;
141
142pub const WHOLE_READ_SKIP_REASON: &str =
144 "Skipped: reading this file would exceed the 16 MiB in-memory read ceiling.";
145
146pub fn allow_whole(site: Site, path: &std::path::Path, size: u64) -> std::io::Result<()> {
147 if size <= WHOLE_READ_CEILING {
148 return Ok(());
149 }
150 skip(site, path);
151 Err(std::io::Error::other(WHOLE_READ_SKIP_REASON))
152}
153
154pub fn skip(site: Site, path: &std::path::Path) {
155 with_counter(site, |counter| {
156 counter.skipped.fetch_add(1, Ordering::Relaxed);
157 });
158 crate::bounded_io::note_large_read_skip(path);
159}
160
161pub fn is_ceiling_error(error: &std::io::Error) -> bool {
162 error.to_string() == WHOLE_READ_SKIP_REASON
163}