diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_prof/src/lib.rs | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/crates/ra_prof/src/lib.rs b/crates/ra_prof/src/lib.rs index 1cc8e361d..121a62813 100644 --- a/crates/ra_prof/src/lib.rs +++ b/crates/ra_prof/src/lib.rs | |||
@@ -6,7 +6,7 @@ use std::iter::repeat; | |||
6 | use std::collections::{HashSet}; | 6 | use std::collections::{HashSet}; |
7 | use std::default::Default; | 7 | use std::default::Default; |
8 | use std::iter::FromIterator; | 8 | use std::iter::FromIterator; |
9 | use std::sync::RwLock; | 9 | use std::sync::{RwLock, atomic::{AtomicBool, Ordering}}; |
10 | use lazy_static::lazy_static; | 10 | use lazy_static::lazy_static; |
11 | 11 | ||
12 | /// Set profiling filter. It specifies descriptions allowed to profile. | 12 | /// Set profiling filter. It specifies descriptions allowed to profile. |
@@ -24,6 +24,7 @@ use lazy_static::lazy_static; | |||
24 | /// ``` | 24 | /// ``` |
25 | /// | 25 | /// |
26 | pub fn set_filter(f: Filter) { | 26 | pub fn set_filter(f: Filter) { |
27 | PROFILING_ENABLED.store(f.depth > 0, Ordering::SeqCst); | ||
27 | let set = HashSet::from_iter(f.allowed.iter().cloned()); | 28 | let set = HashSet::from_iter(f.allowed.iter().cloned()); |
28 | let mut old = FILTER.write().unwrap(); | 29 | let mut old = FILTER.write().unwrap(); |
29 | let filter_data = FilterData { depth: f.depth, allowed: set, version: old.version + 1 }; | 30 | let filter_data = FilterData { depth: f.depth, allowed: set, version: old.version + 1 }; |
@@ -62,6 +63,11 @@ pub fn set_filter(f: Filter) { | |||
62 | /// ``` | 63 | /// ``` |
63 | /// | 64 | /// |
64 | pub fn profile(desc: &str) -> Profiler { | 65 | pub fn profile(desc: &str) -> Profiler { |
66 | assert!(!desc.is_empty()); | ||
67 | if !PROFILING_ENABLED.load(Ordering::Relaxed) { | ||
68 | return Profiler { desc: None }; | ||
69 | } | ||
70 | |||
65 | PROFILE_STACK.with(|stack| { | 71 | PROFILE_STACK.with(|stack| { |
66 | let mut stack = stack.borrow_mut(); | 72 | let mut stack = stack.borrow_mut(); |
67 | if stack.starts.len() == 0 { | 73 | if stack.starts.len() == 0 { |
@@ -74,14 +80,14 @@ pub fn profile(desc: &str) -> Profiler { | |||
74 | Err(_) => (), | 80 | Err(_) => (), |
75 | }; | 81 | }; |
76 | } | 82 | } |
77 | let desc_str = desc.to_string(); | 83 | |
78 | if desc_str.is_empty() { | 84 | if stack.starts.len() > stack.filter_data.depth { |
79 | Profiler { desc: None } | 85 | return Profiler { desc: None }; |
80 | } else if stack.starts.len() < stack.filter_data.depth | 86 | } |
81 | && stack.filter_data.allowed.contains(&desc_str) | 87 | |
82 | { | 88 | if stack.filter_data.allowed.is_empty() || stack.filter_data.allowed.contains(desc) { |
83 | stack.starts.push(Instant::now()); | 89 | stack.starts.push(Instant::now()); |
84 | Profiler { desc: Some(desc_str) } | 90 | Profiler { desc: Some(desc.to_string()) } |
85 | } else { | 91 | } else { |
86 | Profiler { desc: None } | 92 | Profiler { desc: None } |
87 | } | 93 | } |
@@ -128,6 +134,8 @@ struct FilterData { | |||
128 | allowed: HashSet<String>, | 134 | allowed: HashSet<String>, |
129 | } | 135 | } |
130 | 136 | ||
137 | static PROFILING_ENABLED: AtomicBool = AtomicBool::new(false); | ||
138 | |||
131 | lazy_static! { | 139 | lazy_static! { |
132 | static ref FILTER: RwLock<FilterData> = RwLock::new(Default::default()); | 140 | static ref FILTER: RwLock<FilterData> = RwLock::new(Default::default()); |
133 | } | 141 | } |