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

Add feature to stop and start captures #18

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions examples/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ fn main() {
println!("fibonacci_serial(24) -> {}", fibonacci(24));
println!("took {} s", before.elapsed().as_secs_f32());
if let Some(guard) = &guard {
guard.start_new(None);
guard.start_capture(None);
}
let before = std::time::Instant::now();
println!("fibonacci_parallel(24) -> {}", fibonacci_parallel(24));
println!("took {} s", before.elapsed().as_secs_f32());
if let Some(guard) = &guard {
guard.start_new(None);
guard.start_capture(None);
}
let before = std::time::Instant::now();
println!("fibonacci_parallel(20) -> {}", fibonacci_parallel(20));
Expand Down
41 changes: 33 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,23 @@ impl FlushGuard {
}
}

/// Finishes the current trace and starts a new one.
/// Starts a new trace, if one was already running it will stop the current trace.
///
/// If a [`Write`](std::io::Write) implementation is supplied,
/// the new trace is written to it. Otherwise, the new trace
/// goes to `./trace-{unix epoc in micros}.json`.
pub fn start_new(&self, writer: Option<Box<dyn Write + Send>>) {
pub fn start_capture(&self, writer: Option<Box<dyn Write + Send>>) {
if let Some(handle) = self.handle.take() {
let _ignored = self.sender.send(Message::StartNew(writer));
let _ignored = self.sender.send(Message::StartCapture(writer));
self.handle.set(Some(handle));
}
}

/// Stops the current trace.
///
pub fn stop_capture(&self) {
if let Some(handle) = self.handle.take() {
let _ignored = self.sender.send(Message::StopCapture);
self.handle.set(Some(handle));
}
}
Expand Down Expand Up @@ -258,7 +267,8 @@ enum Message {
NewThread(u64, String),
Flush,
Drop,
StartNew(Option<Box<dyn Write + Send>>),
StopCapture,
StartCapture(Option<Box<dyn Write + Send>>),
}

/// Represents either an [`Event`](tracing_core::Event) or [`SpanRef`](tracing_subscriber::registry::SpanRef).
Expand Down Expand Up @@ -301,13 +311,14 @@ where

let mut has_started = false;
let mut thread_names: Vec<(u64, String)> = Vec::new();
let mut capturing = false;
for msg in rx {
if let Message::Flush = &msg {
write.flush().unwrap();
continue;
} else if let Message::Drop = &msg {
break;
} else if let Message::StartNew(writer) = msg {
} else if let Message::StartCapture(writer) = msg {
// Finish off current file
write.write_all(b"\n]").unwrap();
write.flush().unwrap();
Expand All @@ -317,6 +328,7 @@ where
write = BufWriter::new(out_writer);
write.write_all(b"[\n").unwrap();
has_started = false;
capturing = true;

// Write saved thread names
for (tid, name) in thread_names.iter() {
Expand All @@ -335,6 +347,18 @@ where
has_started = true;
}
continue;
} else if let Message::StopCapture = msg {
if capturing {
// Finish off current file
write.write_all(b"\n]").unwrap();
write.flush().unwrap();
}

capturing = false;
}

if !capturing {
continue;
}

let mut entry = Object::new();
Expand All @@ -350,9 +374,10 @@ where
("e", Some(ts), Some(callsite), Some(root_id))
}
Message::NewThread(_tid, _name) => ("M", None, None, None),
Message::Flush | Message::Drop | Message::StartNew(_) => {
panic!("Was supposed to break by now.")
}
Message::Flush
| Message::Drop
| Message::StartCapture(_)
| Message::StopCapture => panic!("Was supposed to break by now."),
};
entry.insert("ph", ph.to_string().into());
entry.insert("pid", 1.into());
Expand Down