Debian Codegraph @

s_tui/sources/intel_therm.py

raw symbols SCIP
1 """Intel IA32_THERM_STATUS (0x19C) throttle reason decoder.
2
3 Reads per-core thermal status MSR and reports which throttle reasons
4 are active. Requires root + msr kernel module.
5
6 Reference: Intel SDM Vol 4, Table 2-39.
7 """
8
9 from __future__ import annotations
10
11 from typing import NamedTuple
12
13 from s_tui.sources.msr import msr_available, read_msr
14
15 IA32_THERM_STATUS = 0x19C
16
17 # Status bits (real-time, even-numbered) — set only while actively throttling
18 THERMAL_STATUS = 1 << 0
19 PROCHOT_STATUS = 1 << 2
20 CRITICAL_STATUS = 1 << 4
21 POWER_LIMIT_STATUS = 1 << 10
22 CURRENT_LIMIT_STATUS = 1 << 12
23 CROSS_DOMAIN_STATUS = 1 << 14
24
25 _REASON_BITS = (
26 (THERMAL_STATUS, "T"),
27 (PROCHOT_STATUS, "H"),
28 (CRITICAL_STATUS, "C"),
29 (POWER_LIMIT_STATUS, "W"),
30 (CURRENT_LIMIT_STATUS, "A"),
31 (CROSS_DOMAIN_STATUS, "X"),
32 )
33
34
35 class ThrottleStatus(NamedTuple):
36 thermal: bool
37 prochot: bool
38 critical: bool
39 power_limit: bool
40 current_limit: bool
41 cross_domain: bool
42
43 @property
44 def any_active(self) -> bool:
45 return any(self)
46
47 @property
48 def label(self) -> str:
49 """Slash-separated label string, e.g. 'T/W' or empty."""
50 parts = [lbl for (_, lbl), active in zip(_REASON_BITS, self) if active]
51 return "/".join(parts)
52
53
54 def read_therm_status(cpu: int) -> ThrottleStatus:
55 """Read IA32_THERM_STATUS for a CPU and decode throttle reasons."""
56 val = read_msr(cpu, IA32_THERM_STATUS)
57 return ThrottleStatus(
58 thermal=bool(val & THERMAL_STATUS),
59 prochot=bool(val & PROCHOT_STATUS),
60 critical=bool(val & CRITICAL_STATUS),
61 power_limit=bool(val & POWER_LIMIT_STATUS),
62 current_limit=bool(val & CURRENT_LIMIT_STATUS),
63 cross_domain=bool(val & CROSS_DOMAIN_STATUS),
64 )
65
66
67 def available() -> bool:
68 """Check if Intel MSR throttle detection is usable."""
69 if not msr_available():
70 return False
71 try:
72 read_msr(0, IA32_THERM_STATUS)
73 return True
74 except (OSError, ValueError):
75 return False