(&self, top: usize)
| 423 | } |
| 424 | |
| 425 | pub fn functions_report(&self, top: usize) -> MemoryFunctionReport { |
| 426 | let Some(_guard) = ReentryGuard::enter() else { |
| 427 | return MemoryFunctionReport { |
| 428 | generated_at_ms: now_ms(), |
| 429 | top: normalize_top(top), |
| 430 | total_live_bytes_estimate: 0, |
| 431 | process_rss_bytes: 0, |
| 432 | entries: Vec::new(), |
| 433 | }; |
| 434 | }; |
| 435 | let top = normalize_top(top); |
| 436 | let (process_rss_bytes, _) = read_process_memory(); |
| 437 | let (total_live_bytes, rows) = self.collect_stack_rows(); |
| 438 | |
| 439 | let mut agg: HashMap<String, FunctionAggregate> = HashMap::new(); |
| 440 | for (stack, stats) in rows { |
| 441 | let function = self.resolve_function_for_stack(&stack); |
| 442 | let module = module_name_from_function(&function); |
| 443 | let entry = agg |
| 444 | .entry(function.clone()) |
| 445 | .or_insert_with(|| FunctionAggregate { |
| 446 | function, |
| 447 | module, |
| 448 | live_bytes: 0, |
| 449 | alloc_bytes_total: 0, |
| 450 | stack_count: 0, |
| 451 | alloc_count: 0, |
| 452 | free_count: 0, |
| 453 | }); |
| 454 | entry.live_bytes = entry.live_bytes.saturating_add(stats.live_bytes); |
| 455 | entry.alloc_bytes_total = entry |
| 456 | .alloc_bytes_total |
| 457 | .saturating_add(stats.alloc_bytes_total); |
| 458 | entry.stack_count = entry.stack_count.saturating_add(1); |
| 459 | entry.alloc_count = entry.alloc_count.saturating_add(stats.alloc_count); |
| 460 | entry.free_count = entry.free_count.saturating_add(stats.free_count); |
| 461 | } |
| 462 | |
| 463 | let mut values = agg.into_values().collect::<Vec<_>>(); |
| 464 | values.sort_by(|left, right| right.live_bytes.cmp(&left.live_bytes)); |
| 465 | |
| 466 | let entries = values |
| 467 | .into_iter() |
| 468 | .take(top) |
| 469 | .map(|item| MemoryFunctionEntry { |
| 470 | function: item.function, |
| 471 | module: item.module, |
| 472 | live_bytes_estimate: item.live_bytes, |
| 473 | alloc_bytes_total_estimate: item.alloc_bytes_total, |
| 474 | stack_count: item.stack_count, |
| 475 | alloc_count: item.alloc_count, |
| 476 | free_count: item.free_count, |
| 477 | live_ratio_heap: ratio(item.live_bytes, total_live_bytes), |
| 478 | live_ratio_rss: ratio(item.live_bytes, process_rss_bytes), |
| 479 | }) |
| 480 | .collect(); |
| 481 | |
| 482 | MemoryFunctionReport { |
no test coverage detected