From 6cf703387dfad799b23b79a467dd20af74b47848 Mon Sep 17 00:00:00 2001 From: Louis Dureuil Date: Tue, 23 Jan 2024 10:21:50 +0100 Subject: [PATCH] Format the bytes as human readable bytes Uses the same `byte_unit` version as `meilisearch` --- Cargo.lock | 2 +- tracing-trace/Cargo.toml | 6 ++- .../src/processor/firefox_profiler.rs | 41 ++++++++++++------- tracing-trace/src/processor/fmt.rs | 20 ++++++++- 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d149733c..8ef99a0cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/tracing-trace/Cargo.toml b/tracing-trace/Cargo.toml index 8cd863368..04b7494d4 100644 --- a/tracing-trace/Cargo.toml +++ b/tracing-trace/Cargo.toml @@ -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", +] } + diff --git a/tracing-trace/src/processor/firefox_profiler.rs b/tracing-trace/src/processor/firefox_profiler.rs index ce04d8bcf..84b88aafb 100644 --- a/tracing-trace/src/processor/firefox_profiler.rs +++ b/tracing-trace/src/processor/firefox_profiler.rs @@ -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( 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( 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( 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(), diff --git a/tracing-trace/src/processor/fmt.rs b/tracing-trace/src/processor/fmt.rs index 8e99752cb..31fc1d6cb 100644 --- a/tracing-trace/src/processor/fmt.rs +++ b/tracing-trace/src/processor/fmt.rs @@ -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}" + ) }