-
Notifications
You must be signed in to change notification settings - Fork 113
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
Jardynq
wants to merge
9
commits into
tikv:master
Choose a base branch
from
Jardynq:seperate-platform-impl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+539
−427
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
588ea16
Separated platform specific implementation into separate files
Jardynq d6a2d40
rename from platform_* to *_impl
Jardynq 35cbaa2
remove linux tests from profiler
Jardynq 46f2c30
move platform specific backtrace into ./platform
Jardynq 45d0a6c
Use fmt::write instead of push_str
Jardynq 20c4ad7
fix linux tests
Jardynq c12b87a
Moved trigger_lazy import into tests module
Jardynq c855009
Moved platform impl into traits
Jardynq e9f8cf7
fix write_thread_name_fallback and bench addr_validate
Jardynq File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
#[error("{0}")] | ||
OsError(#[from] nix::Error), | ||
|
||
#[error("{0}")] | ||
IoError(#[from] std::io::Error), | ||
#[error("create profiler error")] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#[cfg(any(target_os = "linux", target_os = "macos"))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
mod nix_impl { | ||
mod addr_validate; | ||
mod profiler; | ||
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 { | ||
mod addr_validate; | ||
mod profiler; | ||
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"))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
pub use nix_impl::*; | ||
|
||
#[cfg(target_os = "windows")] | ||
pub use windows_impl::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
use std::os::raw::c_int; | ||
use std::time::SystemTime; | ||
|
||
use smallvec::SmallVec; | ||
|
||
use nix::sys::signal; | ||
|
||
use crate::backtrace::{Frame, Trace, TraceImpl}; | ||
use crate::error::Result; | ||
use crate::profiler::{write_thread_name_fallback, Profiler, ProfilerImpl, PROFILER}; | ||
use crate::{MAX_DEPTH, MAX_THREAD_NAME}; | ||
|
||
impl ProfilerImpl for Profiler { | ||
fn register(&mut self) -> Result<()> { | ||
let handler = signal::SigHandler::SigAction(perf_signal_handler); | ||
let sigaction = signal::SigAction::new( | ||
handler, | ||
signal::SaFlags::SA_SIGINFO, | ||
signal::SigSet::empty(), | ||
); | ||
unsafe { signal::sigaction(signal::SIGPROF, &sigaction) }?; | ||
|
||
Ok(()) | ||
} | ||
fn unregister(&mut self) -> Result<()> { | ||
let handler = signal::SigHandler::SigIgn; | ||
unsafe { signal::signal(signal::SIGPROF, handler) }?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(not(all(any(target_os = "linux", target_os = "macos"), target_env = "gnu")))] | ||
fn write_thread_name(current_thread: libc::pthread_t, name: &mut [libc::c_char]) { | ||
write_thread_name_fallback(current_thread as usize as u128, name); | ||
} | ||
|
||
#[cfg(all(any(target_os = "linux", target_os = "macos"), target_env = "gnu"))] | ||
fn write_thread_name(current_thread: libc::pthread_t, name: &mut [libc::c_char]) { | ||
let name_ptr = name as *mut [libc::c_char] as *mut libc::c_char; | ||
let ret = unsafe { libc::pthread_getname_np(current_thread, name_ptr, MAX_THREAD_NAME) }; | ||
|
||
if ret != 0 { | ||
write_thread_name_fallback(current_thread as usize as u128, name); | ||
} | ||
} | ||
|
||
struct ErrnoProtector(libc::c_int); | ||
|
||
impl ErrnoProtector { | ||
fn new() -> Self { | ||
unsafe { | ||
#[cfg(target_os = "linux")] | ||
{ | ||
let errno = *libc::__errno_location(); | ||
Self(errno) | ||
} | ||
#[cfg(target_os = "macos")] | ||
{ | ||
let errno = *libc::__error(); | ||
Self(errno) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Drop for ErrnoProtector { | ||
fn drop(&mut self) { | ||
unsafe { | ||
#[cfg(target_os = "linux")] | ||
{ | ||
*libc::__errno_location() = self.0; | ||
} | ||
#[cfg(target_os = "macos")] | ||
{ | ||
*libc::__error() = self.0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
#[cfg_attr( | ||
not(all(any(target_arch = "x86_64", target_arch = "aarch64"))), | ||
allow(unused_variables) | ||
)] | ||
extern "C" fn perf_signal_handler( | ||
_signal: c_int, | ||
_siginfo: *mut libc::siginfo_t, | ||
ucontext: *mut libc::c_void, | ||
) { | ||
let _errno = ErrnoProtector::new(); | ||
|
||
if let Some(mut guard) = PROFILER.try_write() { | ||
if let Ok(profiler) = guard.as_mut() { | ||
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] | ||
if !ucontext.is_null() { | ||
let ucontext: *mut libc::ucontext_t = ucontext as *mut libc::ucontext_t; | ||
|
||
#[cfg(all(target_arch = "x86_64", target_os = "linux"))] | ||
let addr = | ||
unsafe { (*ucontext).uc_mcontext.gregs[libc::REG_RIP as usize] as usize }; | ||
|
||
#[cfg(all(target_arch = "x86_64", target_os = "macos"))] | ||
let addr = unsafe { | ||
let mcontext = (*ucontext).uc_mcontext; | ||
if mcontext.is_null() { | ||
0 | ||
} else { | ||
(*mcontext).__ss.__rip as usize | ||
} | ||
}; | ||
|
||
#[cfg(all(target_arch = "aarch64", target_os = "linux"))] | ||
let addr = unsafe { (*ucontext).uc_mcontext.pc as usize }; | ||
|
||
#[cfg(all(target_arch = "aarch64", target_os = "macos"))] | ||
let addr = unsafe { | ||
let mcontext = (*ucontext).uc_mcontext; | ||
if mcontext.is_null() { | ||
0 | ||
} else { | ||
(*mcontext).__ss.__pc as usize | ||
} | ||
}; | ||
|
||
if profiler.is_blocklisted(addr) { | ||
return; | ||
} | ||
} | ||
|
||
let mut bt: SmallVec<[<TraceImpl as Trace>::Frame; MAX_DEPTH]> = | ||
SmallVec::with_capacity(MAX_DEPTH); | ||
let mut index = 0; | ||
|
||
let sample_timestamp: SystemTime = SystemTime::now(); | ||
TraceImpl::trace(ucontext, |frame| { | ||
let ip = Frame::ip(frame); | ||
if profiler.is_blocklisted(ip) { | ||
return false; | ||
} | ||
|
||
if index < MAX_DEPTH { | ||
bt.push(frame.clone()); | ||
index += 1; | ||
true | ||
} else { | ||
false | ||
} | ||
}); | ||
|
||
let current_thread = unsafe { libc::pthread_self() }; | ||
let mut name = [0; MAX_THREAD_NAME]; | ||
let name_ptr = &mut name as *mut [libc::c_char] as *mut libc::c_char; | ||
|
||
write_thread_name(current_thread, &mut name); | ||
|
||
let name = unsafe { std::ffi::CStr::from_ptr(name_ptr) }; | ||
profiler.sample(bt, name.to_bytes(), current_thread as u64, sample_timestamp); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::profiler::trigger_lazy; | ||
|
||
use std::cell::RefCell; | ||
use std::ffi::c_void; | ||
use std::ptr::null_mut; | ||
|
||
#[cfg(not(target_env = "gnu"))] | ||
#[allow(clippy::wrong_self_convention)] | ||
#[allow(non_upper_case_globals)] | ||
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void> = None; | ||
|
||
extern "C" { | ||
#[cfg(target_env = "gnu")] | ||
static mut __malloc_hook: Option<extern "C" fn(size: usize) -> *mut c_void>; | ||
|
||
fn malloc(size: usize) -> *mut c_void; | ||
} | ||
|
||
thread_local! { | ||
static FLAG: RefCell<bool> = RefCell::new(false); | ||
} | ||
|
||
extern "C" fn malloc_hook(size: usize) -> *mut c_void { | ||
unsafe { | ||
__malloc_hook = None; | ||
} | ||
|
||
FLAG.with(|flag| { | ||
flag.replace(true); | ||
}); | ||
let p = unsafe { malloc(size) }; | ||
|
||
unsafe { | ||
__malloc_hook = Some(malloc_hook); | ||
} | ||
|
||
p | ||
} | ||
|
||
#[inline(never)] | ||
fn is_prime_number(v: usize, prime_numbers: &[usize]) -> bool { | ||
if v < 10000 { | ||
let r = prime_numbers.binary_search(&v); | ||
return r.is_ok(); | ||
} | ||
|
||
for n in prime_numbers { | ||
if v % n == 0 { | ||
return false; | ||
} | ||
} | ||
|
||
true | ||
} | ||
|
||
#[inline(never)] | ||
fn prepare_prime_numbers() -> Vec<usize> { | ||
// bootstrap: Generate a prime table of 0..10000 | ||
let mut prime_number_table: [bool; 10000] = [true; 10000]; | ||
prime_number_table[0] = false; | ||
prime_number_table[1] = false; | ||
for i in 2..10000 { | ||
if prime_number_table[i] { | ||
let mut v = i * 2; | ||
while v < 10000 { | ||
prime_number_table[v] = false; | ||
v += i; | ||
} | ||
} | ||
} | ||
let mut prime_numbers = vec![]; | ||
for (i, item) in prime_number_table.iter().enumerate().skip(2) { | ||
if *item { | ||
prime_numbers.push(i); | ||
} | ||
} | ||
prime_numbers | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
#[test] | ||
fn malloc_free() { | ||
trigger_lazy(); | ||
|
||
let prime_numbers = prepare_prime_numbers(); | ||
|
||
let mut _v = 0; | ||
|
||
unsafe { | ||
__malloc_hook = Some(malloc_hook); | ||
} | ||
for i in 2..50000 { | ||
if is_prime_number(i, &prime_numbers) { | ||
_v += 1; | ||
perf_signal_handler(27, null_mut(), null_mut()); | ||
} | ||
} | ||
unsafe { | ||
__malloc_hook = None; | ||
} | ||
|
||
FLAG.with(|flag| { | ||
assert!(!*flag.borrow()); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0. | ||
|
||
use crate::timer::{Timer, TimerImpl}; | ||
|
||
use std::os::raw::c_int; | ||
use std::ptr::null_mut; | ||
|
||
extern "C" { | ||
fn setitimer(which: c_int, new_value: *mut Itimerval, old_value: *mut Itimerval) -> c_int; | ||
} | ||
|
||
const ITIMER_PROF: c_int = 2; | ||
|
||
#[repr(C)] | ||
#[derive(Clone)] | ||
struct Timeval { | ||
pub tv_sec: i64, | ||
pub tv_usec: i64, | ||
} | ||
#[repr(C)] | ||
#[derive(Clone)] | ||
struct Itimerval { | ||
pub it_interval: Timeval, | ||
pub it_value: Timeval, | ||
} | ||
|
||
impl TimerImpl for Timer { | ||
fn start(&mut self) { | ||
let interval = 1e6 as i64 / i64::from(self.frequency); | ||
let it_interval = Timeval { | ||
tv_sec: interval / 1e6 as i64, | ||
tv_usec: interval % 1e6 as i64, | ||
}; | ||
let it_value = it_interval.clone(); | ||
|
||
unsafe { | ||
setitimer( | ||
ITIMER_PROF, | ||
&mut Itimerval { | ||
it_interval, | ||
it_value, | ||
}, | ||
null_mut(), | ||
) | ||
}; | ||
} | ||
fn stop(&mut self) { | ||
let it_interval = Timeval { | ||
tv_sec: 0, | ||
tv_usec: 0, | ||
}; | ||
let it_value = it_interval.clone(); | ||
unsafe { | ||
setitimer( | ||
ITIMER_PROF, | ||
&mut Itimerval { | ||
it_interval, | ||
it_value, | ||
}, | ||
null_mut(), | ||
) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use crate::validator::{Validator, ValidatorImpl}; | ||
|
||
impl ValidatorImpl for Validator { | ||
fn addr_validate(_: *const libc::c_void) -> bool { | ||
unimplemented!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use crate::error::Result; | ||
use crate::profiler::{Profiler, ProfilerImpl}; | ||
|
||
impl ProfilerImpl for Profiler { | ||
fn register(&mut self) -> Result<()> { | ||
unimplemented!() | ||
} | ||
fn unregister(&mut self) -> Result<()> { | ||
unimplemented!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use crate::timer::{Timer, TimerImpl}; | ||
|
||
impl TimerImpl for Timer { | ||
fn start(&mut self) {} | ||
fn stop(&mut self) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
pub struct Validator {} | ||
pub trait ValidatorImpl { | ||
fn addr_validate(addr: *const libc::c_void) -> bool; | ||
} | ||
|
||
pub fn addr_validate(addr: *const libc::c_void) -> bool { | ||
Validator::addr_validate(addr) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andmacos
may failRest of the code looks good