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

Code cleanup and maintenance #85

Merged
merged 7 commits into from
Dec 27, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
rust_toolchain: [nightly, stable, 1.65.0]
rust_toolchain: [nightly, stable, 1.70.0]
os: [ubuntu-latest, windows-latest, macOS-latest]
flags: ["", "--release", "--no-default-features", "--all-features"]
timeout-minutes: 20
Expand Down
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ members = [
"tracy-client",
"tracing-tracy",
]
resolver = "2"

[workspace.package]
authors = ["Simonas Kazlauskas <[email protected]>"]
edition = "2021"
repository = "https://github.com/nagisa/rust_tracy_client"
homepage = "https://github.com/nagisa/rust_tracy_client"
license = "MIT/Apache-2.0"
rust-version = "1.70.0"
13 changes: 7 additions & 6 deletions tracing-tracy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
name = "tracing-tracy"
version = "0.10.4"
authors = ["Simonas Kazlauskas <[email protected]>"]
license = "MIT/Apache-2.0"
edition = "2018"
authors.workspace = true
license.workspace = true
edition.workspace = true
rust-version.workspace = true
readme = "README.mkd"
repository = "https://github.com/nagisa/rust_tracy_client"
homepage = "https://github.com/nagisa/rust_tracy_client"
repository.workspace = true
homepage.workspace = true
documentation = "https://docs.rs/tracing-tracy"
description = """
Inspect tracing-enabled Rust applications with Tracy
Expand All @@ -27,7 +28,7 @@ tokio = { version = "1", features = ["full"] }
tracing-attributes = { version = "0.1"}
tracing-futures = { version = "0.2" }
futures = "0.3"
criterion = "0.3"
criterion = "0.5"

[features]
# Refer to FEATURES.mkd for documentation on features.
Expand Down
161 changes: 81 additions & 80 deletions tracing-tracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl TracyLayer<DefaultFields> {
/// Create a new `TracyLayer`.
///
/// Defaults to collecting stack traces.
#[must_use]
SUPERCILEX marked this conversation as resolved.
Show resolved Hide resolved
pub fn new() -> Self {
Self {
fmt: DefaultFields::default(),
Expand All @@ -99,12 +100,14 @@ impl<F> TracyLayer<F> {
/// Note that enabling callstack collection can and will introduce a non-trivial overhead at
/// every instrumentation point. Specifying 0 frames (which is the default) will disable stack
/// trace collection.
pub fn with_stackdepth(mut self, stack_depth: u16) -> Self {
#[must_use]
pub const fn with_stackdepth(mut self, stack_depth: u16) -> Self {
self.stack_depth = stack_depth;
self
}

/// Use a custom field formatting implementation.
#[must_use]
pub fn with_formatter<Fmt>(self, fmt: Fmt) -> TracyLayer<Fmt> {
SUPERCILEX marked this conversation as resolved.
Show resolved Hide resolved
TracyLayer {
fmt,
Expand All @@ -121,8 +124,7 @@ impl<F> TracyLayer<F> {
error_msg: &'static str,
) -> &'d str {
// From AllocSourceLocation
let mut max_len =
usize::from(u16::max_value()) - 2 - 4 - 4 - function.len() - 1 - file.len() - 1;
let mut max_len = usize::from(u16::MAX) - 2 - 4 - 4 - function.len() - 1 - file.len() - 1;
if data.len() >= max_len {
while !data.is_char_boundary(max_len) {
max_len -= 1;
Expand All @@ -148,64 +150,86 @@ where
F: for<'writer> FormatFields<'writer> + 'static,
{
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) {
if let Some(span) = ctx.span(id) {
let mut extensions = span.extensions_mut();
if extensions.get_mut::<FormattedFields<F>>().is_none() {
let mut fields = FormattedFields::<F>::new(String::with_capacity(64));
if self.fmt.format_fields(fields.as_writer(), attrs).is_ok() {
extensions.insert(fields);
}
let Some(span) = ctx.span(id) else { return };

let mut extensions = span.extensions_mut();
if extensions.get_mut::<FormattedFields<F>>().is_none() {
let mut fields = FormattedFields::<F>::new(String::with_capacity(64));
if self.fmt.format_fields(fields.as_writer(), attrs).is_ok() {
extensions.insert(fields);
}
}
}

fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) {
if let Some(span) = ctx.span(id) {
let mut extensions = span.extensions_mut();
if let Some(fields) = extensions.get_mut::<FormattedFields<F>>() {
let _ = self.fmt.add_fields(fields, values);
} else {
let mut fields = FormattedFields::<F>::new(String::with_capacity(64));
if self.fmt.format_fields(fields.as_writer(), values).is_ok() {
extensions.insert(fields);
}
let Some(span) = ctx.span(id) else { return };

let mut extensions = span.extensions_mut();
if let Some(fields) = extensions.get_mut::<FormattedFields<F>>() {
let _ = self.fmt.add_fields(fields, values);
} else {
let mut fields = FormattedFields::<F>::new(String::with_capacity(64));
if self.fmt.format_fields(fields.as_writer(), values).is_ok() {
extensions.insert(fields);
}
}
}

fn on_event(&self, event: &Event, _: Context<'_, S>) {
let mut visitor = TracyEventFieldVisitor {
dest: String::with_capacity(64),
first: true,
frame_mark: false,
};
event.record(&mut visitor);
if !visitor.first {
self.client.message(
self.truncate_to_length(
&visitor.dest,
"",
"",
"event message is too long and was truncated",
),
self.stack_depth,
);
}
if visitor.frame_mark {
self.client.frame_mark();
}
}

fn on_enter(&self, id: &Id, ctx: Context<S>) {
if let Some(span_data) = ctx.span(id) {
let metadata = span_data.metadata();
let file = metadata.file().unwrap_or("<not available>");
let line = metadata.line().unwrap_or(0);
let name: Cow<str> =
if let Some(fields) = span_data.extensions().get::<FormattedFields<F>>() {
if fields.fields.as_str().is_empty() {
metadata.name().into()
} else {
format!("{}{{{}}}", metadata.name(), fields.fields.as_str()).into()
}
} else {
metadata.name().into()
};
TRACY_SPAN_STACK.with(|s| {
s.borrow_mut().push_back((
self.client.clone().span_alloc(
Some(self.truncate_to_length(
&name,
file,
"",
"span information is too long and was truncated",
)),
"",
let Some(span) = ctx.span(id) else { return };

let metadata = span.metadata();
let file = metadata.file().unwrap_or("<not available>");
let line = metadata.line().unwrap_or(0);
let name: Cow<str> = if let Some(fields) = span.extensions().get::<FormattedFields<F>>() {
if fields.fields.is_empty() {
metadata.name().into()
} else {
format!("{}{{{}}}", metadata.name(), fields.fields.as_str()).into()
}
} else {
metadata.name().into()
};
TRACY_SPAN_STACK.with(|s| {
s.borrow_mut().push_back((
self.client.clone().span_alloc(
Some(self.truncate_to_length(
&name,
file,
line,
self.stack_depth,
),
id.into_u64(),
));
});
}
"",
"span information is too long and was truncated",
)),
"",
file,
line,
self.stack_depth,
),
id.into_u64(),
));
});
}

fn on_exit(&self, id: &Id, _: Context<S>) {
Expand All @@ -229,29 +253,6 @@ where
}
});
}

fn on_event(&self, event: &Event, _: Context<'_, S>) {
let mut visitor = TracyEventFieldVisitor {
dest: String::with_capacity(64),
first: true,
frame_mark: false,
};
event.record(&mut visitor);
if !visitor.first {
self.client.message(
self.truncate_to_length(
&visitor.dest,
"",
"",
"event message is too long and was truncated",
),
self.stack_depth,
);
}
if visitor.frame_mark {
self.client.frame_mark();
}
}
}

struct TracyEventFieldVisitor {
Expand All @@ -261,6 +262,13 @@ struct TracyEventFieldVisitor {
}

impl Visit for TracyEventFieldVisitor {
fn record_bool(&mut self, field: &Field, value: bool) {
match (value, field.name()) {
(true, "tracy.frame_mark") => self.frame_mark = true,
_ => self.record_debug(field, &value),
}
}

fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
// FIXME: this is a very crude formatter, but we don’t have
// an easy way to do anything better...
Expand All @@ -271,13 +279,6 @@ impl Visit for TracyEventFieldVisitor {
let _ = write!(&mut self.dest, ", {} = {:?}", field.name(), value);
}
}

fn record_bool(&mut self, field: &Field, value: bool) {
match (value, field.name()) {
(true, "tracy.frame_mark") => self.frame_mark = true,
_ => self.record_debug(field, &value),
}
}
}

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions tracing-tracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn benchmark_span(c: &mut Criterion) {
bencher.iter(|| {
let _span = tracing::error_span!("message").entered();
});
})
});
});

c.bench_function("span/no_callstack", |bencher| {
Expand All @@ -137,7 +137,7 @@ fn benchmark_span(c: &mut Criterion) {
bencher.iter(|| {
let _span = tracing::error_span!("message").entered();
});
})
});
});
}

Expand All @@ -149,7 +149,7 @@ fn benchmark_message(c: &mut Criterion) {
bencher.iter(|| {
tracing::error!("message");
});
})
});
});

c.bench_function("event/no_callstack", |bencher| {
Expand All @@ -159,7 +159,7 @@ fn benchmark_message(c: &mut Criterion) {
bencher.iter(|| {
tracing::error!("message");
});
})
});
});
}

Expand Down
11 changes: 6 additions & 5 deletions tracy-client-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[package]
name = "tracy-client-sys"
version = "0.22.0" # AUTO-BUMP
authors = ["Simonas Kazlauskas <[email protected]>"]
authors.workspace = true
build = "build.rs"
license = "(MIT OR Apache-2.0) AND BSD-3-Clause"
edition = "2018"
edition.workspace = true
rust-version.workspace = true
readme = "README.mkd"
repository = "https://github.com/nagisa/rust_tracy_client"
homepage = "https://github.com/nagisa/rust_tracy_client"
repository.workspace = true
homepage.workspace = true
documentation = "https://docs.rs/tracy-client-sys"
description = """
Low level bindings to the client libraries for the Tracy profiler
Expand All @@ -22,7 +23,7 @@ required-features = ["fibers"]
[dependencies]

