//! Deterministic source-versus-binary package policy. //! //! The rule is: //! * cheap compiles are done from source; //! * packages where local compilation or USE customisation matters are //! compiled; //! * expensive packages with negligible practical benefit use a signed //! Gentoo binary when one is available. //! //! Decisions are deterministic and never consult a model. Measured compile //! times can override the built-in estimates. use std::collections::{HashMap, HashSet}; use serde::{Deserialize, Serialize}; use crate::config::PackagePolicy; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum InstallMethod { Source, Binary, } impl InstallMethod { pub fn as_str(self) -> &'static str { match self { InstallMethod::Source => "source", InstallMethod::Binary => "binary", } } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum PackageDecisionReason { CheapCompile, RequiredUseFlags, NativeOptimizationBenefit, HighBuildCostLowBenefit, BinaryUnavailable, UserPreference, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] pub struct PackageDecision { pub method: InstallMethod, pub reason: PackageDecisionReason, pub estimated_seconds: u64, } pub struct PolicyContext { pub policy: PackagePolicy, pub binhost_available: bool, pub memory_mib: u64, pub cores: u32, } pub struct PackagePolicyEngine { estimates: HashMap<&'static str, u64>, binary_preferred: HashSet<&'static str>, source_preferred: HashSet<&'static str>, history: HashMap, } impl Default for PackagePolicyEngine { fn default() -> Self { Self::new() } } impl PackagePolicyEngine { pub fn new() -> Self { let mut estimates = HashMap::new(); for (pkg, secs) in [ ("app-editors/vim", 45u64), ("app-misc/tmux", 20), ("app-shells/zsh", 60), ("dev-lang/python", 900), ("dev-lang/rust", 3600), ("dev-libs/glib", 120), ("dev-util/cmake", 300), ("dev-vcs/git", 120), ("media-video/ffmpeg", 900), ("net-misc/curl", 90), ("sys-apps/busybox", 60), ("sys-apps/coreutils", 120), ("sys-apps/systemd", 420), ("sys-devel/gcc", 3600), ("sys-kernel/gentoo-kernel", 1800), ("sys-kernel/gentoo-sources", 120), ("sys-libs/glibc", 900), ("www-client/firefox", 4200), ("www-client/chromium", 7200), ("app-office/libreoffice", 5400), ("dev-qt/qtbase", 900), ("kde-plasma/plasma-desktop", 600), ("net-im/discord-bin", 0), ("games-util/steam-launcher", 0), ] { estimates.insert(pkg, secs); } let binary_preferred: HashSet<&'static str> = [ "www-client/firefox", "www-client/chromium", "app-office/libreoffice", "mail-client/thunderbird", "dev-lang/rust", "dev-lang/llvm", "dev-qt/qtwebengine", "sys-devel/gcc", "sys-libs/glibc", "net-im/discord-bin", "games-util/steam-launcher", ] .into_iter() .collect(); let source_preferred: HashSet<&'static str> = [ "sys-kernel/gentoo-kernel", "sys-kernel/gentoo-sources", "sys-apps/systemd", "app-shells/zsh", "sys-apps/busybox", "sys-fs/btrfs-progs", "sys-fs/cryptsetup", ] .into_iter() .collect(); Self { estimates, binary_preferred, source_preferred, history: HashMap::new(), } } /// Install measured compile times (seconds) keyed by package atom. pub fn with_history(mut self, history: HashMap) -> Self { self.history = history; self } pub fn estimate_seconds(&self, pkg: &str) -> u64 { if let Some(secs) = self.history.get(pkg) { return *secs; } self.estimates.get(pkg).copied().unwrap_or(300) } pub fn decide(&self, pkg: &str, ctx: &PolicyContext) -> PackageDecision { let seconds = self.estimate_seconds(pkg); let binary = |reason| PackageDecision { method: InstallMethod::Binary, reason, estimated_seconds: seconds, }; let source = |reason| PackageDecision { method: InstallMethod::Source, reason, estimated_seconds: seconds, }; match ctx.policy { PackagePolicy::SourceFirst => { return source(PackageDecisionReason::UserPreference); } PackagePolicy::BinaryFirst => { if ctx.binhost_available { return binary(PackageDecisionReason::UserPreference); } return source(PackageDecisionReason::BinaryUnavailable); } PackagePolicy::Balanced => {} } if self.binary_preferred.contains(pkg) && ctx.binhost_available { return binary(PackageDecisionReason::HighBuildCostLowBenefit); } if self.source_preferred.contains(pkg) { return source(PackageDecisionReason::NativeOptimizationBenefit); } if seconds <= 120 { return source(PackageDecisionReason::CheapCompile); } if ctx.binhost_available { return binary(PackageDecisionReason::HighBuildCostLowBenefit); } source(PackageDecisionReason::BinaryUnavailable) } /// Extra `emerge` flags for a package, based on its decision. pub fn emerge_args(&self, pkg: &str, ctx: &PolicyContext) -> Vec { match self.decide(pkg, ctx).method { InstallMethod::Binary => { vec!["--getbinpkg".into(), "--binpkg-respect-use=y".into()] } InstallMethod::Source => Vec::new(), } } } /// Read a measured compile-time history file. /// /// Format: `category/packagesecondsupdates-per-year`. pub fn load_history(path: &std::path::Path) -> HashMap { let mut out = HashMap::new(); let Ok(text) = std::fs::read_to_string(path) else { return out; }; for line in text.lines() { let line = line.trim(); if line.is_empty() || line.starts_with('#') { continue; } let mut parts = line.split('\t'); let (Some(pkg), Some(secs)) = (parts.next(), parts.next()) else { continue; }; if let Ok(secs) = secs.trim().parse::() { out.insert(pkg.trim().to_string(), secs); } } out } /// Write a measured compile-time history file, replacing its contents. pub fn save_history(path: &std::path::Path, history: &HashMap) -> std::io::Result<()> { if let Some(parent) = path.parent() { std::fs::create_dir_all(parent)?; } let mut keys: Vec<&String> = history.keys().collect(); keys.sort(); let mut out = String::new(); out.push_str("# Phoebus measured compile times (seconds) and update frequency.\n"); out.push_str("# atom\tseconds\tupdates-per-year\n"); for key in keys { out.push_str(&format!("{key}\t{}\t6\n", history[key])); } std::fs::write(path, out) } /// Append a measured compile time to the history file. pub fn append_history( path: &std::path::Path, pkg: &str, seconds: u64, updates_per_year: u64, ) -> std::io::Result<()> { use std::io::Write; if let Some(parent) = path.parent() { std::fs::create_dir_all(parent)?; } let mut file = std::fs::OpenOptions::new() .create(true) .append(true) .open(path)?; writeln!(file, "{pkg}\t{seconds}\t{updates_per_year}") } #[cfg(test)] mod tests { use super::*; fn ctx(policy: PackagePolicy) -> PolicyContext { PolicyContext { policy, binhost_available: true, memory_mib: 8192, cores: 8, } } #[test] fn balanced_compiles_cheap_and_binaries_expensive() { let e = PackagePolicyEngine::new(); let c = ctx(PackagePolicy::Balanced); assert_eq!( e.decide("app-editors/vim", &c).method, InstallMethod::Source ); assert_eq!( e.decide("www-client/firefox", &c).method, InstallMethod::Binary ); assert_eq!( e.decide("sys-kernel/gentoo-kernel", &c).method, InstallMethod::Source ); } #[test] fn source_first_always_compiles() { let e = PackagePolicyEngine::new(); let c = ctx(PackagePolicy::SourceFirst); assert_eq!( e.decide("www-client/firefox", &c).method, InstallMethod::Source ); } #[test] fn binary_first_prefers_binary_but_falls_back() { let e = PackagePolicyEngine::new(); assert_eq!( e.decide("app-editors/vim", &ctx(PackagePolicy::BinaryFirst)) .method, InstallMethod::Binary ); let mut no_binhost = ctx(PackagePolicy::BinaryFirst); no_binhost.binhost_available = false; assert_eq!( e.decide("www-client/firefox", &no_binhost).method, InstallMethod::Source ); } #[test] fn history_overrides_estimate() { let mut hist = HashMap::new(); hist.insert("app-editors/vim".to_string(), 7u64); let e = PackagePolicyEngine::new().with_history(hist); assert_eq!(e.estimate_seconds("app-editors/vim"), 7); } #[test] fn unknown_package_defaults_to_300s() { let e = PackagePolicyEngine::new(); assert_eq!(e.estimate_seconds("dev-libs/unknown"), 300); } #[test] fn emerge_args_match_method() { let e = PackagePolicyEngine::new(); let c = ctx(PackagePolicy::Balanced); assert!(e .emerge_args("www-client/firefox", &c) .contains(&"--getbinpkg".to_string())); assert!(e.emerge_args("app-editors/vim", &c).is_empty()); } #[test] fn history_file_roundtrip() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("compile-times.tsv"); append_history(&path, "app-editors/vim", 7, 25).unwrap(); let hist = load_history(&path); assert_eq!(hist.get("app-editors/vim"), Some(&7)); } }