Commit be6b69e5 authored by Tobias Koppers's avatar Tobias Koppers

Turbopack: more efficient hanging detection implementation when not enabled

use macros to avoid constructing notes and descriptions when not enabled.
This also avoids looking up the task type for every task when hanging detection is not enabled.
parent 53a46b39
......@@ -32,8 +32,10 @@ use turbo_tasks::{
Backend, BackendJobId, CachedTaskType, CellContent, TaskExecutionSpec, TransientTaskRoot,
TransientTaskType, TurboTasksExecutionError, TypedCellContent,
},
event::{Event, EventListener},
event::{Event, EventListener, event_note},
listen_event,
message_queue::TimingEvent,
new_event,
registry::{self, get_value_type_global_name},
task_statistics::TaskStatisticsApi,
trace::TraceRawVcs,
......@@ -256,9 +258,9 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
snapshot_completed: Condvar::new(),
last_snapshot: AtomicU64::new(0),
stopping: AtomicBool::new(false),
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()),
stopping_event: new_event!(|| "TurboTasksBackend::stopping_event".to_string()),
idle_start_event: new_event!(|| "TurboTasksBackend::idle_start_event".to_string()),
idle_end_event: new_event!(|| "TurboTasksBackend::idle_end_event".to_string()),
#[cfg(feature = "verify_aggregation_graph")]
is_idle: AtomicBool::new(false),
task_statistics: TaskStatisticsApi::default(),
......@@ -448,39 +450,42 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let mut ctx = self.execute_context(turbo_tasks);
let mut task = ctx.task(task_id, TaskDataCategory::All);
fn listen_to_done_event<B: BackingStorage>(
#[allow(dead_code, reason = "used for hanging detection")]
fn done_event_note<B: BackingStorage>(
this: &TurboTasksBackendInner<B>,
reader: Option<TaskId>,
done_event: &Event,
) -> EventListener {
let reader_desc = reader.map(|r| this.get_task_desc_fn(r));
done_event.listen_with_note(move || {
) -> impl Fn() -> String + Send + Sync + 'static {
let reader_desc = reader.map(|r| this.get_task_event_note(r));
move || {
if let Some(reader_desc) = reader_desc.as_ref() {
format!("try_read_task_output from {}", reader_desc())
} else {
"try_read_task_output (untracked)".to_string()
}
})
}
}
fn check_in_progress<B: BackingStorage>(
this: &TurboTasksBackendInner<B>,
_this: &TurboTasksBackendInner<B>,
task: &impl TaskGuard,
reader: Option<TaskId>,
_reader: Option<TaskId>,
ctx: &impl ExecuteContext<'_>,
) -> Option<std::result::Result<std::result::Result<RawVc, EventListener>, anyhow::Error>>
{
match get!(task, InProgress) {
Some(InProgressState::Scheduled { done_event, .. }) => {
Some(Ok(Err(listen_to_done_event(this, reader, done_event))))
}
Some(InProgressState::Scheduled { done_event, .. }) => Some(Ok(Err(
listen_event!(done_event, done_event_note(_this, _reader)),
))),
Some(InProgressState::InProgress(box InProgressStateInner {
done,
done_event,
..
})) => {
if !*done {
Some(Ok(Err(listen_to_done_event(this, reader, done_event))))
Some(Ok(Err(listen_event!(
done_event,
done_event_note(_this, _reader)
))))
} else {
None
}
......@@ -557,7 +562,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
}
get!(task, Activeness).unwrap()
};
let listener = activeness.all_clean_event.listen_with_note(move || {
let listener = listen_event!(activeness.all_clean_event, move || {
format!("try_read_task_output (strongly consistent) from {reader:?}")
});
drop(task);
......@@ -612,19 +617,21 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
return result;
}
let reader_desc = reader.map(|r| self.get_task_desc_fn(r));
let note = move || {
if let Some(reader_desc) = reader_desc.as_ref() {
format!("try_read_task_output (recompute) from {}", reader_desc())
} else {
"try_read_task_output (recompute, untracked)".to_string()
let note = event_note!({
let reader_desc = reader.map(|r| self.get_task_event_note(r));
move || {
if let Some(reader_desc) = reader_desc.as_ref() {
format!("try_read_task_output (recompute) from {}", reader_desc())
} else {
"try_read_task_output (recompute, untracked)".to_string()
}
}
};
});
// Output doesn't exist. We need to schedule the task to compute it.
let (item, listener) = CachedDataItem::new_scheduled_with_listener(
TaskExecutionReason::OutputNotAvailable,
self.get_task_desc_fn(task_id),
self.get_task_event_note(task_id),
note,
);
// It's not possible that the task is InProgress at this point. If it is InProgress {
......@@ -756,7 +763,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
} else if !is_scheduled
&& task.add(CachedDataItem::new_scheduled(
TaskExecutionReason::CellNotAvailable,
self.get_task_desc_fn(task_id),
self.get_task_event_note(task_id),
))
{
turbo_tasks.schedule(task_id);
......@@ -769,24 +776,26 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
&self,
task: &mut impl TaskGuard,
task_id: TaskId,
reader: Option<TaskId>,
_reader: Option<TaskId>,
cell: CellId,
) -> (EventListener, bool) {
let reader_desc = reader.map(|r| self.get_task_desc_fn(r));
let note = move || {
if let Some(reader_desc) = reader_desc.as_ref() {
format!("try_read_task_cell (in progress) from {}", reader_desc())
} else {
"try_read_task_cell (in progress, untracked)".to_string()
let _note = event_note!({
let reader_desc = _reader.map(|r| self.get_task_event_note(r));
move || {
if let Some(reader_desc) = reader_desc.as_ref() {
format!("try_read_task_cell (in progress) from {}", reader_desc())
} else {
"try_read_task_cell (in progress, untracked)".to_string()
}
}
};
});
if let Some(in_progress) = get!(task, InProgressCell { cell }) {
// Someone else is already computing the cell
let listener = in_progress.event.listen_with_note(note);
let listener = listen_event!(in_progress.event, _note);
return (listener, false);
}
let in_progress = InProgressCellState::new(task_id, cell);
let listener = in_progress.event.listen_with_note(note);
let listener = listen_event!(in_progress.event, _note);
task.add_new(CachedDataItem::InProgressCell {
cell,
value: in_progress,
......@@ -812,15 +821,16 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
None
}
// TODO feature flag that for hanging detection only
fn get_task_desc_fn(&self, task_id: TaskId) -> impl Fn() -> String + Send + Sync + 'static {
let task_type = self.lookup_task_type(task_id);
move || {
task_type.as_ref().map_or_else(
|| format!("{task_id:?} transient"),
|task_type| format!("{task_id:?} {task_type}"),
)
}
fn get_task_event_note(&self, _task_id: TaskId) -> impl Fn() -> String + Send + Sync + 'static {
event_note!({
let task_type = self.lookup_task_type(_task_id);
move || {
task_type.as_ref().map_or_else(
|| format!("{_task_id:?} transient"),
|task_type| format!("{_task_id:?} {task_type}"),
)
}
})
}
fn snapshot(&self) -> Option<(Instant, bool)> {
......
......@@ -1219,7 +1219,7 @@ impl AggregationUpdateQueue {
None
};
if let Some(reason) = should_schedule {
let description = ctx.get_task_desc_fn(task_id);
let description = ctx.get_task_event_note(task_id);
if task.add(CachedDataItem::new_scheduled(reason, description)) {
ctx.schedule(task_id);
}
......
......@@ -27,7 +27,7 @@ impl ConnectChildOperation {
if !ctx.should_track_children() {
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 description = ctx.get_task_event_note(child_task_id);
let should_schedule = task.add(CachedDataItem::new_scheduled(
TaskExecutionReason::Connect,
description,
......@@ -80,7 +80,7 @@ impl ConnectChildOperation {
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 description = ctx.get_task_event_note(child_task_id);
let should_schedule = task.add(CachedDataItem::new_scheduled(
TaskExecutionReason::Connect,
description,
......
......@@ -296,7 +296,7 @@ pub fn make_task_dirty_internal(
};
if should_schedule {
let description = ctx.get_task_desc_fn(task_id);
let description = ctx.get_task_event_note(task_id);
if task.add(CachedDataItem::new_scheduled(
TaskExecutionReason::Invalidated,
description,
......
......@@ -60,7 +60,7 @@ pub trait ExecuteContext<'e>: Sized {
where
T: Clone + Into<AnyOperation>;
fn suspending_requested(&self) -> bool;
fn get_task_desc_fn(&self, task_id: TaskId) -> impl Fn() -> String + Send + Sync + 'static;
fn get_task_event_note(&self, task_id: TaskId) -> impl Fn() -> String + Send + Sync + 'static;
fn get_task_description(&self, task_id: TaskId) -> String;
fn should_track_children(&self) -> bool;
fn should_track_dependencies(&self) -> bool;
......@@ -260,8 +260,8 @@ where
self.backend.suspending_requested()
}
fn get_task_desc_fn(&self, task_id: TaskId) -> impl Fn() -> String + Send + Sync + 'static {
self.backend.get_task_desc_fn(task_id)
fn get_task_event_note(&self, task_id: TaskId) -> impl Fn() -> String + Send + Sync + 'static {
self.backend.get_task_event_note(task_id)
}
fn get_task_description(&self, task_id: TaskId) -> String {
......
......@@ -7,7 +7,7 @@ use turbo_tasks::{
TypedSharedReference, ValueTypeId,
backend::TurboTasksExecutionError,
event::{Event, EventListener},
registry,
listen_event, new_event, registry,
};
use crate::{
......@@ -89,12 +89,14 @@ pub struct ActivenessState {
}
impl ActivenessState {
pub fn new(id: TaskId) -> Self {
pub fn new(_id: TaskId) -> Self {
Self {
active_counter: 0,
root_ty: None,
active_until_clean: false,
all_clean_event: Event::new(move || format!("ActivenessState::all_clean_event {id:?}")),
all_clean_event: new_event!(move || format!(
"ActivenessState::all_clean_event {_id:?}"
)),
}
}
......@@ -534,9 +536,9 @@ transient_traits!(InProgressCellState);
impl Eq for InProgressCellState {}
impl InProgressCellState {
pub fn new(task_id: TaskId, cell: CellId) -> Self {
pub fn new(_task_id: TaskId, _cell: CellId) -> Self {
InProgressCellState {
event: Event::new(move || format!("InProgressCellState::event ({task_id} {cell:?})")),
event: new_event!(move || format!("InProgressCellState::event ({_task_id} {_cell:?})")),
}
}
}
......@@ -725,11 +727,11 @@ impl CachedDataItem {
pub fn new_scheduled(
reason: TaskExecutionReason,
description: impl Fn() -> String + Sync + Send + 'static,
_description: impl Fn() -> String + Sync + Send + 'static,
) -> Self {
CachedDataItem::InProgress {
value: InProgressState::Scheduled {
done_event: Event::new(move || format!("{} done_event", description())),
done_event: new_event!(move || format!("{} done_event", _description())),
reason,
},
}
......@@ -737,11 +739,11 @@ impl CachedDataItem {
pub fn new_scheduled_with_listener(
reason: TaskExecutionReason,
description: impl Fn() -> String + Sync + Send + 'static,
note: impl Fn() -> String + Sync + Send + 'static,
_description: impl Fn() -> String + Sync + Send + 'static,
_note: impl Fn() -> String + Sync + Send + 'static,
) -> (Self, EventListener) {
let done_event = Event::new(move || format!("{} done_event", description()));
let listener = done_event.listen_with_note(note);
let done_event = new_event!(move || format!("{} done_event", _description()));
let listener = listen_event!(done_event, _note);
(
CachedDataItem::InProgress {
value: InProgressState::Scheduled { done_event, reason },
......
......@@ -11,7 +11,7 @@ use criterion::{
};
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use tokio::runtime::Runtime;
use turbo_tasks::event::Event;
use turbo_tasks::event::new_event;
use turbo_tasks_fs::rope::{Rope, RopeBuilder};
fn bench_file_watching(c: &mut Criterion) {
......@@ -32,7 +32,7 @@ fn bench_file_watching(c: &mut Criterion) {
BenchmarkId::new("bench_file_watching", "change file"),
move |b| {
let (tx, rx) = channel();
let event = Arc::new(Event::new(|| "test event".to_string()));
let event = Arc::new(new_event!(|| "test event".to_string()));
let mut watcher = RecommendedWatcher::new(tx, Config::default()).unwrap();
watcher.watch(temp_path, RecursiveMode::Recursive).unwrap();
......
......@@ -3,7 +3,7 @@ use std::{collections::hash_map::Entry, hash::Hash, marker::PhantomData};
use parking_lot::Mutex;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use turbo_tasks::event::Event;
use turbo_tasks::{event::Event, new_event};
pub struct MutexMap<K> {
map: Mutex<FxHashMap<K, Option<(Event, usize)>>>,
......@@ -30,7 +30,7 @@ impl<'a, K: Eq + Hash + Clone> MutexMap<K> {
event.listen()
}
None => {
let event = Event::new(|| "MutexMap".to_string());
let event = new_event!(|| "MutexMap".to_string());
let listener = event.listen();
*state = Some((event, 0));
listener
......
......@@ -20,6 +20,7 @@ use turbo_tasks::{
backend::{CellContent, TaskCollectiblesMap, TypedCellContent},
event::{Event, EventListener},
message_queue::CompilationEvent,
new_event,
test_helpers::with_turbo_tasks_for_testing,
util::{SharedError, StaticOrArc},
};
......@@ -51,7 +52,7 @@ impl VcStorage {
let i = {
let mut tasks = self.tasks.lock().unwrap();
let i = tasks.len();
tasks.push(Task::Spawned(Event::new(move || {
tasks.push(Task::Spawned(new_event!(move || {
format!("Task({i})::event")
})));
i
......
......@@ -22,6 +22,7 @@ use crate::{
emit,
event::{Event, EventListener},
manager::turbo_tasks_future_scope,
new_event,
trace::TraceRawVcs,
util::SharedError,
};
......@@ -85,7 +86,7 @@ impl EffectInstance {
EffectState::NotStarted(_) => {
let EffectState::NotStarted(inner) = std::mem::replace(
&mut *guard,
EffectState::Started(Event::new(|| "Effect".to_string())),
EffectState::Started(new_event!(|| "Effect".to_string())),
) else {
unreachable!();
};
......
......@@ -27,30 +27,26 @@ pub struct Event {
#[cfg(not(feature = "hanging_detection"))]
impl Event {
/// see [event_listener::Event]::new
#[allow(
clippy::new_without_default,
reason = "We don't want the Default trait to be consistent with the hanging_detection \
version"
)]
#[inline(always)]
pub fn new(_description: impl Fn() -> String + Sync + Send + 'static) -> Self {
pub fn new() -> Self {
Self {
event: event_listener::Event::new(),
}
}
/// see [event_listener::Event]::listen
#[inline(always)]
pub fn listen(&self) -> EventListener {
EventListener {
listener: self.event.listen(),
}
}
/// see [event_listener::Event]::listen
pub fn listen_with_note(
&self,
_note: impl Fn() -> String + Sync + Send + 'static,
) -> EventListener {
EventListener {
listener: self.event.listen(),
}
}
/// pulls out the event listener, leaving a new, empty event in its place.
pub fn take(&mut self) -> Self {
Self {
......@@ -74,7 +70,7 @@ impl Event {
pub fn listen(&self) -> EventListener {
EventListener {
description: self.description.clone(),
note: Arc::new(|| String::new()),
note: Arc::new(String::new),
future: Some(Box::pin(timeout(
Duration::from_secs(10),
self.event.listen(),
......@@ -210,3 +206,59 @@ impl Future for EventListener {
Poll::Ready(())
}
}
pub fn no_note() -> String {
String::new()
}
#[cfg(not(feature = "hanging_detection"))]
#[macro_export]
macro_rules! listen_event {
($self:expr, $note:expr) => {
$self.listen()
};
}
#[cfg(not(feature = "hanging_detection"))]
#[macro_export]
macro_rules! new_event {
($description:expr) => {
$crate::event::Event::new()
};
}
#[cfg(not(feature = "hanging_detection"))]
#[macro_export]
macro_rules! event_note {
($note:expr) => {
$crate::event::no_note
};
}
#[cfg(feature = "hanging_detection")]
#[macro_export]
macro_rules! listen_event {
($self:expr, $note:expr) => {
$self.listen_with_note($note)
};
}
#[cfg(feature = "hanging_detection")]
#[macro_export]
macro_rules! new_event {
($description:expr) => {
$crate::event::Event::new($description)
};
}
#[cfg(feature = "hanging_detection")]
#[macro_export]
macro_rules! event_note {
($note:expr) => {
$note
};
}
pub use event_note;
pub use listen_event;
pub use new_event;
......@@ -34,9 +34,11 @@ use crate::{
event::{Event, EventListener},
id::{BackendJobId, ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TraitTypeId},
id_factory::IdFactoryWithReuse,
listen_event,
macro_helpers::NativeFunction,
magic_any::MagicAny,
message_queue::{CompilationEvent, CompilationEventQueue},
new_event,
raw_vc::{CellId, RawVc},
registry,
serialization_invalidation::SerializationInvalidator,
......@@ -507,10 +509,10 @@ impl<B: Backend + 'static> TurboTasks<B> {
scheduled_tasks: AtomicUsize::new(0),
start: Default::default(),
aggregated_update: Default::default(),
event: Event::new(|| "TurboTasks::event".to_string()),
event_start: Event::new(|| "TurboTasks::event_start".to_string()),
event_foreground: Event::new(|| "TurboTasks::event_foreground".to_string()),
event_background: Event::new(|| "TurboTasks::event_background".to_string()),
event: new_event!(|| "TurboTasks::event".to_string()),
event_start: new_event!(|| "TurboTasks::event_start".to_string()),
event_foreground: new_event!(|| "TurboTasks::event_foreground".to_string()),
event_background: new_event!(|| "TurboTasks::event_background".to_string()),
program_start: Instant::now(),
compilation_events: CompilationEventQueue::default(),
});
......@@ -794,7 +796,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
.with(|gts| {
let mut gts_write = gts.write().unwrap();
let local_task_id = gts_write.create_local_task(LocalTask::Scheduled {
done_event: Event::new({
done_event: new_event!({
move || format!("LocalTask({task_type})::done_event")
}),
});
......@@ -983,9 +985,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
aggregation: Duration,
timeout: Duration,
) -> Option<UpdateInfo> {
let listener = self
.event
.listen_with_note(|| "wait for update info".to_string());
let listener = listen_event!(self.event, || "wait for update info".to_string());
let wait_for_finish = {
let (update, reason_set) = &mut *self.aggregated_update.lock().unwrap();
if aggregation.is_zero() {
......@@ -1009,9 +1009,8 @@ impl<B: Backend + 'static> TurboTasks<B> {
listener.await;
} else {
// wait for start, then wait for finish or timeout
let start_listener = self
.event_start
.listen_with_note(|| "wait for update info".to_string());
let start_listener =
listen_event!(self.event_start, || "wait for update info".to_string());
if self.currently_scheduled_tasks.load(Ordering::Acquire) == 0 {
start_listener.await;
} else {
......@@ -1029,7 +1028,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
() = tokio::time::sleep(aggregation) => {
break;
}
() = self.event.listen_with_note(|| "wait for update info".to_string()) => {
() = listen_event!(self.event, || "wait for update info".to_string()) => {
// Resets the sleep
}
}
......@@ -1063,7 +1062,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
self.backend.stopping(self);
self.stopped.store(true, Ordering::Release);
{
let listener = self.event.listen_with_note(|| "wait for stop".to_string());
let listener = listen_event!(self.event, || "wait for stop".to_string());
if self.currently_scheduled_tasks.load(Ordering::Acquire) != 0 {
listener.await;
}
......@@ -1096,7 +1095,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
TURBO_TASKS
.scope(this.clone(), async move {
while this.currently_scheduled_tasks.load(Ordering::Acquire) != 0 {
let listener = this.event.listen_with_note(|| {
let listener = listen_event!(this.event, || {
"background job waiting for execution".to_string()
});
if this.currently_scheduled_tasks.load(Ordering::Acquire) != 0 {
......
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