diff options
Diffstat (limited to 'pkgs/populate/src/populate.rs')
| -rw-r--r-- | pkgs/populate/src/populate.rs | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/pkgs/populate/src/populate.rs b/pkgs/populate/src/populate.rs index 86639ac..884ecd9 100644 --- a/pkgs/populate/src/populate.rs +++ b/pkgs/populate/src/populate.rs @@ -9,10 +9,13 @@ use crate::run::{self, Context, Failure, Result}; use crate::sh; use crate::spec::{Source, Spec}; use serde_json::{json, Map, Value}; +use std::collections::BTreeMap; use std::fs; use std::os::unix::fs::DirBuilderExt; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::Mutex; /// The commands populate runs on the target to deliver spec's sources, each /// followed by the attribute name in nixpkgs of the package that has it. @@ -59,6 +62,7 @@ pub fn populate(context: &Context, spec: &Spec) -> Result<()> { context.run(&sh::backup(), &[dir])?; } let probed = probe::probe(context, spec, probed)?; + let mut gathered = gathered(spec, &probed); for (name, source) in &spec.source { let path = format!("{dir}/{name}"); eprintln!("populating '{}'...", context.name(&path)); @@ -80,6 +84,7 @@ pub fn populate(context: &Context, spec: &Spec) -> Result<()> { path: &path, workdir, target: probed.get(name), + gathered: std::cell::Cell::new(gathered.remove(name)), }; context.note(json!({ "begin": name })); let outcome = kind.deliver(&delivery)?; @@ -195,6 +200,9 @@ struct Delivery<'a> { /// What the probe found at the target path, for a source whose delivery /// may find nothing to do, see probed. target: Option<&'a probe::Target>, + /// What gathered found for a file source, ahead of its turn, which + /// File::deliver takes. + gathered: std::cell::Cell<Option<Result<rsync::Gathered>>>, } impl Delivery<'_> { @@ -303,6 +311,49 @@ fn file_config(source: &Source, source_path: &str) -> Config { } } +/// What rsync::gather finds for the file sources whose target path the +/// probe could list, by name: for all of them at once, at most 16 at a +/// time, ahead of their turns, since listing what rsync would transfer of +/// a source with filters takes a run of rsync, which takes about 50 ms, +/// also with nothing to do. gather prints nothing, so messages still come +/// at each source's turn, from rsync::finish. +fn gathered( + spec: &Spec, + probed: &BTreeMap<String, probe::Target>, +) -> BTreeMap<String, Result<rsync::Gathered>> { + let jobs: Vec<(&String, &Source, &str)> = spec + .source + .iter() + .filter(|(name, source)| { + source.kind == "file" + && probed + .get(*name) + .is_some_and(|target| target.listing.is_some()) + }) + .filter_map(|(name, source)| { + let path = string(&source.settings, "path").ok()?; + Some((name, source, path)) + }) + .collect(); + let next = AtomicUsize::new(0); + let results = Mutex::new(BTreeMap::new()); + std::thread::scope(|scope| { + for _ in 0..jobs.len().min(16) { + scope.spawn(|| { + while let Some((name, source, path)) = + jobs.get(next.fetch_add(1, Ordering::Relaxed)) + { + let gathered = rsync::gather(&file_config(source, path), path, true); + if let Ok(mut results) = results.lock() { + results.insert((*name).clone(), gathered); + } + } + }); + } + }); + results.into_inner().unwrap_or_else(|e| e.into_inner()) +} + /// What the probe looks for at a source's target path, see probe::probe: /// the listing of a file source that's a directory, that rsync compares by /// size and time, not checksum, the symlink of a symlink source, and the @@ -331,7 +382,10 @@ impl Kind for File { // Nothing to do if what rsync would transfer and what's at the // target path are the same as far as rsync's quick check goes. let listing = d.target.and_then(|target| target.listing.as_ref()); - let prepared = rsync::prepare(&config, source_path, listing.is_some())?; + let prepared = match d.gathered.take() { + Some(gathered) => rsync::finish(&config, gathered?)?, + None => rsync::prepare(&config, source_path, listing.is_some())?, + }; let changed = if prepared.listing.is_some() && prepared.listing.as_ref() == listing { Vec::new() } else { |
