Skip to content

add feature flag to verify aggregation graph #78964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions turbopack/crates/turbo-tasks-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ workspace = true
[features]
default = []
verify_serialization = []
verify_aggregation_graph = []
trace_aggregation_update = []
trace_find_and_schedule = []
trace_task_completion = []
Expand Down
162 changes: 159 additions & 3 deletions turbopack/crates/turbo-tasks-backend/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,15 @@ struct TurboTasksBackendInner<B: BackingStorage> {
stopping_event: Event,
idle_start_event: Event,
idle_end_event: Event,
#[cfg(feature = "verify_aggregation_graph")]
is_idle: AtomicBool,

task_statistics: TaskStatisticsApi,

backing_storage: B,

#[cfg(feature = "verify_aggregation_graph")]
root_tasks: Mutex<FxHashSet<TaskId>>,
}

impl<B: BackingStorage> TurboTasksBackend<B> {
Expand Down Expand Up @@ -237,8 +242,12 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
stopping_event: Event::new(|| "TurboTasksBackend::stopping_event".to_string()),
idle_start_event: Event::new(|| "TurboTasksBackend::idle_start_event".to_string()),
idle_end_event: Event::new(|| "TurboTasksBackend::idle_end_event".to_string()),
#[cfg(feature = "verify_aggregation_graph")]
is_idle: AtomicBool::new(false),
task_statistics: TaskStatisticsApi::default(),
backing_storage,
#[cfg(feature = "verify_aggregation_graph")]
root_tasks: Default::default(),
}
}

Expand Down Expand Up @@ -993,11 +1002,37 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
}
}

fn idle_start(&self) {
#[allow(unused_variables)]
fn idle_start(self: &Arc<Self>, turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>) {
self.idle_start_event.notify(usize::MAX);

#[cfg(feature = "verify_aggregation_graph")]
{
use tokio::select;

self.is_idle.store(true, Ordering::Release);
let this = self.clone();
let turbo_tasks = turbo_tasks.pin();
tokio::task::spawn(async move {
select! {
_ = tokio::time::sleep(Duration::from_secs(5)) => {
// do nothing
}
_ = this.idle_end_event.listen() => {
return;
}
}
if !this.is_idle.load(Ordering::Relaxed) {
return;
}
this.verify_aggregation_graph(&*turbo_tasks);
});
}
}

fn idle_end(&self) {
#[cfg(feature = "verify_aggregation_graph")]
self.is_idle.store(false, Ordering::Release);
self.idle_end_event.notify(usize::MAX);
}

Expand Down Expand Up @@ -2155,6 +2190,8 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
RootType::OnceTask => "Once Task".to_string(),
}));
}
#[cfg(feature = "verify_aggregation_graph")]
self.root_tasks.lock().insert(task_id);
task_id
}

Expand All @@ -2163,6 +2200,9 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
task_id: TaskId,
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
) {
#[cfg(feature = "verify_aggregation_graph")]
self.root_tasks.lock().remove(&task_id);

let mut ctx = self.execute_context(turbo_tasks);
let mut task = ctx.task(task_id, TaskDataCategory::All);
let is_dirty = get!(task, Dirty).map_or(false, |dirty| dirty.get(self.session_id));
Expand All @@ -2183,6 +2223,122 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
}
}

#[cfg(feature = "verify_aggregation_graph")]
fn verify_aggregation_graph(
&self,
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
) {
use std::collections::VecDeque;

use crate::backend::operation::{get_uppers, is_aggregating_node};

let mut ctx = self.execute_context(turbo_tasks);
let root_tasks = self.root_tasks.lock().clone();

for task_id in root_tasks {
let mut queue = VecDeque::new();
let mut visited = FxHashSet::default();
let mut aggregated_nodes = FxHashSet::default();
let mut collectibles = FxHashMap::default();
let root_task_id = task_id;
visited.insert(task_id);
aggregated_nodes.insert(task_id);
queue.push_back(task_id);
while let Some(task_id) = queue.pop_front() {
let task = ctx.task(task_id, TaskDataCategory::All);
if !self.is_idle.load(Ordering::Relaxed) {
return;
}

let uppers = get_uppers(&task);
if task_id != root_task_id
&& !uppers.iter().any(|upper| aggregated_nodes.contains(upper))
{
println!(
"Task {} {} doesn't report to any root but is reachable from one (uppers: \
{:?})",
task_id,
ctx.get_task_description(task_id),
uppers
);
}

let aggregated_collectibles: Vec<_> = get_many!(task, AggregatedCollectible { collectible } value if *value > 0 => {collectible});
for collectible in aggregated_collectibles {
collectibles
.entry(collectible)
.or_insert_with(|| (false, Vec::new()))
.1
.push(task_id);
}

let own_collectibles: Vec<_> = get_many!(task, Collectible { collectible } value if *value > 0 => {collectible});
for collectible in own_collectibles {
if let Some((flag, _)) = collectibles.get_mut(&collectible) {
*flag = true
} else {
println!(
"Task {} has a collectible {:?} that is not in any upper task",
task_id, collectible
);
}
}

let is_dirty = get!(task, Dirty).is_some_and(|dirty| dirty.get(self.session_id));
let has_dirty_container = get!(task, AggregatedDirtyContainerCount)
.is_some_and(|count| count.get(self.session_id) > 0);
let should_be_in_upper = is_dirty || has_dirty_container;

let aggregation_number = get_aggregation_number(&task);
if is_aggregating_node(aggregation_number) {
aggregated_nodes.insert(task_id);
}
// println!(
// "{task_id}: {} agg_num = {aggregation_number}, uppers = {:#?}",
// ctx.get_task_description(task_id),
// uppers
// );

for child_id in iter_many!(task, Child { task } => task) {
// println!("{task_id}: child -> {child_id}");
if visited.insert(child_id) {
queue.push_back(child_id);
}
}
drop(task);

if should_be_in_upper {
for upper_id in uppers {
let task = ctx.task(task_id, TaskDataCategory::All);
let in_upper = get!(task, AggregatedDirtyContainer { task: task_id })
.is_some_and(|dirty| dirty.get(self.session_id) > 0);
if !in_upper {
println!(
"Task {} is dirty, but is not listed in the upper task {}",
task_id, upper_id
);
}
}
}
}

for (collectible, (flag, task_ids)) in collectibles {
if !flag {
println!(
"{:?} that is not emitted in any child task but in these aggregated \
tasks: {:#?}",
collectible,
task_ids
.iter()
.map(|t| format!("{t} {}", ctx.get_task_description(*t)))
.collect::<Vec<_>>()
);
}
}
println!("visited {task_id} {} tasks", visited.len());
}
}

fn assert_not_persistent_calling_transient(
&self,
parent_id: TaskId,
Expand Down Expand Up @@ -2265,8 +2421,8 @@ impl<B: BackingStorage> Backend for TurboTasksBackend<B> {
self.0.stop();
}

fn idle_start(&self, _turbo_tasks: &dyn TurboTasksBackendApi<Self>) {
self.0.idle_start();
fn idle_start(&self, turbo_tasks: &dyn TurboTasksBackendApi<Self>) {
self.0.idle_start(turbo_tasks);
}

fn idle_end(&self, _turbo_tasks: &dyn TurboTasksBackendApi<Self>) {
Expand Down
Loading