[build-dependencies]
cc = { version = "1.0.82", default-features = false }
cc = { version = "1.0.83", default-features = false }

[features]
# Refer to FEATURES.mkd for documentation on features.
Expand Down
6 changes: 3 additions & 3 deletions tracy-client-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn link_dependencies() {
Ok("windows") => println!("cargo:rustc-link-lib=user32"),
Ok(_) => {}
Err(e) => {
writeln!(::std::io::stderr(), "Unable to get target_os=`{}`!", e)
writeln!(::std::io::stderr(), "Unable to get target_os=`{e}`!")
.expect("could not report the error");
::std::process::exit(0xfd);
}
Expand Down Expand Up @@ -84,7 +84,7 @@ fn build_tracy_client() {
fn main() {
if let Ok(lib) = std::env::var("TRACY_CLIENT_LIB") {
if let Ok(lib_path) = std::env::var("TRACY_CLIENT_LIB_PATH") {
println!("cargo:rustc-link-search=native={}", lib_path);
println!("cargo:rustc-link-search=native={lib_path}");
}
let kind = std::env::var_os("TRACY_CLIENT_STATIC");
let mode = if kind.is_none() || kind.as_deref() == Some(std::ffi::OsStr::new("0")) {
Expand All @@ -93,7 +93,7 @@ fn main() {
link_dependencies();
"static"
};
println!("cargo:rustc-link-lib={}={}", mode, lib);
println!("cargo:rustc-link-lib={mode}={lib}");
} else {
build_tracy_client();
}
Expand Down
8 changes: 7 additions & 1 deletion tracy-client-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@
//! request rather than statically at the DLL load at the expense of atomic load on each request
//! to the profiler data. Corresponds to the `TRACY_DELAYED_INIT` define.
#![doc = include_str!("../FEATURES.mkd")]
#![allow(non_snake_case, non_camel_case_types, unused_variables, deref_nullptr)]
#![allow(
non_snake_case,
non_camel_case_types,
non_upper_case_globals,
unused_variables,
deref_nullptr
)]
#![cfg_attr(tracy_client_sys_docs, feature(doc_auto_cfg))]

#[cfg(feature = "enable")]
Expand Down
6 changes: 3 additions & 3 deletions tracy-client-sys/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ mod tests {
}
}

/// Cannot use a libtest harness here because we need manual control over the profiler startup and
/// shutdown.
/// Cannot use a libtest harness here because we need manual control over
/// the profiler startup and shutdown.
pub(crate) fn main() {
unsafe {
___tracy_startup_profiler();
Expand All @@ -39,5 +39,5 @@ mod tests {

fn main() {
#[cfg(all(feature = "enable", test))]
tests::main()
tests::main();
}
Loading