-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor memory for tracing track memory for tasks
- Loading branch information
Showing
14 changed files
with
220 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
sync::{Arc, Mutex}, | ||
task::{Context, Poll}, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
use pin_project_lite::pin_project; | ||
use tokio::{task::futures::TaskLocalFuture, task_local}; | ||
use turbo_tasks_malloc::{AllocationInfo, TurboMalloc}; | ||
|
||
task_local! { | ||
static EXTRA: Arc<Mutex<(Duration, usize, usize)>>; | ||
} | ||
|
||
pin_project! { | ||
pub struct CaptureFuture<T, F: Future<Output = T>> { | ||
cell: Arc<Mutex<(Duration, usize, usize)>>, | ||
#[pin] | ||
future: TaskLocalFuture<Arc<Mutex<(Duration, usize, usize)>>, F>, | ||
duration: Duration, | ||
allocations: usize, | ||
deallocations: usize, | ||
} | ||
} | ||
|
||
impl<T, F: Future<Output = T>> CaptureFuture<T, F> { | ||
pub fn new(future: F) -> Self { | ||
let cell = Arc::new(Mutex::new((Duration::ZERO, 0, 0))); | ||
Self { | ||
future: EXTRA.scope(cell.clone(), future), | ||
cell, | ||
duration: Duration::ZERO, | ||
allocations: 0, | ||
deallocations: 0, | ||
} | ||
} | ||
} | ||
|
||
pub fn add_duration(duration: Duration) { | ||
EXTRA.with(|cell| cell.lock().unwrap().0 += duration); | ||
} | ||
|
||
pub fn add_allocation_info(alloc_info: AllocationInfo) { | ||
EXTRA.with(|cell| { | ||
let mut guard = cell.lock().unwrap(); | ||
guard.1 += alloc_info.allocations; | ||
guard.2 += alloc_info.deallocations; | ||
}); | ||
} | ||
|
||
impl<T, F: Future<Output = T>> Future for CaptureFuture<T, F> { | ||
type Output = (T, Duration, Instant, usize); | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let this = self.project(); | ||
let start = Instant::now(); | ||
let start_allocations = TurboMalloc::allocations(); | ||
let result = this.future.poll(cx); | ||
let elapsed = start.elapsed(); | ||
let allocations = start_allocations.until_now(); | ||
*this.duration += elapsed; | ||
*this.allocations += allocations.allocations; | ||
*this.deallocations += allocations.deallocations; | ||
match result { | ||
Poll::Ready(r) => { | ||
let (duration, allocations, deallocations) = *this.cell.lock().unwrap(); | ||
Poll::Ready(( | ||
r, | ||
*this.duration + duration, | ||
start + elapsed, | ||
allocations.saturating_sub(deallocations), | ||
)) | ||
} | ||
Poll::Pending => Poll::Pending, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.