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 phantom data for criterion output #68

Merged
merged 4 commits into from Jul 13, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/criterion.rs
Expand Up @@ -11,6 +11,7 @@ use criterion::profiler::Profiler;
use std::fs::File;
#[cfg(feature = "protobuf")]
use std::io::Write;
use std::marker::PhantomData;
use std::os::raw::c_int;
use std::path::Path;

Expand All @@ -20,6 +21,11 @@ pub enum Output<'a> {

#[cfg(feature = "protobuf")]
Protobuf,

#[deprecated(
note = "This branch is used to include lifetime parameter. Don't use it directly."
)]
_Phantom(PhantomData<&'a ()>),
}

pub struct PProfProfiler<'a, 'b> {
Expand All @@ -38,6 +44,9 @@ impl<'a, 'b> PProfProfiler<'a, 'b> {
}
}

#[cfg(not(any(feature = "protobuf", feature = "flamegraph")))]
compile_error!("Either feature \"protobuf\" or \"flamegraph\" must be enabled when \"criterion\" feature is enabled.");

impl<'a, 'b> Profiler for PProfProfiler<'a, 'b> {
fn start_profiling(&mut self, _benchmark_id: &str, _benchmark_dir: &Path) {
self.active_profiler = Some(ProfilerGuard::new(self.frequency).unwrap());
Expand All @@ -51,6 +60,10 @@ impl<'a, 'b> Profiler for PProfProfiler<'a, 'b> {
Output::Flamegraph(_) => "flamegraph.svg",
#[cfg(feature = "protobuf")]
Output::Protobuf => "profile.pb",
// This is `""` but not `unreachable!()`, because `unreachable!()`
// will result in another compile error, so that the user may not
Copy link
Member

Choose a reason for hiding this comment

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

Can really unreachable!() cause compile error? I do not realize it.

Copy link
Member Author

Choose a reason for hiding this comment

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

If the only branch is "unreachable!()", the type of "filename" will be resolved as (), and it's illegal to pass filename to benchmark_dir.join.

Though, adding a redundant type annotation : &str to the filename will fix this issue, it will also bring a big warning (because the following statement is unreachable statement in that case), so I prefer to return an empty string, and let the compile_error prints the only error.

// realize the error thrown by `compile_error!()` at the first time.
_ => "",
};
let output_path = benchmark_dir.join(filename);
let output_file = File::create(&output_path).unwrap_or_else(|_| {
Expand Down Expand Up @@ -87,6 +100,8 @@ impl<'a, 'b> Profiler for PProfProfiler<'a, 'b> {
.write_all(&content)
.expect("Error while writing protobuf");
}

_ => unreachable!(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/frames.rs
Expand Up @@ -40,6 +40,7 @@ impl Debug for UnresolvedFrames {
}

impl UnresolvedFrames {
#[allow(clippy::uninit_assumed_init)]
pub fn new(bt: &[Frame], tn: &[u8], thread_id: u64) -> Self {
let depth = bt.len();
let mut frames: [Frame; MAX_DEPTH] =
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -29,6 +29,7 @@ pub const MAX_DEPTH: usize = 32;
/// Define the MAX supported thread name length. TODO: make this variable mutable.
pub const MAX_THREAD_NAME: usize = 16;

#[allow(clippy::uninit_assumed_init)]
mod collector;
mod error;
mod frames;
Expand Down
1 change: 1 addition & 0 deletions src/profiler.rs
Expand Up @@ -117,6 +117,7 @@ fn write_thread_name(current_thread: libc::pthread_t, name: &mut [libc::c_char])
}

#[no_mangle]
#[allow(clippy::uninit_assumed_init)]
extern "C" fn perf_signal_handler(_signal: c_int) {
if let Some(mut guard) = PROFILER.try_write() {
if let Ok(profiler) = guard.as_mut() {
Expand Down