| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import logging |
| 22 |
import os |
| 23 |
|
| 24 |
import psutil |
| 25 |
|
| 26 |
from s_tui.helper_functions import cat |
| 27 |
from s_tui.sources import intel_therm |
| 28 |
from s_tui.sources.source import Source |
| 29 |
|
| 30 |
SYSFS_THERMAL_THROTTLE = "/sys/devices/system/cpu/cpu{}/thermal_throttle" |
| 31 |
|
| 32 |
|
| 33 |
def _read_throttle_count(core_id: int, counter: str) -> int | None: |
| 34 |
"""Read a thermal_throttle counter from sysfs. Returns None if unavailable.""" |
| 35 |
path = os.path.join(SYSFS_THERMAL_THROTTLE.format(core_id), counter) |
| 36 |
raw = cat(path, fallback=None, binary=False) |
| 37 |
try: |
| 38 |
return int(raw) if raw is not None else None |
| 39 |
except ValueError: |
| 40 |
return None |
| 41 |
|
| 42 |
|
| 43 |
class FreqSource(Source): |
| 44 |
"""Source class implementing CPU frequency information polling""" |
| 45 |
|
| 46 |
def __init__(self): |
| 47 |
self.is_available = True |
| 48 |
if not hasattr(psutil, "cpu_freq"): |
| 49 |
self.is_available = False |
| 50 |
logging.debug("cpu_freq is not available from psutil") |
| 51 |
return |
| 52 |
|
| 53 |
Source.__init__(self) |
| 54 |
|
| 55 |
self.name = "Frequency" |
| 56 |
self.measurement_unit = "MHz" |
| 57 |
self.pallet = ( |
| 58 |
"freq light", |
| 59 |
"freq dark", |
| 60 |
"freq light smooth", |
| 61 |
"freq dark smooth", |
| 62 |
) |
| 63 |
self.alert_pallet = ( |
| 64 |
"freq throttle light", |
| 65 |
"freq throttle dark", |
| 66 |
"freq throttle light smooth", |
| 67 |
"freq throttle dark smooth", |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
try: |
| 72 |
per_cpu_freq = psutil.cpu_freq(True) |
| 73 |
except (OSError, NotImplementedError): |
| 74 |
per_cpu_freq = None |
| 75 |
|
| 76 |
try: |
| 77 |
overall_freq = psutil.cpu_freq(False) |
| 78 |
except (OSError, NotImplementedError): |
| 79 |
overall_freq = None |
| 80 |
|
| 81 |
total_cores = self._get_total_core_count() |
| 82 |
if per_cpu_freq and len(per_cpu_freq) > total_cores: |
| 83 |
total_cores = len(per_cpu_freq) |
| 84 |
|
| 85 |
self.top_freq = overall_freq.max if overall_freq else 0.0 |
| 86 |
self.max_freq = self.top_freq |
| 87 |
|
| 88 |
self.available_sensors = ["Avg"] |
| 89 |
for core_id in range(total_cores): |
| 90 |
self.available_sensors.append("Core " + str(core_id)) |
| 91 |
|
| 92 |
self.last_measurement = [0.0] * len(self.available_sensors) |
| 93 |
self.sensor_available = [True] * len(self.available_sensors) |
| 94 |
|
| 95 |
self.last_thresholds: list[float | None] = [None] * len(self.available_sensors) |
| 96 |
|
| 97 |
self._mark_offline_cores(total_cores, self._get_online_cpu_ids()) |
| 98 |
|
| 99 |
if self.top_freq == 0.0 and max(self.last_measurement) >= 0: |
| 100 |
self.max_freq = max(self.last_measurement) |
| 101 |
|
| 102 |
") |
| 103 |
self._num_cores = total_cores |
| 104 |
self._throttle_labels: list[str] = [""] * total_cores |
| 105 |
self._cached_suffixes: list[str] = [""] * len(self.available_sensors) |
| 106 |
self._cached_alerts: list[str | None] = [None] * len(self.available_sensors) |
| 107 |
self._use_msr = intel_therm.available() |
| 108 |
|
| 109 |
|
| 110 |
self._prev_core_throttle: list[int | None] = [None] * total_cores |
| 111 |
self._prev_pkg_throttle: int | None = None |
| 112 |
self._throttle_available = self._use_msr or self._init_sysfs(total_cores) |
| 113 |
|
| 114 |
def _init_sysfs(self, total_cores: int) -> bool: |
| 115 |
"""Initialize sysfs throttle counter baselines.""" |
| 116 |
any_available = False |
| 117 |
for core_id in range(total_cores): |
| 118 |
count = _read_throttle_count(core_id, "core_throttle_count") |
| 119 |
if count is not None: |
| 120 |
self._prev_core_throttle[core_id] = count |
| 121 |
any_available = True |
| 122 |
pkg_count = _read_throttle_count(0, "package_throttle_count") |
| 123 |
if pkg_count is not None: |
| 124 |
self._prev_pkg_throttle = pkg_count |
| 125 |
any_available = True |
| 126 |
return any_available |
| 127 |
|
| 128 |
def _update_throttle_state(self) -> None: |
| 129 |
"""Update per-core throttle labels, thresholds, and cached outputs.""" |
| 130 |
if not self._throttle_available: |
| 131 |
return |
| 132 |
|
| 133 |
if self._use_msr: |
| 134 |
self._update_throttle_msr() |
| 135 |
else: |
| 136 |
self._update_throttle_sysfs() |
| 137 |
|
| 138 |
|
| 139 |
any_throttled = any(self._throttle_labels) |
| 140 |
self.last_thresholds[0] = 0.0 if any_throttled else None |
| 141 |
for core_id in range(self._num_cores): |
| 142 |
self.last_thresholds[core_id + 1] = ( |
| 143 |
0.0 if self._throttle_labels[core_id] else None |
| 144 |
) |
| 145 |
|
| 146 |
|
| 147 |
suffixes = [""] * len(self.available_sensors) |
| 148 |
alerts: list[str | None] = [None] * len(self.available_sensors) |
| 149 |
for core_id in range(self._num_cores): |
| 150 |
idx = core_id + 1 |
| 151 |
if idx < len(suffixes) and self.sensor_available[idx]: |
| 152 |
label = self._throttle_labels[core_id] |
| 153 |
if label: |
| 154 |
suffixes[idx] = label |
| 155 |
alerts[idx] = "throttle txt" |
| 156 |
if any_throttled: |
| 157 |
suffixes[0] = next(lbl for lbl in self._throttle_labels if lbl) |
| 158 |
alerts[0] = "throttle txt" |
| 159 |
self._cached_suffixes = suffixes |
| 160 |
self._cached_alerts = alerts |
| 161 |
|
| 162 |
def _update_throttle_msr(self) -> None: |
| 163 |
"""Read IA32_THERM_STATUS per core for precise throttle reasons.""" |
| 164 |
for core_id in range(self._num_cores): |
| 165 |
try: |
| 166 |
status = intel_therm.read_therm_status(core_id) |
| 167 |
self._throttle_labels[core_id] = status.label |
| 168 |
except OSError: |
| 169 |
self._throttle_labels[core_id] = "" |
| 170 |
|
| 171 |
def _update_throttle_sysfs(self) -> None: |
| 172 |
"""Detect throttling via sysfs counter deltas.""" |
| 173 |
for core_id in range(self._num_cores): |
| 174 |
count = _read_throttle_count(core_id, "core_throttle_count") |
| 175 |
prev = self._prev_core_throttle[core_id] |
| 176 |
core_throttled = count is not None and prev is not None and count > prev |
| 177 |
if count is not None: |
| 178 |
self._prev_core_throttle[core_id] = count |
| 179 |
self._throttle_labels[core_id] = "Tc" if core_throttled else "" |
| 180 |
|
| 181 |
pkg_count = _read_throttle_count(0, "package_throttle_count") |
| 182 |
prev_pkg = self._prev_pkg_throttle |
| 183 |
pkg_throttled = ( |
| 184 |
pkg_count is not None and prev_pkg is not None and pkg_count > prev_pkg |
| 185 |
) |
| 186 |
if pkg_count is not None: |
| 187 |
self._prev_pkg_throttle = pkg_count |
| 188 |
|
| 189 |
|
| 190 |
if pkg_throttled: |
| 191 |
for core_id in range(self._num_cores): |
| 192 |
if not self._throttle_labels[core_id]: |
| 193 |
self._throttle_labels[core_id] = "Tp" |
| 194 |
|
| 195 |
def update(self) -> None: |
| 196 |
try: |
| 197 |
per_cpu_freq = psutil.cpu_freq(True) |
| 198 |
except (OSError, AttributeError, NotImplementedError) as e: |
| 199 |
logging.debug("cpu_freq() raised %s: %s", type(e).__name__, e) |
| 200 |
for i in range(1, len(self.sensor_available)): |
| 201 |
self.sensor_available[i] = False |
| 202 |
return |
| 203 |
|
| 204 |
if not per_cpu_freq: |
| 205 |
return |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
num_cores = len(self.available_sensors) - 1 |
| 210 |
online_freqs = [] |
| 211 |
|
| 212 |
for core_id in range(num_cores): |
| 213 |
if core_id < len(per_cpu_freq) and per_cpu_freq[core_id].current > 0: |
| 214 |
self.last_measurement[core_id + 1] = per_cpu_freq[core_id].current |
| 215 |
online_freqs.append(per_cpu_freq[core_id].current) |
| 216 |
self.sensor_available[core_id + 1] = True |
| 217 |
else: |
| 218 |
self.sensor_available[core_id + 1] = False |
| 219 |
|
| 220 |
self.last_measurement[0] = ( |
| 221 |
sum(online_freqs) / len(online_freqs) if online_freqs else 0.0 |
| 222 |
) |
| 223 |
|
| 224 |
self._update_throttle_state() |
| 225 |
|
| 226 |
def _format_measurement(self, value: float) -> str: |
| 227 |
return str(int(value)) |
| 228 |
|
| 229 |
def get_edge_triggered(self) -> bool: |
| 230 |
return False |
| 231 |
|
| 232 |
def get_sensor_suffixes(self) -> list[str]: |
| 233 |
return self._cached_suffixes |
| 234 |
|
| 235 |
def get_sensor_alerts(self) -> list[str | None]: |
| 236 |
return self._cached_alerts |
| 237 |
|
| 238 |
def get_maximum(self) -> float: |
| 239 |
return self.max_freq |
| 240 |
|
| 241 |
def get_top(self) -> float: |
| 242 |
logging.debug("Returning top %s", self.top_freq) |
| 243 |
return self.top_freq |