difftreelog
feat derivation graph to spans
in: trunk
6 files changed
cmds/fleet/Cargo.tomldiffbeforeafterboth--- a/cmds/fleet/Cargo.toml
+++ b/cmds/fleet/Cargo.toml
@@ -50,7 +50,7 @@
tracing-opentelemetry.workspace = true
[features]
-default = []
+default = ["indicatif"]
# Not quite stable
indicatif = [
"dep:tracing-indicatif",
cmds/fleet/src/main.rsdiffbeforeafterboth--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -120,7 +120,8 @@
let indicatif_layer = {
use std::time::Duration;
- IndicatifLayer::new().with_progress_style(
+ IndicatifLayer::new().with_max_progress_bars(10, Some(ProgressStyle::default_spinner()))
+ .with_progress_style(
ProgressStyle::with_template(
"{color_start}{span_child_prefix} {span_name}{{{span_fields}}}{color_end} {wide_msg} {color_start}{download_progress} {elapsed}{color_end}",
)
crates/nix-eval/build.rsdiffbeforeafterboth--- a/crates/nix-eval/build.rs
+++ b/crates/nix-eval/build.rs
@@ -18,6 +18,7 @@
"nix-util",
"nix-util-c",
"nix-store",
+ "nix-store-c",
"nix-expr",
"nix-flake",
"nix-fetchers",
@@ -72,6 +73,12 @@
.include_paths
.into_iter()
.chain(
+ pkg_config::probe_library("nix-store-c")
+ .expect("nix-store-c")
+ .include_paths
+ .into_iter(),
+ )
+ .chain(
pkg_config::probe_library("nix-flake-c")
.expect("nix-flake-c")
.include_paths
crates/nix-eval/src/drv.rsdiffbeforeafterboth1use std::collections::{HashMap, HashSet, VecDeque};2use std::ffi::CString;34use anyhow::{Result, bail};5use serde::Deserialize;67use crate::nix_raw::{derivation_free, derivation_to_json, store_drv_from_store_path};8use crate::{copy_nix_str, with_store_context};910fn store_dir() -> Result<String> {11 let mut out = String::new();12 with_store_context(|c, store, _| unsafe {13 crate::nix_raw::store_get_storedir(c, store, Some(copy_nix_str), (&raw mut out).cast())14 })?;15 Ok(out)16}1718fn to_absolute_store_path(store_dir: &str, path: &str) -> String {19 if path.starts_with('/') {20 path.to_owned()21 } else {22 format!("{store_dir}/{path}")23 }24}2526pub struct Derivation(*mut crate::nix_raw::derivation);27unsafe impl Send for Derivation {}2829impl Derivation {30 pub fn from_path(drv_path: &str) -> Result<Self> {31 let path_c = CString::new(drv_path)?;32 let store_path = with_store_context(|c, store, _| unsafe {33 crate::nix_raw::store_parse_path(c, store, path_c.as_ptr())34 })?;35 let drv = with_store_context(|c, store, _| unsafe {36 store_drv_from_store_path(c, store, store_path)37 });38 unsafe { crate::nix_raw::store_path_free(store_path) };39 let drv = drv?;40 if drv.is_null() {41 bail!("failed to read derivation from {drv_path}");42 }43 Ok(Self(drv))44 }4546 pub fn to_json_string(&self) -> Result<String> {47 let mut out = String::new();48 with_store_context(|c, _, _| unsafe {49 derivation_to_json(c, self.0, Some(copy_nix_str), (&raw mut out).cast())50 })?;51 Ok(out)52 }5354 pub fn parsed(&self) -> Result<DrvParsed> {55 let s = self.to_json_string()?;56 Ok(serde_json::from_str(&s)?)57 }58}5960impl Drop for Derivation {61 fn drop(&mut self) {62 unsafe { derivation_free(self.0) };63 }64}6566#[derive(Debug, Deserialize)]67pub struct DrvParsed {68 pub inputs: DrvInputs,69 pub outputs: HashMap<String, serde_json::Value>,70}7172#[derive(Debug, Deserialize)]73pub struct DrvInputs {74 #[serde(default)]75 pub srcs: Vec<String>,76 #[serde(default)]77 pub drvs: HashMap<String, DrvInputEntry>,78}7980#[derive(Debug, Deserialize)]81pub struct DrvInputEntry {82 pub outputs: Vec<String>,83}8485#[derive(Debug)]86pub struct DrvGraph {87 pub root: String,88 pub nodes: HashMap<String, DrvNode>,89}9091#[derive(Debug)]92pub struct DrvNode {93 pub name: String,94 pub input_drvs: HashMap<String, Vec<String>>,95 pub input_srcs: Vec<String>,96 pub outputs: Vec<String>,97}9899impl DrvGraph {100 pub fn resolve(drv_path: &str) -> Result<Self> {101 let sd = store_dir()?;102 let root = to_absolute_store_path(&sd, drv_path);103104 let mut nodes = HashMap::new();105 let mut queue = VecDeque::new();106 let mut visited = HashSet::new();107 queue.push_back(root.clone());108 visited.insert(root.clone());109110 while let Some(path) = queue.pop_front() {111 let drv = Derivation::from_path(&path)?;112 let parsed = drv.parsed()?;113114 let input_drvs: HashMap<String, Vec<String>> = parsed115 .inputs116 .drvs117 .into_iter()118 .map(|(k, v)| (to_absolute_store_path(&sd, &k), v.outputs))119 .collect();120121 for dep_path in input_drvs.keys() {122 if visited.insert(dep_path.clone()) {123 queue.push_back(dep_path.clone());124 }125 }126127 nodes.insert(128 path.clone(),129 DrvNode {130 name: extract_drv_name(&path),131 input_drvs,132 input_srcs: parsed.inputs.srcs,133 outputs: parsed.outputs.into_keys().collect(),134 },135 );136 }137138 Ok(Self { root, nodes })139 }140}141142fn extract_drv_name(drv_path: &str) -> String {143 drv_path144 .rsplit('/')145 .next()146 .and_then(|f| f.strip_suffix(".drv"))147 .and_then(|f| f.split_once('-').map(|(_, name)| name))148 .unwrap_or(drv_path)149 .to_owned()150}crates/nix-eval/src/lib.rsdiffbeforeafterboth--- a/crates/nix-eval/src/lib.rs
+++ b/crates/nix-eval/src/lib.rs
@@ -13,7 +13,7 @@
use std::mem::transmute;
pub use anyhow::Result;
-use tracing::{Instrument, info, instrument, warn};
+use tracing::{Span, instrument, warn};
use self::logging::{ErrorInfoBuilder, nix_logging_cxx};
use self::nix_cxx::set_fetcher_setting;
@@ -26,8 +26,9 @@
clear_err, copy_value, err_NIX_ERR_KEY, err_NIX_ERR_NIX_ERROR, err_NIX_ERR_OVERFLOW,
err_NIX_ERR_UNKNOWN, err_code, err_info_msg, err_msg, eval_state_build,
eval_state_builder_load, eval_state_builder_new, eval_state_builder_set_eval_setting,
- expr_eval_from_string, fetchers_settings, fetchers_settings_free, fetchers_settings_new,
- flake_lock, flake_lock_flags, flake_lock_flags_free, flake_lock_flags_new, flake_reference,
+ expr_eval_from_string, fetchers_settings,
+ fetchers_settings_free, fetchers_settings_new, flake_lock, flake_lock_flags,
+ flake_lock_flags_free, flake_lock_flags_new, flake_reference,
flake_reference_and_fragment_from_string, flake_reference_parse_flags,
flake_reference_parse_flags_free, flake_reference_parse_flags_new,
flake_reference_parse_flags_set_base_directory, flake_settings, flake_settings_free,
@@ -43,6 +44,7 @@
};
// Contains macros helpers
+pub mod drv;
pub mod logging;
#[doc(hidden)]
pub mod macros;
@@ -321,7 +323,7 @@
thread_local! {
static THREAD_STATE: RefCell<ThreadState> = RefCell::new(ThreadState::new().expect("thread state init shouldn't fail"));
}
-fn with_default_context<T>(f: impl FnOnce(*mut c_context, *mut c_eval_state) -> T) -> Result<T> {
+pub(crate) fn with_default_context<T>(f: impl FnOnce(*mut c_context, *mut c_eval_state) -> T) -> Result<T> {
let global = &GLOBAL_STATE.state;
let (ctx, state) = THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.0));
let mut ctx = NixContext(ctx);
@@ -331,6 +333,20 @@
v
}
+/// Same as with_default_context, but also passes store...
+/// Yep, this code is garbage and needs to be refactored.
+pub(crate) fn with_store_context<T>(
+ f: impl FnOnce(*mut c_context, *mut c_store, *mut c_eval_state) -> T,
+) -> Result<T> {
+ let global = &GLOBAL_STATE;
+ let (ctx, store, state) =
+ THREAD_STATE.with_borrow_mut(|w| (w.ctx.0, global.store.0, global.state.0));
+ let mut ctx = NixContext(ctx);
+ let v = ctx.run_in_context(|c| f(c, store, state));
+ std::mem::forget(ctx);
+ v
+}
+
pub fn set_setting(s: &CStr, v: &CStr) -> Result<()> {
with_default_context(|c, _| unsafe { setting_set(c, s.as_ptr(), v.as_ptr()) }).map(|_| ())
}
@@ -423,7 +439,7 @@
}
}
-unsafe extern "C" fn copy_nix_str(start: *const c_char, n: c_uint, user_data: *mut c_void) {
+pub(crate) unsafe extern "C" fn copy_nix_str(start: *const c_char, n: c_uint, user_data: *mut c_void) {
let s = unsafe { slice::from_raw_parts(start.cast::<u8>(), n as usize) };
let s = std::str::from_utf8(s).expect("c string has invalid utf-8");
unsafe { *user_data.cast::<String>() = s.to_owned() };
@@ -836,6 +852,7 @@
})?;
Ok(out)
}
+ #[instrument(name = "build", skip(self), fields(output))]
pub fn build(&self, output: &str) -> Result<PathBuf> {
if !self.is_derivation() {
bail!("expected derivation to build")
@@ -853,11 +870,19 @@
} else {
self.clone()
};
+
+ let drv_path = v
+ .get_field("drvPath")
+ .context("getting drvPath")?
+ .to_string()?;
+ let graph = drv::DrvGraph::resolve(&drv_path)?;
+ let _guard = logging::register_build_graph(&Span::current(), &graph);
+
// to_string here blocks until the path is built
let s = v.builtin_to_string()?;
let rs = s.to_realised_string()?;
- let drv_path = rs.as_str().to_owned();
- Ok(PathBuf::from(drv_path))
+ let out_path = rs.as_str().to_owned();
+ Ok(PathBuf::from(out_path))
}
pub fn as_json<T: DeserializeOwned>(&self) -> Result<T> {
let to_json = Self::eval("builtins.toJSON")?;
@@ -1102,11 +1127,19 @@
assert_eq!(test_result, "PREFIX_BODY_SUFFIX");
let test_result: String = nix_go_json!(builtins.uppercaseSuffix2("test")("suffix"));
assert_eq!(test_result, "TESTsuffix");
-
- let nix_ctx = NixContext::new();
- let store = GLOBAL_STATE.store.parse_path(s.as_c_str())?;
- // nix_raw::store_get_fs_closure(1);
+ let drv_path = nix_go!(attrs.packages["x86_64-linux"]["fleet-install-secrets"].drvPath)
+ .to_string()?;
+ let graph = drv::DrvGraph::resolve(&drv_path)?;
+ eprintln!(
+ "fleet-install-secrets dependency graph: {} nodes",
+ graph.nodes.len()
+ );
+ for (path, node) in &graph.nodes {
+ if !node.input_drvs.is_empty() {
+ eprintln!(" {} ({} deps)", node.name, node.input_drvs.len());
+ }
+ }
Ok(())
}
crates/nix-eval/src/logging.rsdiffbeforeafterboth--- a/crates/nix-eval/src/logging.rs
+++ b/crates/nix-eval/src/logging.rs
@@ -1,4 +1,4 @@
-use std::collections::HashMap;
+use std::collections::{HashMap, VecDeque};
use std::fmt::Arguments;
use std::sync::{LazyLock, Mutex};
@@ -285,6 +285,135 @@
static NIX_SPAN_MAPPING: LazyLock<Mutex<HashMap<u64, Span>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
+struct DrvGraphEntry {
+ name: String,
+ parent: Option<String>,
+ span: Option<Span>,
+ refcount: usize,
+}
+
+static DRV_GRAPH: LazyLock<Mutex<HashMap<String, DrvGraphEntry>>> =
+ LazyLock::new(|| Mutex::new(HashMap::new()));
+
+static ACTIVITY_TO_DRV: LazyLock<Mutex<HashMap<u64, String>>> =
+ LazyLock::new(|| Mutex::new(HashMap::new()));
+
+pub struct BuildGraphGuard {
+ paths: Vec<String>,
+}
+
+impl Drop for BuildGraphGuard {
+ fn drop(&mut self) {
+ let mut drv_graph = DRV_GRAPH.lock().expect("not poisoned");
+ for path in &self.paths {
+ if let Some(entry) = drv_graph.get_mut(path) {
+ entry.refcount -= 1;
+ if entry.refcount == 0 {
+ drv_graph.remove(path);
+ }
+ }
+ }
+ }
+}
+
+pub fn register_build_graph(parent: &Span, graph: &crate::drv::DrvGraph) -> BuildGraphGuard {
+ let mut drv_graph = DRV_GRAPH.lock().expect("not poisoned");
+ let mut paths = Vec::new();
+
+ drv_graph
+ .entry(graph.root.clone())
+ .and_modify(|e| e.refcount += 1)
+ .or_insert_with(|| DrvGraphEntry {
+ name: graph.nodes[&graph.root].name.clone(),
+ parent: None,
+ span: Some(parent.clone()),
+ refcount: 1,
+ });
+ paths.push(graph.root.clone());
+
+ let mut queue = VecDeque::new();
+ queue.push_back(graph.root.clone());
+
+ let mut visited = std::collections::HashSet::new();
+ visited.insert(graph.root.clone());
+
+ while let Some(path) = queue.pop_front() {
+ let Some(node) = graph.nodes.get(&path) else {
+ continue;
+ };
+ for dep_path in node.input_drvs.keys() {
+ if !visited.insert(dep_path.clone()) {
+ continue;
+ }
+ let Some(dep_node) = graph.nodes.get(dep_path) else {
+ continue;
+ };
+ if let Some(entry) = drv_graph.get_mut(dep_path) {
+ entry.refcount += 1;
+ } else {
+ drv_graph.insert(dep_path.clone(), DrvGraphEntry {
+ name: dep_node.name.clone(),
+ parent: Some(path.clone()),
+ span: None,
+ refcount: 1,
+ });
+ }
+ paths.push(dep_path.clone());
+ queue.push_back(dep_path.clone());
+ }
+ }
+
+ BuildGraphGuard { paths }
+}
+
+fn ensure_drv_span(drv_path: &str) -> Option<Span> {
+ let mut drv_graph = DRV_GRAPH.lock().expect("not poisoned");
+
+ if let Some(span) = drv_graph.get(drv_path).and_then(|e| e.span.clone()) {
+ return Some(span);
+ }
+
+ let mut chain = vec![];
+ let mut current = drv_path.to_owned();
+ loop {
+ let Some(entry) = drv_graph.get(¤t) else {
+ break;
+ };
+ if entry.span.is_some() {
+ chain.push(current);
+ break;
+ }
+ chain.push(current.clone());
+ match &entry.parent {
+ Some(p) => current = p.clone(),
+ None => break,
+ }
+ }
+
+ if chain.is_empty() {
+ return None;
+ }
+
+ for i in (0..chain.len()).rev() {
+ let path = &chain[i];
+ if drv_graph.get(path).unwrap().span.is_some() {
+ continue;
+ }
+ let parent_span = chain
+ .get(i + 1)
+ .and_then(|p| drv_graph.get(p))
+ .and_then(|e| e.span.clone());
+ let name = drv_graph.get(path).unwrap().name.clone();
+ let span = {
+ let _enter = parent_span.as_ref().map(|s| s.enter());
+ info_span!(target: "nix::build", "building", drv = %name)
+ };
+ drv_graph.get_mut(path).unwrap().span = Some(span);
+ }
+
+ drv_graph.get(drv_path).and_then(|e| e.span.clone())
+}
+
#[derive(Debug)]
enum FieldValue {
Int(i32),
@@ -306,57 +435,33 @@
self.fields.push(FieldValue::Str(v.to_string()));
}
fn emit(&mut self, parent: u64, s: &str) {
- let mut mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");
-
- let parent = mapping.get(&parent);
+ let graph_span = if matches!(self.typ, ActivityType::Build) {
+ self.fields.first().and_then(|f| match f {
+ FieldValue::Str(drv_path) => {
+ let clean = parse_path(drv_path);
+ let span = ensure_drv_span(clean);
+ if span.is_some() {
+ ACTIVITY_TO_DRV
+ .lock()
+ .expect("not poisoned")
+ .insert(self.activity_id, clean.to_owned());
+ }
+ span
+ }
+ _ => None,
+ })
+ } else {
+ None
+ };
- // let meta = spans.alloc_metadata(
- // self.typ.name(),
- // self.verbosity.into(),
- // MetadataKind::Span,
- // "nix activity start",
- // None,
- // None,
- // None,
- // self.typ.fields(),
- // );
- //
- // let mut fields = meta.fields().iter();
- // let span = if let Some(parent) = parent {
- // let s = Span::new(
- // meta,
- // &match meta.fields().len() {
- // 1 => meta.fields().value_set(
- // &<[_; 1]>::try_from([(
- // &fields.next().expect("has field"),
- // Some(&format_args!("Test") as &dyn tracing::Value),
- // )])
- // .expect("valid size"),
- // ),
- // _ => unreachable!(),
- // },
- // );
- // s.follows_from(parent);
- // s
- // } else {
- // Span::new_root(
- // meta,
- // &match meta.fields().len() {
- // 1 => meta.fields().value_set(
- // &<[_; 1]>::try_from([(
- // &fields.next().expect("has field"),
- // Some(&format_args!("Test") as &dyn tracing::Value),
- // )])
- // .expect("valid size"),
- // ),
- // _ => unreachable!(),
- // },
- // )
- // };
- //
- // let id = span.id().expect("id created");
+ let mut mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");
- let span = {
+ let span = if let Some(span) = graph_span {
+ #[cfg(feature = "indicatif")]
+ span.pb_start();
+ span
+ } else {
+ let parent = mapping.get(&parent);
let _in_parent = parent.map(|p| p.enter());
let level: Level = self.verbosity.into();
if level == Level::ERROR {
@@ -380,7 +485,7 @@
let s = ansi_filter(s);
#[cfg(feature = "indicatif")]
{
- span.pb_set_message(s);
+ span.pb_set_message(&s);
}
let _e = span.enter();
let level: Level = self.verbosity.into();
@@ -454,8 +559,15 @@
warn!(target: "nix::eval", "{v}")
}
fn emit_stop(v: u64) {
- let mut mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");
- mapping.remove(&v);
+ {
+ let mut mapping = NIX_SPAN_MAPPING.lock().expect("not poisoned");
+ mapping.remove(&v);
+ }
+ if let Some(drv_path) = ACTIVITY_TO_DRV.lock().expect("not poisoned").remove(&v) {
+ if let Some(entry) = DRV_GRAPH.lock().expect("not poisoned").get_mut(&drv_path) {
+ entry.span = None;
+ }
+ }
}
fn emit_log(lvl: u32, v: &[u8]) {
let verbosity = Verbosity::from_int(lvl);