File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed
turbopack/crates/turbo-tasks-malloc/src Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,10 @@ use std::{
6
6
7
7
use crate :: AllocationCounters ;
8
8
9
+ /// Tracks the current total amount of memory allocated through all the [ThreadLocalCounter]
10
+ /// instances. This is an overestimate as individual threads 'preallocate' a [TARGET_BUFFER] bytes
11
+ /// to reduce the number of global synchronizations. This means at any given time this might
12
+ /// overcount by up to [MAX_BUFFER] bytes for each thread.
9
13
static ALLOCATED : AtomicUsize = AtomicUsize :: new ( 0 ) ;
10
14
const KB : usize = 1024 ;
11
15
/// When global counter is updates we will keep a thread-local buffer of this
@@ -26,6 +30,12 @@ struct ThreadLocalCounter {
26
30
}
27
31
28
32
impl ThreadLocalCounter {
33
+ const fn new ( ) -> Self {
34
+ Self {
35
+ buffer : 0 ,
36
+ allocation_counters : AllocationCounters :: new ( ) ,
37
+ }
38
+ }
29
39
fn add ( & mut self , size : usize ) {
30
40
self . allocation_counters . allocations += size;
31
41
self . allocation_counters . allocation_count += 1 ;
@@ -88,7 +98,7 @@ impl ThreadLocalCounter {
88
98
}
89
99
90
100
thread_local ! {
91
- static LOCAL_COUNTER : UnsafeCell <ThreadLocalCounter > = UnsafeCell :: new( ThreadLocalCounter :: default ( ) ) ;
101
+ static LOCAL_COUNTER : UnsafeCell <ThreadLocalCounter > = const { UnsafeCell :: new( ThreadLocalCounter :: new ( ) ) } ;
92
102
}
93
103
94
104
pub fn get ( ) -> usize {
Original file line number Diff line number Diff line change @@ -34,6 +34,15 @@ pub struct AllocationCounters {
34
34
}
35
35
36
36
impl AllocationCounters {
37
+ const fn new ( ) -> Self {
38
+ Self {
39
+ allocation_count : 0 ,
40
+ deallocation_count : 0 ,
41
+ allocations : 0 ,
42
+ deallocations : 0 ,
43
+ _not_send : PhantomData { } ,
44
+ }
45
+ }
37
46
pub fn until_now ( & self ) -> AllocationInfo {
38
47
let new = TurboMalloc :: allocation_counters ( ) ;
39
48
AllocationInfo {
@@ -50,6 +59,7 @@ impl AllocationCounters {
50
59
pub struct TurboMalloc ;
51
60
52
61
impl TurboMalloc {
62
+ // Returns the current amount of memory
53
63
pub fn memory_usage ( ) -> usize {
54
64
get ( )
55
65
}
You can’t perform that action at this time.
0 commit comments