Pretty print the collected profiling data of all thread local [`Profiler`]s to the given writer
(out: &mut W)
| 246 | |
| 247 | /// Pretty print the collected profiling data of all thread local [`Profiler`]s to the given writer |
| 248 | pub fn write<W: io::Write>(out: &mut W) -> io::Result<()> { |
| 249 | let mut merged_scopes = HashMap::<ScopeId, Scope>::new(); |
| 250 | let mut roots = HashSet::<ScopeId>::new(); |
| 251 | |
| 252 | // Collect scopes over all threads |
| 253 | for profiler in PROFILER.iter() { |
| 254 | let profiler = profiler.read(); |
| 255 | roots.extend(profiler.roots.iter()); |
| 256 | |
| 257 | for (&id, scope) in &profiler.scopes { |
| 258 | merged_scopes |
| 259 | .entry(id) |
| 260 | .and_modify(|s| s.merge(scope)) |
| 261 | .or_insert_with(|| scope.clone()); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // Sort and filter root scopes |
| 266 | let sorted_roots = { |
| 267 | let root_hash = ScopeId::get_hash(None); |
| 268 | let mut roots = roots |
| 269 | .into_iter() |
| 270 | // Remove roots that are not actual roots (happens if their parent was set manually) |
| 271 | .filter(|id| id.parent_hash == root_hash) |
| 272 | // Get (id, scope) tuple |
| 273 | .flat_map(|id| merged_scopes.get(&id).cloned().map(|s| (id, s))) |
| 274 | .collect::<Vec<_>>(); |
| 275 | |
| 276 | roots.sort_unstable_by_key(|(_, s)| s.first_call); |
| 277 | roots |
| 278 | }; |
| 279 | |
| 280 | // Sort all scopes by first call time |
| 281 | let sorted_scopes = { |
| 282 | let mut scopes = merged_scopes.into_iter().collect::<Vec<_>>(); |
| 283 | scopes.sort_unstable_by_key(|(_, s)| s.first_call); |
| 284 | scopes |
| 285 | }; |
| 286 | |
| 287 | // Print the stats |
| 288 | for root in &sorted_roots { |
| 289 | write_recursively(out, sorted_scopes.as_slice(), root, None, 0, false)?; |
| 290 | } |
| 291 | |
| 292 | Ok(()) |
| 293 | } |
| 294 | |
| 295 | /// Returns the pretty printed output of the collected profiling data as a `String` |
| 296 | pub fn write_to_string() -> Result<String, Box<dyn Error>> { |
no test coverage detected