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

Separate platform implementation #150

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ backtrace = { version = "0.3" }
once_cell = "1.9"
libc = "^0.2.66"
log = "0.4"
nix = { version = "0.24", default-features = false, features = ["signal", "fs"] }

parking_lot = "0.12"
tempfile = "3.1"
thiserror = "1.0"
Expand All @@ -37,7 +37,15 @@ inferno = { version = "0.11", default-features = false, features = ["nameattr"],
prost = { version = "0.10", optional = true }
prost-derive = { version = "0.10", optional = true }
protobuf = { version = "2.0", optional = true }
criterion = {version = "0.3", optional = true}
criterion = { version = "0.3", optional = true }

[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
Copy link

@maksymsur maksymsur Jul 27, 2022

Choose a reason for hiding this comment

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

Perhaps it's better to use more generic and inclusive template unless some other UNIX targets but tested linux and macos may fail

[target.'cfg(target_family = "unix")'.dependencies]

Rest of the code looks good

nix = { version = "0.24", default-features = false, features = [
"signal",
"fs",
] }

[target.'cfg(target_os = "windows")'.dependencies]

[dependencies.symbolic-demangle]
version = "9.0"
Expand Down
24 changes: 2 additions & 22 deletions src/backtrace/mod.rs → src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use libc::c_void;
use std::path::PathBuf;

pub use crate::platform::TraceImpl;

pub trait Symbol: Sized {
fn name(&self) -> Option<Vec<u8>>;
fn addr(&self) -> Option<*mut c_void>;
Expand Down Expand Up @@ -43,25 +45,3 @@ pub trait Trace {
where
Self: Sized;
}

#[cfg(not(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer"
)))]
mod backtrace_rs;
#[cfg(not(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer"
)))]
pub use backtrace_rs::Trace as TraceImpl;

#[cfg(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer"
))]
pub mod frame_pointer;
#[cfg(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer"
))]
pub use frame_pointer::Trace as TraceImpl;
9 changes: 8 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

// TODO Windows error is not finished
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[cfg(target_os = "windows")]
#[error("{0}")]
NixError(#[from] nix::Error),
OsError(i32),

#[cfg(any(target_os = "linux", target_os = "macos"))]

Choose a reason for hiding this comment

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

[target.'cfg(target_family = "unix")'.dependencies]

#[error("{0}")]
OsError(#[from] nix::Error),

#[error("{0}")]
IoError(#[from] std::io::Error),
#[error("create profiler error")]
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,19 @@ 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;

mod addr_validate;
mod platform;

mod backtrace;
mod collector;
mod error;
mod frames;
mod profiler;
mod report;
mod timer;

pub use self::addr_validate::validate;
pub use self::collector::{Collector, HashCounter};
pub use self::error::{Error, Result};
pub use self::frames::{Frames, Symbol};
pub use self::platform::addr_validate::validate;
pub use self::profiler::{ProfilerGuard, ProfilerGuardBuilder};
pub use self::report::{Report, ReportBuilder, UnresolvedReport};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
impl super::Frame for backtrace::Frame {
impl crate::backtrace::Frame for backtrace::Frame {
type S = backtrace::Symbol;

fn ip(&self) -> usize {
Expand All @@ -16,7 +16,7 @@ impl super::Frame for backtrace::Frame {

pub struct Trace {}

impl super::Trace for Trace {
impl crate::backtrace::Trace for Trace {
type Frame = backtrace::Frame;

fn trace<F: FnMut(&Self::Frame) -> bool>(_: *mut libc::c_void, cb: F) {
Expand Down
49 changes: 49 additions & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#[cfg(any(target_os = "linux", target_os = "macos"))]

Choose a reason for hiding this comment

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

[target.'cfg(target_family = "unix")'.dependencies]

mod nix_impl {
pub mod addr_validate;
pub mod profiler;
pub mod timer;

#[cfg(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer",
))]
mod frame_pointer;
#[cfg(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer",
))]
pub use frame_pointer::Trace as TraceImpl;

#[cfg(not(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer",
)))]
#[path = "../backtrace_rs.rs"]
mod backtrace_rs;
#[cfg(not(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
feature = "frame-pointer",
)))]
pub use backtrace_rs::Trace as TraceImpl;
}

#[cfg(target_os = "windows")]
mod windows_impl {
pub mod addr_validate;
pub mod profiler;
pub mod timer;

#[cfg(feature = "frame-pointer")]
std::compile_error!("frame-pointer feature is currently not supported on windows.");

#[path = "../backtrace_rs.rs"]
mod backtrace_rs;
pub use backtrace_rs::Trace as TraceImpl;
}

#[cfg(any(target_os = "linux", target_os = "macos"))]

Choose a reason for hiding this comment

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

[target.'cfg(target_family = "unix")'.dependencies]

pub use nix_impl::*;

#[cfg(target_os = "windows")]
pub use windows_impl::*;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ptr::null_mut;

use libc::c_void;

use crate::addr_validate::validate;
use crate::validate;

#[derive(Clone, Debug)]
pub struct Frame {
Expand All @@ -16,7 +16,7 @@ extern "C" {

}

impl super::Frame for Frame {
impl crate::backtrace::Frame for Frame {
type S = backtrace::Symbol;

fn ip(&self) -> usize {
Expand All @@ -37,7 +37,7 @@ impl super::Frame for Frame {
}

pub struct Trace {}
impl super::Trace for Trace {
impl crate::backtrace::Trace for Trace {
type Frame = Frame;

fn trace<F: FnMut(&Self::Frame) -> bool>(ucontext: *mut libc::c_void, mut cb: F) {
Expand Down
Loading