Skip to content

Commit 2f48b75

Browse files
committed
use the statistics?
1 parent 0dff8c6 commit 2f48b75

File tree

4 files changed

+44
-34
lines changed

4 files changed

+44
-34
lines changed

crates/next-build-test/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ fn main() {
127127
noop_backing_storage(),
128128
));
129129
let result = main_inner(&tt, strat, factor, limit, files).await;
130-
let memory = TurboMalloc::memory_usage();
131-
tracing::info!("memory usage: {} MiB", memory / 1024 / 1024);
130+
let memory = TurboMalloc::global_allocation_counters();
131+
tracing::info!("memory usage: {}", memory);
132132
let start = Instant::now();
133133
drop(tt);
134134
tracing::info!("drop {:?}", start.elapsed());

turbopack/crates/turbo-tasks-malloc/src/counter.rs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ thread_local! {
140140
static LOCAL_COUNTER: UnsafeCell<ThreadLocalCounter> = const {UnsafeCell::new(ThreadLocalCounter::new())};
141141
}
142142

143-
/// Returns an estimate of the
143+
// stores an estimate of the peak memory allocated.
144+
static MAX_ALLOCATED: AtomicUsize = AtomicUsize::new(0);
145+
146+
/// Returns an estimate of the total memory statistics
144147
pub fn global_counters() -> AllocationCounters {
145148
let mut counters = AllocationCounters::new();
146149
{
@@ -161,6 +164,11 @@ pub fn global_counters() -> AllocationCounters {
161164
counters.deallocations += thread.deallocations.load(Ordering::Acquire);
162165
}
163166
}
167+
168+
MAX_ALLOCATED.fetch_max(
169+
counters.allocations - counters.deallocations,
170+
Ordering::AcqRel,
171+
);
164172
counters
165173
}
166174

@@ -214,29 +222,32 @@ mod tests {
214222
use super::*;
215223

216224
#[test]
217-
fn counting() {
218-
let mut expected = get();
219-
add(100);
220-
// Initial change should fill up the buffer
221-
expected += TARGET_BUFFER + 100;
222-
assert_eq!(get(), expected);
223-
add(100);
224-
// Further changes should use the buffer
225-
assert_eq!(get(), expected);
226-
add(MAX_BUFFER);
227-
// Large changes should require more buffer space
228-
expected += 100 + MAX_BUFFER;
229-
assert_eq!(get(), expected);
230-
remove(100);
231-
// Small changes should use the buffer
232-
// buffer size is now TARGET_BUFFER + 100
233-
assert_eq!(get(), expected);
234-
remove(MAX_BUFFER);
235-
// The buffer should not grow over MAX_BUFFER
236-
// buffer size would be TARGET_BUFFER + 100 + MAX_BUFFER
237-
// but it will be reduce to TARGET_BUFFER
238-
// this means the global counter should reduce by 100 + MAX_BUFFER
239-
expected -= MAX_BUFFER + 100;
240-
assert_eq!(get(), expected);
225+
fn test_2_threads() {
226+
let mut t1 = ThreadLocalCounter::new();
227+
let mut t2 = ThreadLocalCounter::new();
228+
229+
assert_eq!(0, global_counters().allocations);
230+
231+
t1.register();
232+
t2.register();
233+
assert_eq!(0, global_counters().allocations);
234+
235+
t1.add(100);
236+
t2.add(300);
237+
assert_eq!(400, global_counters().allocations);
238+
239+
t2.remove(300);
240+
t1.remove(100);
241+
assert_eq!(
242+
AllocationCounters {
243+
allocations: 400,
244+
allocation_count: 2,
245+
deallocations: 400,
246+
deallocation_count: 2,
247+
..Default::default()
248+
},
249+
global_counters()
250+
);
251+
assert_eq!(400, MAX_ALLOCATED.load(Ordering::Acquire));
241252
}
242253
}

turbopack/crates/turbo-tasks-malloc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl AllocationInfo {
2525
}
2626
}
2727

28-
#[derive(Default, Clone, Debug)]
28+
#[derive(Default, Clone, Debug, Eq, PartialEq)]
2929
pub struct AllocationCounters {
3030
pub allocations: usize,
3131
pub deallocations: usize,

turbopack/crates/turbopack-cli/src/dev/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use rustc_hash::FxHashSet;
1414
use turbo_rcstr::{RcStr, rcstr};
1515
use turbo_tasks::{
1616
NonLocalValue, OperationVc, ResolvedVc, TransientInstance, TurboTasks, UpdateInfo, Vc,
17-
trace::TraceRawVcs,
18-
util::{FormatBytes, FormatDuration},
17+
trace::TraceRawVcs, util::FormatDuration,
1918
};
2019
use turbo_tasks_backend::{
2120
BackendOptions, NoopBackingStorage, TurboTasksBackend, noop_backing_storage,
@@ -444,7 +443,7 @@ pub async fn start_server(args: &DevArguments) -> Result<()> {
444443
"{event_type} - initial compilation {start} ({memory})",
445444
event_type = "event".purple(),
446445
start = FormatDuration(start.elapsed()),
447-
memory = FormatBytes(TurboMalloc::memory_usage())
446+
memory = TurboMalloc::global_allocation_counters()
448447
);
449448
}
450449

@@ -470,7 +469,7 @@ pub async fn start_server(args: &DevArguments) -> Result<()> {
470469
event_type = "event".purple(),
471470
duration = FormatDuration(duration),
472471
tasks = tasks,
473-
memory = FormatBytes(TurboMalloc::memory_usage())
472+
memory = TurboMalloc::global_allocation_counters()
474473
);
475474
}
476475
(true, false) => {
@@ -480,7 +479,7 @@ pub async fn start_server(args: &DevArguments) -> Result<()> {
480479
event_type = "event".purple(),
481480
duration = FormatDuration(duration),
482481
tasks = tasks,
483-
memory = FormatBytes(TurboMalloc::memory_usage())
482+
memory = TurboMalloc::global_allocation_counters()
484483
);
485484
}
486485
(false, true) => {
@@ -506,7 +505,7 @@ pub async fn start_server(args: &DevArguments) -> Result<()> {
506505
print!(
507506
"\x1b[2K{event_type} - updating for {progress_counter}s... ({memory})\r",
508507
event_type = "event".purple(),
509-
memory = FormatBytes(TurboMalloc::memory_usage())
508+
memory = TurboMalloc::global_allocation_counters()
510509
);
511510
} else {
512511
print!(

0 commit comments

Comments
 (0)