|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import platform |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +try: |
| 8 | + from .cpuopt_amd import annotate as annotate_amd |
| 9 | + from .cpuopt_arm import annotate as annotate_arm |
| 10 | + from .cpuopt_thermal import discover_hwmon, discover_thermal |
| 11 | + from .cpuopt_utils import first_existing, list_dirs, normalize_whitespace, read_int, read_text |
| 12 | +except ImportError: |
| 13 | + from cpuopt_amd import annotate as annotate_amd |
| 14 | + from cpuopt_arm import annotate as annotate_arm |
| 15 | + from cpuopt_thermal import discover_hwmon, discover_thermal |
| 16 | + from cpuopt_utils import first_existing, list_dirs, normalize_whitespace, read_int, read_text |
| 17 | + |
| 18 | + |
| 19 | +def _parse_cpuinfo(cpuinfo_path: Path) -> dict[str, str]: |
| 20 | + parsed: dict[str, str] = {} |
| 21 | + text = read_text(cpuinfo_path) |
| 22 | + if not text: |
| 23 | + return parsed |
| 24 | + for line in text.splitlines(): |
| 25 | + if ":" not in line: |
| 26 | + continue |
| 27 | + key, value = line.split(":", 1) |
| 28 | + key = key.strip() |
| 29 | + if key not in parsed: |
| 30 | + parsed[key] = value.strip() |
| 31 | + return parsed |
| 32 | + |
| 33 | + |
| 34 | +def _detect_cpuinfo(sysfs_root: Path) -> dict[str, str]: |
| 35 | + return _parse_cpuinfo( |
| 36 | + first_existing( |
| 37 | + [ |
| 38 | + sysfs_root / "proc" / "cpuinfo", |
| 39 | + sysfs_root.parent / "proc" / "cpuinfo", |
| 40 | + Path("/proc/cpuinfo"), |
| 41 | + ] |
| 42 | + ) |
| 43 | + or Path("/proc/cpuinfo") |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def _detect_kernel_release(sysfs_root: Path) -> str: |
| 48 | + value = read_text( |
| 49 | + first_existing( |
| 50 | + [ |
| 51 | + sysfs_root / "proc" / "sys" / "kernel" / "osrelease", |
| 52 | + sysfs_root.parent / "proc" / "sys" / "kernel" / "osrelease", |
| 53 | + Path("/proc/sys/kernel/osrelease"), |
| 54 | + ] |
| 55 | + ) |
| 56 | + or Path("/proc/sys/kernel/osrelease") |
| 57 | + ) |
| 58 | + return value or platform.release() |
| 59 | + |
| 60 | + |
| 61 | +def _policy_data(policy: Path) -> dict[str, Any]: |
| 62 | + available_governors = (read_text(policy / "scaling_available_governors") or "").split() |
| 63 | + available_freqs = (read_text(policy / "scaling_available_frequencies") or "").split() |
| 64 | + available_epp = (read_text(policy / "energy_performance_available_preferences") or "").split() |
| 65 | + return { |
| 66 | + "name": policy.name, |
| 67 | + "path": str(policy.resolve()), |
| 68 | + "related_cpus": read_text(policy / "related_cpus"), |
| 69 | + "scaling_driver": read_text(policy / "scaling_driver"), |
| 70 | + "scaling_governor": read_text(policy / "scaling_governor"), |
| 71 | + "available_governors": available_governors, |
| 72 | + "available_frequencies": available_freqs, |
| 73 | + "cpuinfo_min_freq": read_int(policy / "cpuinfo_min_freq"), |
| 74 | + "cpuinfo_max_freq": read_int(policy / "cpuinfo_max_freq"), |
| 75 | + "scaling_min_freq": read_int(policy / "scaling_min_freq"), |
| 76 | + "scaling_max_freq": read_int(policy / "scaling_max_freq"), |
| 77 | + "scaling_cur_freq": read_int(policy / "scaling_cur_freq"), |
| 78 | + "energy_performance_preference": read_text(policy / "energy_performance_preference"), |
| 79 | + "energy_performance_available_preferences": available_epp, |
| 80 | + "energy_perf_bias": read_text(policy / "energy_perf_bias"), |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | +def _cpuidle_data(sysfs_root: Path) -> list[dict[str, Any]]: |
| 85 | + cpu_root = sysfs_root / "devices" / "system" / "cpu" |
| 86 | + states: list[dict[str, Any]] = [] |
| 87 | + for cpu in list_dirs(cpu_root, "cpu[0-9]*"): |
| 88 | + for state in list_dirs(cpu / "cpuidle", "state*"): |
| 89 | + states.append( |
| 90 | + { |
| 91 | + "cpu": cpu.name, |
| 92 | + "state": state.name, |
| 93 | + "name": read_text(state / "name"), |
| 94 | + "desc": read_text(state / "desc"), |
| 95 | + "latency": read_int(state / "latency"), |
| 96 | + "disable": read_text(state / "disable"), |
| 97 | + } |
| 98 | + ) |
| 99 | + return states |
| 100 | + |
| 101 | + |
| 102 | +def _topology_data(sysfs_root: Path) -> list[dict[str, Any]]: |
| 103 | + cpu_root = sysfs_root / "devices" / "system" / "cpu" |
| 104 | + items: list[dict[str, Any]] = [] |
| 105 | + for cpu in list_dirs(cpu_root, "cpu[0-9]*"): |
| 106 | + topo = cpu / "topology" |
| 107 | + items.append( |
| 108 | + { |
| 109 | + "cpu": cpu.name, |
| 110 | + "core_id": read_text(topo / "core_id"), |
| 111 | + "physical_package_id": read_text(topo / "physical_package_id"), |
| 112 | + "thread_siblings_list": read_text(topo / "thread_siblings_list"), |
| 113 | + } |
| 114 | + ) |
| 115 | + return items |
| 116 | + |
| 117 | + |
| 118 | +def _numa_data(sysfs_root: Path) -> list[dict[str, Any]]: |
| 119 | + node_root = sysfs_root / "devices" / "system" / "node" |
| 120 | + items: list[dict[str, Any]] = [] |
| 121 | + for node in list_dirs(node_root, "node*"): |
| 122 | + items.append( |
| 123 | + { |
| 124 | + "name": node.name, |
| 125 | + "cpulist": read_text(node / "cpulist"), |
| 126 | + "distance": read_text(node / "distance"), |
| 127 | + } |
| 128 | + ) |
| 129 | + return items |
| 130 | + |
| 131 | + |
| 132 | +def discover(sysfs_root: str = "/sys") -> dict[str, Any]: |
| 133 | + root = Path(sysfs_root) |
| 134 | + cpuinfo = _detect_cpuinfo(root) |
| 135 | + cpu_root = root / "devices" / "system" / "cpu" |
| 136 | + cpufreq_root = cpu_root / "cpufreq" |
| 137 | + intel_pstate_root = cpu_root / "intel_pstate" |
| 138 | + amd_pstate_root = cpu_root / "amd_pstate" |
| 139 | + |
| 140 | + policies = [_policy_data(policy) for policy in list_dirs(cpufreq_root, "policy*")] |
| 141 | + thermal = discover_thermal(root) |
| 142 | + hwmon = discover_hwmon(root) |
| 143 | + |
| 144 | + vulnerabilities: dict[str, str] = {} |
| 145 | + vuln_root = cpu_root / "vulnerabilities" |
| 146 | + if vuln_root.exists(): |
| 147 | + for candidate in sorted(vuln_root.iterdir()): |
| 148 | + if candidate.is_file(): |
| 149 | + value = read_text(candidate) |
| 150 | + if value is not None: |
| 151 | + vulnerabilities[candidate.name] = value |
| 152 | + |
| 153 | + data: dict[str, Any] = { |
| 154 | + "sysfs_root": str(root), |
| 155 | + "vendor": cpuinfo.get("vendor_id") or cpuinfo.get("CPU implementer") or platform.machine(), |
| 156 | + "arch": platform.machine(), |
| 157 | + "model_name": normalize_whitespace(cpuinfo.get("model name") or cpuinfo.get("Processor")), |
| 158 | + "family": cpuinfo.get("cpu family"), |
| 159 | + "model": cpuinfo.get("model"), |
| 160 | + "stepping": cpuinfo.get("stepping"), |
| 161 | + "kernel": _detect_kernel_release(root), |
| 162 | + "smt_control": read_text(cpu_root / "smt" / "control"), |
| 163 | + "intel_pstate": { |
| 164 | + "exists": intel_pstate_root.exists(), |
| 165 | + "status": read_text(intel_pstate_root / "status"), |
| 166 | + "no_turbo": read_text(intel_pstate_root / "no_turbo"), |
| 167 | + }, |
| 168 | + "amd_pstate": { |
| 169 | + "exists": amd_pstate_root.exists(), |
| 170 | + "status": read_text(amd_pstate_root / "status"), |
| 171 | + }, |
| 172 | + "cpufreq_boost": read_text(cpufreq_root / "boost"), |
| 173 | + "policies": policies, |
| 174 | + "cpuidle_states": _cpuidle_data(root), |
| 175 | + "thermal": thermal, |
| 176 | + "hwmon": hwmon, |
| 177 | + "topology": _topology_data(root), |
| 178 | + "numa": _numa_data(root), |
| 179 | + "vulnerabilities": vulnerabilities, |
| 180 | + "warnings": ["Fan control is platform-specific; not directly controlled unless safely exposed."], |
| 181 | + "vendor_notes": [], |
| 182 | + } |
| 183 | + |
| 184 | + vendor = str(data["vendor"]) |
| 185 | + if "AuthenticAMD" in vendor: |
| 186 | + data = annotate_amd(data) |
| 187 | + if "ARM" in vendor or "aarch64" in vendor or "arm" in str(data["arch"]).lower(): |
| 188 | + data = annotate_arm(data) |
| 189 | + |
| 190 | + return data |
0 commit comments