Tracing trace: toggle the profiling of memory at runtime

This commit is contained in:
Louis Dureuil 2024-02-07 15:48:07 +01:00
parent 79cabb1e79
commit cfb27adb3e
No known key found for this signature in database
2 changed files with 11 additions and 5 deletions

View File

@ -20,24 +20,26 @@ pub struct TraceLayer {
sender: tokio::sync::mpsc::UnboundedSender<Entry>, sender: tokio::sync::mpsc::UnboundedSender<Entry>,
callsites: RwLock<HashMap<OpaqueIdentifier, ResourceId>>, callsites: RwLock<HashMap<OpaqueIdentifier, ResourceId>>,
start_time: std::time::Instant, start_time: std::time::Instant,
profile_memory: bool,
} }
impl Trace { impl Trace {
pub fn new() -> (Self, TraceLayer) { pub fn new(profile_memory: bool) -> (Self, TraceLayer) {
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel(); let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
let trace = Trace { receiver }; let trace = Trace { receiver };
let layer = TraceLayer { let layer = TraceLayer {
sender, sender,
callsites: Default::default(), callsites: Default::default(),
start_time: std::time::Instant::now(), start_time: std::time::Instant::now(),
profile_memory,
}; };
(trace, layer) (trace, layer)
} }
} }
impl<W: Write> TraceWriter<W> { impl<W: Write> TraceWriter<W> {
pub fn new(writer: W) -> (Self, TraceLayer) { pub fn new(writer: W, profile_memory: bool) -> (Self, TraceLayer) {
let (trace, layer) = Trace::new(); let (trace, layer) = Trace::new(profile_memory);
(trace.into_writer(writer), layer) (trace.into_writer(writer), layer)
} }
@ -97,7 +99,11 @@ impl TraceLayer {
} }
fn memory_stats(&self) -> Option<MemoryStats> { fn memory_stats(&self) -> Option<MemoryStats> {
if self.profile_memory {
MemoryStats::fetch() MemoryStats::fetch()
} else {
None
}
} }
fn send(&self, entry: Entry) { fn send(&self, entry: Entry) {

View File

@ -74,7 +74,7 @@ fn on_panic(info: &std::panic::PanicInfo) {
fn main() { fn main() {
let (mut trace, profiling_layer) = let (mut trace, profiling_layer) =
tracing_trace::TraceWriter::new(std::fs::File::create("trace.json").unwrap()); tracing_trace::TraceWriter::new(std::fs::File::create("trace.json").unwrap(), true);
let subscriber = tracing_subscriber::registry() let subscriber = tracing_subscriber::registry()
// any number of other subscriber layers may be added before or // any number of other subscriber layers may be added before or