filter by time

This commit is contained in:
Aleksey Kladov 2019-04-14 23:18:58 +03:00
parent b228947b68
commit 5b7012318c
3 changed files with 44 additions and 29 deletions

View file

@ -27,7 +27,8 @@ pub fn set_filter(f: Filter) {
PROFILING_ENABLED.store(f.depth > 0, Ordering::SeqCst);
let set = HashSet::from_iter(f.allowed.iter().cloned());
let mut old = FILTER.write().unwrap();
let filter_data = FilterData { depth: f.depth, allowed: set, version: old.version + 1 };
let filter_data =
FilterData { depth: f.depth, allowed: set, cutoff: f.cutoff, version: old.version + 1 };
*old = filter_data;
}
@ -101,15 +102,41 @@ pub struct Profiler {
pub struct Filter {
depth: usize,
allowed: Vec<String>,
cutoff: Duration,
}
impl Filter {
pub fn disabled() -> Filter {
Filter::new(0, Vec::new())
// 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 from_spec(mut spec: &str) -> Filter {
let cutoff = if let Some(idx) = spec.rfind(">") {
let cutoff = spec[idx + 1..].parse().expect("invalid profile cutoff");
spec = &spec[..idx];
Duration::from_millis(cutoff)
} else {
Duration::new(0, 0)
};
let depth = if let Some(idx) = spec.rfind("@") {
let depth: usize = spec[idx + 1..].parse().expect("invalid profile depth");
spec = &spec[..idx];
depth
} else {
999
};
let allowed =
if spec == "*" { Vec::new() } else { spec.split("|").map(String::from).collect() };
Filter::new(depth, allowed, cutoff)
}
pub fn new(depth: usize, allowed: Vec<String>) -> Filter {
Filter { depth, allowed }
pub fn disabled() -> Filter {
Filter::new(0, Vec::new(), Duration::new(0, 0))
}
pub fn new(depth: usize, allowed: Vec<String>, cutoff: Duration) -> Filter {
Filter { depth, allowed, cutoff }
}
}
@ -136,6 +163,7 @@ struct FilterData {
depth: usize,
version: usize,
allowed: HashSet<String>,
cutoff: Duration,
}
static PROFILING_ENABLED: AtomicBool = AtomicBool::new(false);
@ -159,7 +187,9 @@ impl Drop for Profiler {
stack.messages.push(Message { level, duration, message });
if level == 0 {
let stdout = stderr();
print(0, &stack.messages, &mut stdout.lock());
if duration >= stack.filter_data.cutoff {
print(0, &stack.messages, &mut stdout.lock());
}
stack.messages.clear();
}
});