Skip to content

Commit

Permalink
fix counting of reallocations (#8422)
Browse files Browse the repository at this point in the history
### Description

reallocations were not counted correctly in the tracing

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->
  • Loading branch information
sokra committed Jun 11, 2024
1 parent 0bddf6e commit 943d6b5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
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

0 comments on commit 943d6b5

Please sign in to comment.