| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
"""This module reads intel power measurements""" |
| 19 |
|
| 20 |
from __future__ import annotations |
| 21 |
|
| 22 |
import glob |
| 23 |
import logging |
| 24 |
import os |
| 25 |
import re |
| 26 |
from collections import namedtuple |
| 27 |
from multiprocessing import cpu_count |
| 28 |
|
| 29 |
from s_tui.helper_functions import cat |
| 30 |
from s_tui.sources.msr import msr_available, read_msr |
| 31 |
|
| 32 |
INTER_RAPL_DIR = "/sys/class/powercap/intel-rapl/" |
| 33 |
AMD_ENERGY_DIR_GLOB = "/sys/devices/platform/amd_energy.0/hwmon/hwmon*/" |
| 34 |
MICRO_JOULE_IN_JOULE = 1000000.0 |
| 35 |
|
| 36 |
|
| 37 |
UNIT_MSR = 0xC0010299 |
| 38 |
CORE_MSR = 0xC001029A |
| 39 |
PACKAGE_MSR = 0xC001029B |
| 40 |
ENERGY_UNIT_MASK = 0x1F00 |
| 41 |
|
| 42 |
|
| 43 |
RaplStats = namedtuple("rapl", ["label", "current", "max"]) |
| 44 |
|
| 45 |
|
| 46 |
class RaplReader: |
| 47 |
def __init__(self) -> None: |
| 48 |
basenames = glob.glob("/sys/class/powercap/intel-rapl:*/") |
| 49 |
self.basenames = sorted(set(basenames)) |
| 50 |
|
| 51 |
def read_power(self) -> list[RaplStats]: |
| 52 |
"""Read power stats and return dictionary""" |
| 53 |
|
| 54 |
pjoin = os.path.join |
| 55 |
ret = [] |
| 56 |
for path in self.basenames: |
| 57 |
name = None |
| 58 |
try: |
| 59 |
name = cat(pjoin(path, "name"), fallback=None, binary=False) |
| 60 |
except (OSError, ValueError) as err: |
| 61 |
logging.warning("ignoring %r for file %r", (err, path), RuntimeWarning) |
| 62 |
continue |
| 63 |
if name: |
| 64 |
try: |
| 65 |
current = cat(pjoin(path, "energy_uj")) |
| 66 |
max_reading = 0.0 |
| 67 |
ret.append(RaplStats(name, float(current), max_reading)) |
| 68 |
except (OSError, ValueError) as err: |
| 69 |
logging.warning( |
| 70 |
"ignoring %r for file %r", (err, path), RuntimeWarning |
| 71 |
) |
| 72 |
return ret |
| 73 |
|
| 74 |
@staticmethod |
| 75 |
def available() -> bool: |
| 76 |
return os.path.exists("/sys/class/powercap/intel-rapl") |
| 77 |
|
| 78 |
|
| 79 |
class AMDEnergyReader: |
| 80 |
def __init__(self) -> None: |
| 81 |
self.inputs = list( |
| 82 |
zip( |
| 83 |
( |
| 84 |
cat(filename, binary=False) |
| 85 |
for filename in sorted( |
| 86 |
glob.glob(AMD_ENERGY_DIR_GLOB + "energy*_label") |
| 87 |
) |
| 88 |
), |
| 89 |
sorted(glob.glob(AMD_ENERGY_DIR_GLOB + "energy*_input")), |
| 90 |
) |
| 91 |
) |
| 92 |
|
| 93 |
|
| 94 |
socket_number = sum(1 for label, _ in self.inputs if "socket" in label) |
| 95 |
self.inputs.sort(key=lambda x: self.get_input_position(x[0], socket_number)) |
| 96 |
|
| 97 |
@staticmethod |
| 98 |
def match_label(label: str) -> re.Match[str] | None: |
| 99 |
return re.search(r"E(core|socket)([0-9]+)", label) |
| 100 |
|
| 101 |
@staticmethod |
| 102 |
def get_input_position(label: str, socket_number: int) -> int: |
| 103 |
m = AMDEnergyReader.match_label(label) |
| 104 |
assert m is not None, f"Unexpected label format: {label}" |
| 105 |
num = int(m.group(2)) |
| 106 |
if "socket" in label: |
| 107 |
return num |
| 108 |
else: |
| 109 |
return num + socket_number |
| 110 |
|
| 111 |
def read_power(self) -> list[RaplStats]: |
| 112 |
ret = [] |
| 113 |
for label, inp in self.inputs: |
| 114 |
value = cat(inp) |
| 115 |
ret.append(RaplStats(label, float(value), 0.0)) |
| 116 |
return ret |
| 117 |
|
| 118 |
@staticmethod |
| 119 |
def available() -> bool: |
| 120 |
return os.path.exists("/sys/devices/platform/amd_energy.0") |
| 121 |
|
| 122 |
|
| 123 |
class AMDRaplMsrReader: |
| 124 |
def __init__(self) -> None: |
| 125 |
self.core_cpus: dict[int, int] = {} |
| 126 |
self.package_cpus: dict[int, int] = {} |
| 127 |
for i in range(cpu_count()): |
| 128 |
curr_core_id = int( |
| 129 |
cat( |
| 130 |
f"/sys/devices/system/cpu/cpu{i}/topology/core_id", |
| 131 |
binary=False, |
| 132 |
) |
| 133 |
) |
| 134 |
if curr_core_id not in self.core_cpus: |
| 135 |
self.core_cpus[curr_core_id] = i |
| 136 |
|
| 137 |
curr_package_id = int( |
| 138 |
cat( |
| 139 |
f"/sys/devices/system/cpu/cpu{i}/topology/physical_package_id", |
| 140 |
binary=False, |
| 141 |
) |
| 142 |
) |
| 143 |
if curr_package_id not in self.package_cpus: |
| 144 |
self.package_cpus[curr_package_id] = i |
| 145 |
|
| 146 |
def read_power(self) -> list[RaplStats]: |
| 147 |
ret = [] |
| 148 |
first_cpu = next(iter(self.package_cpus.values())) |
| 149 |
unit_msr = read_msr(first_cpu, UNIT_MSR) |
| 150 |
energy_factor = 0.5 ** ((unit_msr & ENERGY_UNIT_MASK) >> 8) |
| 151 |
|
| 152 |
for i, cpu in self.package_cpus.items(): |
| 153 |
package_msr = read_msr(cpu, PACKAGE_MSR) |
| 154 |
ret.append( |
| 155 |
RaplStats( |
| 156 |
"Package " + str(i + 1), |
| 157 |
package_msr * energy_factor * MICRO_JOULE_IN_JOULE, |
| 158 |
0.0, |
| 159 |
) |
| 160 |
) |
| 161 |
|
| 162 |
for i, cpu in self.core_cpus.items(): |
| 163 |
core_msr = read_msr(cpu, CORE_MSR) |
| 164 |
ret.append( |
| 165 |
RaplStats( |
| 166 |
"Core " + str(i + 1), |
| 167 |
core_msr * energy_factor * MICRO_JOULE_IN_JOULE, |
| 168 |
0.0, |
| 169 |
) |
| 170 |
) |
| 171 |
|
| 172 |
return ret |
| 173 |
|
| 174 |
@staticmethod |
| 175 |
def available() -> bool: |
| 176 |
try: |
| 177 |
cpuinfo = cat("/proc/cpuinfo", binary=False) |
| 178 |
|
| 179 |
m = re.search(r"vendor_id[\s]+: ([A-Za-z]+)", cpuinfo) |
| 180 |
|
| 181 |
if not m or m is None: |
| 182 |
return False |
| 183 |
|
| 184 |
if m.group(1) != "AuthenticAMD": |
| 185 |
return False |
| 186 |
|
| 187 |
m = re.search(r"cpu family[\s]+: ([0-9]+)", cpuinfo) |
| 188 |
if not m: |
| 189 |
return False |
| 190 |
if int(m[1]) != 0x17: |
| 191 |
return False |
| 192 |
except (FileNotFoundError, PermissionError): |
| 193 |
return False |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
return msr_available() |
| 211 |
|
| 212 |
|
| 213 |
def get_power_reader() -> RaplReader | AMDEnergyReader | AMDRaplMsrReader | None: |
| 214 |
for ReaderType in (RaplReader, AMDEnergyReader, AMDRaplMsrReader): |
| 215 |
if ReaderType.available(): |
| 216 |
return ReaderType() |
| 217 |
return None |