Unverified Commit f5f1581a authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

feat(turbopack): Switch `turbo-tasks-memory/print_task_invalidation`…

feat(turbopack): Switch `turbo-tasks-memory/print_task_invalidation` compile-time feature to an environment variable (#72300)

With this PR, `turbo-tasks-memory` (the new backend doesn't support this yet) can print invalidation information (basically just function names) to stdout when run with the following environment variable:

```
NEXT_TURBOPACK_PRINT_TASK_INVALIDATION=1
```

![Screenshot 2024-11-04 at 5.05.10 PM.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/HAZVitxRNnZz8QMiPn4a/3d334ef6-184c-40df-9769-fd0373de00fb.png)

This was previously behind a compilation feature flag, however:

- We have to generate all the support code regardless of if the flag is set, so it doesn't seem like it would save us anything on binary size.
- The branch should be pretty cheap to check (and probably easy for the CPU to predict), so runtime performance when this flag isn't set should be negligible.
- It's a pretty big lift to ask users to try custom builds.

**Why?** The hope is that this can help debug cases where users get stuck in a fast refresh update loop: https://vercel.slack.com/archives/C03S8ED1DKM/p1730737898652749

This is similar to #72300, except that prints the *reason*, this prints the *tasks* that were actually invalidated. Printing the tasks works even if no reason was supplied, so it can probably be helpful in debugging those cases.

Closes PACK-3375
parent dbad89b5
use std::{ use std::{
collections::HashMap, future::Future, ops::Deref, path::PathBuf, sync::Arc, time::Duration, collections::HashMap, env, future::Future, ops::Deref, path::PathBuf, sync::Arc, time::Duration,
}; };
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
...@@ -133,9 +133,11 @@ pub fn create_turbo_tasks( ...@@ -133,9 +133,11 @@ pub fn create_turbo_tasks(
)?), )?),
)) ))
} else { } else {
NextTurboTasks::Memory(TurboTasks::new(turbo_tasks_memory::MemoryBackend::new( let mut backend = turbo_tasks_memory::MemoryBackend::new(memory_limit);
memory_limit, if env::var_os("NEXT_TURBOPACK_PRINT_TASK_INVALIDATION").is_some() {
))) backend.print_task_invalidation(true);
}
NextTurboTasks::Memory(TurboTasks::new(backend))
}) })
} }
......
...@@ -48,7 +48,6 @@ turbo-tasks-build = { workspace = true } ...@@ -48,7 +48,6 @@ turbo-tasks-build = { workspace = true }
[features] [features]
track_unfinished = [] track_unfinished = []
print_task_invalidation = []
default = [] default = []
[[bench]] [[bench]]
......
...@@ -62,6 +62,7 @@ pub struct MemoryBackend { ...@@ -62,6 +62,7 @@ pub struct MemoryBackend {
gc_queue: Option<GcQueue>, gc_queue: Option<GcQueue>,
idle_gc_active: AtomicBool, idle_gc_active: AtomicBool,
task_statistics: TaskStatisticsApi, task_statistics: TaskStatisticsApi,
pub(crate) print_task_invalidation: bool,
} }
impl Default for MemoryBackend { impl Default for MemoryBackend {
...@@ -71,7 +72,7 @@ impl Default for MemoryBackend { ...@@ -71,7 +72,7 @@ impl Default for MemoryBackend {
} }
impl MemoryBackend { impl MemoryBackend {
pub fn new(memory_limit: usize) -> Self { pub fn new(memory_limit_bytes: usize) -> Self {
let shard_amount = let shard_amount =
(std::thread::available_parallelism().map_or(1, usize::from) * 32).next_power_of_two(); (std::thread::available_parallelism().map_or(1, usize::from) * 32).next_power_of_two();
Self { Self {
...@@ -84,13 +85,22 @@ impl MemoryBackend { ...@@ -84,13 +85,22 @@ impl MemoryBackend {
Default::default(), Default::default(),
shard_amount, shard_amount,
), ),
memory_limit: AtomicUsize::new(memory_limit), memory_limit: AtomicUsize::new(memory_limit_bytes),
gc_queue: (memory_limit != usize::MAX).then(GcQueue::new), gc_queue: (memory_limit_bytes != usize::MAX).then(GcQueue::new),
idle_gc_active: AtomicBool::new(false), idle_gc_active: AtomicBool::new(false),
task_statistics: TaskStatisticsApi::default(), task_statistics: TaskStatisticsApi::default(),
print_task_invalidation: false,
} }
} }
/// A debug feature that prints detailed task invalidation information to stdout if enabled.
///
/// To enable this in next.js, use the `NEXT_TURBOPACK_PRINT_TASK_INVALIDATION` environment
/// variable.
pub fn print_task_invalidation(&mut self, value: bool) {
self.print_task_invalidation = value;
}
fn connect_task_child( fn connect_task_child(
&self, &self,
parent: TaskId, parent: TaskId,
......
...@@ -894,9 +894,7 @@ impl Task { ...@@ -894,9 +894,7 @@ impl Task {
InProgress(..) => match result { InProgress(..) => match result {
Ok(Ok(result)) => { Ok(Ok(result)) => {
if state.output != result { if state.output != result {
if cfg!(feature = "print_task_invalidation") if backend.print_task_invalidation && state.output.content.is_some() {
&& state.output.content.is_some()
{
println!( println!(
"Task {{ id: {}, name: {} }} invalidates:", "Task {{ id: {}, name: {} }} invalidates:",
*self.id, self.ty *self.id, self.ty
...@@ -1146,7 +1144,7 @@ impl Task { ...@@ -1146,7 +1144,7 @@ impl Task {
drop(state); drop(state);
change_job.apply(&aggregation_context); change_job.apply(&aggregation_context);
if cfg!(feature = "print_task_invalidation") { if backend.print_task_invalidation {
println!("invalidated Task {{ id: {}, name: {} }}", *self.id, self.ty); println!("invalidated Task {{ id: {}, name: {} }}", *self.id, self.ty);
} }
turbo_tasks.schedule(self.id); turbo_tasks.schedule(self.id);
......
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