Skip to content

Commit

Permalink
Add loongarch64 support
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaixiaojuan committed Nov 9, 2022
1 parent 383921f commit 9c3c310
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 14 deletions.
3 changes: 3 additions & 0 deletions src/backtrace/frame_pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ impl super::Trace for Trace {
}
};

#[cfg(all(target_arch = "loongarch64", target_os = "linux"))]
let frame_pointer = unsafe { (*ucontext).uc_mcontext.sc_regs[22] as usize };

let mut frame_pointer = frame_pointer as *mut FramePointerLayout;

let mut last_frame_pointer: *mut FramePointerLayout = null_mut();
Expand Down
24 changes: 20 additions & 4 deletions src/backtrace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,39 @@ pub trait Trace {
}

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

#[cfg(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
),
feature = "frame-pointer"
))]
pub mod frame_pointer;
#[cfg(all(
any(target_arch = "x86_64", target_arch = "aarch64"),
any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
),
feature = "frame-pointer"
))]
pub use frame_pointer::Trace as TraceImpl;
63 changes: 53 additions & 10 deletions src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use once_cell::sync::Lazy;
use parking_lot::RwLock;
use smallvec::SmallVec;

#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
))]
use findshlibs::{Segment, SharedLibrary, TargetSharedLibrary};

use crate::backtrace::{Trace, TraceImpl};
Expand All @@ -29,14 +33,22 @@ pub struct Profiler {

running: bool,

#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64")))]
#[cfg(all(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
)))]
blocklist_segments: Vec<(usize, usize)>,
}

#[derive(Clone)]
pub struct ProfilerGuardBuilder {
frequency: c_int,
#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64")))]
#[cfg(all(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
)))]
blocklist_segments: Vec<(usize, usize)>,
}

Expand All @@ -45,7 +57,11 @@ impl Default for ProfilerGuardBuilder {
ProfilerGuardBuilder {
frequency: 99,

#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64")))]
#[cfg(all(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
)))]
blocklist_segments: Vec::new(),
}
}
Expand All @@ -56,7 +72,11 @@ impl ProfilerGuardBuilder {
Self { frequency, ..self }
}

#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64")))]
#[cfg(all(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
)))]
pub fn blocklist<T: AsRef<str>>(self, blocklist: &[T]) -> Self {
let blocklist_segments = {
let mut segments = Vec::new();
Expand Down Expand Up @@ -101,7 +121,11 @@ impl ProfilerGuardBuilder {
Err(Error::CreatingError)
}
Ok(profiler) => {
#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64")))]
#[cfg(all(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
)))]
{
profiler.blocklist_segments = self.blocklist_segments;
}
Expand Down Expand Up @@ -234,7 +258,11 @@ impl Drop for ErrnoProtector {

#[no_mangle]
#[cfg_attr(
not(all(any(target_arch = "x86_64", target_arch = "aarch64"))),
not(all(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
))),
allow(unused_variables)
)]
extern "C" fn perf_signal_handler(
Expand All @@ -246,7 +274,11 @@ extern "C" fn perf_signal_handler(

if let Some(mut guard) = PROFILER.try_write() {
if let Ok(profiler) = guard.as_mut() {
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
#[cfg(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
))]
if !ucontext.is_null() {
let ucontext: *mut libc::ucontext_t = ucontext as *mut libc::ucontext_t;

Expand Down Expand Up @@ -277,6 +309,9 @@ extern "C" fn perf_signal_handler(
}
};

#[cfg(all(target_arch = "loongarch64", target_os = "linux"))]
let addr = unsafe { (*ucontext).uc_mcontext.sc_pc as usize };

if profiler.is_blocklisted(addr) {
return;
}
Expand Down Expand Up @@ -324,12 +359,20 @@ impl Profiler {
sample_counter: 0,
running: false,

#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64")))]
#[cfg(all(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
)))]
blocklist_segments: Vec::new(),
})
}

#[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64")))]
#[cfg(all(any(
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "loongarch64"
)))]
fn is_blocklisted(&self, addr: usize) -> bool {
for libs in &self.blocklist_segments {
if addr > libs.0 && addr < libs.1 {
Expand Down

0 comments on commit 9c3c310

Please sign in to comment.