Skip to content

Commit 295dd49

Browse files
committed
Turbopack: add task execution reason to tracing
1 parent 6b55cdc commit 295dd49

File tree

9 files changed

+127
-57
lines changed

9 files changed

+127
-57
lines changed

turbopack/crates/turbo-tasks-backend/src/backend/mod.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use smallvec::{SmallVec, smallvec};
2525
use tokio::time::{Duration, Instant};
2626
use turbo_tasks::{
2727
CellId, FxDashMap, KeyValuePair, RawVc, ReadCellOptions, ReadConsistency, SessionId,
28-
TRANSIENT_TASK_BIT, TaskId, TraitTypeId, TurboTasksBackendApi, ValueTypeId,
28+
TRANSIENT_TASK_BIT, TaskExecutionReason, TaskId, TraitTypeId, TurboTasksBackendApi,
29+
ValueTypeId,
2930
backend::{
3031
Backend, BackendJobId, CachedTaskType, CellContent, TaskExecutionSpec, TransientTaskRoot,
3132
TransientTaskType, TurboTasksExecutionError, TypedCellContent,
@@ -617,8 +618,11 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
617618
};
618619

619620
// Output doesn't exist. We need to schedule the task to compute it.
620-
let (item, listener) =
621-
CachedDataItem::new_scheduled_with_listener(self.get_task_desc_fn(task_id), note);
621+
let (item, listener) = CachedDataItem::new_scheduled_with_listener(
622+
TaskExecutionReason::OutputNotAvailable,
623+
self.get_task_desc_fn(task_id),
624+
note,
625+
);
622626
// It's not possible that the task is InProgress at this point. If it is InProgress {
623627
// done: true } it must have Output and would early return.
624628
task.add_new(item);
@@ -749,6 +753,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
749753
bail!("{} was canceled", ctx.get_task_description(task_id));
750754
} else if !is_scheduled
751755
&& task.add(CachedDataItem::new_scheduled(
756+
TaskExecutionReason::CellNotAvailable,
752757
self.get_task_desc_fn(task_id),
753758
))
754759
{
@@ -1400,7 +1405,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
14001405
let mut task = ctx.task(task_id, TaskDataCategory::Data);
14011406
if let Some(in_progress) = remove!(task, InProgress) {
14021407
match in_progress {
1403-
InProgressState::Scheduled { done_event } => done_event.notify(usize::MAX),
1408+
InProgressState::Scheduled {
1409+
done_event,
1410+
reason: _,
1411+
} => done_event.notify(usize::MAX),
14041412
InProgressState::InProgress(box InProgressStateInner { done_event, .. }) => {
14051413
done_event.notify(usize::MAX)
14061414
}
@@ -1431,14 +1439,16 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
14311439
} else {
14321440
return None;
14331441
};
1442+
let execution_reason;
14341443
{
14351444
let mut ctx = self.execute_context(turbo_tasks);
14361445
let mut task = ctx.task(task_id, TaskDataCategory::All);
14371446
let in_progress = remove!(task, InProgress)?;
1438-
let InProgressState::Scheduled { done_event } = in_progress else {
1447+
let InProgressState::Scheduled { done_event, reason } = in_progress else {
14391448
task.add_new(CachedDataItem::InProgress { value: in_progress });
14401449
return None;
14411450
};
1451+
execution_reason = reason;
14421452
task.add_new(CachedDataItem::InProgress {
14431453
value: InProgressState::InProgress(Box::new(InProgressStateInner {
14441454
stale: false,
@@ -1533,7 +1543,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
15331543
arg,
15341544
} = &*task_type;
15351545
(
1536-
native_fn.span(task_id.persistence()),
1546+
native_fn.span(task_id.persistence(), execution_reason),
15371547
native_fn.execute(*this, &**arg),
15381548
)
15391549
}
@@ -1613,7 +1623,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
16131623
unreachable!();
16141624
};
16151625
task.add_new(CachedDataItem::InProgress {
1616-
value: InProgressState::Scheduled { done_event },
1626+
value: InProgressState::Scheduled {
1627+
done_event,
1628+
reason: TaskExecutionReason::Stale,
1629+
},
16171630
});
16181631
// Remove old children from new_children to leave only the children that had their
16191632
// active count increased
@@ -1781,7 +1794,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
17811794
unreachable!();
17821795
};
17831796
task.add_new(CachedDataItem::InProgress {
1784-
value: InProgressState::Scheduled { done_event },
1797+
value: InProgressState::Scheduled {
1798+
done_event,
1799+
reason: TaskExecutionReason::Stale,
1800+
},
17851801
});
17861802
drop(task);
17871803

@@ -1842,7 +1858,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
18421858
// If the task is stale, reschedule it
18431859
if stale {
18441860
task.add_new(CachedDataItem::InProgress {
1845-
value: InProgressState::Scheduled { done_event },
1861+
value: InProgressState::Scheduled {
1862+
done_event,
1863+
reason: TaskExecutionReason::Stale,
1864+
},
18461865
});
18471866
return true;
18481867
}
@@ -2300,10 +2319,13 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
23002319
value: ActivenessState::new_root(root_type, task_id),
23012320
});
23022321
}
2303-
task.add(CachedDataItem::new_scheduled(move || match root_type {
2304-
RootType::RootTask => "Root Task".to_string(),
2305-
RootType::OnceTask => "Once Task".to_string(),
2306-
}));
2322+
task.add(CachedDataItem::new_scheduled(
2323+
TaskExecutionReason::Initial,
2324+
move || match root_type {
2325+
RootType::RootTask => "Root Task".to_string(),
2326+
RootType::OnceTask => "Once Task".to_string(),
2327+
},
2328+
));
23072329
}
23082330
#[cfg(feature = "verify_aggregation_graph")]
23092331
self.root_tasks.lock().insert(task_id);

turbopack/crates/turbo-tasks-backend/src/backend/operation/aggregation_update.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use smallvec::{SmallVec, smallvec};
1919
feature = "trace_find_and_schedule"
2020
))]
2121
use tracing::{span::Span, trace_span};
22-
use turbo_tasks::{FxIndexMap, SessionId, TaskId};
22+
use turbo_tasks::{FxIndexMap, SessionId, TaskExecutionReason, TaskId};
2323

