Unverified Commit be0a2550 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] allow to disable active tracking in turbo-tasks (#75899)

### What?

* allow to disable active tracking
* disable active tracking when dependency tracking is disabled

This allows to skip a lot of active counter increases and decreases for
better performance.

Without dependency tracking there can't be any inactive tasks in the
system, so we don't need to track that at all.
parent f50164ed
......@@ -121,6 +121,13 @@ pub struct BackendOptions {
/// considered as active. Collectibles are disabled.
pub children_tracking: bool,
/// Enables active tracking.
///
/// Automatically disabled when `dependency_tracking` is disabled.
///
/// When disabled: All tasks are considered as active.
pub active_tracking: bool,
/// Enables the backing storage.
pub storage_mode: Option<StorageMode>,
}
......@@ -130,6 +137,7 @@ impl Default for BackendOptions {
Self {
dependency_tracking: true,
children_tracking: true,
active_tracking: true,
storage_mode: Some(StorageMode::ReadWrite),
}
}
......@@ -194,10 +202,13 @@ impl<B: BackingStorage> TurboTasksBackend<B> {
}
impl<B: BackingStorage> TurboTasksBackendInner<B> {
pub fn new(options: BackendOptions, backing_storage: B) -> Self {
pub fn new(mut options: BackendOptions, backing_storage: B) -> Self {
let shard_amount =
(available_parallelism().map_or(4, |v| v.get()) * 64).next_power_of_two();
let need_log = matches!(options.storage_mode, Some(StorageMode::ReadWrite));
if !options.dependency_tracking {
options.active_tracking = false;
}
Self {
options,
start_time: Instant::now(),
......@@ -340,6 +351,10 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
self.options.dependency_tracking
}
fn should_track_activeness(&self) -> bool {
self.options.active_tracking
}
fn should_track_children(&self) -> bool {
self.options.children_tracking
}
......@@ -499,6 +514,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
// is automatically removed when this task is clean.
get_mut_or_insert_with!(task, Activeness, || ActivenessState::new(task_id))
.set_active_until_clean();
if ctx.should_track_activeness() {
// A newly added Activeness need to make sure to schedule the tasks
task_ids_to_schedule = get_many!(
task,
......@@ -509,6 +525,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
}
);
task_ids_to_schedule.push(task_id);
}
get!(task, Activeness).unwrap()
};
let listener = root.all_clean_event.listen_with_note(move || {
......@@ -1403,14 +1420,15 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let mut queue = AggregationUpdateQueue::new();
if has_children {
let has_active_count =
get!(task, Activeness).map_or(false, |activeness| activeness.active_counter > 0);
let has_active_count = ctx.should_track_activeness()
&& get!(task, Activeness).map_or(false, |activeness| activeness.active_counter > 0);
connect_children(
task_id,
&mut task,
new_children,
&mut queue,
has_active_count,
ctx.should_track_activeness(),
);
}
......@@ -1855,9 +1873,11 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
effective: u32::MAX,
},
});
if self.should_track_activeness() {
task.add(CachedDataItem::Activeness {
value: ActivenessState::new_root(root_type, task_id),
});
}
task.add(CachedDataItem::new_scheduled(move || match root_type {
RootType::RootTask => "Root Task".to_string(),
RootType::OnceTask => "Once Task".to_string(),
......
......@@ -283,6 +283,7 @@ impl AggregatedDataUpdate {
&self,
task: &mut impl TaskGuard,
session_id: SessionId,
should_track_activeness: bool,
queue: &mut AggregationUpdateQueue,
) -> AggregatedDataUpdate {
let Self {
......@@ -291,12 +292,14 @@ impl AggregatedDataUpdate {
} = self;
let mut result = Self::default();
if let Some((dirty_container_id, count)) = dirty_container_update {
if should_track_activeness {
// When a dirty container count is increased and the task is considered as active
// we need to schedule the dirty tasks in the new dirty container
let current_session_update = count.get(session_id);
if current_session_update > 0 && task.has_key(&CachedDataItemKey::Activeness {}) {
queue.push_find_and_schedule_dirty(*dirty_container_id)
}
}
let mut aggregated_update = DirtyContainerCount::default();
update!(
......@@ -332,8 +335,8 @@ impl AggregatedDataUpdate {
});
if let Some((_, count)) = result.dirty_container_update.as_ref() {
if count.get(session_id) < 0 {
// When the current task is no longer dirty, we need to fire the aggregate
// root events and do some cleanup
// When the current task is no longer dirty, we need to fire the
// aggregate root events and do some cleanup
if let Some(root_state) = get_mut!(task, Activeness) {
root_state.all_clean_event.notify(usize::MAX);
root_state.unset_active_until_clean();
......@@ -1035,7 +1038,12 @@ impl AggregationUpdateQueue {
// followers
let data = AggregatedDataUpdate::from_task(&mut task);
let followers = get_followers(&task);
let diff = data.apply(&mut upper, ctx.session_id(), self);
let diff = data.apply(
&mut upper,
ctx.session_id(),
ctx.should_track_activeness(),
self,
);
if !upper_ids.is_empty() && !diff.is_empty() {
// Notify uppers about changed aggregated data
......@@ -1054,7 +1062,9 @@ impl AggregationUpdateQueue {
});
}
if upper.has_key(&CachedDataItemKey::Activeness {}) {
if ctx.should_track_activeness()
&& upper.has_key(&CachedDataItemKey::Activeness {})
{
// If the upper node is has `Activeness` we need to schedule the
// dirty tasks in the new dirty container
self.push_find_and_schedule_dirty(task_id);
......@@ -1069,6 +1079,7 @@ impl AggregationUpdateQueue {
});
}
if ctx.should_track_activeness() {
// Follower was removed, we might need to update the active count
let has_active_count =
get!(upper, Activeness).is_some_and(|a| a.active_counter > 0);
......@@ -1077,6 +1088,7 @@ impl AggregationUpdateQueue {
self.push(AggregationUpdateJob::DecreaseActiveCount { task: task_id })
}
}
}
std::cmp::Ordering::Equal => {}
}
} else if should_be_follower {
......@@ -1099,11 +1111,15 @@ impl AggregationUpdateQueue {
if count!(upper, Follower).is_power_of_two() {
self.push_optimize_task(upper_id);
}
if ctx.should_track_activeness() {
// update active count
let has_active_count =
get!(task, Activeness).is_some_and(|a| a.active_counter > 0);
if has_active_count {
self.push(AggregationUpdateJob::IncreaseActiveCount { task: task_id });
self.push(AggregationUpdateJob::IncreaseActiveCount {
task: task_id,
});
}
}
// notify uppers about new follower
if !upper_ids.is_empty() {
......@@ -1118,7 +1134,12 @@ impl AggregationUpdateQueue {
// followers
let data = AggregatedDataUpdate::from_task(&mut task).invert();
let followers = get_followers(&task);
let diff = data.apply(&mut upper, ctx.session_id(), self);
let diff = data.apply(
&mut upper,
ctx.session_id(),
ctx.should_track_activeness(),
self,
);
if !upper_ids.is_empty() && !diff.is_empty() {
self.push(
AggregatedDataUpdateJob {
......@@ -1157,7 +1178,7 @@ impl AggregationUpdateQueue {
/// Schedules the task if it's dirty.
///
/// For aggregating nodes that are
/// Only used when activeness is tracked.
fn find_and_schedule_dirty(&mut self, task_id: TaskId, ctx: &mut impl ExecuteContext) {
#[cfg(feature = "trace_find_and_schedule")]
let _span = trace_span!(
......@@ -1205,7 +1226,12 @@ impl AggregationUpdateQueue {
) {
for upper_id in upper_ids {
let mut upper = ctx.task(upper_id, TaskDataCategory::Meta);
let diff = update.apply(&mut upper, ctx.session_id(), self);
let diff = update.apply(
&mut upper,
ctx.session_id(),
ctx.should_track_activeness(),
self,
);
if !diff.is_empty() {
let upper_ids = get_uppers(&upper);
if !upper_ids.is_empty() {
......@@ -1264,7 +1290,12 @@ impl AggregationUpdateQueue {
for upper_id in upper_ids.iter() {
// remove data from upper
let mut upper = ctx.task(*upper_id, TaskDataCategory::Meta);
let diff = data.apply(&mut upper, ctx.session_id(), self);
let diff = data.apply(
&mut upper,
ctx.session_id(),
ctx.should_track_activeness(),
self,
);
if !diff.is_empty() {
let upper_ids = get_uppers(&upper);
self.push(
......@@ -1304,8 +1335,8 @@ impl AggregationUpdateQueue {
self.push_optimize_task(upper_id);
}
let has_active_count =
get!(upper, Activeness).is_some_and(|a| a.active_counter > 0);
let has_active_count = ctx.should_track_activeness()
&& get!(upper, Activeness).is_some_and(|a| a.active_counter > 0);
let upper_ids = get_uppers(&upper);
drop(upper);
// update active count
......@@ -1365,7 +1396,12 @@ impl AggregationUpdateQueue {
if !data.is_empty() {
// remove data from upper
let mut upper = ctx.task(upper_id, TaskDataCategory::Meta);
let diff = data.apply(&mut upper, ctx.session_id(), self);
let diff = data.apply(
&mut upper,
ctx.session_id(),
ctx.should_track_activeness(),
self,
);
if !diff.is_empty() {
let upper_ids = get_uppers(&upper);
self.push(
......@@ -1461,12 +1497,14 @@ impl AggregationUpdateQueue {
self.push_optimize_task(upper_id);
}
if ctx.should_track_activeness() {
// update active count
let has_active_count =
get!(upper, Activeness).is_some_and(|a| a.active_counter > 0);
if has_active_count {
tasks_for_which_increment_active_count.push(new_follower_id);
}
}
// notify uppers about new follower
upper_upper_ids_with_new_follower.extend(iter_uppers(&upper));
}
......@@ -1483,7 +1521,8 @@ impl AggregationUpdateQueue {
false
} else {
// It's an inner node, continue with the list
if upper.has_key(&CachedDataItemKey::Activeness {}) {
if ctx.should_track_activeness() && upper.has_key(&CachedDataItemKey::Activeness {})
{
is_active = true;
}
true
......@@ -1531,7 +1570,12 @@ impl AggregationUpdateQueue {
for upper_id in upper_ids.iter() {
// add data to upper
let mut upper = ctx.task(*upper_id, TaskDataCategory::Meta);
let diff = data.apply(&mut upper, ctx.session_id(), self);
let diff = data.apply(
&mut upper,
ctx.session_id(),
ctx.should_track_activeness(),
self,
);
if !diff.is_empty() {
let upper_ids = get_uppers(&upper);
self.push(
......@@ -1597,15 +1641,17 @@ impl AggregationUpdateQueue {
.collect::<Vec<_>>();
let mut new_followers_of_upper_uppers = SmallVec::new();
let is_active;
let has_active_count;
let mut is_active = false;
let mut has_active_count = false;
let mut upper_upper_ids_for_new_followers = SmallVec::new();
let upper_aggregation_number;
{
let mut upper = ctx.task(upper_id, TaskDataCategory::Meta);
if ctx.should_track_activeness() {
let activeness_state = get!(upper, Activeness);
is_active = activeness_state.is_some();
has_active_count = activeness_state.is_some_and(|a| a.active_counter > 0);
}
// decide if it should be an inner or follower
upper_aggregation_number = get_aggregation_number(&upper);
......@@ -1694,7 +1740,12 @@ impl AggregationUpdateQueue {
let diffs = upper_data_updates
.into_iter()
.filter_map(|data| {
let diff = data.apply(&mut upper, ctx.session_id(), self);
let diff = data.apply(
&mut upper,
ctx.session_id(),
ctx.should_track_activeness(),
self,
);
(!diff.is_empty()).then_some(diff)
})
.collect::<Vec<_>>();
......@@ -1761,17 +1812,21 @@ impl AggregationUpdateQueue {
) {
#[cfg(feature = "trace_aggregation_update")]
let _span = trace_span!("process new follower").entered();
let should_track_activeness = ctx.should_track_activeness();
let (follower_aggregation_number, already_active) = {
let follower = ctx.task(new_follower_id, TaskDataCategory::Meta);
(
get_aggregation_number(&follower),
follower.has_key(&CachedDataItemKey::Activeness {}),
should_track_activeness && follower.has_key(&CachedDataItemKey::Activeness {}),
)
};
let mut upper = ctx.task(upper_id, TaskDataCategory::Meta);
if !already_active && upper.has_key(&CachedDataItemKey::Activeness {}) {
if should_track_activeness
&& !already_active
&& upper.has_key(&CachedDataItemKey::Activeness {})
{
self.push_find_and_schedule_dirty(new_follower_id);
}
// decide if it should be an inner or follower
......@@ -1796,8 +1851,8 @@ impl AggregationUpdateQueue {
self.push_optimize_task(upper_id);
}
let has_active_count =
get!(upper, Activeness).is_some_and(|a| a.active_counter > 0);
let has_active_count = ctx.should_track_activeness()
&& get!(upper, Activeness).is_some_and(|a| a.active_counter > 0);
let upper_ids = get_uppers(&upper);
drop(upper);
// update active count
......@@ -1843,7 +1898,12 @@ impl AggregationUpdateQueue {
if !data.is_empty() {
// add data to upper
let mut upper = ctx.task(upper_id, TaskDataCategory::Meta);
let diff = data.apply(&mut upper, ctx.session_id(), self);
let diff = data.apply(
&mut upper,
ctx.session_id(),
ctx.should_track_activeness(),
self,
);
if !diff.is_empty() {
let upper_ids = get_uppers(&upper);
self.push(
......@@ -1865,6 +1925,9 @@ impl AggregationUpdateQueue {
}
}
/// Decreases the active count of a task.
///
/// Only used when activeness is tracked.
fn decrease_active_count(&mut self, ctx: &mut impl ExecuteContext, task_id: TaskId) {
#[cfg(feature = "trace_aggregation_update")]
let _span = trace_span!("decrease active count").entered();
......@@ -1887,6 +1950,9 @@ impl AggregationUpdateQueue {
}
}
/// Increases the active count of a task.
///
/// Only used when activeness is tracked.
fn increase_active_count(&mut self, ctx: &mut impl ExecuteContext, task_id: TaskId) {
#[cfg(feature = "trace_aggregation_update")]
let _span = trace_span!("increase active count").entered();
......
......@@ -102,7 +102,8 @@ impl Operation for CleanupOldEdgesOperation {
});
} else {
let upper_ids = get_uppers(&task);
let has_active_count = get!(task, Activeness)
let has_active_count = ctx.should_track_activeness()
&& get!(task, Activeness)
.is_some_and(|a| a.active_counter > 0);
drop(task);
if has_active_count {
......
......@@ -26,7 +26,7 @@ pub enum ConnectChildOperation {
impl ConnectChildOperation {
pub fn run(parent_task_id: TaskId, child_task_id: TaskId, mut ctx: impl ExecuteContext) {
if !ctx.should_track_children() {
let mut task = ctx.task(child_task_id, TaskDataCategory::Data);
let mut task = ctx.task(child_task_id, TaskDataCategory::All);
if !task.has_key(&CachedDataItemKey::Output {}) {
let description = ctx.get_task_desc_fn(child_task_id);
let should_schedule = task.add(CachedDataItem::new_scheduled(description));
......@@ -67,9 +67,21 @@ impl ConnectChildOperation {
});
}
if ctx.should_track_activeness() {
queue.push(AggregationUpdateJob::IncreaseActiveCount {
task: child_task_id,
});
} else {
let mut task = ctx.task(child_task_id, TaskDataCategory::All);
if !task.has_key(&CachedDataItemKey::Output {}) {
let description = ctx.get_task_desc_fn(child_task_id);
let should_schedule = task.add(CachedDataItem::new_scheduled(description));
drop(task);
if should_schedule {
ctx.schedule(child_task_id);
}
}
}
ConnectChildOperation::UpdateAggregation {
aggregation_update: queue,
......
......@@ -16,6 +16,7 @@ pub fn connect_children(
new_children: FxHashSet<TaskId>,
queue: &mut AggregationUpdateQueue,
has_active_count: bool,
should_track_activeness: bool,
) {
if new_children.is_empty() {
return;
......@@ -49,7 +50,7 @@ pub fn connect_children(
// We need to decrease the active count because we temporarily increased it during
// connect_child. We need to increase the active count when the parent has active
// count, because it's added as follower.
if !has_active_count {
if should_track_activeness && !has_active_count {
queue.push(AggregationUpdateJob::DecreaseActiveCounts {
task_ids: new_follower_ids,
})
......@@ -62,8 +63,10 @@ pub fn connect_children(
});
// We need to decrease the active count because we temporarily increased it during
// connect_child.
if should_track_activeness {
queue.push(AggregationUpdateJob::DecreaseActiveCounts {
task_ids: new_follower_ids,
})
});
}
}
}
......@@ -22,7 +22,6 @@ use crate::{
#[derive(Serialize, Deserialize, Clone, Default)]
#[allow(clippy::large_enum_variant)]
pub enum InvalidateOperation {
// TODO DetermineActiveness
MakeDirty {
task_ids: SmallVec<[TaskId; 4]>,
#[cfg(feature = "trace_task_dirty")]
......@@ -274,7 +273,7 @@ pub fn make_task_dirty_internal(
AggregatedDataUpdate::new().dirty_container_update(task_id, aggregated_update),
));
}
task.has_key(&CachedDataItemKey::Activeness {})
!ctx.should_track_activeness() || task.has_key(&CachedDataItemKey::Activeness {})
} else {
true
};
......
......@@ -90,6 +90,7 @@ pub trait ExecuteContext<'e>: Sized {
fn get_task_description(&self, task_id: TaskId) -> String;
fn should_track_children(&self) -> bool;
fn should_track_dependencies(&self) -> bool;
fn should_track_activeness(&self) -> bool;
}
pub struct ParentRef<'a> {
......@@ -357,6 +358,10 @@ where
fn should_track_dependencies(&self) -> bool {
self.backend.should_track_dependencies()
}
fn should_track_activeness(&self) -> bool {
self.backend.should_track_activeness()
}
}
pub trait TaskGuard: Debug {
......
......@@ -171,6 +171,7 @@ async fn run(resource: PathBuf, snapshot_mode: IssueSnapshotMode) -> Result<JsRe
let tt = TurboTasks::new(TurboTasksBackend::new(
BackendOptions {
storage_mode: None,
dependency_tracking: false,
..Default::default()
},
noop_backing_storage(),
......
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