Unverified Commit 24e8c18a authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[turbopack] Store TaskDirtyCause in Dirtyness and pass to NativeFunction::span (#94057)

### What?

Track the cause of task invalidation in turbo-tasks and surface it to
the tracing span when a task executes, so traces can show *why* a task
was re-run.

A new `task_dirty_cause` Cargo feature gates the storage and parameter
plumbing for this information. The existing `trace_task_dirty` feature
now depends on it.

### Why?

When investigating Turbopack performance and incremental-recompute
behavior, traces show which tasks ran but not what invalidated them.
Recording the `TaskDirtyCause` on the `turbo_tasks::function` span makes
it possible to attribute task re-runs to their triggering change (cell
update, output change, collectible change, etc.) directly from a trace.

### How?

- Replace `Dirtyness::Dirty(TaskPriority)` with a struct variant `Dirty
{ parent_priority, cause: TaskDirtyCause }`. The `cause` field is only
present when the `task_dirty_cause` feature is enabled.
- Move `TaskDirtyCause` from `turbo-tasks-backend` into `turbo-tasks` so
the span signature in `turbo-tasks` can reference it.
- Replace `OutputChange { task_description }` with `OutputChange {
function: FunctionId }`, and add a new `ResolveOutputChange { function:
FunctionId }` variant used when the task's output is
`OutputValue::Output(_)`. This avoids constructing a `String` for the
cause at invalidation time.
- `NativeFunction::span` now accepts a feature-gated `cause:
Option<&TaskDirtyCause>` argument and records it as a `cause` field on
the `turbo_tasks::function` span. The cause is read from the dirty state
in `try_start_task_execution` before execution starts.
- Split the `trace_task_dirty` feature: the new `task_dirty_cause`
feature owns the storage/parameter plumbing, and `trace_task_dirty` now
depends on it. This lets consumers opt into the storage cost without the
full tracing overhead, and keeps the default build unaffected.

Closes PACK-
<!-- NEXT_JS_LLM_PR -->
parent 859c4edc
......@@ -22,6 +22,7 @@ verify_serialization = []
verify_aggregation_graph = []
verify_immutable = []
verify_determinism = ["turbo-tasks/verify_determinism"]
task_dirty_cause = ["turbo-tasks/task_dirty_cause"]
trace_aggregation_update_stats = []
trace_aggregation_update_queue = []
trace_aggregation_update = ["trace_aggregation_update_queue"]
......@@ -29,7 +30,7 @@ trace_leaf_distance_update = []
trace_find_and_schedule = ["trace_aggregation_update_queue"]
trace_task_completion = []
trace_task_modification = []
trace_task_dirty = ["trace_aggregation_update_queue"]
trace_task_dirty = ["trace_aggregation_update_queue", "task_dirty_cause"]
trace_task_output_dependencies = []
trace_task_details = []
trace_prepare_tasks = []
......
......@@ -48,13 +48,13 @@ use turbo_tasks::{
trace::TraceRawVcs,
util::{IdFactoryWithReuse, good_chunk_size, into_chunks},
};
#[cfg(feature = "task_dirty_cause")]
use turbo_tasks::{FunctionId, TaskDirtyCause};
pub use self::{
operation::AnyOperation,
storage::{EvictionCounts, SpecificTaskDataCategory, TaskDataCategory},
};
#[cfg(feature = "trace_task_dirty")]
use crate::backend::operation::TaskDirtyCause;
use crate::{
backend::{
operation::{
......@@ -424,6 +424,8 @@ struct TaskExecutionCompletePrepareResult {
pub is_now_immutable: bool,
#[cfg(feature = "verify_determinism")]
pub no_output_set: bool,
#[cfg(feature = "task_dirty_cause")]
pub function_id: Option<FunctionId>,
pub new_output: Option<OutputValue>,
pub output_dependent_tasks: SmallVec<[TaskId; 4]>,
pub is_recomputation: bool,
......@@ -1700,7 +1702,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
}
operation::InvalidateOperation::run(
smallvec![task_id],
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
TaskDirtyCause::Invalidator,
self.execute_context(turbo_tasks),
);
......@@ -1716,7 +1718,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
}
operation::InvalidateOperation::run(
tasks.iter().copied().collect(),
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
TaskDirtyCause::Unknown,
self.execute_context(turbo_tasks),
);
......@@ -1732,7 +1734,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
}
operation::InvalidateOperation::run(
tasks.iter().copied().collect(),
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
TaskDirtyCause::Unknown,
self.execute_context(turbo_tasks),
);
......@@ -1841,6 +1843,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
) -> Option<TaskExecutionSpec<'_>> {
let execution_reason;
let task_type;
#[cfg(feature = "task_dirty_cause")]
let cause;
{
let mut ctx = self.execute_context(turbo_tasks);
let mut task = ctx.task(task_id, TaskDataCategory::All);
......@@ -1858,6 +1862,13 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
return None;
};
execution_reason = reason;
#[cfg(feature = "task_dirty_cause")]
{
cause = match task.get_dirty() {
Some(Dirtyness::Dirty { cause, .. }) => Some(cause.clone()),
_ => None,
};
}
let old = task.set_in_progress(InProgressState::InProgress(Box::new(
InProgressStateInner {
stale: false,
......@@ -1916,7 +1927,13 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
arg,
} = &*task_type;
(
native_fn.span(task_id.persistence(), execution_reason, priority),
native_fn.span(
task_id.persistence(),
execution_reason,
priority,
#[cfg(feature = "task_dirty_cause")]
cause.as_ref(),
),
native_fn.execute(*this, &**arg),
)
}
......@@ -1991,6 +2008,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
#[cfg(feature = "verify_determinism")]
no_output_set,
new_output,
#[cfg(feature = "task_dirty_cause")]
function_id,
output_dependent_tasks,
is_recomputation,
is_session_dependent,
......@@ -2027,6 +2046,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
self.task_execution_completed_invalidate_output_dependent(
&mut ctx,
task_id,
#[cfg(feature = "task_dirty_cause")]
function_id,
output_dependent_tasks,
);
}
......@@ -2104,6 +2125,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
is_now_immutable: false,
#[cfg(feature = "verify_determinism")]
no_output_set: false,
#[cfg(feature = "task_dirty_cause")]
function_id: None,
new_output: None,
output_dependent_tasks: Default::default(),
is_recomputation,
......@@ -2158,6 +2181,15 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
// take the children from the task to process them
let mut new_children = take(new_children);
// get the function id for the dirty cause
#[cfg(feature = "task_dirty_cause")]
let function_id = match task.get_task_type() {
TaskTypeRef::Cached(task_type) => {
Some(turbo_tasks::registry::get_function_id(task_type.native_fn))
}
TaskTypeRef::Transient(_) => None,
};
// handle stateful (only tracked when verify_determinism is enabled)
#[cfg(feature = "verify_determinism")]
if stateful {
......@@ -2353,6 +2385,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
is_now_immutable,
#[cfg(feature = "verify_determinism")]
no_output_set,
#[cfg(feature = "task_dirty_cause")]
function_id,
new_output,
output_dependent_tasks,
is_recomputation,
......@@ -2364,12 +2398,16 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
&self,
ctx: &mut impl ExecuteContext<'_>,
task_id: TaskId,
#[cfg(feature = "task_dirty_cause")] function_id: Option<FunctionId>,
output_dependent_tasks: SmallVec<[TaskId; 4]>,
) {
debug_assert!(!output_dependent_tasks.is_empty());
#[cfg(feature = "trace_task_dirty")]
let task_description = self.debug_get_task_description(task_id);
#[cfg(feature = "task_dirty_cause")]
let cause = match function_id {
Some(function) => TaskDirtyCause::OutputChange { function },
None => TaskDirtyCause::RootOutputChange,
};
if output_dependent_tasks.len() > 1 {
ctx.prepare_tasks(
......@@ -2383,7 +2421,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
fn process_output_dependents(
ctx: &mut impl ExecuteContext<'_>,
task_id: TaskId,
#[cfg(feature = "trace_task_dirty")] task_description: &str,
#[cfg(feature = "task_dirty_cause")] cause: &TaskDirtyCause,
dependent_task_id: TaskId,
queue: &mut AggregationUpdateQueue,
) {
......@@ -2423,10 +2461,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
dependent,
dependent_task_id,
make_stale,
#[cfg(feature = "trace_task_dirty")]
TaskDirtyCause::OutputChange {
task_description: task_description.to_string(),
},
#[cfg(feature = "task_dirty_cause")]
cause.clone(),
queue,
ctx,
);
......@@ -2440,8 +2476,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let _ = scope_and_block(chunks.len(), |scope| {
for chunk in chunks {
let child_ctx = ctx.child_context();
#[cfg(feature = "trace_task_dirty")]
let task_description = &task_description;
#[cfg(feature = "task_dirty_cause")]
let cause = &cause;
scope.spawn(move || {
let mut ctx = child_ctx.create();
let mut queue = AggregationUpdateQueue::new();
......@@ -2449,8 +2485,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
process_output_dependents(
&mut ctx,
task_id,
#[cfg(feature = "trace_task_dirty")]
task_description,
#[cfg(feature = "task_dirty_cause")]
cause,
dependent_task_id,
&mut queue,
)
......@@ -2465,8 +2501,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
process_output_dependents(
ctx,
task_id,
#[cfg(feature = "trace_task_dirty")]
&task_description,
#[cfg(feature = "task_dirty_cause")]
&cause,
dependent_task_id,
&mut queue,
);
......@@ -2493,7 +2529,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
child_task,
child_id,
false,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
TaskDirtyCause::InitialDirty,
&mut queue,
ctx,
......
......@@ -22,10 +22,10 @@ use tracing::span::Span;
feature = "trace_find_and_schedule"
))]
use tracing::trace_span;
#[cfg(feature = "task_dirty_cause")]
use turbo_tasks::TaskDirtyCause;
use turbo_tasks::{FxIndexMap, TaskExecutionReason, TaskId, TaskPriority, event::EventDescription};
#[cfg(feature = "trace_task_dirty")]
use crate::backend::operation::invalidate::TaskDirtyCause;
use crate::{
backend::{
TaskDataCategory,
......@@ -285,7 +285,7 @@ pub enum AggregationUpdateJob {
/// Invalidates tasks that are dependent on a collectible type.
InvalidateDueToCollectiblesChange {
task_ids: TaskIdVec,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
collectible_type: turbo_tasks::TraitTypeId,
},
/// Increases the active counter of the task
......@@ -616,7 +616,7 @@ impl AggregatedDataUpdate {
if !dependent.is_empty() {
queue.push(AggregationUpdateJob::InvalidateDueToCollectiblesChange {
task_ids: dependent,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
collectible_type: ty,
})
}
......@@ -1432,13 +1432,13 @@ impl AggregationUpdateQueue {
}
AggregationUpdateJob::InvalidateDueToCollectiblesChange {
task_ids,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
collectible_type,
} => {
for task_id in task_ids {
make_task_dirty(
task_id,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
TaskDirtyCause::CollectiblesChange { collectible_type },
self,
ctx,
......
......@@ -156,7 +156,7 @@ impl CleanupOldEdgesOperation {
queue.push(
AggregationUpdateJob::InvalidateDueToCollectiblesChange {
task_ids,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
collectible_type: ty,
},
);
......
use bincode::{Decode, Encode};
use smallvec::SmallVec;
#[cfg(feature = "task_dirty_cause")]
use turbo_tasks::TaskDirtyCause;
use turbo_tasks::{TaskExecutionReason, TaskId, TaskPriority, event::EventDescription};
use crate::{
......@@ -20,7 +22,7 @@ use crate::{
pub enum InvalidateOperation {
MakeDirty {
task_ids: SmallVec<[TaskId; 4]>,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
cause: TaskDirtyCause,
},
AggregationUpdate {
......@@ -33,12 +35,12 @@ pub enum InvalidateOperation {
impl InvalidateOperation {
pub fn run(
task_ids: SmallVec<[TaskId; 4]>,
#[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause,
#[cfg(feature = "task_dirty_cause")] cause: TaskDirtyCause,
mut ctx: impl ExecuteContext<'_>,
) {
InvalidateOperation::MakeDirty {
task_ids,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
cause,
}
.execute(&mut ctx)
......@@ -52,14 +54,14 @@ impl Operation for InvalidateOperation {
match self {
InvalidateOperation::MakeDirty {
task_ids,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
cause,
} => {
let mut queue = AggregationUpdateQueue::new();
for task_id in task_ids {
make_task_dirty(
task_id,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
cause.clone(),
&mut queue,
ctx,
......@@ -85,88 +87,9 @@ impl Operation for InvalidateOperation {
}
}
#[cfg(feature = "trace_task_dirty")]
#[derive(Encode, Decode, Clone, Debug)]
pub enum TaskDirtyCause {
InitialDirty,
CellChange {
value_type: turbo_tasks::ValueTypeId,
keys: SmallVec<[Option<u64>; 2]>,
},
CellRemoved {
value_type: turbo_tasks::ValueTypeId,
},
OutputChange {
task_description: String,
},
CollectiblesChange {
collectible_type: turbo_tasks::TraitTypeId,
},
Invalidator,
Unknown,
}
// NOTE: `TaskDirtyCause` is formatted for tracing inside `make_task_dirty_internal`, which
// already holds the dependent task's `StorageWriteGuard`. The `Display` impl below must NOT
// acquire any task guard — doing so would take a second map shard write lock with no ordering
// guarantee against the first and two concurrent invalidations of each other's outputs would
// form a classic hold-and-wait deadlock on the dashmap. `OutputChange::task_description` is
// therefore filled at the call site (before any guard is held) and only formatted here.
// The `TaskLockCounter` debug-assert that normally catches this kind of nested acquire is
// `cfg(debug_assertions)`-only, so release builds hang silently.
#[cfg(feature = "trace_task_dirty")]
impl std::fmt::Display for TaskDirtyCause {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TaskDirtyCause::InitialDirty => write!(f, "initial dirty"),
TaskDirtyCause::CellChange { value_type, keys } => {
if keys.is_empty() {
write!(
f,
"{} cell changed",
turbo_tasks::registry::get_value_type(*value_type).ty.name
)
} else {
write!(
f,
"{} cell changed (keys: {})",
turbo_tasks::registry::get_value_type(*value_type).ty.name,
keys.iter()
.map(|key| match key {
Some(k) => k.to_string(),
None => "*".to_string(),
})
.collect::<Vec<_>>()
.join(", ")
)
}
}
TaskDirtyCause::CellRemoved { value_type } => {
write!(
f,
"{} cell removed",
turbo_tasks::registry::get_value_type(*value_type).ty.name
)
}
TaskDirtyCause::OutputChange { task_description } => {
write!(f, "task {task_description} output changed")
}
TaskDirtyCause::CollectiblesChange { collectible_type } => {
write!(
f,
"{} collectible changed",
turbo_tasks::registry::get_trait(*collectible_type).ty.name
)
}
TaskDirtyCause::Invalidator => write!(f, "invalidator"),
TaskDirtyCause::Unknown => write!(f, "unknown"),
}
}
}
pub fn make_task_dirty(
task_id: TaskId,
#[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause,
#[cfg(feature = "task_dirty_cause")] cause: TaskDirtyCause,
queue: &mut AggregationUpdateQueue,
ctx: &mut impl ExecuteContext<'_>,
) {
......@@ -175,7 +98,7 @@ pub fn make_task_dirty(
task,
task_id,
true,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
cause,
queue,
ctx,
......@@ -186,7 +109,7 @@ pub fn make_task_dirty_internal(
mut task: impl TaskGuard,
task_id: TaskId,
make_stale: bool,
#[cfg(feature = "trace_task_dirty")] cause: TaskDirtyCause,
#[cfg(feature = "task_dirty_cause")] cause: TaskDirtyCause,
queue: &mut AggregationUpdateQueue,
ctx: &mut impl ExecuteContext<'_>,
) {
......@@ -194,9 +117,9 @@ pub fn make_task_dirty_internal(
// immutable.
#[cfg(any(debug_assertions, feature = "verify_immutable"))]
if task.immutable() {
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
let extra_info = format!(" Invalidation cause: {cause}");
#[cfg(not(feature = "trace_task_dirty"))]
#[cfg(not(feature = "task_dirty_cause"))]
let extra_info = "";
panic!(
......@@ -235,7 +158,10 @@ pub fn make_task_dirty_internal(
parent_priority
};
let (old_self_dirty, old_current_session_self_clean, parent_priority) = match current {
Some(Dirtyness::Dirty(current_priority)) => {
Some(Dirtyness::Dirty {
parent_priority: current_priority,
..
}) => {
#[cfg(feature = "trace_task_dirty")]
let _span = tracing::trace_span!(
"task already dirty",
......@@ -249,12 +175,20 @@ pub fn make_task_dirty_internal(
|| *current_priority > parent_priority
{
// Update the priority to be the lower one
task.set_dirty(Dirtyness::Dirty(parent_priority));
task.set_dirty(Dirtyness::Dirty {
parent_priority,
#[cfg(feature = "task_dirty_cause")]
cause,
});
}
return;
}
Some(Dirtyness::SessionDependent) => {
task.set_dirty(Dirtyness::Dirty(parent_priority));
task.set_dirty(Dirtyness::Dirty {
parent_priority,
#[cfg(feature = "task_dirty_cause")]
cause: cause.clone(),
});
// It was a session-dependent dirty before, so we need to remove that clean count
let was_current_session_clean = task.current_session_clean();
if was_current_session_clean {
......@@ -275,7 +209,11 @@ pub fn make_task_dirty_internal(
}
}
None => {
task.set_dirty(Dirtyness::Dirty(parent_priority));
task.set_dirty(Dirtyness::Dirty {
parent_priority,
#[cfg(feature = "task_dirty_cause")]
cause: cause.clone(),
});
// It was clean before, so we need to increase the dirty count
(false, false, parent_priority)
}
......
......@@ -1124,7 +1124,9 @@ pub trait TaskGuard: Debug + TaskStorageAccessors {
fn is_dirty(&self) -> Option<TaskPriority> {
self.get_dirty().and_then(|dirtyness| match dirtyness {
Dirtyness::Dirty(priority) => Some(*priority),
Dirtyness::Dirty {
parent_priority, ..
} => Some(*parent_priority),
Dirtyness::SessionDependent => {
if !self.current_session_clean() {
Some(TaskPriority::leaf())
......@@ -1135,8 +1137,9 @@ pub trait TaskGuard: Debug + TaskStorageAccessors {
})
}
fn dirtyness_and_session(&self) -> Option<(Dirtyness, bool)> {
match self.get_dirty()? {
Dirtyness::Dirty(priority) => Some((Dirtyness::Dirty(*priority), false)),
let dirtyness = self.get_dirty()?;
match dirtyness {
Dirtyness::Dirty { .. } => Some((dirtyness.clone(), false)),
Dirtyness::SessionDependent => {
Some((Dirtyness::SessionDependent, self.current_session_clean()))
}
......@@ -1146,7 +1149,7 @@ pub trait TaskGuard: Debug + TaskStorageAccessors {
fn dirty_state(&self) -> (bool, bool) {
match self.get_dirty() {
None => (false, false),
Some(Dirtyness::Dirty(_)) => (true, false),
Some(Dirtyness::Dirty { .. }) => (true, false),
Some(Dirtyness::SessionDependent) => (true, self.current_session_clean()),
}
}
......@@ -1168,7 +1171,7 @@ pub trait TaskGuard: Debug + TaskStorageAccessors {
let (old_self_dirty, old_current_session_self_clean) = self.dirty_state();
let (new_self_dirty, new_current_session_self_clean) = match new_dirtyness {
None => (false, false),
Some(Dirtyness::Dirty(_)) => (true, false),
Some(Dirtyness::Dirty { .. }) => (true, false),
Some(Dirtyness::SessionDependent) => (true, true),
};
if old_dirtyness != new_dirtyness {
......@@ -1509,8 +1512,6 @@ impl_operation!(CleanupOldEdges cleanup_old_edges::CleanupOldEdgesOperation);
impl_operation!(AggregationUpdate aggregation_update::AggregationUpdateQueue);
impl_operation!(LeafDistanceUpdate leaf_distance_update::LeafDistanceUpdateQueue);
#[cfg(feature = "trace_task_dirty")]
pub use self::invalidate::TaskDirtyCause;
pub use self::{
aggregation_update::{
AggregatedDataUpdate, AggregationUpdateJob, get_aggregation_number, get_uppers,
......
......@@ -3,14 +3,14 @@ use std::{cell::LazyCell, mem::take};
use bincode::{Decode, Encode};
use rustc_hash::FxHashSet;
use smallvec::SmallVec;
#[cfg(feature = "task_dirty_cause")]
use turbo_tasks::TaskDirtyCause;
use turbo_tasks::{
CellId, FxIndexMap, TaskId, TypedSharedReference, ValueTypePersistence,
backend::{CellContent, CellHash, VerificationMode},
registry,
};
#[cfg(feature = "trace_task_dirty")]
use crate::backend::operation::invalidate::TaskDirtyCause;
use crate::{
backend::{
TaskDataCategory,
......@@ -30,7 +30,7 @@ pub enum UpdateCellOperation {
cell_ref: CellRef,
#[bincode(with = "turbo_bincode::indexmap")]
dependent_tasks: FxIndexMap<TaskId, SmallVec<[Option<u64>; 2]>>,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
has_updated_key_hashes: bool,
content: Option<TypedSharedReference>,
queue: AggregationUpdateQueue,
......@@ -125,7 +125,7 @@ impl UpdateCellOperation {
}
};
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
let has_updated_key_hashes = updated_key_hashes.is_some();
let updated_key_hashes_set = updated_key_hashes.map(|updated_key_hashes| {
LazyCell::new(|| updated_key_hashes.into_iter().collect::<FxHashSet<u64>>())
......@@ -195,7 +195,7 @@ impl UpdateCellOperation {
cell,
},
dependent_tasks,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
has_updated_key_hashes,
content: content.map(|r| r.into_typed(cell.type_id)),
queue: AggregationUpdateQueue::new(),
......@@ -272,7 +272,7 @@ impl Operation for UpdateCellOperation {
UpdateCellOperation::InvalidateWhenCellDependency {
cell_ref,
ref mut dependent_tasks,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
has_updated_key_hashes,
ref mut content,
ref mut queue,
......@@ -302,7 +302,7 @@ impl Operation for UpdateCellOperation {
dependent,
dependent_task_id,
make_stale,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
TaskDirtyCause::CellChange {
value_type: cell_ref.cell.type_id,
keys: has_updated_key_hashes.then_some(keys).unwrap_or_default(),
......
......@@ -63,7 +63,7 @@ impl UpdateCollectibleOperation {
if !dependent.is_empty() {
queue.push(AggregationUpdateJob::InvalidateDueToCollectiblesChange {
task_ids: dependent,
#[cfg(feature = "trace_task_dirty")]
#[cfg(feature = "task_dirty_cause")]
collectible_type: ty,
})
}
......
......@@ -8,6 +8,8 @@ use anyhow::Result;
use bincode::{Decode, Encode};
use parking_lot::Mutex;
use rustc_hash::FxHashSet;
#[cfg(feature = "task_dirty_cause")]
use turbo_tasks::TaskDirtyCause;
use turbo_tasks::{
CellId, RawVc, TaskExecutionReason, TaskId, TaskPriority, TraitTypeId,
backend::TransientTaskRoot,
......@@ -266,9 +268,13 @@ impl Display for TransientTask {
transient_traits!(TransientTask);
#[derive(Debug, Clone, Copy, Encode, Decode, PartialEq, Eq)]
#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
pub enum Dirtyness {
Dirty(TaskPriority),
Dirty {
parent_priority: TaskPriority,
#[cfg(feature = "task_dirty_cause")]
cause: TaskDirtyCause,
},
SessionDependent,
}
......
......@@ -13,6 +13,7 @@ default = []
tokio_tracing = ["tokio/tracing"]
hanging_detection = []
task_id_details = []
task_dirty_cause = []
verify_determinism = []
# TODO(bgw): This feature is here to unblock turning on local tasks by default. It's only turned on
......
......@@ -53,6 +53,8 @@ pub mod small_duration;
mod spawn;
mod state;
pub mod task;
#[cfg(feature = "task_dirty_cause")]
mod task_dirty_cause;
mod task_execution_reason;
pub mod task_statistics;
mod tiny_vec;
......@@ -72,6 +74,8 @@ use rustc_hash::FxHasher;
pub use shrink_to_fit::ShrinkToFit;
pub use turbo_tasks_macros::{DeterministicHash, turbobail, turbofmt};
#[cfg(feature = "task_dirty_cause")]
pub use crate::task_dirty_cause::TaskDirtyCause;
pub use crate::{
capture_future::TurboTasksPanic,
collectibles::CollectiblesSource,
......@@ -82,7 +86,9 @@ pub use crate::{
},
effect::{Effect, EffectError, EffectStateStorage, Effects, emit_effect, take_effects},
error::PrettyPrintError,
id::{ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId},
id::{
ExecutionId, FunctionId, LocalTaskId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId,
},
invalidation::{
InvalidationReason, InvalidationReasonKind, InvalidationReasonSet, Invalidator,
get_invalidator,
......
......@@ -7,6 +7,8 @@ use tracing::Span;
use turbo_bincode::{AnyDecodeFn, AnyEncodeFn, new_hash_encoder};
use turbo_tasks_hash::DeterministicHasher;
#[cfg(feature = "task_dirty_cause")]
use crate::TaskDirtyCause;
use crate::{
RawVc, TaskExecutionReason, TaskInput, TaskPersistence, TaskPriority,
dyn_task_inputs::{
......@@ -285,18 +287,33 @@ impl NativeFunction {
persistence: TaskPersistence,
reason: TaskExecutionReason,
priority: TaskPriority,
#[cfg(feature = "task_dirty_cause")] cause: Option<&TaskDirtyCause>,
) -> Span {
let flags = match persistence {
TaskPersistence::Persistent => "",
TaskPersistence::Transient => "transient",
};
tracing::trace_span!(
"turbo_tasks::function",
name = self.ty.name,
priority = %priority,
flags = flags,
reason = reason.as_str()
)
#[cfg(feature = "task_dirty_cause")]
{
tracing::trace_span!(
"turbo_tasks::function",
name = self.ty.name,
priority = %priority,
flags = flags,
reason = reason.as_str(),
cause = cause.map(tracing::field::display),
)
}
#[cfg(not(feature = "task_dirty_cause"))]
{
tracing::trace_span!(
"turbo_tasks::function",
name = self.ty.name,
priority = %priority,
flags = flags,
reason = reason.as_str(),
)
}
}
pub fn resolve_span(&'static self, priority: TaskPriority) -> Span {
......
use bincode::{Decode, Encode};
use smallvec::SmallVec;
use crate::{FunctionId, TraitTypeId, ValueTypeId, registry};
#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
pub enum TaskDirtyCause {
InitialDirty,
CellChange {
value_type: ValueTypeId,
keys: SmallVec<[Option<u64>; 2]>,
},
CellRemoved {
value_type: ValueTypeId,
},
OutputChange {
function: FunctionId,
},
RootOutputChange,
CollectiblesChange {
collectible_type: TraitTypeId,
},
Invalidator,
Unknown,
}
// NOTE: `TaskDirtyCause` is formatted for tracing inside `make_task_dirty_internal`, which
// already holds the dependent task's `StorageWriteGuard`. The `Display` impl below must NOT
// acquire any task guard — doing so would take a second map shard write lock with no ordering
// guarantee against the first and two concurrent invalidations of each other's outputs would
// form a classic hold-and-wait deadlock on the dashmap. The function name lookup via
// `get_native_function` only touches the global function registry and never acquires a task
// guard, so it is safe. The `TaskLockCounter` debug-assert that normally catches this kind
// of nested acquire is `cfg(debug_assertions)`-only, so release builds hang silently.
impl std::fmt::Display for TaskDirtyCause {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TaskDirtyCause::InitialDirty => write!(f, "initial dirty"),
TaskDirtyCause::CellChange { value_type, keys } => {
if keys.is_empty() {
write!(
f,
"{} cell changed",
registry::get_value_type(*value_type).ty.name
)
} else {
write!(
f,
"{} cell changed (keys: {})",
registry::get_value_type(*value_type).ty.name,
keys.iter()
.map(|key| match key {
Some(k) => k.to_string(),
None => "*".to_string(),
})
.collect::<Vec<_>>()
.join(", ")
)
}
}
TaskDirtyCause::CellRemoved { value_type } => {
write!(
f,
"{} cell removed",
registry::get_value_type(*value_type).ty.name
)
}
TaskDirtyCause::OutputChange { function } => {
write!(
f,
"{} output changed",
registry::get_native_function(*function).ty.name
)
}
TaskDirtyCause::RootOutputChange => {
write!(f, "root output changed")
}
TaskDirtyCause::CollectiblesChange { collectible_type } => {
write!(
f,
"{} collectible changed",
registry::get_trait(*collectible_type).ty.name
)
}
TaskDirtyCause::Invalidator => write!(f, "invalidator"),
TaskDirtyCause::Unknown => write!(f, "unknown"),
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment