"""Read-only GPU discovery; run manually with python3, outside the app sandbox. Enumerates names, not private counter semantics. A matching name is a research candidate, never proof of per-core utilization. Does not create subscriptions, submit GPU work, request root, or collect process/application information. """ import ctypes as C import json import plistlib import subprocess from collections import Counter def command(*args): return subprocess.check_output(args, timeout=60) def registry_devices(): roots = plistlib.loads(command("ioreg", "-a", "-r", "-c", "IOAccelerator", "-d", "0")) if isinstance(roots, dict): roots = [roots] keys = ("model", "gpu-core-count", "GPUConfigurationVariable", "PerformanceStatistics", "GPURawCounterBundleName", "GPURawCounterPluginClassName") return [{k: n[k] for k in keys if k in n} for n in roots] def ioreport_channels(): cf = C.CDLL("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation") io = C.CDLL("/usr/lib/libIOReport.dylib") io.IOReportCopyAllChannels.argtypes = [C.c_uint64, C.c_uint64] io.IOReportCopyAllChannels.restype = C.c_void_p cf.CFPropertyListCreateData.argtypes = [C.c_void_p, C.c_void_p, C.c_long, C.c_ulong, C.c_void_p] cf.CFPropertyListCreateData.restype = C.c_void_p cf.CFDataGetLength.argtypes = [C.c_void_p] cf.CFDataGetLength.restype = C.c_long cf.CFDataGetBytePtr.argtypes = [C.c_void_p] cf.CFDataGetBytePtr.restype = C.c_void_p cf.CFRelease.argtypes = [C.c_void_p] channels = io.IOReportCopyAllChannels(0, 0) if not channels: raise RuntimeError("IOReportCopyAllChannels returned nil") data = None try: data = cf.CFPropertyListCreateData(None, channels, 100, 0, None) if not data: raise RuntimeError("IOReport channel serialization failed") report = plistlib.loads(C.string_at(cf.CFDataGetBytePtr(data), cf.CFDataGetLength(data))) all_channels = report.get("IOReportChannels", []) selected = [] for channel in all_channels: driver = channel.get("DriverName", "").split(" 2 else "" if any(term in " ".join((driver, group, subgroup, name)).lower() for term in ("gpu", "agx", "gfx")): selected.append(dict(driver=driver, group=group, subgroup=subgroup, name=name)) counts = Counter((x["driver"], x["group"], x["subgroup"]) for x in selected) return dict(totalChannels=len(all_channels), gpuRelatedChannels=len(selected), groups=[dict(driver=d, group=g, subgroup=s, count=n) for (d, g, s), n in sorted(counts.items())], channels=selected) finally: if data: cf.CFRelease(data) cf.CFRelease(channels) def metal_counters(): source = '''import Foundation import Metal let devices: [[String: Any]] = MTLCopyAllDevices().map { device in ["name": device.name, "counterSets": (device.counterSets ?? []).map { set in ["name": set.name, "counters": set.counters.map { $0.name }] as [String: Any] }] } let data = try JSONSerialization.data(withJSONObject: devices, options: [.sortedKeys]) print(String(decoding: data, as: UTF8.self)) ''' return json.loads(command("xcrun", "swift", "-e", source)) if __name__ == "__main__": result = {"os": command("sw_vers").decode().strip(), "scope": "unsandboxed discovery only"} for key, probe in (("registry", registry_devices), ("ioreport", ioreport_channels), ("metal", metal_counters)): try: result[key] = probe() except (AttributeError, OSError, RuntimeError, ValueError, subprocess.SubprocessError) as error: result[key] = {"error": str(error)} print(json.dumps(result, ensure_ascii=False, indent=2))