Drains the timeline, totals and counters.
(&self, file_path: &dyn Fn(u32) -> Option<String>)
| 422 | |
| 423 | /// Drains the timeline, totals and counters. |
| 424 | pub(super) fn take(&self, file_path: &dyn Fn(u32) -> Option<String>) -> PerfTake { |
| 425 | let mut threads = Vec::new(); |
| 426 | let mut events = Vec::new(); |
| 427 | let mut counters = [0; Counter::NAMES.len()]; |
| 428 | let mut summary = RustSummary::default(); |
| 429 | let mut slowest: HashMap<&'static str, Vec<Slow>> = HashMap::new(); |
| 430 | for buf in self.0.threads.lock().unwrap().iter() { |
| 431 | let mut buf = buf.lock().unwrap(); |
| 432 | threads.push((buf.tid, buf.name.clone())); |
| 433 | events.append(&mut buf.events); |
| 434 | for (total, n) in counters.iter_mut().zip(&mut buf.counters) { |
| 435 | *total += std::mem::take(n); |
| 436 | } |
| 437 | for (name, hits) in buf.cache_hits.drain() { |
| 438 | summary.queries.entry(name).or_default().cache_hits += hits; |
| 439 | } |
| 440 | for (name, agg) in buf.queries.drain() { |
| 441 | let stats = summary.queries.entry(name).or_default(); |
| 442 | stats.count += agg.count; |
| 443 | stats.busy_us += agg.busy / 1000; |
| 444 | stats.self_us += agg.self_ns / 1000; |
| 445 | stats.queued_us += agg.queued / 1000; |
| 446 | stats.wall_us += agg.wall / 1000; |
| 447 | stats.cancelled += agg.cancelled; |
| 448 | slowest |
| 449 | .entry(name) |
| 450 | .or_insert_with(Vec::new) |
| 451 | .extend(agg.slowest); |
| 452 | } |
| 453 | for (name, (count, self_ns)) in buf.spans.drain() { |
| 454 | let stats = summary.spans.entry(name).or_insert(SpanStats::default()); |
| 455 | stats.count += count; |
| 456 | stats.self_us += self_ns / 1000; |
| 457 | } |
| 458 | } |
| 459 | summary.counters = Counter::NAMES.into_iter().zip(counters).collect(); |
| 460 | // Paths are only looked up for the winners. |
| 461 | for (name, mut list) in slowest { |
| 462 | list.sort_by(|a, b| b.busy.cmp(&a.busy)); |
| 463 | list.truncate(SLOWEST); |
| 464 | summary.queries.entry(name).or_default().slowest = list |
| 465 | .into_iter() |
| 466 | .map(|s| SlowQuery { |
| 467 | q: s.seq, |
| 468 | busy_us: s.busy / 1000, |
| 469 | self_us: s.self_ns / 1000, |
| 470 | wall_us: s.wall / 1000, |
| 471 | file: s.file.and_then(file_path), |
| 472 | }) |
| 473 | .collect(); |
| 474 | } |
| 475 | threads.sort(); |
| 476 | let mut all = vec![TraceEvent::metadata("process_name", 0, "ng-analyze".into())]; |
| 477 | all.extend( |
| 478 | threads |
| 479 | .into_iter() |
| 480 | .map(|(tid, name)| TraceEvent::metadata("thread_name", tid, name)), |
| 481 | ); |