2424
#[cfg(feature = "trace_task_dirty")]
2525
use crate::backend::operation::invalidate::TaskDirtyCause;
@@ -1217,10 +1217,16 @@ impl AggregationUpdateQueue {
12171217
let session_id = ctx.session_id();
12181218
// Task need to be scheduled if it's dirty or doesn't have output
12191219
let dirty = get!(task, Dirty).map_or(false, |d| d.get(session_id));
1220-
let should_schedule = dirty || !task.has_key(&CachedDataItemKey::Output {});
1221-
if should_schedule {
1220+
let should_schedule = if dirty {
1221+
Some(TaskExecutionReason::ActivateDirty)
1222+
} else if !task.has_key(&CachedDataItemKey::Output {}) {
1223+
Some(TaskExecutionReason::ActivateInitial)
1224+
} else {
1225+
None
1226+
};
1227+
if let Some(reason) = should_schedule {
12221228
let description = ctx.get_task_desc_fn(task_id);
1223-
if task.add(CachedDataItem::new_scheduled(description)) {
1229+
if task.add(CachedDataItem::new_scheduled(reason, description)) {
12241230
ctx.schedule(task_id);
12251231
}
12261232
}

turbopack/crates/turbo-tasks-backend/src/backend/operation/connect_child.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use serde::{Deserialize, Serialize};
2-
use turbo_tasks::TaskId;
2+
use turbo_tasks::{TaskExecutionReason, TaskId};
33

44
use crate::{
55
backend::{
@@ -36,7 +36,10 @@ impl ConnectChildOperation {
3636
}
3737
if !task.has_key(&CachedDataItemKey::Output {}) {
3838
let description = ctx.get_task_desc_fn(child_task_id);
39-
let should_schedule = task.add(CachedDataItem::new_scheduled(description));
39+
let should_schedule = task.add(CachedDataItem::new_scheduled(
40+
TaskExecutionReason::Connect,
41+
description,
42+
));
4043
drop(task);
4144
if should_schedule {
4245
ctx.schedule(child_task_id);
@@ -97,7 +100,10 @@ impl ConnectChildOperation {
97100

98101
if !task.has_key(&CachedDataItemKey::Output {}) {
99102
let description = ctx.get_task_desc_fn(child_task_id);
100-
let should_schedule = task.add(CachedDataItem::new_scheduled(description));
103+
let should_schedule = task.add(CachedDataItem::new_scheduled(
104+
TaskExecutionReason::Connect,
105+
description,
106+
));
101107
drop(task);
102108
if should_schedule {
103109
ctx.schedule(child_task_id);

turbopack/crates/turbo-tasks-backend/src/backend/operation/invalidate.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use serde::{Deserialize, Serialize};
22
use smallvec::SmallVec;
3-
use turbo_tasks::TaskId;
3+
use turbo_tasks::{TaskExecutionReason, TaskId};
44

55
use crate::{
66
backend::{
@@ -278,7 +278,10 @@ pub fn make_task_dirty_internal(
278278

279279
if should_schedule {
280280
let description = ctx.get_task_desc_fn(task_id);
281-
if task.add(CachedDataItem::new_scheduled(description)) {
281+
if task.add(CachedDataItem::new_scheduled(
282+
TaskExecutionReason::Invalidated,
283+
description,
284+
)) {
282285
ctx.schedule(task_id);
283286
}
284287
}

turbopack/crates/turbo-tasks-backend/src/data.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::cmp::Ordering;
33
use rustc_hash::FxHashMap;
44
use serde::{Deserialize, Serialize};
55
use turbo_tasks::{
6-
CellId, KeyValuePair, SessionId, TaskId, TraitTypeId, TypedSharedReference, ValueTypeId,
6+
CellId, KeyValuePair, SessionId, TaskExecutionReason, TaskId, TraitTypeId,
7+
TypedSharedReference, ValueTypeId,
78
backend::TurboTasksExecutionError,
89
event::{Event, EventListener},
910
registry,
@@ -322,7 +323,13 @@ pub struct InProgressStateInner {
322323

323324
#[derive(Debug)]
324325
pub enum InProgressState {
325-
Scheduled { done_event: Event },
326+
Scheduled {
327+
/// Event that is triggered when the task output is available (completed flag set).
328+
/// This is used to wait for completion when reading the task output before it's available.
329+
done_event: Event,
330+
/// Reason for scheduling the task.
331+
reason: TaskExecutionReason,
332+
},
326333
InProgress(Box<InProgressStateInner>),
327334
Canceled,
328335
}
@@ -522,23 +529,28 @@ impl CachedDataItem {
522529
}
523530
}
524531

525-
pub fn new_scheduled(description: impl Fn() -> String + Sync + Send + 'static) -> Self {
532+
pub fn new_scheduled(
533+
reason: TaskExecutionReason,
534+
description: impl Fn() -> String + Sync + Send + 'static,
535+
) -> Self {
526536
CachedDataItem::InProgress {
527537
value: InProgressState::Scheduled {
528538
done_event: Event::new(move || format!("{} done_event", description())),
539+
reason,
529540
},
530541
}
531542
}
532543

533544
pub fn new_scheduled_with_listener(
545+
reason: TaskExecutionReason,
534546
description: impl Fn() -> String + Sync + Send + 'static,
535547
note: impl Fn() -> String + Sync + Send + 'static,
536548
) -> (Self, EventListener) {
537549
let done_event = Event::new(move || format!("{} done_event", description()));
538550
let listener = done_event.listen_with_note(note);
539551
(
540552
CachedDataItem::InProgress {
541-
value: InProgressState::Scheduled { done_event },
553+
value: InProgressState::Scheduled { done_event, reason },
542554
},
543555
listener,
544556
)

turbopack/crates/turbo-tasks/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ mod serialization_invalidation;
7575
pub mod small_duration;
7676
mod state;
7777
pub mod task;
78+
mod task_execution_reason;
7879
pub mod task_statistics;
7980
pub mod trace;
8081
mod trait_ref;
@@ -120,6 +121,7 @@ pub use serialization_invalidation::SerializationInvalidator;
120121
pub use shrink_to_fit::ShrinkToFit;
121122
pub use state::{State, TransientState};
122123
pub use task::{SharedReference, TypedSharedReference, task_input::TaskInput};
124+
pub use task_execution_reason::TaskExecutionReason;
123125
pub use trait_ref::{IntoTraitRef, TraitRef};
124126
pub use turbo_tasks_macros::{TaskInput, function, value_impl};
125127
pub use value::{TransientInstance, TransientValue};

turbopack/crates/turbo-tasks/src/native_function.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
66
use tracing::Span;
77

88
use crate::{
9-
RawVc, TaskInput, TaskPersistence,
9+
RawVc, TaskExecutionReason, TaskInput, TaskPersistence,
1010
magic_any::{MagicAny, MagicAnyDeserializeSeed, MagicAnySerializeSeed},
1111
registry::register_function,
1212
task::{
@@ -253,36 +253,27 @@ impl NativeFunction {
253253
}
254254
}
255255

256-
pub fn span(&'static self, persistence: TaskPersistence) -> Span {
257-
match persistence {
258-
TaskPersistence::Persistent => {
259-
tracing::trace_span!("turbo_tasks::function", name = self.name)
260-
}
261-
TaskPersistence::Transient => {
262-
tracing::trace_span!("turbo_tasks::function", name = self.name, transient = true,)
263-
}
264-
TaskPersistence::Local => {
265-
tracing::trace_span!("turbo_tasks::function", name = self.name, local = true,)
266-
}
267-
}
256+
pub fn span(&'static self, persistence: TaskPersistence, reason: TaskExecutionReason) -> Span {
257+
let flags = match persistence {
258+
TaskPersistence::Persistent => "",
259+
TaskPersistence::Transient => "transient",
260+
TaskPersistence::Local => "local",
261+
};
262+
tracing::trace_span!(
263+
"turbo_tasks::function",
264+
name = self.name,
265+
flags = flags,
266+
reason = reason.as_str()
267+
)
268268
}
269269

270270
pub fn resolve_span(&'static self, persistence: TaskPersistence) -> Span {
271-
match persistence {
272-
TaskPersistence::Persistent => {
273-
tracing::trace_span!("turbo_tasks::resolve_call", name = self.name)
274-
}
275-
TaskPersistence::Transient => {
276-
tracing::trace_span!(
277-
"turbo_tasks::resolve_call",
278-
name = self.name,
279-
transient = true,
280-
)
281-
}
282-
TaskPersistence::Local => {
283-
tracing::trace_span!("turbo_tasks::resolve_call", name = self.name, local = true,)
284-
}
285-
}
271+
let flags = match persistence {
272+
TaskPersistence::Persistent => "",
273+
TaskPersistence::Transient => "transient",
274+
TaskPersistence::Local => "local",
275+
};
276+
tracing::trace_span!("turbo_tasks::resolve_call", name = self.name, flags = flags)
286277
}
287278

288279
pub fn register(&'static self, global_name: &'static str) {

turbopack/crates/turbo-tasks/src/task/local_task.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::{fmt, sync::Arc};
33
use anyhow::{Result, anyhow};
44

55
use crate::{
6-
MagicAny, OutputContent, RawVc, TaskPersistence, TraitMethod, TurboTasksBackendApi,
7-
ValueTypeId,
6+
MagicAny, OutputContent, RawVc, TaskExecutionReason, TaskPersistence, TraitMethod,
7+
TurboTasksBackendApi, ValueTypeId,
88
backend::{Backend, TaskExecutionSpec, TypedCellContent},
99
event::Event,
1010
macro_helpers::NativeFunction,
@@ -26,7 +26,7 @@ pub fn get_local_task_execution_spec<'a>(
2626
) -> TaskExecutionSpec<'a> {
2727
match ty.task_type {
2828
LocalTaskType::Native { native_fn } => {
29-
let span = native_fn.span(TaskPersistence::Local);
29+
let span = native_fn.span(TaskPersistence::Local, TaskExecutionReason::Local);
3030
let entered = span.enter();
3131
let future = native_fn.execute(ty.this, &*ty.arg);
3232
drop(entered);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#[derive(Debug)]
2+
pub enum TaskExecutionReason {
3+
Initial,
4+
Local,
5+
OutputNotAvailable,
6+
CellNotAvailable,
7+
Invalidated,
8+
ActivateDirty,
9+
ActivateInitial,
10+
Connect,
11+
Stale,
12+
}
13+
14+
impl TaskExecutionReason {
15+
pub fn as_str(&self) -> &'static str {
16+
match self {
17+
TaskExecutionReason::Initial => "initial",
18+
TaskExecutionReason::Local => "local",
19+
TaskExecutionReason::OutputNotAvailable => "output_not_available",
20+
TaskExecutionReason::CellNotAvailable => "cell_not_available",
21+
TaskExecutionReason::Invalidated => "invalidated",
22+
TaskExecutionReason::ActivateDirty => "activate_dirty",
23+
TaskExecutionReason::ActivateInitial => "activate_initial",
24+
TaskExecutionReason::Connect => "connect",
25+
TaskExecutionReason::Stale => "stale",
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)