Skip to content

Commit

Permalink
Remove ability to disable the client at runtime
Browse files Browse the repository at this point in the history
One of the requirements we would need to maintain is that the profiler
is started and shutdown in the same thread in all cases. This seems like
a hard problem, so lets just not solve it for now.

I don't think people will have significant issues with the profiler just
being there until the process terminates (not to mention that kernel is
probably much better at cleaning up everything than we are anyway).
  • Loading branch information
nagisa committed Apr 30, 2022
1 parent 69e4497 commit 13b335a
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 215 deletions.
6 changes: 3 additions & 3 deletions tracing-tracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! * Events show up as messages in Tracy, however Tracy can struggle with large numbers of
//! messages;
//! * Some additional functionality such as plotting and memory allocation profiling is only
//! available as part of the [tracy-client](tracy_client) crate.
//! available as part of the [tracy-client](client) crate.
//!
//! # Examples
//!
Expand Down Expand Up @@ -86,7 +86,7 @@ impl TracyLayer<DefaultFields> {
Self {
fmt: DefaultFields::default(),
stack_depth: 64,
client: Client::enable(),
client: Client::start(),
}
}
}
Expand Down Expand Up @@ -185,7 +185,7 @@ where
};
TRACY_SPAN_STACK.with(|s| {
s.borrow_mut().push_back((
self.client.span_alloc(
self.client.clone().span_alloc(
self.truncate_to_length(
&name,
file,
Expand Down
19 changes: 4 additions & 15 deletions tracy-client/benches/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,10 @@ fn bench_clone(c: &mut Criterion) {
c.bench_function("clone", |b| b.iter(|| clients.push(client.clone())));
}

fn bench_start_stop(c: &mut Criterion) {
c.bench_function("start_stop", |b| b.iter(|| Client::start()));
fn bench_running(c: &mut Criterion) {
let _client = Client::start();
c.bench_function("running", |b| b.iter(|| Client::running()));
}

fn bench_counting(c: &mut Criterion) {
let client = Client::start();
c.bench_function("counting", |b| b.iter(|| Client::start()));
drop(client);
}

criterion_group!(
benches,
bench_start,
bench_clone,
bench_start_stop,
bench_counting,
);
criterion_group!(benches, bench_start, bench_clone, bench_running,);
criterion_main!(benches);
26 changes: 15 additions & 11 deletions tracy-client/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ impl Client {
///
/// In a traditional rendering scenarios a frame mark should be inserted after a buffer swap.
///
/// ```no_run
/// ```
/// use tracy_client::Client;
/// # fn swap_buffers() {}
/// let client = tracy_client::Client::enable();
/// # let client = tracy_client::Client::start();
/// // loop {
/// // ...
/// swap_buffers();
/// client.frame_mark();
/// Client::running().expect("client must be running").frame_mark();
/// // }
/// ```
pub fn frame_mark(&self) {
Expand All @@ -43,14 +43,16 @@ impl Client {
/// Much like with the primary frame mark, the secondary (named) frame mark should be inserted
/// after some continuously repeating operation finishes one iteration of its processing.
///
/// ```no_run
/// use tracy_client::{Client, frame_name};
/// ```
/// use tracy_client::frame_name;
/// # fn physics_tick() {}
/// let client = tracy_client::Client::enable();
/// # let client = tracy_client::Client::start();
/// // loop {
/// // ...
/// physics_tick();
/// client.secondary_frame_mark(frame_name!("physics"));
/// tracy_client::Client::running()
/// .expect("client must be running")
/// .secondary_frame_mark(frame_name!("physics"));
/// // }
/// ```
pub fn secondary_frame_mark(&self, name: FrameName) {
Expand All @@ -67,10 +69,12 @@ impl Client {
///
/// # Examples
///
/// ```no_run
/// use tracy_client::{Client, frame_name};
/// let client = tracy_client::Client::enable();
/// client.non_continuous_frame(frame_name!("a frame"));
/// ```
/// use tracy_client::frame_name;
/// # let client = tracy_client::Client::start();
/// tracy_client::Client::running()
/// .expect("client must be running")
/// .non_continuous_frame(frame_name!("a frame"));
/// ```
pub fn non_continuous_frame(&self, name: FrameName) -> Frame {
#[cfg(feature = "enable")]
Expand Down
10 changes: 8 additions & 2 deletions tracy-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pub mod internal {
/// still hasn't been delivered to the profiler application.
pub struct Client(());

/// Instrumentation methods for outputting events occurring at a specific instant.
///
/// Data provided by this instrumentation can largely be considered to be equivalent to logs.
impl Client {
/// Output a message.
///
Expand All @@ -120,7 +123,9 @@ impl Client {
sys::___tracy_emit_messageC(message.as_ptr().cast(), message.len(), rgba >> 8, depth)
}
}
}

impl Client {
/// Set the current thread name to the provided value.
pub fn set_thread_name(&self, name: &str) {
#[cfg(feature = "enable")]
Expand All @@ -136,8 +141,8 @@ impl Client {
///
/// See documentation for [`std::alloc`](std::alloc) for more information about global allocators.
///
/// Note that to use this wrapper correctly you must ensure that the client is enabled before the
/// first allocation occurs. The client must not not be disabled if this wrapper is used.
/// Note that this wrapper will start up the client on the first allocation, if not enabled
/// already.
///
/// # Examples
///
Expand All @@ -160,6 +165,7 @@ impl<T> ProfiledAllocator<T> {
fn emit_alloc(&self, ptr: *mut u8, size: usize) {
#[cfg(feature = "enable")]
unsafe {
Client::start();
if self.1 == 0 {
sys::___tracy_emit_memory_alloc(ptr.cast(), size, 1);
} else {
Expand Down
6 changes: 4 additions & 2 deletions tracy-client/src/plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ impl Client {
/// # Examples
///
/// ```
/// let client = tracy_client::Client::enable();
/// client.plot(tracy_client::plot_name!("temperature"), 37.0);
/// # let client = tracy_client::Client::start();
/// tracy_client::Client::running()
/// .expect("client must be running")
/// .plot(tracy_client::plot_name!("temperature"), 37.0);
/// ```
pub fn plot(&self, plot_name: PlotName, value: f64) {
#[cfg(feature = "enable")]
Expand Down
11 changes: 7 additions & 4 deletions tracy-client/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub struct Span {
/// A statically allocated location information for a span.
///
/// Construct with the [`span_location!`](crate::span_location) macro.
#[cfg(feature = "enable")]
pub struct SpanLocation {
#[cfg(feature = "enable")]
pub(crate) _function_name: CString,
Expand All @@ -41,14 +40,16 @@ impl Client {
/// `callstack_depth` specifies the maximum number of stack frames the client should collect.
/// On some systems this value may be clamped to a maximum value supported by the target.
///
/// The [`span!`](crate::span!) macro is a convenience wrapper over this method.
///
/// # Example
///
/// In the following example the span is created with the location at which the
/// `span_location!` macro appears and will measure the execution of the 100ms long sleep.
///
/// ```rust
/// use tracy_client::{Client, span_location};
/// let client = Client::enable();
/// let client = Client::start();
/// {
/// let _span = client.span(span_location!("sleeping"), 100);
/// std::thread::sleep(std::time::Duration::from_millis(100));
Expand Down Expand Up @@ -92,7 +93,7 @@ impl Client {
///
/// ```rust
/// use tracy_client::Client;
/// let client = Client::enable();
/// let client = Client::start();
/// {
/// let _span = client.span_alloc("hello", "my_function", "hello.rs", 42, 100);
/// std::thread::sleep(std::time::Duration::from_millis(100));
Expand Down Expand Up @@ -221,14 +222,16 @@ macro_rules! span_location {
/// Begin a span region, which will be terminated once `_span` goes out of scope:
///
/// ```
/// use tracy_client::span;
/// use tracy_client::{Client, span};
/// # let _client = tracy_client::Client::start();
/// let _span = span!("some span");
/// ```
///
/// It is also possible to specify the number of frames to capture in the callstack:
///
/// ```
/// use tracy_client::span;
/// # let _client = tracy_client::Client::start();
/// let _span = span!("some span", 32);
/// ```
#[macro_export]
Expand Down
Loading

0 comments on commit 13b335a

Please sign in to comment.