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

Set Tracy thread names according to Rust std thread names #38

Open
wants to merge 1 commit into
base: main
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
22 changes: 21 additions & 1 deletion tracing-tracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@
//! Refer to the [`client::sys`] crate for documentation on crate features. This crate re-exports
//! all the features from [`client`].

use std::{borrow::Cow, cell::RefCell, collections::VecDeque, fmt::Write};
use std::{
borrow::Cow,
cell::{Cell, RefCell},
collections::VecDeque,
fmt::Write,
};
use tracing_core::{
field::{Field, Visit},
span::{Attributes, Id, Record},
Expand All @@ -68,6 +73,9 @@ thread_local! {
/// A stack of spans currently active on the current thread.
static TRACY_SPAN_STACK: RefCell<VecDeque<(Span, u64)>> =
RefCell::new(VecDeque::with_capacity(16));

/// Indicator of whether `Client::set_thread_name_from_std` has been called for this thread.
static TRACY_THREAD_NAME_INIT: Cell<bool> = Cell::new(false);
}

/// A tracing layer that collects data in Tracy profiling format.
Expand Down Expand Up @@ -129,6 +137,15 @@ impl<F> TracyLayer<F> {
data
}
}

fn set_tracy_thread_name_once(&self) {
TRACY_THREAD_NAME_INIT.with(|is_init| {
if !is_init.get() {
is_init.set(true);
self.client.set_thread_name_from_std();
}
});
}
}

impl Default for TracyLayer {
Expand Down Expand Up @@ -200,6 +217,7 @@ where
id.into_u64(),
));
});
self.set_tracy_thread_name_once();
}
}

Expand All @@ -223,6 +241,7 @@ where
);
}
});
self.set_tracy_thread_name_once();
}

fn on_event(&self, event: &Event, _: Context<'_, S>) {
Expand All @@ -246,6 +265,7 @@ where
if visitor.frame_mark {
self.client.frame_mark();
}
self.set_tracy_thread_name_once();
}
}

Expand Down
17 changes: 17 additions & 0 deletions tracy-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ impl Client {
internal::set_thread_name(name.as_ptr().cast());
}
}

/// Set the current thread name according to Rust's [`Thread::name()`].
///
/// This is useful if the thread name is already set with Rust's standard library APIs, and you
/// want to copy the name to Tracy. Does nothing if the current thread is not named according
/// to [`Thread::name()`].
///
/// [`Thread::name()`]: std::thread::Thread::name
pub fn set_thread_name_from_std(&self) {
#[cfg(feature = "enable")]
{
if let Some(name) = std::thread::current().name() {
// This will not panic because Rust disallows thread names with null bytes.
self.set_thread_name(name);
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is trivial for downstream code to write themselves; is it really worth having a method for it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put the method here to raise some awareness of the Rust thread naming API, as set_thread_name alone may imply thread naming being some Tracy-specific feature.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could that be solved by pure documentation instead?

}

/// Convenience macro for [`Client::set_thread_name`] on the current client.
Expand Down
15 changes: 15 additions & 0 deletions tracy-client/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ fn tls_confusion() {
fn set_thread_name() {
let _client = Client::start();
set_thread_name!("test thread");
// Test with unnamed threads.
let t1 = std::thread::spawn(|| {
let client = Client::start();
client.set_thread_name_from_std();
});
let _ = t1.join();
// Test with named threads.
let t2 = std::thread::Builder::new()
.name("thread name".to_owned())
.spawn(|| {
let client = Client::start();
client.set_thread_name_from_std();
})
.unwrap();
let _ = t2.join();
}

fn main() {
Expand Down