Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix counting of reallocations #8422

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions crates/turbo-tasks-malloc/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,35 @@ impl ThreadLocalCounter {
}
}

fn update(&mut self, old_size: usize, new_size: usize) {
self.allocation_counters.deallocations += old_size;
self.allocation_counters.deallocation_count += 1;
self.allocation_counters.allocations += new_size;
self.allocation_counters.allocation_count += 1;
match old_size.cmp(&new_size) {
std::cmp::Ordering::Equal => {}
std::cmp::Ordering::Less => {
let size = new_size - old_size;
if self.buffer >= size {
self.buffer -= size;
} else {
let offset = size - self.buffer + TARGET_BUFFER;
self.buffer = TARGET_BUFFER;
ALLOCATED.fetch_add(offset, Ordering::Relaxed);
}
}
std::cmp::Ordering::Greater => {
let size = old_size - new_size;
self.buffer += size;
if self.buffer > MAX_BUFFER {
let offset = self.buffer - TARGET_BUFFER;
self.buffer = TARGET_BUFFER;
ALLOCATED.fetch_sub(offset, Ordering::Relaxed);
}
}
}
}

fn unload(&mut self) {
if self.buffer > 0 {
ALLOCATED.fetch_sub(self.buffer, Ordering::Relaxed);
Expand Down Expand Up @@ -93,6 +122,11 @@ pub fn remove(size: usize) {
with_local_counter(|local| local.remove(size));
}

/// Adds some `size` to the global counter in a thread-local buffered way.
pub fn update(old_size: usize, new_size: usize) {
with_local_counter(|local| local.update(old_size, new_size));
}

/// Flushes the thread-local buffer to the global counter. This should be called
/// e. g. when a thread is stopped or goes to sleep for a long time.
pub fn flush() {
Expand Down
14 changes: 3 additions & 11 deletions crates/turbo-tasks-malloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
marker::PhantomData,
};

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

#[derive(Default, Clone, Debug)]
pub struct AllocationInfo {
Expand Down Expand Up @@ -97,11 +97,7 @@ unsafe impl GlobalAlloc for TurboMalloc {
let ret = mimalloc::MiMalloc.realloc(ptr, layout, new_size);
if !ret.is_null() {
let old_size = layout.size();
if old_size < new_size {
add(new_size - old_size);
} else {
remove(old_size - new_size);
}
update(old_size, new_size);
}
ret
}
Expand Down Expand Up @@ -137,11 +133,7 @@ unsafe impl GlobalAlloc for TurboMalloc {
let ret = std::alloc::System.realloc(ptr, layout, new_size);
if !ret.is_null() {
let old_size = layout.size();
if old_size < new_size {
add(new_size - old_size);
} else {
remove(old_size - new_size);
}
update(old_size, new_size);
}
ret
}
Expand Down
Loading