mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Scratch folder queries: address second review
A stuck close no longer holds up the files queued behind it (up to four closer threads), and a query identifies the current file by the configured folder. Tests don't read the room and the status across a late answer, and the growth-check step proves its query is stalled.
This commit is contained in:
1 parent
e01c8719a0
commit
cd2250cb25
4 files changed
+42
-16
No files matched your search
+14
-10
@@ -657,7 +657,7 @@ fn run_query(dir: PathBuf, prepare: bool, done: Arc<QueryDone>) {
|
||||
// counted again on top of it. Another folder's file says nothing about this one.
|
||||
let (open, used) = {
|
||||
let c = M.config();
|
||||
let current = c.file.as_ref().filter(|f| f.path().parent() == Some(dir.as_path()));
|
||||
let current = c.file.as_ref().filter(|_| c.dir.as_deref() == Some(dir.as_path()));
|
||||
(prepare && current.is_none(), current.map_or(0, |f| f.used()))
|
||||
};
|
||||
let file = open.then(|| std::fs::create_dir_all(&dir).and_then(|()| ScratchFile::create(&dir)));
|
||||
@@ -679,22 +679,26 @@ fn query_overdue(dir: Option<&Path>) -> bool {
|
||||
/// Scratch files on their way out, oldest first.
|
||||
struct Retired {
|
||||
files: VecDeque<(Arc<ScratchFile>, Arc<QueryDone>)>,
|
||||
closing: bool,
|
||||
/// Closer threads alive.
|
||||
closers: usize,
|
||||
}
|
||||
|
||||
static RETIRED: Mutex<Retired> = Mutex::new(Retired { files: VecDeque::new(), closing: false });
|
||||
/// Most closer threads at once: enough that one close stuck on a dead mount doesn't hold up the
|
||||
/// files behind it, few enough that stuck closes can't pile up threads.
|
||||
const MAX_CLOSERS: usize = 4;
|
||||
|
||||
static RETIRED: Mutex<Retired> = Mutex::new(Retired { files: VecDeque::new(), closers: 0 });
|
||||
|
||||
/// Let go of a scratch file off this thread: the last reference closes it, which can flush to its
|
||||
/// folder, and a dead mount blocks that, while leaving a dead folder is how a user recovers. One
|
||||
/// closer thread however many folders are left, so stuck closes can't pile up threads. The returned
|
||||
/// handle finishes when this reference is gone.
|
||||
/// folder, and a dead mount blocks that, while leaving a dead folder is how a user recovers. The
|
||||
/// returned handle finishes when this reference is gone.
|
||||
fn retire(file: Arc<ScratchFile>) -> Arc<QueryDone> {
|
||||
let done = Arc::new(QueryDone::default());
|
||||
let mut retired = RETIRED.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
retired.files.push_back((file, done.clone()));
|
||||
if !retired.closing {
|
||||
// If no thread can start, the file waits in the queue for the next one rather than closing here.
|
||||
retired.closing = std::thread::Builder::new().name("pc-scratch-close".into()).spawn(close_retired).is_ok();
|
||||
// If no thread can start, the file waits in the queue for the next one rather than closing here.
|
||||
if retired.closers < MAX_CLOSERS && std::thread::Builder::new().name("pc-scratch-close".into()).spawn(close_retired).is_ok() {
|
||||
retired.closers += 1;
|
||||
}
|
||||
done
|
||||
}
|
||||
@@ -705,7 +709,7 @@ fn close_retired() {
|
||||
let mut retired = RETIRED.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
let next = retired.files.pop_front();
|
||||
if next.is_none() {
|
||||
retired.closing = false;
|
||||
retired.closers = retired.closers.saturating_sub(1);
|
||||
}
|
||||
next
|
||||
};
|
||||
|
||||
@@ -4,6 +4,23 @@
|
||||
|
||||
use photocraft_raster::spill;
|
||||
|
||||
/// The room, once no query is outstanding. A query that outlives its 2 s wait on a slow machine
|
||||
/// counts as no answer, and its late answer would change the status between two reads, so ask again
|
||||
/// until the room and the status describe the same answer.
|
||||
fn answered_capacity() -> Option<u64> {
|
||||
let mut room = None;
|
||||
for _ in 0..10 {
|
||||
room = spill::scratch_capacity();
|
||||
while spill::query_threads_for_tests() != 0 {
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
if room.is_none() == spill::free_space_unmeasured() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
room
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scratch_capacity_follows_spilling_the_size_limit_and_pauses() {
|
||||
let root = std::env::temp_dir().join(format!("photocraft-scratch-capacity-{}", std::process::id()));
|
||||
@@ -16,14 +33,14 @@ fn scratch_capacity_follows_spilling_the_size_limit_and_pauses() {
|
||||
spill::configure(Some(&dir), 1 << 20).unwrap();
|
||||
assert!(spill::enabled() && !spill::paused());
|
||||
// With no size limit, the flag says exactly when the capacity is unknown.
|
||||
let unknown = spill::scratch_capacity().is_none();
|
||||
let unknown = answered_capacity().is_none();
|
||||
assert_eq!(spill::free_space_unmeasured(), unknown);
|
||||
// The free-space reserve applies; an unknown volume without a size limit promises nothing.
|
||||
if let Some(room) = spill::scratch_capacity() {
|
||||
assert!(room < u64::MAX / 2, "the reserve and margin are applied: {room}");
|
||||
}
|
||||
|
||||
let measurable = spill::scratch_capacity().is_some();
|
||||
let measurable = answered_capacity().is_some();
|
||||
|
||||
// A folder that stops answering is flagged and clears once it answers again. Windows keeps the
|
||||
// open scratch file's folder, so this part needs a folder that can be removed.
|
||||
@@ -32,7 +49,7 @@ fn scratch_capacity_follows_spilling_the_size_limit_and_pauses() {
|
||||
assert!(spill::free_space_unmeasured());
|
||||
assert!(spill::stats().free_space_unmeasured);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let unknown = spill::scratch_capacity().is_none();
|
||||
let unknown = answered_capacity().is_none();
|
||||
assert_eq!(spill::free_space_unmeasured(), unknown);
|
||||
assert_eq!(spill::stats().free_space_unmeasured, unknown);
|
||||
if measurable {
|
||||
|
||||
@@ -180,12 +180,16 @@ fn a_folder_that_stops_answering_never_freezes_the_app() {
|
||||
poll_until("the late answer to measure the volume", || !spill::free_space_unmeasured());
|
||||
}
|
||||
|
||||
poll_until("the previous query to finish", || spill::query_threads_for_tests() == 0);
|
||||
|
||||
// H. The growth check never stalls eviction: tiles still spill while it is unanswered.
|
||||
spill::set_budget(8 * TILE_BYTES);
|
||||
arm();
|
||||
spill::set_size_cap(Some(1 << 50));
|
||||
let trigger_tiles = surface(1, 32);
|
||||
poll_until("the growth query to reach the stall", || ENTERED.load(Ordering::SeqCst) >= 1);
|
||||
let evictions = spill::stats().evictions;
|
||||
let tiles = surface(1, 32);
|
||||
let tiles = surface(33, 32);
|
||||
prompt("settle", spill::settle);
|
||||
assert!(spill::stats().evictions > evictions, "{:?}", spill::stats());
|
||||
poll_until("the unanswered growth check to count as unknown", spill::free_space_unmeasured);
|
||||
@@ -198,6 +202,7 @@ fn a_folder_that_stops_answering_never_freezes_the_app() {
|
||||
// Cleanup.
|
||||
spill::set_query_hook_for_tests(None);
|
||||
drop(tiles);
|
||||
drop(trigger_tiles);
|
||||
spill::configure(None, 0).unwrap();
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
}
|
||||
@@ -64,8 +64,8 @@ still the current one and is discarded otherwise. The free-space check every 64
|
||||
waits, and with an engine session tile allocation leaves retries to its supervisor. The wait is on
|
||||
the calling thread: changing Scratch Disks preferences or checking room for an import may wait up
|
||||
to those 2 s once for a newly hung folder. A whole folder change waits at most 2 s in total, the new
|
||||
folder first, with closing the old scratch file getting what is left; old files close on one
|
||||
background thread. On macOS the purgeable-space lookup asks every mounted volume, so it runs
|
||||
folder first, with closing the old scratch file getting what is left; old files close on at most
|
||||
four background threads, so one stuck close doesn't hold up the others. On macOS the purgeable-space lookup asks every mounted volume, so it runs
|
||||
separately for at most 0.5 s, and an unrelated dead mount only loses the purgeable figure, never
|
||||
the folder's own numbers. Limit: reading or writing tile pixels already spilled
|
||||
to a hung folder still blocks the thread doing it; only opening and measuring are bounded.
|
||||
|
||||
Reference in new issue
Block a user