1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! Tile-level profiling methods for BrickProfiler.
//!
//! TILING-SPEC-001: Tile-Level Profiling Support (Phase 15).
//! Extracted from mod.rs to keep file sizes manageable.
use super::BrickProfiler;
use crate::brick::profiler::tile_stats::{TileLevel, TileStats, TileTimer};
use std::time::Instant;
impl BrickProfiler {
// ========================================================================
// TILING-SPEC-001: Tile-Level Profiling (Phase 15)
// ========================================================================
/// Enable tile-level profiling.
///
/// When enabled, `start_tile()`/`stop_tile()` record per-tile statistics
/// for Macro/Midi/Micro tile hierarchy.
pub fn enable_tile_profiling(&mut self) {
self.tile_profiling_enabled = true;
}
/// Disable tile-level profiling.
pub fn disable_tile_profiling(&mut self) {
self.tile_profiling_enabled = false;
}
/// Check if tile profiling is enabled.
#[must_use]
pub fn is_tile_profiling_enabled(&self) -> bool {
self.tile_profiling_enabled
}
/// Start timing a tile execution.
///
/// Returns a `TileTimer` that should be passed to `stop_tile()` after
/// the tile computation completes.
///
/// # Arguments
/// - `level`: Tile hierarchy level (Macro/Midi/Micro)
/// - `row`: Row index within parent tile
/// - `col`: Column index within parent tile
///
/// # Example
/// ```rust,ignore
/// let timer = profiler.start_tile(TileLevel::Macro, 0, 0);
/// // ... execute tile computation ...
/// profiler.stop_tile(timer, 256 * 256, 2 * 256 * 256 * 256);
/// ```
#[must_use]
pub fn start_tile(&self, level: TileLevel, row: u32, col: u32) -> TileTimer {
TileTimer { level, _row: row, _col: col, start: Instant::now() }
}
/// Stop timing and record tile statistics.
///
/// # Arguments
/// - `timer`: Timer handle from `start_tile()`
/// - `elements`: Number of elements processed by this tile
/// - `flops`: Number of floating-point operations performed
pub fn stop_tile(&mut self, timer: TileTimer, elements: u64, flops: u64) {
if !self.tile_profiling_enabled {
return;
}
let elapsed_ns = timer.start.elapsed().as_nanos() as u64;
let idx = timer.level as usize;
self.tile_stats[idx].add_sample(elapsed_ns, elements, flops);
}
/// Get tile statistics for a given level.
#[must_use]
pub fn tile_stats(&self, level: TileLevel) -> &TileStats {
&self.tile_stats[level as usize]
}
/// Get mutable tile statistics for a given level.
pub fn tile_stats_mut(&mut self, level: TileLevel) -> &mut TileStats {
&mut self.tile_stats[level as usize]
}
/// Get all tile statistics as a slice.
#[must_use]
pub fn all_tile_stats(&self) -> &[TileStats; 3] {
&self.tile_stats
}
/// Reset tile statistics for all levels.
pub fn reset_tile_stats(&mut self) {
self.tile_stats = [
TileStats::new(TileLevel::Macro),
TileStats::new(TileLevel::Midi),
TileStats::new(TileLevel::Micro),
];
}
}