build_profile,deb,sandbox,test: move phases and profiling out of deb
CI / build (push) Successful in 1m42s
CI / test (push) Skipped
CI / publish (push) Skipped
CI / snap (push) Successful in 2m45s
CI / release (push) Skipped

`Phase`, its classifier selection and `BuildProfiler` described the
build flow from inside `deb`, so `sandbox` had to import them from
`deb` to announce chroot work. Moving them to `src/build_profile.rs`
leaves `sandbox` depending on `deb` only for the session record.

`PhaseTiming` and `BuildProfile` move with the profiler, since they are
the record shape it produces. `deb` re-exports `Phase` and `session`
re-exports the record types, so their public paths stay the same.
This commit is contained in:
vhaudiquet committed 2026-11-25 07:40:46 +01:00
1 parent 8e6035c185
commit 24bade82ec
10 files changed
+302 -276

No files matched your search

+3 -233
View File
@@ -4,190 +4,15 @@ pub(crate) mod local;
/// host-tree snapshot/sync that keeps build artifacts alive across attempts
pub mod session;
use crate::logfmt::{
AptInstallClassifier, AptUpdateClassifier, Classifier, GenericClassifier, MakeClassifier,
MmdebstrapClassifier, QuiltClassifier,
};
pub use crate::build_profile::Phase;
use crate::build_profile::{BuildProfiler, enter_phase};
use crate::logfmt::Classifier;
use crate::report::{BuildTarget, BuildView, TargetKind};
use crate::sandbox::{Host, PutOutcome, ReuseState, Sandbox, SandboxExt, StageOptions, Unshare};
use std::error::Error;
use std::path::{Path, PathBuf};
use std::sync::Arc;
/// Phases of a binary build, announced to the [`BuildView`]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Phase {
/// Downloading the chroot tarball (mmdebstrap)
PreparingChroot,
/// Extracting the chroot tarball
ExtractingChroot,
/// Device nodes, /proc bind mount, etc.
FinalizingChroot,
/// apt-get update
UpdatingPackageLists,
/// Installing build-essential & co
InstallingEssentials,
/// quilt push -a
ApplyingPatches,
/// --inject packages
InjectingPackages,
/// apt-get build-dep
InstallingBuildDeps,
/// Drop the resumed tree's input-untracked stamp caches (quirk:
/// `resume_clear`)
ClearingResumeCaches,
/// Establish the unprivileged build identity for the rules steps
/// (rootless builds): a nested user namespace mapping the chroot's
/// root ownership onto the identity — the tree needs no chown — or,
/// when unavailable, a build user with a chown tree handover
PreparingBuildUser,
/// debian/rules build
Building,
/// fakeroot debian/rules binary
ProducingBinaries,
/// Retrieving produced .deb files
RetrievingArtifacts,
}
impl Phase {
/// Human-readable label displayed in the status bar
pub fn label(&self) -> &'static str {
match self {
Phase::PreparingChroot => "Preparing chroot",
Phase::ExtractingChroot => "Extracting chroot",
Phase::FinalizingChroot => "Finalizing chroot",
Phase::UpdatingPackageLists => "Updating package lists",
Phase::InstallingEssentials => "Installing essential packages",
Phase::ApplyingPatches => "Applying patches",
Phase::InjectingPackages => "Injecting packages",
Phase::InstallingBuildDeps => "Installing build dependencies",
Phase::ClearingResumeCaches => "Clearing resumed build caches",
Phase::PreparingBuildUser => "Preparing the build user",
Phase::Building => "Building package",
Phase::ProducingBinaries => "Producing binary packages",
Phase::RetrievingArtifacts => "Retrieving artifacts",
}
}
/// Stable machine name of the phase, as recorded in a build
/// record's profile (`pkh deb profile`); unlike [`Self::label`]
/// this is a storage key, never reworded.
pub fn key(&self) -> &'static str {
match self {
Phase::PreparingChroot => "preparing_chroot",
Phase::ExtractingChroot => "extracting_chroot",
Phase::FinalizingChroot => "finalizing_chroot",
Phase::UpdatingPackageLists => "updating_package_lists",
Phase::InstallingEssentials => "installing_essentials",
Phase::ApplyingPatches => "applying_patches",
Phase::InjectingPackages => "injecting_packages",
Phase::InstallingBuildDeps => "installing_build_deps",
Phase::ClearingResumeCaches => "clearing_resume_caches",
Phase::PreparingBuildUser => "preparing_build_user",
Phase::Building => "building",
Phase::ProducingBinaries => "producing_binaries",
Phase::RetrievingArtifacts => "retrieving_artifacts",
}
}
/// The phase `key` names, when it is one of the known machine
/// names; a key written by an older pkh whose phase has since been
/// renamed renders as its raw key.
pub fn from_key(key: &str) -> Option<Phase> {
let phase = match key {
"preparing_chroot" => Phase::PreparingChroot,
"extracting_chroot" => Phase::ExtractingChroot,
"finalizing_chroot" => Phase::FinalizingChroot,
"updating_package_lists" => Phase::UpdatingPackageLists,
"installing_essentials" => Phase::InstallingEssentials,
"applying_patches" => Phase::ApplyingPatches,
"injecting_packages" => Phase::InjectingPackages,
"installing_build_deps" => Phase::InstallingBuildDeps,
"clearing_resume_caches" => Phase::ClearingResumeCaches,
"preparing_build_user" => Phase::PreparingBuildUser,
"building" => Phase::Building,
"producing_binaries" => Phase::ProducingBinaries,
"retrieving_artifacts" => Phase::RetrievingArtifacts,
_ => return None,
};
Some(phase)
}
}
/// Default line classifier rewriting a phase's subprocess output
fn default_classifier(phase: Phase) -> Box<dyn Classifier> {
match phase {
Phase::PreparingChroot => Box::new(MmdebstrapClassifier::new()),
Phase::ExtractingChroot | Phase::FinalizingChroot => Box::new(GenericClassifier::new()),
Phase::UpdatingPackageLists => Box::new(AptUpdateClassifier::new()),
Phase::InstallingEssentials => Box::new(AptInstallClassifier::new("Installing essentials")),
Phase::ApplyingPatches => Box::new(QuiltClassifier::new(0)),
Phase::InjectingPackages => Box::new(AptInstallClassifier::new("Injecting packages")),
Phase::InstallingBuildDeps => {
Box::new(AptInstallClassifier::new("Installing build dependencies"))
}
Phase::ClearingResumeCaches | Phase::PreparingBuildUser => {
Box::new(GenericClassifier::new())
}
Phase::Building | Phase::ProducingBinaries => Box::new(MakeClassifier::new()),
Phase::RetrievingArtifacts => Box::new(GenericClassifier::new()),
}
}
/// Records the wall-clock duration of every [`Phase`] the build flow
/// announces: one profile entry per executed phase, in execution
/// order. A phase closes when the next one is entered or at
/// [`BuildProfiler::finish`] — so a failed build's profile ends at the
/// phase the error propagated out of, its entry covering the phase
/// until the failure. Conditional phases that never ran (the chroot
/// tarball was cached, the device nodes were ready) record nothing.
pub(crate) struct BuildProfiler {
/// The phase being timed, since when.
open: Option<(Phase, std::time::Instant)>,
/// Closed phases, in execution order.
phases: Vec<session::PhaseTiming>,
}
impl BuildProfiler {
/// A profiler with nothing recorded yet.
pub(crate) fn new() -> Self {
Self {
open: None,
phases: Vec::new(),
}
}
/// Close the phase being timed (if any) and open `phase`.
pub(crate) fn enter(&mut self, phase: Phase) {
self.close_open();
self.open = Some((phase, std::time::Instant::now()));
}
/// Close the open phase and return the recorded profile.
pub(crate) fn finish(&mut self) -> session::BuildProfile {
self.close_open();
session::BuildProfile {
phases: std::mem::take(&mut self.phases),
}
}
fn close_open(&mut self) {
if let Some((phase, started)) = self.open.take() {
self.phases.push(session::PhaseTiming {
phase: phase.key().to_string(),
millis: started.elapsed().as_millis() as u64,
});
}
}
}
/// Enter `phase` on the view with its default line classifier, timing
/// it into `profiler`
pub(crate) fn enter_phase(view: &dyn BuildView, phase: Phase, profiler: &mut BuildProfiler) {
profiler.enter(phase);
view.phase(phase.label(), default_classifier(phase));
}
/// Parameters of one [`build_binary_package`] call.
pub struct DebBuildOptions<'a> {
/// Target architecture; defaults to the host architecture.
@@ -1174,61 +999,6 @@ mod tests {
assert!(!is_archive_source_pinned(mirror, None));
}
/// Every phase's machine key round-trips through `Phase::from_key`:
/// the profile renderer relies on it to show a recorded entry as
/// the phase's human label, and a key/from_key drift would
/// downgrade the row to the raw key.
#[test]
fn phase_keys_round_trip() {
for phase in [
Phase::PreparingChroot,
Phase::ExtractingChroot,
Phase::FinalizingChroot,
Phase::UpdatingPackageLists,
Phase::InstallingEssentials,
Phase::ApplyingPatches,
Phase::InjectingPackages,
Phase::InstallingBuildDeps,
Phase::ClearingResumeCaches,
Phase::PreparingBuildUser,
Phase::Building,
Phase::ProducingBinaries,
Phase::RetrievingArtifacts,
] {
assert_eq!(Phase::from_key(phase.key()), Some(phase), "{}", phase.key());
assert_ne!(phase.key(), "", "{}", phase.key());
}
assert_eq!(Phase::from_key("nonesuch"), None);
}
/// The profiler closes each phase when the next opens and the last
/// one at `finish`, recording entries in execution order; an empty
/// run records nothing (the record must not claim an empty
/// profile), and a phase never entered leaves no entry.
#[test]
fn profiler_records_phases_in_execution_order() {
let mut profiler = BuildProfiler::new();
assert!(profiler.finish().is_empty());
let mut profiler = BuildProfiler::new();
profiler.enter(Phase::PreparingChroot);
std::thread::sleep(std::time::Duration::from_millis(5));
profiler.enter(Phase::UpdatingPackageLists);
std::thread::sleep(std::time::Duration::from_millis(2));
profiler.enter(Phase::Building);
std::thread::sleep(std::time::Duration::from_millis(2));
let profile = profiler.finish();
let phases: Vec<&str> = profile.phases.iter().map(|p| p.phase.as_str()).collect();
assert_eq!(
phases,
vec!["preparing_chroot", "updating_package_lists", "building"]
);
assert!(profile.total_millis() >= 9, "{profile:?}");
// finish() closes the last phase: nothing stays open afterwards.
assert!(profiler.finish().is_empty());
}
/// An unshare sandbox mapped over `chroot_root` on a host base:
/// exists/list_files/is_dir answer through the path mapping, no
/// namespace privileges needed.