|
| 1 | +"""CPU profiling logic for Scalene.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import math |
| 6 | +import threading |
| 7 | +from typing import TYPE_CHECKING, Callable, cast |
| 8 | + |
| 9 | +from scalene.runningstats import RunningStats |
| 10 | +from scalene.scalene_funcutils import ScaleneFuncUtils |
| 11 | +from scalene.scalene_statistics import ( |
| 12 | + ByteCodeIndex, |
| 13 | + Filename, |
| 14 | + LineNumber, |
| 15 | + ScaleneStatistics, |
| 16 | +) |
| 17 | +from scalene.scalene_utility import add_stack, enter_function_meta |
| 18 | +from scalene.time_info import TimeInfo |
| 19 | + |
| 20 | +if TYPE_CHECKING: |
| 21 | + from types import FrameType |
| 22 | + |
| 23 | + |
| 24 | +class ScaleneCPUProfiler: |
| 25 | + """Handles CPU profiling sample processing.""" |
| 26 | + |
| 27 | + def __init__(self, stats: ScaleneStatistics, available_cpus: int) -> None: |
| 28 | + """Initialize the CPU profiler. |
| 29 | +
|
| 30 | + Args: |
| 31 | + stats: The statistics object to update with CPU samples. |
| 32 | + available_cpus: Number of available CPUs for utilization calculations. |
| 33 | + """ |
| 34 | + self._stats = stats |
| 35 | + self._available_cpus = available_cpus |
| 36 | + |
| 37 | + def process_cpu_sample( |
| 38 | + self, |
| 39 | + new_frames: list[tuple[FrameType, int, FrameType]], |
| 40 | + now: TimeInfo, |
| 41 | + gpu_load: float, |
| 42 | + gpu_mem_used: float, |
| 43 | + prev: TimeInfo, |
| 44 | + is_thread_sleeping: dict[int, bool], |
| 45 | + should_trace: Callable[[Filename, str], bool], |
| 46 | + last_cpu_interval: float, |
| 47 | + stacks_enabled: bool, |
| 48 | + ) -> None: |
| 49 | + """Handle interrupts for CPU profiling. |
| 50 | +
|
| 51 | + Args: |
| 52 | + new_frames: List of (frame, thread_id, original_frame) tuples. |
| 53 | + now: Current time information. |
| 54 | + gpu_load: Current GPU load (0.0-1.0). |
| 55 | + gpu_mem_used: Current GPU memory usage. |
| 56 | + prev: Previous time information. |
| 57 | + is_thread_sleeping: Dict mapping thread IDs to sleep status. |
| 58 | + should_trace: Function to check if a file/function should be traced. |
| 59 | + last_cpu_interval: The last CPU sampling interval. |
| 60 | + stacks_enabled: Whether stack collection is enabled. |
| 61 | + """ |
| 62 | + if not new_frames: |
| 63 | + return |
| 64 | + |
| 65 | + elapsed = now - prev |
| 66 | + |
| 67 | + # Skip samples with negative values (can occur in multi-process settings) |
| 68 | + if any([elapsed.virtual < 0, elapsed.wallclock < 0, elapsed.user < 0]): |
| 69 | + return |
| 70 | + |
| 71 | + # Calculate CPU utilization |
| 72 | + cpu_utilization = 0.0 |
| 73 | + if elapsed.wallclock != 0: |
| 74 | + cpu_utilization = elapsed.user / elapsed.wallclock |
| 75 | + |
| 76 | + core_utilization = cpu_utilization / self._available_cpus |
| 77 | + if cpu_utilization > 1.0: |
| 78 | + cpu_utilization = 1.0 |
| 79 | + elapsed.wallclock = elapsed.user |
| 80 | + |
| 81 | + # Handle NaN GPU load |
| 82 | + if math.isnan(gpu_load): |
| 83 | + gpu_load = 0.0 |
| 84 | + assert 0.0 <= gpu_load <= 1.0 |
| 85 | + |
| 86 | + gpu_time = gpu_load * elapsed.wallclock |
| 87 | + self._stats.gpu_stats.total_gpu_samples += gpu_time |
| 88 | + |
| 89 | + python_time = last_cpu_interval |
| 90 | + c_time = max(elapsed.virtual - python_time, 0) |
| 91 | + total_time = python_time + c_time |
| 92 | + |
| 93 | + # Count non-sleeping frames |
| 94 | + total_frames = sum( |
| 95 | + not is_thread_sleeping[tident] for frame, tident, orig_frame in new_frames |
| 96 | + ) |
| 97 | + if total_frames == 0: |
| 98 | + total_frames = 1 |
| 99 | + |
| 100 | + normalized_time = total_time / total_frames |
| 101 | + average_python_time = python_time / total_frames |
| 102 | + average_c_time = c_time / total_frames |
| 103 | + average_cpu_time = (python_time + c_time) / total_frames |
| 104 | + |
| 105 | + # Process main thread |
| 106 | + main_thread_frame = new_frames[0][0] |
| 107 | + |
| 108 | + if stacks_enabled: |
| 109 | + add_stack( |
| 110 | + main_thread_frame, |
| 111 | + should_trace, |
| 112 | + self._stats.stacks, |
| 113 | + average_python_time, |
| 114 | + average_c_time, |
| 115 | + average_cpu_time, |
| 116 | + ) |
| 117 | + |
| 118 | + enter_function_meta(main_thread_frame, should_trace, self._stats) |
| 119 | + fname = Filename(main_thread_frame.f_code.co_filename) |
| 120 | + lineno = LineNumber(main_thread_frame.f_lineno) |
| 121 | + |
| 122 | + main_tid = cast(int, threading.main_thread().ident) |
| 123 | + if not is_thread_sleeping[main_tid]: |
| 124 | + self._update_main_thread_stats( |
| 125 | + fname, |
| 126 | + lineno, |
| 127 | + now, |
| 128 | + average_python_time, |
| 129 | + average_c_time, |
| 130 | + average_cpu_time, |
| 131 | + cpu_utilization, |
| 132 | + core_utilization, |
| 133 | + gpu_load, |
| 134 | + gpu_mem_used, |
| 135 | + elapsed, |
| 136 | + ) |
| 137 | + |
| 138 | + # Process other threads |
| 139 | + for frame, tident, orig_frame in new_frames: |
| 140 | + if frame == main_thread_frame: |
| 141 | + continue |
| 142 | + |
| 143 | + add_stack( |
| 144 | + frame, |
| 145 | + should_trace, |
| 146 | + self._stats.stacks, |
| 147 | + average_python_time, |
| 148 | + average_c_time, |
| 149 | + average_cpu_time, |
| 150 | + ) |
| 151 | + |
| 152 | + fname = Filename(frame.f_code.co_filename) |
| 153 | + lineno = LineNumber(frame.f_lineno) |
| 154 | + enter_function_meta(frame, should_trace, self._stats) |
| 155 | + |
| 156 | + if is_thread_sleeping[tident]: |
| 157 | + continue |
| 158 | + |
| 159 | + self._update_thread_stats( |
| 160 | + fname, |
| 161 | + lineno, |
| 162 | + orig_frame, |
| 163 | + normalized_time, |
| 164 | + cpu_utilization, |
| 165 | + core_utilization, |
| 166 | + ) |
| 167 | + |
| 168 | + # Cleanup |
| 169 | + del new_frames[:] |
| 170 | + del new_frames |
| 171 | + del is_thread_sleeping |
| 172 | + self._stats.cpu_stats.total_cpu_samples += total_time |
| 173 | + |
| 174 | + def _update_main_thread_stats( |
| 175 | + self, |
| 176 | + fname: Filename, |
| 177 | + lineno: LineNumber, |
| 178 | + now: TimeInfo, |
| 179 | + average_python_time: float, |
| 180 | + average_c_time: float, |
| 181 | + average_cpu_time: float, |
| 182 | + cpu_utilization: float, |
| 183 | + core_utilization: float, |
| 184 | + gpu_load: float, |
| 185 | + gpu_mem_used: float, |
| 186 | + elapsed: TimeInfo, |
| 187 | + ) -> None: |
| 188 | + """Update statistics for the main thread.""" |
| 189 | + cpu_stats = self._stats.cpu_stats |
| 190 | + gpu_stats = self._stats.gpu_stats |
| 191 | + |
| 192 | + cpu_stats.cpu_samples_list[fname][lineno].append(now.wallclock) |
| 193 | + cpu_stats.cpu_samples_python[fname][lineno] += average_python_time |
| 194 | + cpu_stats.cpu_samples_c[fname][lineno] += average_c_time |
| 195 | + cpu_stats.cpu_samples[fname] += average_cpu_time |
| 196 | + cpu_stats.cpu_utilization[fname][lineno].push(cpu_utilization) |
| 197 | + cpu_stats.core_utilization[fname][lineno].push(core_utilization) |
| 198 | + |
| 199 | + gpu_stats.gpu_samples[fname][lineno] += gpu_load * elapsed.wallclock |
| 200 | + gpu_stats.n_gpu_samples[fname][lineno] += elapsed.wallclock |
| 201 | + gpu_stats.gpu_mem_samples[fname][lineno].push(gpu_mem_used) |
| 202 | + |
| 203 | + def _update_thread_stats( |
| 204 | + self, |
| 205 | + fname: Filename, |
| 206 | + lineno: LineNumber, |
| 207 | + orig_frame: FrameType, |
| 208 | + normalized_time: float, |
| 209 | + cpu_utilization: float, |
| 210 | + core_utilization: float, |
| 211 | + ) -> None: |
| 212 | + """Update statistics for non-main threads.""" |
| 213 | + cpu_stats = self._stats.cpu_stats |
| 214 | + |
| 215 | + # Check if the original caller is stuck inside a call |
| 216 | + if ScaleneFuncUtils.is_call_function( |
| 217 | + orig_frame.f_code, |
| 218 | + ByteCodeIndex(orig_frame.f_lasti), |
| 219 | + ): |
| 220 | + # Attribute time to native |
| 221 | + cpu_stats.cpu_samples_c[fname][lineno] += normalized_time |
| 222 | + else: |
| 223 | + # Attribute time to Python |
| 224 | + cpu_stats.cpu_samples_python[fname][lineno] += normalized_time |
| 225 | + |
| 226 | + cpu_stats.cpu_samples[fname] += normalized_time |
| 227 | + cpu_stats.cpu_utilization[fname][lineno].push(cpu_utilization) |
| 228 | + cpu_stats.core_utilization[fname][lineno].push(core_utilization) |
0 commit comments