minor clenup

This commit is contained in:
Aleksey Kladov 2020-04-25 14:52:23 +02:00
parent 5671bacfa6
commit 7623db1106
2 changed files with 15 additions and 16 deletions

View file

@ -26,11 +26,18 @@ pub use crate::memory_usage::{Bytes, MemoryUsage};
#[global_allocator] #[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
/// Filtering syntax
/// env RA_PROFILE=* // dump everything
/// env RA_PROFILE=foo|bar|baz // enabled only selected entries
/// env RA_PROFILE=*@3>10 // dump everything, up to depth 3, if it takes more than 10 ms
pub fn init() { pub fn init() {
set_filter(match std::env::var("RA_PROFILE") { let spec = std::env::var("RA_PROFILE").unwrap_or_default();
Ok(spec) => Filter::from_spec(&spec), init_from(&spec);
Err(_) => Filter::disabled(), }
});
pub fn init_from(spec: &str) {
let filter = if spec.is_empty() { Filter::disabled() } else { Filter::from_spec(spec) };
set_filter(filter);
} }
/// Set profiling filter. It specifies descriptions allowed to profile. /// Set profiling filter. It specifies descriptions allowed to profile.
@ -43,7 +50,7 @@ pub fn init() {
/// let f = Filter::from_spec("profile1|profile2@2"); /// let f = Filter::from_spec("profile1|profile2@2");
/// set_filter(f); /// set_filter(f);
/// ``` /// ```
pub fn set_filter(f: Filter) { fn set_filter(f: Filter) {
PROFILING_ENABLED.store(f.depth > 0, Ordering::SeqCst); PROFILING_ENABLED.store(f.depth > 0, Ordering::SeqCst);
let set: HashSet<_> = f.allowed.iter().cloned().collect(); let set: HashSet<_> = f.allowed.iter().cloned().collect();
let mut old = FILTER.write().unwrap(); let mut old = FILTER.write().unwrap();
@ -127,18 +134,14 @@ impl Profiler {
} }
} }
pub struct Filter { struct Filter {
depth: usize, depth: usize,
allowed: Vec<String>, allowed: Vec<String>,
longer_than: Duration, longer_than: Duration,
} }
impl Filter { impl Filter {
// Filtering syntax fn from_spec(mut spec: &str) -> Filter {
// env RA_PROFILE=* // dump everything
// env RA_PROFILE=foo|bar|baz // enabled only selected entries
// env RA_PROFILE=*@3>10 // dump everything, up to depth 3, if it takes more than 10 ms
pub fn from_spec(mut spec: &str) -> Filter {
let longer_than = if let Some(idx) = spec.rfind('>') { let longer_than = if let Some(idx) = spec.rfind('>') {
let longer_than = spec[idx + 1..].parse().expect("invalid profile longer_than"); let longer_than = spec[idx + 1..].parse().expect("invalid profile longer_than");
spec = &spec[..idx]; spec = &spec[..idx];

View file

@ -62,11 +62,7 @@ impl<'a> Project<'a> {
static INIT: Once = Once::new(); static INIT: Once = Once::new();
INIT.call_once(|| { INIT.call_once(|| {
env_logger::builder().is_test(true).try_init().unwrap(); env_logger::builder().is_test(true).try_init().unwrap();
ra_prof::set_filter(if crate::PROFILE.is_empty() { ra_prof::init_from(crate::PROFILE);
ra_prof::Filter::disabled()
} else {
ra_prof::Filter::from_spec(&crate::PROFILE)
});
}); });
let mut paths = vec![]; let mut paths = vec![];