rt store: a lookup reads no state
What: a lookup read the 24-byte state record (young's number) from the state file each time. The store now keeps young's number as it last read or wrote the state: read at open, refreshed by every update under the lock, and only growing. A lookup reads it from memory. A move to the next generation that another store on the directory made is seen at this store's next write. Until then its lookups try the generations it knew: a file the other wrote into the new young is a miss, and a hit in what is now old is not moved into young. Both cost a miss at worst, and every file still checks itself. In rt a miss is followed by a write in nearly every case (a lowering or refusal kept; a pipeline's data kept by the reaper, unless more than 64 caches wait for it), and a write reads the state under the lock. Why: the planner asked for the second render state's own time on the UHD to come down. Probe job m3-max-45357 (homelab-mac-mini-2018, NTFS) split its 319 us of store lookups: about 50 us were the state record read once per lookup, over three lookups. Test: a_lookup_reads_no_state_and_sees_another_stores_move_at_its_next_write counts the state reads of 20 lookups (none), and shows a move another store made seen after the next write.
This commit is contained in:
1 parent
3cf9519fce
commit
17ce150bbe
2 files changed
+75
-9
No files matched your search
@@ -26,10 +26,13 @@
|
||||
//! number, its bytes and a checksum of both, read and written under a lock on the file, which
|
||||
//! every store on the directory, in this process or another, holds open for its life. The write
|
||||
//! that takes young past half the cap makes the next generation young: young becomes old, and the
|
||||
//! old one is dead. A thread of the store's own removes the dead generation and ends, so no
|
||||
//! thread that submits, waits for or completes work removes files. So a file stays at least half
|
||||
//! the cap and at most the cap of other writes and moves after its last use, opening reads one
|
||||
//! file whatever the store holds, and nothing per file is kept in memory.
|
||||
//! old one is dead. A store keeps young's number as it last read or wrote the state, so a lookup
|
||||
//! reads no state: a move another store made is seen at this store's next write, and until then
|
||||
//! its lookups try the generations it knew, a miss at worst. A thread of the store's own removes
|
||||
//! the dead generation and ends, so no thread that submits, waits for or completes work removes
|
||||
//! files. So a file stays at least half the cap and at most the cap of other writes and moves
|
||||
//! after its last use, opening reads one file whatever the store holds, and nothing per file is
|
||||
//! kept in memory.
|
||||
//!
|
||||
//! Every write is a temporary file renamed over its name, counted before it is written and given
|
||||
//! back where it fails, so the count is never below what young holds. Every file checks itself
|
||||
@@ -94,9 +97,15 @@ pub(crate) struct Store {
|
||||
writer: Mutex<()>,
|
||||
/// Set while the store's removal thread runs.
|
||||
removing: AtomicBool,
|
||||
/// Young's number as this store last read or wrote the state file, 0 before it has: what a
|
||||
/// lookup tries, without reading the file. It only grows.
|
||||
young: AtomicU64,
|
||||
/// The times the store listed the cache directory.
|
||||
#[cfg(test)]
|
||||
pub(crate) listings: AtomicUsize,
|
||||
/// The times the store read the state file outside its lock.
|
||||
#[cfg(test)]
|
||||
pub(crate) state_reads: AtomicUsize,
|
||||
}
|
||||
|
||||
impl Store {
|
||||
@@ -122,10 +131,14 @@ impl Store {
|
||||
writable,
|
||||
writer: Mutex::new(()),
|
||||
removing: AtomicBool::new(false),
|
||||
young: AtomicU64::new(0),
|
||||
#[cfg(test)]
|
||||
listings: AtomicUsize::new(0),
|
||||
#[cfg(test)]
|
||||
state_reads: AtomicUsize::new(0),
|
||||
});
|
||||
if let Some(state) = store.current() {
|
||||
store.young.fetch_max(state.young, Relaxed);
|
||||
if state.young >= 2 && store.generation(state.young - 2).exists() {
|
||||
store.remove_dead();
|
||||
}
|
||||
@@ -175,9 +188,24 @@ impl Store {
|
||||
/// The state, read without the lock where it checks, else under it, rebuilt where it is
|
||||
/// missing or does not check.
|
||||
pub(crate) fn current(&self) -> Option<State> {
|
||||
#[cfg(test)]
|
||||
self.state_reads.fetch_add(1, Relaxed);
|
||||
read_state(self.state.as_ref()?).or_else(|| self.update(|state| Some(*state)))
|
||||
}
|
||||
|
||||
/// Young's number as this store last read or wrote it, read from the state file only where
|
||||
/// it has not been yet.
|
||||
fn young(&self) -> Option<u64> {
|
||||
match self.young.load(Relaxed) {
|
||||
0 => {
|
||||
let young = self.current()?.young;
|
||||
self.young.fetch_max(young, Relaxed);
|
||||
Some(young)
|
||||
}
|
||||
young => Some(young),
|
||||
}
|
||||
}
|
||||
|
||||
/// Generation `number`'s directory.
|
||||
pub(crate) fn generation(&self, number: u64) -> PathBuf {
|
||||
self.root.join(format!("g{number}"))
|
||||
@@ -202,7 +230,7 @@ impl Store {
|
||||
decode: impl FnOnce(&[u8]) -> Option<T>,
|
||||
torn: bool,
|
||||
) -> Option<T> {
|
||||
let young = self.current()?.young;
|
||||
let young = self.young()?;
|
||||
let mut decode = Some(decode);
|
||||
for (number, old) in [(young, false), (young.saturating_sub(1), true)] {
|
||||
let path = self.generation(number).join(file);
|
||||
@@ -277,6 +305,7 @@ impl Store {
|
||||
let mut state = read_state(file).or_else(|| self.rebuild(file))?;
|
||||
let answer = change(&mut state)?;
|
||||
write_state(file, state).ok()?;
|
||||
self.young.fetch_max(state.young, Relaxed);
|
||||
Some(answer)
|
||||
})();
|
||||
let _ = file.unlock();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
//! The cache directory on the host: two generations under one cap, a hit in old moved into young,
|
||||
//! the state file read at open with no listing and rebuilt where it is torn or missing, a removal
|
||||
//! cut short (a process's exit in it included) finished by the next open, torn files removed, and
|
||||
//! stores in threads and processes
|
||||
//! sharing one directory without losing a count or reading a file another was writing.
|
||||
//! a lookup that reads no state, the state file read at open with no listing and rebuilt where it
|
||||
//! is torn or missing, a removal cut short (a process's exit in it included) finished by the next
|
||||
//! open, torn files removed, and stores in threads and processes sharing one directory without
|
||||
//! losing a count or reading a file another was writing.
|
||||
|
||||
use crate::cache::{frame, unframe, State, Store};
|
||||
use std::fs;
|
||||
@@ -139,6 +139,43 @@ fn a_hit_in_old_moves_into_young_and_outlives_the_next_generation() {
|
||||
assert_eq!(store.get(&name(0), whole), Some(vec![0; 100]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_lookup_reads_no_state_and_sees_another_stores_move_at_its_next_write() {
|
||||
let scratch = Scratch::new("store-young");
|
||||
let first = Store::open(scratch.0.clone(), 1000);
|
||||
let second = Store::open(scratch.0.clone(), 1000);
|
||||
assert!(first.put(&name(0), &[0; 100]));
|
||||
let reads = first.state_reads.load(Relaxed);
|
||||
for _ in 0..10 {
|
||||
assert_eq!(first.get(&name(0), whole), Some(vec![0; 100]));
|
||||
assert_eq!(first.get(&name(1), whole), None);
|
||||
}
|
||||
assert_eq!(
|
||||
first.state_reads.load(Relaxed),
|
||||
reads,
|
||||
"no lookup read the state"
|
||||
);
|
||||
// The second store's fifth write makes generation 2 young. The first still knows generation
|
||||
// 1, so the file the second wrote there is a miss for it.
|
||||
for n in 1..6 {
|
||||
assert!(second.put(&name(2 * n), &[n as u8; 100]));
|
||||
}
|
||||
assert_eq!(state(&second).young, 2);
|
||||
assert_eq!(
|
||||
first.get(&name(10), whole),
|
||||
None,
|
||||
"a move the first has not seen"
|
||||
);
|
||||
assert_eq!(
|
||||
first.get(&name(0), whole),
|
||||
Some(vec![0; 100]),
|
||||
"what it knew"
|
||||
);
|
||||
// The first's next write reads the state under its lock, and its lookups follow.
|
||||
assert!(first.put(&name(12), &[6; 100]));
|
||||
assert_eq!(first.get(&name(10), whole), Some(vec![5; 100]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_reopened_store_reads_its_state_and_lists_no_directory() {
|
||||
let scratch = Scratch::new("store-reopen");
|
||||
|
||||
Reference in new issue
Block a user