Format the bytes as human readable bytes

Uses the same `byte_unit` version as `meilisearch`
This commit is contained in:
Louis Dureuil 2024-01-23 10:21:50 +01:00
parent 771861599b
commit 6cf703387d
No known key found for this signature in database
4 changed files with 52 additions and 17 deletions

2
Cargo.lock generated
View File

@ -5710,9 +5710,9 @@ dependencies = [
name = "tracing-trace"
version = "0.1.0"
dependencies = [
"byte-unit",
"color-spantrace",
"fxprof-processed-profile",
"once_cell",
"serde",
"serde_json",
"stats_alloc",

View File

@ -14,4 +14,8 @@ tracing = "0.1.40"
tracing-error = "0.2.0"
tracing-subscriber = "0.3.18"
stats_alloc = { git = "https://github.com/Kerollmops/stats_alloc", branch = "stable-const-fn-trait" }
once_cell = "1.19.0"
byte-unit = { version = "4.0.19", default-features = false, features = [
"std",
"serde",
] }

View File

@ -5,7 +5,6 @@ use fxprof_processed_profile::{
MarkerFieldFormat, MarkerLocation, MarkerSchema, MarkerSchemaField, Profile, ProfilerMarker,
ReferenceTimestamp, SamplingInterval, StringHandle, Timestamp,
};
use once_cell::unsync::Lazy;
use serde_json::json;
use crate::entry::{
@ -33,17 +32,19 @@ pub fn to_firefox_profile<R: std::io::Read>(
let category = profile.add_category("general", fxprof_processed_profile::CategoryColor::Blue);
let subcategory = profile.add_subcategory(category, "subcategory");
// TODO kero: add counters profile.add_counters + last_memory_value
let mut current_memory = MemoryStats::default();
let mut allocations_counter = Lazy::new(|| {
let init_allocations = |profile: &mut Profile| {
profile.add_counter(main, "mimmalloc", "Memory", "Amount of allocation calls")
});
let mut deallocations_counter = Lazy::new(|| {
};
let init_deallocations = |profile: &mut Profile| {
profile.add_counter(main, "mimmalloc", "Memory", "Amount of deallocation calls")
});
let mut reallocations_counter = Lazy::new(|| {
};
let init_reallocations = |profile: &mut Profile| {
profile.add_counter(main, "mimmalloc", "Memory", "Amount of reallocation calls")
});
};
let mut allocations_counter = None;
let mut deallocations_counter = None;
let mut reallocations_counter = None;
for entry in trace {
let entry = entry?;
@ -88,22 +89,28 @@ pub fn to_firefox_profile<R: std::io::Read>(
bytes_reallocated,
} = current_memory - stats;
let counter =
*allocations_counter.get_or_insert_with(|| init_allocations(&mut profile));
profile.add_counter_sample(
*allocations_counter,
counter,
last_timestamp,
bytes_allocated as f64,
allocations.try_into().unwrap(),
);
let counter = *deallocations_counter
.get_or_insert_with(|| init_deallocations(&mut profile));
profile.add_counter_sample(
*deallocations_counter,
counter,
last_timestamp,
bytes_deallocated as f64,
deallocations.try_into().unwrap(),
);
let counter = *reallocations_counter
.get_or_insert_with(|| init_reallocations(&mut profile));
profile.add_counter_sample(
*reallocations_counter,
counter,
last_timestamp,
bytes_reallocated as f64,
reallocations.try_into().unwrap(),
@ -154,22 +161,28 @@ pub fn to_firefox_profile<R: std::io::Read>(
bytes_reallocated,
} = current_memory - stats;
let counter =
*allocations_counter.get_or_insert_with(|| init_allocations(&mut profile));
profile.add_counter_sample(
*allocations_counter,
counter,
last_timestamp,
bytes_allocated as f64,
allocations.try_into().unwrap(),
);
let counter = *deallocations_counter
.get_or_insert_with(|| init_deallocations(&mut profile));
profile.add_counter_sample(
*deallocations_counter,
counter,
last_timestamp,
bytes_deallocated as f64,
deallocations.try_into().unwrap(),
);
let counter = *reallocations_counter
.get_or_insert_with(|| init_reallocations(&mut profile));
profile.add_counter_sample(
*reallocations_counter,
counter,
last_timestamp,
bytes_reallocated as f64,
reallocations.try_into().unwrap(),

View File

@ -147,6 +147,24 @@ fn print_duration(duration: std::time::Duration) -> String {
}
}
/// Format only the allocated bytes, deallocated bytes and reallocated bytes in GiB, MiB, KiB, Bytes.
fn print_memory(memory: MemoryStats) -> String {
// Format only the total allocations in GiB, MiB, KiB, Bytes
use byte_unit::Byte;
let allocated_bytes = Byte::from_bytes(memory.bytes_allocated.try_into().unwrap());
let deallocated_bytes = Byte::from_bytes(memory.bytes_deallocated.try_into().unwrap());
let reallocated_sign = if memory.bytes_reallocated < 0 { "-" } else { "" };
let reallocated_bytes =
Byte::from_bytes(memory.bytes_reallocated.abs_diff(0).try_into().unwrap());
let adjusted_allocated_bytes = allocated_bytes.get_appropriate_unit(true);
let adjusted_deallocated_bytes = deallocated_bytes.get_appropriate_unit(true);
let adjusted_reallocated_bytes = reallocated_bytes.get_appropriate_unit(true);
format!(
"Allocated {adjusted_allocated_bytes:.2}, \
Deallocated {adjusted_deallocated_bytes:.2}, \
Reallocated {reallocated_sign}{adjusted_reallocated_bytes:.2}"
)
}