Remove cpuprofile dependencies

This commit is contained in:
Aleksey Kladov 2019-08-17 15:29:57 +03:00
parent c2dcabef2a
commit e751e4d8a3
5 changed files with 63 additions and 64 deletions

View file

@ -18,5 +18,5 @@ ra_db = { path = "../ra_db" }
[dependencies.ra_prof]
path = "../ra_prof"
# features = [ "cpuprofiler" ]
# features = [ "cpu_profiler" ]
# features = [ "jemalloc" ]

View file

@ -9,10 +9,9 @@ publish = false
once_cell = "0.2.0"
itertools = "0.8.0"
backtrace = "0.3.28"
cpuprofiler = { version = "0.0.3", optional = true }
jemallocator = { version = "0.3.2", optional = true }
jemalloc-ctl = { version = "0.3.2", optional = true }
[features]
jemalloc = [ "jemallocator", "jemalloc-ctl" ]
cpu_profiler = []

View 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);
}

View file

@ -1,4 +1,6 @@
mod memory_usage;
#[cfg(feature = "cpu_profiler")]
mod google_cpu_profiler;
use std::{
cell::RefCell,
@ -268,25 +270,35 @@ impl Drop for Scope {
}
}
/// A wrapper around https://github.com/AtheMathmo/cpuprofiler
/// A wrapper around google_cpu_profiler.
///
/// It can be used to capture sampling profiles of sections of code.
/// It is not exactly out-of-the-box, as it relies on gperftools.
/// See the docs for the crate for more!
/// Usage:
/// 1. Install gpref_tools (https://github.com/gperftools/gperftools), probably packaged with your Linux distro.
/// 2. Build with `cpu_profiler` feature.
/// 3. Tun the code, the *raw* output would be in the `./out.profile` file.
/// 4. Install pprof for visualization (https://github.com/google/pprof).
/// 5. Use something like `pprof -svg target/release/ra_cli ./out.profile` to see the results.
///
/// For example, here's how I run profiling on NixOS:
///
/// ```bash
/// $ nix-shell -p gperftools --run \
/// 'cargo run --release -p ra_cli -- parse < ~/projects/rustbench/parser.rs > /dev/null'
/// ```
#[derive(Debug)]
pub struct CpuProfiler {
_private: (),
}
pub fn cpu_profiler() -> CpuProfiler {
#[cfg(feature = "cpuprofiler")]
#[cfg(feature = "cpu_profiler")]
{
cpuprofiler::PROFILER.lock().unwrap().start("./out.profile").unwrap();
google_cpu_profiler::start("./out.profile".as_ref())
}
#[cfg(not(feature = "cpuprofiler"))]
#[cfg(not(feature = "cpu_profiler"))]
{
eprintln!("cpuprofiler feature is disabled")
eprintln!("cpu_profiler feature is disabled")
}
CpuProfiler { _private: () }
@ -294,9 +306,9 @@ pub fn cpu_profiler() -> CpuProfiler {
impl Drop for CpuProfiler {
fn drop(&mut self) {
#[cfg(feature = "cpuprofiler")]
#[cfg(feature = "cpu_profiler")]
{
cpuprofiler::PROFILER.lock().unwrap().stop().unwrap();
google_cpu_profiler::stop()
}
}
}