facts.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. """Cached OS, architecture, CPU, memory and GPU facts for the current machine.
  2. Native architecture remains accurate when the Python process is emulated. Facts read the registry,
  3. sysctl, IOKit, procfs or sysfs: never the environment, a subprocess or a GPU driver library.
  4. """
  5. from __future__ import annotations
  6. import functools
  7. import os
  8. import platform
  9. import sys
  10. import sysconfig
  11. from typing import NamedTuple
  12. from hermes_platform.host import darwin_pci
  13. _ARCH_NAMES = {
  14. "amd64": "amd64", "x86_64": "amd64", "x64": "amd64",
  15. "arm64": "arm64", "aarch64": "arm64",
  16. "x86": "x86", "i386": "x86", "i686": "x86",
  17. }
  18. IMAGE_FILE_MACHINE = {0x8664: "amd64", 0xAA64: "arm64", 0x014C: "x86"}
  19. # The interpreter's own build: on Windows, platform.machine() names the native machine even
  20. # inside an x64 process emulated on arm64.
  21. _WINDOWS_BUILD_ARCH = {"win-amd64": "amd64", "win-arm64": "arm64", "win32": "x86"}
  22. def normalize_arch(raw: str | None) -> str:
  23. """Return a canonical architecture name."""
  24. return _ARCH_NAMES.get((raw or "").strip().lower(), "unknown")
  25. @functools.cache
  26. def os_family() -> str:
  27. """Return the current platform identifier."""
  28. return sys.platform
  29. @functools.cache
  30. def process_arch() -> str:
  31. """Return the architecture of this Python process."""
  32. if sys.platform == "win32":
  33. return _WINDOWS_BUILD_ARCH.get(sysconfig.get_platform(), "unknown")
  34. return normalize_arch(platform.machine())
  35. def windows_native_arch(*, wow64_native: int | None, machine: str) -> str:
  36. """Return the native architecture: ``IsWow64Process2``'s native machine, else ``platform.machine()``."""
  37. return IMAGE_FILE_MACHINE.get(wow64_native or 0) or normalize_arch(machine)
  38. def _wow64_native_machine() -> int | None:
  39. """Return the IsWow64Process2 native machine, or ``None`` when unavailable."""
  40. import ctypes
  41. from ctypes import wintypes
  42. kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
  43. probe = getattr(kernel32, "IsWow64Process2", None)
  44. if probe is None:
  45. return None
  46. # ctypes otherwise truncates the HANDLE pseudo-handle to c_int.
  47. kernel32.GetCurrentProcess.restype = wintypes.HANDLE
  48. kernel32.GetCurrentProcess.argtypes = []
  49. probe.argtypes = [wintypes.HANDLE, ctypes.POINTER(wintypes.USHORT), ctypes.POINTER(wintypes.USHORT)]
  50. probe.restype = wintypes.BOOL
  51. process_machine = wintypes.USHORT(0)
  52. native_machine = wintypes.USHORT(0)
  53. if not probe(kernel32.GetCurrentProcess(), ctypes.byref(process_machine), ctypes.byref(native_machine)):
  54. return None
  55. return native_machine.value or None
  56. def _darwin_translated() -> bool:
  57. """Return whether this process runs under Rosetta."""
  58. return _sysctl_int(b"sysctl.proc_translated") == 1
  59. @functools.cache
  60. def native_arch() -> str:
  61. """Return the machine architecture even when this process is emulated."""
  62. if sys.platform == "win32":
  63. try:
  64. wow64 = _wow64_native_machine()
  65. except (OSError, AttributeError, TypeError, ValueError):
  66. wow64 = None
  67. return windows_native_arch(wow64_native=wow64, machine=platform.machine())
  68. if sys.platform == "darwin" and process_arch() == "amd64" and _darwin_translated():
  69. return "arm64"
  70. return process_arch()
  71. _CPU_KEY = r"HARDWARE\DESCRIPTION\System\CentralProcessor\0"
  72. def parse_cpuinfo(text: str, *, device_tree_model: str = "") -> str:
  73. """Return the first available CPU or device-tree model."""
  74. for key in ("model name", "Hardware"):
  75. for line in text.splitlines():
  76. if line.lower().startswith(key.lower()) and ":" in line:
  77. return line.split(":", 1)[1].strip()
  78. return device_tree_model.replace("\0", "").replace("_", " ").strip()
  79. def _read_text(path: str, limit: int = 65536) -> str:
  80. try:
  81. with open(path, "r", encoding="utf-8-sig", errors="replace") as handle:
  82. return handle.read(limit)
  83. except OSError:
  84. return ""
  85. def _winreg_str(subkey: str, name: str) -> str:
  86. import winreg
  87. try:
  88. with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, subkey) as key:
  89. value = winreg.QueryValueEx(key, name)[0]
  90. except OSError:
  91. return ""
  92. return str(value).strip()
  93. def _sysctl_int(name: bytes) -> int | None:
  94. import ctypes
  95. import ctypes.util
  96. libc = ctypes.CDLL(ctypes.util.find_library("c"))
  97. value = ctypes.c_int(0)
  98. size = ctypes.c_size_t(ctypes.sizeof(ctypes.c_int))
  99. if libc.sysctlbyname(name, ctypes.byref(value), ctypes.byref(size), None, 0) != 0:
  100. return None
  101. return value.value
  102. def _sysctl_u64(name: bytes) -> int | None:
  103. import ctypes
  104. import ctypes.util
  105. libc = ctypes.CDLL(ctypes.util.find_library("c"))
  106. value = ctypes.c_uint64(0)
  107. size = ctypes.c_size_t(ctypes.sizeof(ctypes.c_uint64))
  108. if libc.sysctlbyname(name, ctypes.byref(value), ctypes.byref(size), None, 0) != 0:
  109. return None
  110. return value.value
  111. def _sysctl_str(name: bytes) -> str:
  112. import ctypes
  113. import ctypes.util
  114. libc = ctypes.CDLL(ctypes.util.find_library("c"))
  115. size = ctypes.c_size_t(0)
  116. if libc.sysctlbyname(name, None, ctypes.byref(size), None, 0) != 0:
  117. return ""
  118. buffer = ctypes.create_string_buffer(size.value)
  119. if libc.sysctlbyname(name, buffer, ctypes.byref(size), None, 0) != 0:
  120. return ""
  121. return buffer.value.decode(errors="replace").strip()
  122. @functools.cache
  123. def cpu_model() -> str:
  124. """Return the CPU model, or an empty string when unavailable."""
  125. if sys.platform == "win32":
  126. return _winreg_str(_CPU_KEY, "ProcessorNameString")
  127. if sys.platform == "darwin":
  128. return _sysctl_str(b"machdep.cpu.brand_string")
  129. return parse_cpuinfo(_read_text("/proc/cpuinfo"), device_tree_model=_read_text("/proc/device-tree/model", 512))
  130. # ARM's MIDR implementer codes, as arm64 Linux prints them in ``CPU implementer``.
  131. _ARM_IMPLEMENTERS = {
  132. 0x41: "ARM", 0x42: "Broadcom", 0x43: "Cavium", 0x46: "Fujitsu", 0x48: "HiSilicon", 0x4E: "NVIDIA",
  133. 0x50: "Ampere", 0x51: "Qualcomm", 0x61: "Apple", 0x69: "Intel", 0x6D: "Microsoft", 0xC0: "Ampere",
  134. }
  135. def parse_cpuinfo_vendor(text: str) -> str:
  136. """Return ``vendor_id`` (x86) or the named ``CPU implementer`` (arm64) from ``/proc/cpuinfo``."""
  137. pairs = (line.split(":", 1) for line in text.splitlines() if ":" in line)
  138. fields = {key.strip(): value.strip() for key, value in pairs}
  139. if fields.get("vendor_id"):
  140. return fields["vendor_id"]
  141. try:
  142. return _ARM_IMPLEMENTERS.get(int(fields.get("CPU implementer", ""), 16), "")
  143. except ValueError:
  144. return ""
  145. @functools.cache
  146. def cpu_vendor() -> str:
  147. """Return the CPU vendor, or an empty string when unavailable.
  148. Windows reports the SoC vendor (``NVIDIA`` on an N1X); arm64 Linux reports the core designer.
  149. """
  150. if sys.platform == "win32":
  151. return _winreg_str(_CPU_KEY, "VendorIdentifier")
  152. if sys.platform == "darwin":
  153. return "Apple" if native_arch() == "arm64" else _sysctl_str(b"machdep.cpu.vendor")
  154. return parse_cpuinfo_vendor(_read_text("/proc/cpuinfo"))
  155. def parse_meminfo_total(text: str) -> int | None:
  156. """Return ``MemTotal`` from ``/proc/meminfo`` text, in bytes."""
  157. for line in text.splitlines():
  158. if line.startswith("MemTotal:"):
  159. fields = line.split()
  160. if len(fields) >= 2 and fields[1].isdigit():
  161. return int(fields[1]) * 1024
  162. return None
  163. def _windows_ram_total() -> int | None:
  164. import ctypes
  165. from ctypes import wintypes
  166. class MEMORYSTATUSEX(ctypes.Structure):
  167. _fields_ = [
  168. ("dwLength", wintypes.DWORD), ("dwMemoryLoad", wintypes.DWORD),
  169. ("ullTotalPhys", ctypes.c_uint64), ("ullAvailPhys", ctypes.c_uint64),
  170. ("ullTotalPageFile", ctypes.c_uint64), ("ullAvailPageFile", ctypes.c_uint64),
  171. ("ullTotalVirtual", ctypes.c_uint64), ("ullAvailVirtual", ctypes.c_uint64),
  172. ("ullAvailExtendedVirtual", ctypes.c_uint64),
  173. ]
  174. status = MEMORYSTATUSEX()
  175. status.dwLength = ctypes.sizeof(MEMORYSTATUSEX)
  176. if not ctypes.windll.kernel32.GlobalMemoryStatusEx(ctypes.byref(status)):
  177. return None
  178. return int(status.ullTotalPhys) or None
  179. @functools.cache
  180. def ram_total_bytes() -> int | None:
  181. """Return the physical memory visible to this OS, or ``None`` when unavailable."""
  182. try:
  183. if sys.platform == "win32":
  184. return _windows_ram_total()
  185. if sys.platform == "darwin":
  186. return _sysctl_u64(b"hw.memsize") or None
  187. except (AttributeError, OSError, TypeError, ValueError):
  188. return None
  189. return parse_meminfo_total(_read_text("/proc/meminfo", 4096))
  190. # PCI vendor ids; a discrete vendor wins over an integrated one on hybrid machines.
  191. _GPU_PCI_VENDORS = {"10de": "nvidia", "1002": "amd", "8086": "intel"}
  192. _GPU_PRIORITY = ("nvidia", "amd", "intel")
  193. _DISPLAY_CLASS_KEY = r"SYSTEM\CurrentControlSet\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}"
  194. _GPU_SCAN_LIMIT = 16
  195. def classify_gpu_vendors(vendor_ids: list[str]) -> str:
  196. """Return the highest-priority GPU class for PCI vendor ids, or ``none``.
  197. ``unknown`` when devices were seen but none of their vendor ids could be read.
  198. """
  199. if vendor_ids and not any(v.strip() for v in vendor_ids):
  200. return "unknown"
  201. found = {_GPU_PCI_VENDORS.get(v.strip().lower().removeprefix("0x")) for v in vendor_ids}
  202. return next((name for name in _GPU_PRIORITY if name in found), "none")
  203. def _linux_gpu_vendor_ids() -> list[str] | None:
  204. """PCI vendor ids of DRM cards; ``None`` when sysfs exposes no DRM tree at all."""
  205. root = "/sys/class/drm"
  206. try:
  207. entries = sorted(os.listdir(root))[:_GPU_SCAN_LIMIT * 4]
  208. except OSError:
  209. return None
  210. cards = [e for e in entries if e.startswith("card") and "-" not in e][:_GPU_SCAN_LIMIT]
  211. return [_read_text(f"{root}/{card}/device/vendor", 16).strip() for card in cards]
  212. def _hex_after(text: str, tag: str) -> str:
  213. return text.split(tag, 1)[1][:4] if tag in text else ""
  214. def parse_windows_display_adapters(rows: list[tuple[str, str, str]]) -> list[tuple[str, str, str]]:
  215. """``(vendor id, device id, description)`` per adapter from ``(subkey, MatchingDeviceId, DriverDesc)`` rows.
  216. Virtual adapters (no ``VEN_`` id, such as the remote-display driver) are skipped; an
  217. adapter subkey without a readable id keeps empty ids so it counts as unreadable.
  218. """
  219. adapters = []
  220. for subkey, matching_id, description in rows:
  221. upper = matching_id.upper()
  222. if "VEN_" in upper:
  223. adapters.append((_hex_after(upper, "VEN_"), _hex_after(upper, "DEV_"), description))
  224. elif subkey.isdigit() and not matching_id:
  225. adapters.append(("", "", description))
  226. return adapters
  227. @functools.cache
  228. def _windows_display_adapters() -> tuple[tuple[str, str, str], ...] | None:
  229. import winreg
  230. rows = []
  231. try:
  232. with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, _DISPLAY_CLASS_KEY) as root:
  233. for index in range(_GPU_SCAN_LIMIT):
  234. try:
  235. subkey = winreg.EnumKey(root, index)
  236. except OSError:
  237. break
  238. path = f"{_DISPLAY_CLASS_KEY}\\{subkey}"
  239. rows.append((subkey, _winreg_str(path, "MatchingDeviceId"), _winreg_str(path, "DriverDesc")))
  240. except OSError:
  241. return None
  242. return tuple(parse_windows_display_adapters(rows))
  243. # The WSL GPU bridge mounts the Windows NVIDIA driver's CUDA library here only when that driver is installed.
  244. _WSL_NVIDIA_CUDA = "/usr/lib/wsl/lib/libcuda.so.1"
  245. @functools.cache
  246. def gpu_class() -> str:
  247. """Return ``nvidia``/``amd``/``intel``/``apple_silicon``/``none``, or ``unknown``.
  248. Apple silicon by architecture; Intel Macs by IOKit PCI display controllers; Windows by the
  249. display-class registry; Linux by the NVIDIA driver's procfs, the WSL GPU bridge, then sysfs DRM cards.
  250. """
  251. try:
  252. if sys.platform == "darwin":
  253. if native_arch() == "arm64":
  254. return "apple_silicon"
  255. vendors = darwin_pci.display_vendor_ids()
  256. elif sys.platform == "win32":
  257. adapters = _windows_display_adapters()
  258. vendors = None if adapters is None else [vendor for vendor, _, _ in adapters]
  259. elif os.path.exists("/proc/driver/nvidia/version") or os.path.exists(_WSL_NVIDIA_CUDA):
  260. return "nvidia"
  261. else:
  262. vendors = _linux_gpu_vendor_ids()
  263. except (AttributeError, OSError, TypeError, ValueError):
  264. return "unknown"
  265. return "unknown" if vendors is None else classify_gpu_vendors(vendors)
  266. _NVIDIA_PROC_GPUS = "/proc/driver/nvidia/gpus"
  267. _NVIDIA_PCI_VENDOR = 0x10DE
  268. _SYS_PCI_DEVICES = "/sys/bus/pci/devices"
  269. def parse_nvidia_gpu_model(information: str) -> str:
  270. """Return the ``Model:`` value of a ``/proc/driver/nvidia/gpus/<bus>/information`` file."""
  271. for line in information.splitlines():
  272. if line.startswith("Model:"):
  273. return line.split(":", 1)[1].strip()
  274. return ""
  275. class NvidiaGpu(NamedTuple):
  276. name: str
  277. pci_id: int | None # NVML packing: device id above the 16-bit vendor id
  278. def _pci_id(device_hex: str) -> int | None:
  279. try:
  280. return int(device_hex, 16) << 16 | _NVIDIA_PCI_VENDOR
  281. except ValueError:
  282. return None
  283. def nvidia_gpus_from_adapters(adapters: list[tuple[str, str, str]]) -> tuple[NvidiaGpu, ...]:
  284. """The NVIDIA entries of parsed Windows display adapters."""
  285. return tuple(NvidiaGpu(description, _pci_id(device)) for vendor, device, description in adapters
  286. if vendor.lower() == "10de")
  287. def _linux_nvidia_gpus() -> tuple[NvidiaGpu, ...]:
  288. try:
  289. buses = sorted(os.listdir(_NVIDIA_PROC_GPUS))[:_GPU_SCAN_LIMIT]
  290. except OSError:
  291. return ()
  292. gpus = (NvidiaGpu(parse_nvidia_gpu_model(_read_text(f"{_NVIDIA_PROC_GPUS}/{bus}/information", 4096)),
  293. _pci_id(_read_text(f"{_SYS_PCI_DEVICES}/{bus}/device", 16).strip().removeprefix("0x")))
  294. for bus in buses)
  295. return tuple(gpu for gpu in gpus if gpu.name)
  296. @functools.cache
  297. def nvidia_gpus() -> tuple[NvidiaGpu, ...]:
  298. """Name and PCI id of each NVIDIA GPU: the Windows display-class registry, or Linux driver procfs
  299. with sysfs PCI ids; empty elsewhere or without the driver."""
  300. if sys.platform == "win32":
  301. try:
  302. return nvidia_gpus_from_adapters(list(_windows_display_adapters() or ()))
  303. except (AttributeError, OSError, TypeError, ValueError):
  304. return ()
  305. return _linux_nvidia_gpus() if sys.platform.startswith("linux") else ()
  306. def _windows_interactive_session() -> bool:
  307. import ctypes
  308. from ctypes import wintypes
  309. kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
  310. process_id = kernel32.GetCurrentProcessId()
  311. session_id = wintypes.DWORD()
  312. if not kernel32.ProcessIdToSessionId(process_id, ctypes.byref(session_id)) or session_id.value == 0:
  313. return False
  314. active_session = kernel32.WTSGetActiveConsoleSessionId()
  315. if active_session != 0xFFFFFFFF and active_session == session_id.value:
  316. return True
  317. return bool(kernel32.GetProcessWindowStation())
  318. def interactive_session() -> bool:
  319. """Return whether this process can reach an interactive user session."""
  320. if sys.platform == "win32":
  321. try:
  322. return _windows_interactive_session()
  323. except (AttributeError, OSError, TypeError, ValueError):
  324. return False
  325. if sys.platform.startswith("linux"):
  326. session_id = _read_text("/proc/self/sessionid", 64).strip()
  327. return bool(session_id and session_id != "4294967295" and os.path.isdir(f"/run/user/{os.getuid()}")) # windows-footgun: ok — Linux branch
  328. return True
  329. def clear_caches() -> None:
  330. """Clear every cached host fact."""
  331. for fact in (os_family, process_arch, native_arch, cpu_model, cpu_vendor, ram_total_bytes, gpu_class,
  332. nvidia_gpus, _windows_display_adapters):
  333. fact.cache_clear()