Skip to content

Commit

Permalink
Merge branch 'main' into sokra/trace-viewer-focus
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra authored May 17, 2024
2 parents b66df1f + 11e59b9 commit d33b477
Show file tree
Hide file tree
Showing 22 changed files with 527 additions and 339 deletions.
405 changes: 195 additions & 210 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ async-recursion = "1.0.2"
# Keep consistent with preset_env_base through swc_core
browserslist-rs = { version = "0.15.0" }
miette = { version = "5.10.0", features = ["fancy"] }
mdxjs = "0.1.23"
modularize_imports = { version = "0.68.9" }
styled_components = { version = "0.96.8" }
styled_jsx = { version = "0.73.13" }
swc_core = { version = "0.90.33", features = [
mdxjs = "0.2.2"
modularize_imports = { version = "0.68.14" }
styled_components = { version = "0.96.15" }
styled_jsx = { version = "0.73.20" }
swc_core = { version = "0.92.5", features = [
"ecma_loader_lru",
"ecma_loader_parking_lot",
] }
swc_emotion = { version = "0.72.8" }
swc_relay = { version = "0.44.8" }
testing = { version = "0.35.23" }
swc_emotion = { version = "0.72.13" }
swc_relay = { version = "0.44.13" }
testing = { version = "0.35.24" }
# Temporary: Reference the latest git minor version of pathfinder_simd until it's published.
pathfinder_simd = "0.5.3"

Expand Down
34 changes: 10 additions & 24 deletions crates/turbo-tasks-malloc/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::{
sync::atomic::{AtomicUsize, Ordering},
};

use crate::AllocationCounters;

static ALLOCATED: AtomicUsize = AtomicUsize::new(0);
const KB: usize = 1024;
/// When global counter is updates we will keep a thread-local buffer of this
Expand All @@ -13,37 +15,20 @@ const TARGET_BUFFER: usize = 100 * KB;
/// global counter.
const MAX_BUFFER: usize = 200 * KB;

#[derive(Default)]
pub struct AllocationInfo {
pub allocations: usize,
pub deallocations: usize,
pub allocation_count: usize,
pub deallocation_count: usize,
}

impl AllocationInfo {
pub fn is_empty(&self) -> bool {
self.allocations == 0
&& self.deallocations == 0
&& self.allocation_count == 0
&& self.deallocation_count == 0
}
}

#[derive(Default)]
struct ThreadLocalCounter {
/// Thread-local buffer of allocated bytes that have been added to the
/// global counter desprite not being allocated yet. It is unsigned so that
/// means the global counter is always equal or greater than the real
/// value.
buffer: usize,
allocation_info: AllocationInfo,
allocation_counters: AllocationCounters,
}

impl ThreadLocalCounter {
fn add(&mut self, size: usize) {
self.allocation_info.allocations += size;
self.allocation_info.allocation_count += 1;
self.allocation_counters.allocations += size;
self.allocation_counters.allocation_count += 1;
if self.buffer >= size {
self.buffer -= size;
} else {
Expand All @@ -54,8 +39,8 @@ impl ThreadLocalCounter {
}

fn remove(&mut self, size: usize) {
self.allocation_info.deallocations += size;
self.allocation_info.deallocation_count += 1;
self.allocation_counters.deallocations += size;
self.allocation_counters.deallocation_count += 1;
self.buffer += size;
if self.buffer > MAX_BUFFER {
let offset = self.buffer - TARGET_BUFFER;
Expand All @@ -69,6 +54,7 @@ impl ThreadLocalCounter {
ALLOCATED.fetch_sub(self.buffer, Ordering::Relaxed);
self.buffer = 0;
}
self.allocation_counters = AllocationCounters::default();
}
}

Expand All @@ -80,8 +66,8 @@ pub fn get() -> usize {
ALLOCATED.load(Ordering::Relaxed)
}

pub fn pop_allocations() -> AllocationInfo {
with_local_counter(|local| std::mem::take(&mut local.allocation_info))
pub fn allocation_counters() -> AllocationCounters {
with_local_counter(|local| local.allocation_counters.clone())
}

fn with_local_counter<T>(f: impl FnOnce(&mut ThreadLocalCounter) -> T) -> T {
Expand Down
47 changes: 44 additions & 3 deletions crates/turbo-tasks-malloc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,50 @@
mod counter;

use std::alloc::{GlobalAlloc, Layout};
use std::{
alloc::{GlobalAlloc, Layout},
marker::PhantomData,
};

use self::counter::{add, flush, get, remove};

#[derive(Default, Clone, Debug)]
pub struct AllocationInfo {
pub allocations: usize,
pub deallocations: usize,
pub allocation_count: usize,
pub deallocation_count: usize,
}

impl AllocationInfo {
pub fn is_empty(&self) -> bool {
self.allocations == 0
&& self.deallocations == 0
&& self.allocation_count == 0
&& self.deallocation_count == 0
}
}

#[derive(Default, Clone, Debug)]
pub struct AllocationCounters {
pub allocations: usize,
pub deallocations: usize,
pub allocation_count: usize,
pub deallocation_count: usize,
_not_send: PhantomData<*mut ()>,
}

impl AllocationCounters {
pub fn until_now(&self) -> AllocationInfo {
let new = TurboMalloc::allocation_counters();
AllocationInfo {
allocations: new.allocations - self.allocations,
deallocations: new.deallocations - self.deallocations,
allocation_count: new.allocation_count - self.allocation_count,
deallocation_count: new.deallocation_count - self.deallocation_count,
}
}
}

/// Turbo's preferred global allocator. This is a new type instead of a type
/// alias because you can't use type aliases to instantiate unit types (E0423).
pub struct TurboMalloc;
Expand All @@ -17,8 +58,8 @@ impl TurboMalloc {
flush();
}

pub fn pop_allocations() -> self::counter::AllocationInfo {
self::counter::pop_allocations()
pub fn allocation_counters() -> AllocationCounters {
self::counter::allocation_counters()
}
}

Expand Down
15 changes: 7 additions & 8 deletions crates/turbo-tasks-memory/src/aggregation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,7 @@ fn many_children() {
}
let children_duration = start.elapsed();
println!("Children: {:?}", children_duration);
let mut number_of_slow_children = 0;
for j in 0..10 {
for j in 0.. {
let start = Instant::now();
for i in 0..CHILDREN {
let node = Node::new(50000 + j * 10000 + i);
Expand All @@ -905,8 +904,12 @@ fn many_children() {
}
let dur = start.elapsed();
println!("Children: {:?}", dur);
if dur > children_duration * 2 {
number_of_slow_children += 1;
let is_slow = dur > children_duration * 2;
if j > 10 && !is_slow {
break;
}
if j > 20 {
panic!("Adding children has become slower over time");
}
}

Expand All @@ -919,10 +922,6 @@ fn many_children() {
}
println!("Roots: {:?}", start.elapsed());

// Technically it should always be 0, but the performance of the environment
// might vary so we accept a few slow children
assert!(number_of_slow_children < 3);

check_invariants(&ctx, roots.iter().cloned().map(NodeRef));

// let root = NodeRef(roots[0].clone());
Expand Down
3 changes: 2 additions & 1 deletion crates/turbo-tasks-memory/src/memory_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,12 @@ impl Backend for MemoryBackend {
task_id: TaskId,
duration: Duration,
instant: Instant,
memory_usage: usize,
stateful: bool,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>,
) -> bool {
let reexecute = self.with_task(task_id, |task| {
task.execution_completed(duration, instant, stateful, self, turbo_tasks)
task.execution_completed(duration, instant, memory_usage, stateful, self, turbo_tasks)
});
if !reexecute {
self.run_gc(false, turbo_tasks);
Expand Down
1 change: 1 addition & 0 deletions crates/turbo-tasks-memory/src/memory_backend_with_pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,7 @@ impl<P: PersistedGraph> Backend for MemoryBackendWithPersistedGraph<P> {
task: TaskId,
duration: Duration,
_instant: Instant,
_memory_usage: usize,
_stateful: bool,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackendWithPersistedGraph<P>>,
) -> bool {
Expand Down
1 change: 1 addition & 0 deletions crates/turbo-tasks-memory/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ impl Task {
&self,
duration: Duration,
instant: Instant,
_memory_usage: usize,
stateful: bool,
backend: &MemoryBackend,
turbo_tasks: &dyn TurboTasksBackendApi<MemoryBackend>,
Expand Down
1 change: 1 addition & 0 deletions crates/turbo-tasks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
turbo-tasks-hash = { workspace = true }
turbo-tasks-macros = { workspace = true }
turbo-tasks-malloc = { workspace = true }

[dev-dependencies]
serde_test = "1.0.157"
Expand Down
1 change: 1 addition & 0 deletions crates/turbo-tasks/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ pub trait Backend: Sync + Send {
task: TaskId,
duration: Duration,
instant: Instant,
memory_usage: usize,
stateful: bool,
turbo_tasks: &dyn TurboTasksBackendApi<Self>,
) -> bool;
Expand Down
76 changes: 76 additions & 0 deletions crates/turbo-tasks/src/capture_future.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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::allocation_counters();
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();
let memory_usage = (*this.allocations + allocations)
.saturating_sub(*this.deallocations + deallocations);
Poll::Ready((r, *this.duration + duration, start + elapsed, memory_usage))
}
Poll::Pending => Poll::Pending,
}
}
}
2 changes: 1 addition & 1 deletion crates/turbo-tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#![feature(never_type)]

pub mod backend;
mod capture_future;
mod collectibles;
mod completion;
pub mod debug;
Expand Down Expand Up @@ -64,7 +65,6 @@ pub mod registry;
pub mod small_duration;
mod state;
pub mod task;
mod timed_future;
pub mod trace;
mod trait_ref;
pub mod util;
Expand Down
Loading

0 comments on commit d33b477

Please sign in to comment.