Commit 7267199a authored by Tobias Koppers's avatar Tobias Koppers Committed by Tobias Koppers

Inherit priority from invalidating task

parent 25a08e43
......@@ -13,7 +13,7 @@ bench = false
workspace = true
[features]
default = []
default = ["trace_task_dirty"]
print_cache_item_size = []
no_fast_stale = []
verify_serialization = []
......
......@@ -25,8 +25,8 @@ use tokio::time::{Duration, Instant};
use tracing::{Span, trace_span};
use turbo_tasks::{
CellId, FxDashMap, KeyValuePair, RawVc, ReadCellOptions, ReadCellTracking, ReadConsistency,
ReadOutputOptions, ReadTracking, TRANSIENT_TASK_BIT, TaskExecutionReason, TaskId, TraitTypeId,
TurboTasksBackendApi, ValueTypeId,
ReadOutputOptions, ReadTracking, TRANSIENT_TASK_BIT, TaskExecutionReason, TaskId, TaskPriority,
TraitTypeId, TurboTasksBackendApi, ValueTypeId,
backend::{
Backend, CachedTaskType, CellContent, TaskExecutionSpec, TransientTaskRoot,
TransientTaskType, TurboTasksExecutionError, TypedCellContent, VerificationMode,
......@@ -1682,6 +1682,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
fn try_start_task_execution(
&self,
task_id: TaskId,
priority: TaskPriority,
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
) -> Option<TaskExecutionSpec<'_>> {
enum TaskType {
......@@ -1803,7 +1804,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
arg,
} = &*task_type;
(
native_fn.span(task_id.persistence(), execution_reason),
native_fn.span(task_id.persistence(), execution_reason, priority),
native_fn.execute(*this, &**arg),
)
}
......@@ -3333,9 +3334,11 @@ impl<B: BackingStorage> Backend for TurboTasksBackend<B> {
fn try_start_task_execution(
&self,
task_id: TaskId,
priority: TaskPriority,
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
) -> Option<TaskExecutionSpec<'_>> {
self.0.try_start_task_execution(task_id, turbo_tasks)
self.0
.try_start_task_execution(task_id, priority, turbo_tasks)
}
fn task_execution_completed(
......
......@@ -566,9 +566,9 @@ where
fn schedule_task(&self, task: Self::TaskGuardImpl) {
let priority = if get!(task, Output).is_some() {
TaskPriority::new(get!(task, LeafDistance).copied().unwrap_or_default().min)
TaskPriority::invalidation(get!(task, LeafDistance).copied().unwrap_or_default().min)
} else {
TaskPriority::root()
TaskPriority::initial()
};
self.turbo_tasks.schedule(task.id(), priority);
}
......
......@@ -73,15 +73,14 @@ async fn spawns_detached(
notify: TransientInstance<NotifyTaskInput>,
sender: TransientInstance<WatchSenderTaskInput<Option<Vc<u32>>>>,
) -> Vc<()> {
tokio::spawn(turbo_tasks().detached_for_testing(Box::pin(async move {
turbo_tasks().spawn_detached_for_testing(Box::pin(async move {
println!("spawns_detached: waiting for notify");
notify.0.notified().await;
println!("spawns_detached: notified, sending value");
// creating cells after the normal lifetime of the task should be okay, as the parent task
// is waiting on us before exiting!
sender.0.send(Some(Vc::cell(42))).unwrap();
Ok(())
})));
}));
Vc::cell(())
}
......@@ -145,10 +144,10 @@ async fn spawns_detached_changing(
changing_input_outer: Vc<ChangingInput>,
) -> Vc<u32> {
let tt = turbo_tasks();
tokio::spawn(tt.clone().detached_for_testing(Box::pin(async move {
tt.clone().spawn_detached_for_testing(Box::pin(async move {
sleep(Duration::from_millis(100)).await;
// nested detached_for_testing calls should work
tokio::spawn(tt.clone().detached_for_testing(Box::pin(async move {
// nested spawn_detached_for_testing calls should work
tt.clone().spawn_detached_for_testing(Box::pin(async move {
sleep(Duration::from_millis(100)).await;
// creating cells after the normal lifetime of the task should be okay, as the parent
// task is waiting on us before exiting!
......@@ -158,10 +157,8 @@ async fn spawns_detached_changing(
*read_changing_input(changing_input_detached).await.unwrap(),
)))
.unwrap();
Ok(())
})));
Ok(())
})));
}));
}));
Vc::cell(*read_changing_input(changing_input_outer).await.unwrap())
}
......
......@@ -292,10 +292,10 @@ impl TurboTasksApi for VcStorage {
// no-op
}
fn detached_for_testing(
fn spawn_detached_for_testing(
&self,
_f: std::pin::Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>,
) -> std::pin::Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>> {
_f: std::pin::Pin<Box<dyn Future<Output = ()> + Send + 'static>>,
) {
unimplemented!()
}
......
......@@ -26,8 +26,8 @@ use turbo_rcstr::RcStr;
use crate::{
RawVc, ReadCellOptions, ReadOutputOptions, ReadRef, SharedReference, TaskId, TaskIdSet,
TraitRef, TraitTypeId, TurboTasksPanic, ValueTypeId, VcRead, VcValueTrait, VcValueType,
event::EventListener, macro_helpers::NativeFunction, magic_any::MagicAny,
TaskPriority, TraitRef, TraitTypeId, TurboTasksPanic, ValueTypeId, VcRead, VcValueTrait,
VcValueType, event::EventListener, macro_helpers::NativeFunction, magic_any::MagicAny,
manager::TurboTasksBackendApi, raw_vc::CellId, registry,
task::shared_reference::TypedSharedReference, task_statistics::TaskStatisticsApi,
triomphe_utils::unchecked_sidecast_triomphe_arc,
......@@ -418,6 +418,7 @@ pub trait Backend: Sync + Send {
fn try_start_task_execution<'a>(
&'a self,
task: TaskId,
priority: TaskPriority,
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
) -> Option<TaskExecutionSpec<'a>>;
......
This diff is collapsed.
......@@ -8,7 +8,7 @@ use tracing::Span;
use turbo_bincode::{AnyDecodeFn, AnyEncodeFn};
use crate::{
RawVc, TaskExecutionReason, TaskInput, TaskPersistence,
RawVc, TaskExecutionReason, TaskInput, TaskPersistence, TaskPriority,
magic_any::{MagicAny, any_as_encode},
task::{
IntoTaskFn, TaskFn,
......@@ -238,7 +238,12 @@ impl NativeFunction {
}
}
pub fn span(&'static self, persistence: TaskPersistence, reason: TaskExecutionReason) -> Span {
pub fn span(
&'static self,
persistence: TaskPersistence,
reason: TaskExecutionReason,
priority: TaskPriority,
) -> Span {
let flags = match persistence {
TaskPersistence::Persistent => "",
TaskPersistence::Transient => "transient",
......@@ -246,13 +251,14 @@ impl NativeFunction {
tracing::trace_span!(
"turbo_tasks::function",
name = self.name,
priority = %priority,
flags = flags,
reason = reason.as_str()
)
}
pub fn resolve_span(&'static self) -> Span {
tracing::trace_span!("turbo_tasks::resolve_call", name = self.name)
pub fn resolve_span(&'static self, priority: TaskPriority) -> Span {
tracing::trace_span!("turbo_tasks::resolve_call", name = self.name, priority = %priority)
}
}
impl PartialEq for NativeFunction {
......
......@@ -13,28 +13,30 @@ use std::{
use parking_lot::Mutex;
use pin_project_lite::pin_project;
use tokio::sync::oneshot::{Receiver, Sender};
pub trait Executor<C, T>: Send + Sync {
pub trait Executor<C, T, P>: Send + Sync {
type Future: Future<Output = ()> + Send;
fn execute(&self, execute_context: &Arc<C>, task: T) -> Self::Future;
fn execute(&self, execute_context: &Arc<C>, task: T, priority: P) -> Self::Future;
}
impl<C, T, F, Fut> Executor<C, T> for F
impl<C, T, P, F, Fut> Executor<C, T, P> for F
where
F: Fn(&Arc<C>, T) -> Fut + Send + Sync,
F: Fn(&Arc<C>, T, P) -> Fut + Send + Sync,
Fut: Future<Output = ()> + Send,
{
type Future = Fut;
fn execute(&self, execute_context: &Arc<C>, task: T) -> Self::Future {
(self)(execute_context, task)
fn execute(&self, execute_context: &Arc<C>, task: T, priority: P) -> Self::Future {
(self)(execute_context, task, priority)
}
}
struct HeapItem<P, T> {
priority: P,
task: T,
tx: Option<Sender<()>>,
}
impl<P: Eq, T> PartialEq for HeapItem<P, T> {
......@@ -61,7 +63,7 @@ pub struct PriorityRunner<
C: Send + Sync + 'static,
T: Send + 'static,
P: Ord + Send + 'static,
E: Executor<C, T> + 'static,
E: Executor<C, T, P> + 'static,
> {
executor: E,
target_workers: usize,
......@@ -78,7 +80,7 @@ impl<
C: Send + Sync + 'static,
T: Send + 'static,
P: Debug + Ord + Send + 'static,
E: Executor<C, T> + 'static,
E: Executor<C, T, P> + 'static,
> PriorityRunner<C, T, P, E>
{
pub fn new(executor: E) -> Self {
......@@ -94,12 +96,33 @@ impl<
}
pub fn schedule(self: &Arc<Self>, execute_context: &Arc<C>, task: T, priority: P) {
self.schedule_internal(execute_context, task, priority, None);
}
pub fn schedule_with_join_handle(
self: &Arc<Self>,
execute_context: &Arc<C>,
task: T,
priority: P,
) -> JoinHandle {
let (tx, rx) = tokio::sync::oneshot::channel();
self.schedule_internal(execute_context, task, priority, Some(tx));
JoinHandle { receiver: rx }
}
fn schedule_internal(
self: &Arc<Self>,
execute_context: &Arc<C>,
task: T,
priority: P,
tx: Option<Sender<()>>,
) {
let mut inner = self.inner.lock();
if !inner.queue.is_empty() {
// If there is already work in the queue, we don't have any
// free capacity so we can just push the task to the queue.
// It will be picked up by existing workers.
inner.queue.push(HeapItem { priority, task });
inner.queue.push(HeapItem { priority, task, tx });
return;
}
// The queue is empty, so we might have free capacity to spawn a new worker.
......@@ -108,11 +131,11 @@ impl<
// We have free capacity, spawn a new worker to execute this task immediately.
drop(inner);
let future = self.executor.execute(execute_context, task);
WorkerFuture::spawn(future, execute_context.clone(), self.clone(), true);
let future = self.executor.execute(execute_context, task, priority);
WorkerFuture::spawn(future, tx, execute_context.clone(), self.clone(), true);
} else {
// No free capacity, push the task to the queue.
inner.queue.push(HeapItem { priority, task });
inner.queue.push(HeapItem { priority, task, tx });
drop(inner);
// Active polls might be reduced in the meantime and we might have free capacity now,
......@@ -127,10 +150,18 @@ impl<
}
}
fn pop_future_from_worker(&self, execute_context: &Arc<C>) -> Option<E::Future> {
fn pop_future_from_worker(
&self,
execute_context: &Arc<C>,
) -> Option<(E::Future, Option<Sender<()>>)> {
let mut inner = self.inner.lock();
if let Some(heap_item) = inner.queue.pop() {
Some(self.executor.execute(execute_context, heap_item.task))
let tx = heap_item.tx;
Some((
self.executor
.execute(execute_context, heap_item.task, heap_item.priority),
tx,
))
} else {
None
}
......@@ -143,13 +174,16 @@ impl<
) -> bool {
let mut inner = self.inner.lock();
if let Some(heap_item) = inner.queue.pop() {
let new_future = self.executor.execute(execute_context, heap_item.task);
let tx = heap_item.tx;
let new_future =
self.executor
.execute(execute_context, heap_item.task, heap_item.priority);
drop(inner);
if !has_active_poll {
self.active_polls.fetch_add(1, Ordering::Relaxed);
}
WorkerFuture::spawn(new_future, execute_context.clone(), self.clone(), true);
WorkerFuture::spawn(new_future, tx, execute_context.clone(), self.clone(), true);
true
} else {
false
......@@ -169,12 +203,13 @@ pin_project! {
P: Ord,
P: Send,
P: 'static,
E: Executor<C, T>,
E: Executor<C, T, P>,
E: 'static,
{
#[pin]
future: E::Future,
tx: Option<Sender<()>>,
execute_context: Arc<C>,
runner: Arc<PriorityRunner<C, T, P, E>>,
has_active_poll: bool,
......@@ -185,17 +220,19 @@ impl<
C: Send + Sync + 'static,
T: Send + 'static,
P: Debug + Ord + Send + 'static,
E: Executor<C, T> + 'static,
E: Executor<C, T, P> + 'static,
> WorkerFuture<C, T, P, E>
{
fn spawn(
future: E::Future,
tx: Option<Sender<()>>,
execute_context: Arc<C>,
runner: Arc<PriorityRunner<C, T, P, E>>,
has_active_poll: bool,
) {
tokio::task::spawn(Self {
future,
tx,
execute_context,
runner,
has_active_poll,
......@@ -207,7 +244,7 @@ impl<
C: Send + Sync + 'static,
T: Send + 'static,
P: Debug + Ord + Send + 'static,
E: Executor<C, T> + 'static,
E: Executor<C, T, P> + 'static,
> Future for WorkerFuture<C, T, P, E>
{
type Output = ();
......@@ -221,9 +258,14 @@ impl<
loop {
match this.future.as_mut().poll(cx) {
Poll::Ready(()) => {
// Notify that the task is done
if let Some(tx) = this.tx.take() {
let _ = tx.send(());
}
// This future is done, we need to check the queue for more tasks,
// so we can continue working on a new future in this worker.
if let Some(new_future) =
if let Some((new_future, new_tx)) =
this.runner.pop_future_from_worker(&this.execute_context)
{
// We are replacing the future with a new one, but the current future is
......@@ -237,6 +279,7 @@ impl<
drop_in_place(future_slot);
future_slot.write(new_future);
}
*this.tx = new_tx;
continue;
} else {
// No more tasks to execute
......@@ -276,6 +319,25 @@ impl<
}
}
pub struct JoinHandle {
receiver: Receiver<()>,
}
impl Future for JoinHandle {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
match Pin::new(&mut this.receiver).poll(cx) {
Poll::Ready(result) => {
let _ = result;
Poll::Ready(())
}
Poll::Pending => Poll::Pending,
}
}
}
#[cfg(test)]
mod tests {
use std::{sync::Arc, thread::sleep, time::Duration};
......@@ -286,10 +348,15 @@ mod tests {
async fn test_cpu_bound_tasks() {
struct ExecutorImpl;
impl Executor<Mutex<Vec<u32>>, u32> for ExecutorImpl {
impl Executor<Mutex<Vec<u32>>, u32, u32> for ExecutorImpl {
type Future = Pin<Box<dyn Future<Output = ()> + Send>>;
fn execute(&self, execute_context: &Arc<Mutex<Vec<u32>>>, task: u32) -> Self::Future {
fn execute(
&self,
execute_context: &Arc<Mutex<Vec<u32>>>,
task: u32,
_priority: u32,
) -> Self::Future {
let execute_context = execute_context.clone();
Box::pin(async move {
println!("Executing task {}...", task);
......@@ -334,10 +401,15 @@ mod tests {
async fn test_waiting_tasks() {
struct ExecutorImpl;
impl Executor<Mutex<Vec<u32>>, u32> for ExecutorImpl {
impl Executor<Mutex<Vec<u32>>, u32, u32> for ExecutorImpl {
type Future = Pin<Box<dyn Future<Output = ()> + Send>>;
fn execute(&self, execute_context: &Arc<Mutex<Vec<u32>>>, task: u32) -> Self::Future {
fn execute(
&self,
execute_context: &Arc<Mutex<Vec<u32>>>,
task: u32,
_priority: u32,
) -> Self::Future {
let execute_context = execute_context.clone();
Box::pin(async move {
println!("Executing task {}...", task);
......
......@@ -9,9 +9,9 @@ use tracing::Span;
use turbo_bincode::{AnyDecodeFn, AnyEncodeFn};
use crate::{
RawVc, SharedReference, VcValueType, id::TraitTypeId, macro_helpers::NativeFunction,
magic_any::any_as_encode, registry, task::shared_reference::TypedSharedReference,
vc::VcCellMode,
RawVc, SharedReference, TaskPriority, VcValueType, id::TraitTypeId,
macro_helpers::NativeFunction, magic_any::any_as_encode, registry,
task::shared_reference::TypedSharedReference, vc::VcCellMode,
};
type RawCellFactoryFn = fn(TypedSharedReference) -> RawVc;
......@@ -231,10 +231,11 @@ impl Debug for TraitMethod {
}
}
impl TraitMethod {
pub(crate) fn resolve_span(&self) -> Span {
pub(crate) fn resolve_span(&self, priority: TaskPriority) -> Span {
tracing::trace_span!(
"turbo_tasks::resolve_trait_call",
name = format_args!("{}::{}", &self.trait_name, &self.method_name),
priority = %priority,
)
}
}
......
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