mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-11-19 03:28:36 +00:00
Rename ra_prof -> profile
This commit is contained in:
parent
98baa9b569
commit
208b7bd7ba
61 changed files with 154 additions and 188 deletions
39
crates/profile/src/google_cpu_profiler.rs
Normal file
39
crates/profile/src/google_cpu_profiler.rs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
//! https://github.com/gperftools/gperftools
|
||||
|
||||
use std::{
|
||||
ffi::CString,
|
||||
os::raw::c_char,
|
||||
path::Path,
|
||||
sync::atomic::{AtomicUsize, Ordering},
|
||||
};
|
||||
|
||||
#[link(name = "profiler")]
|
||||
#[allow(non_snake_case)]
|
||||
extern "C" {
|
||||
fn ProfilerStart(fname: *const c_char) -> i32;
|
||||
fn ProfilerStop();
|
||||
}
|
||||
|
||||
static PROFILER_STATE: AtomicUsize = AtomicUsize::new(OFF);
|
||||
const OFF: usize = 0;
|
||||
const ON: usize = 1;
|
||||
const PENDING: usize = 2;
|
||||
|
||||
pub fn start(path: &Path) {
|
||||
if PROFILER_STATE.compare_and_swap(OFF, PENDING, Ordering::SeqCst) != OFF {
|
||||
panic!("profiler already started");
|
||||
}
|
||||
let path = CString::new(path.display().to_string()).unwrap();
|
||||
if unsafe { ProfilerStart(path.as_ptr()) } == 0 {
|
||||
panic!("profiler failed to start")
|
||||
}
|
||||
assert!(PROFILER_STATE.compare_and_swap(PENDING, ON, Ordering::SeqCst) == PENDING);
|
||||
}
|
||||
|
||||
pub fn stop() {
|
||||
if PROFILER_STATE.compare_and_swap(ON, PENDING, Ordering::SeqCst) != ON {
|
||||
panic!("profiler is not started")
|
||||
}
|
||||
unsafe { ProfilerStop() };
|
||||
assert!(PROFILER_STATE.compare_and_swap(PENDING, OFF, Ordering::SeqCst) == PENDING);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue