Skip to content

Commit 943d6b5

Browse files
authored
fix counting of reallocations (#8422)
### Description reallocations were not counted correctly in the tracing ### Testing Instructions <!-- Give a quick description of steps to test your changes. -->
1 parent 0bddf6e commit 943d6b5

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

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

+34
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,35 @@ impl ThreadLocalCounter {
4949
}
5050
}
5151

52+
fn update(&mut self, old_size: usize, new_size: usize) {
53+
self.allocation_counters.deallocations += old_size;
54+
self.allocation_counters.deallocation_count += 1;
55+
self.allocation_counters.allocations += new_size;
56+
self.allocation_counters.allocation_count += 1;
57+
match old_size.cmp(&new_size) {
58+
std::cmp::Ordering::Equal => {}
59+
std::cmp::Ordering::Less => {
60+
let size = new_size - old_size;
61+
if self.buffer >= size {
62+
self.buffer -= size;
63+
} else {
64+
let offset = size - self.buffer + TARGET_BUFFER;
65+
self.buffer = TARGET_BUFFER;
66+
ALLOCATED.fetch_add(offset, Ordering::Relaxed);
67+
}
68+
}
69+
std::cmp::Ordering::Greater => {
70+
let size = old_size - new_size;
71+
self.buffer += size;
72+
if self.buffer > MAX_BUFFER {
73+
let offset = self.buffer - TARGET_BUFFER;
74+
self.buffer = TARGET_BUFFER;
75+
ALLOCATED.fetch_sub(offset, Ordering::Relaxed);
76+
}
77+
}
78+
}
79+
}
80+
5281
fn unload(&mut self) {
5382
if self.buffer > 0 {
5483
ALLOCATED.fetch_sub(self.buffer, Ordering::Relaxed);
@@ -93,6 +122,11 @@ pub fn remove(size: usize) {
93122
with_local_counter(|local| local.remove(size));
94123
}
95124

125+
/// Adds some `size` to the global counter in a thread-local buffered way.
126+
pub fn update(old_size: usize, new_size: usize) {
127+
with_local_counter(|local| local.update(old_size, new_size));
128+
}
129+
96130
/// Flushes the thread-local buffer to the global counter. This should be called
97131
/// e. g. when a thread is stopped or goes to sleep for a long time.
98132
pub fn flush() {

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

+3-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
marker::PhantomData,
66
};
77

8-
use self::counter::{add, flush, get, remove};
8+
use self::counter::{add, flush, get, remove, update};
99

1010
#[derive(Default, Clone, Debug)]
1111
pub struct AllocationInfo {
@@ -97,11 +97,7 @@ unsafe impl GlobalAlloc for TurboMalloc {
9797
let ret = mimalloc::MiMalloc.realloc(ptr, layout, new_size);
9898
if !ret.is_null() {
9999
let old_size = layout.size();
100-
if old_size < new_size {
101-
add(new_size - old_size);
102-
} else {
103-
remove(old_size - new_size);
104-
}
100+
update(old_size, new_size);
105101
}
106102
ret
107103
}
@@ -137,11 +133,7 @@ unsafe impl GlobalAlloc for TurboMalloc {
137133
let ret = std::alloc::System.realloc(ptr, layout, new_size);
138134
if !ret.is_null() {
139135
let old_size = layout.size();
140-
if old_size < new_size {
141-
add(new_size - old_size);
142-
} else {
143-
remove(old_size - new_size);
144-
}
136+
update(old_size, new_size);
145137
}
146138
ret
147139
}

0 commit comments

Comments
 (0)