| 1 | """Read macOS guest metrics through the public Mach and sysctl interfaces.""" |
| 2 | import ctypes |
| 3 | import json |
| 4 | import os |
| 5 | |
| 6 | |
| 7 | lib = ctypes.CDLL("/usr/lib/libSystem.B.dylib", use_errno=True) |
| 8 | u32 = ctypes.c_uint32 |
| 9 | u64 = ctypes.c_uint64 |
| 10 | |
| 11 | |
| 12 | class VMStatistics(ctypes.Structure): |
| 13 | |
| 14 | _fields_ = [ |
| 15 | ("free", u32), ("active", u32), ("inactive", u32), ("wired", u32), |
| 16 | ("zero_fill", u64), ("reactivations", u64), ("pageins", u64), |
| 17 | ("pageouts", u64), ("faults", u64), ("cow_faults", u64), |
| 18 | ("lookups", u64), ("hits", u64), ("purges", u64), |
| 19 | ("purgeable", u32), ("speculative", u32), |
| 20 | ("decompressions", u64), ("compressions", u64), |
| 21 | ("swapins", u64), ("swapouts", u64), |
| 22 | ("compressor", u32), ("throttled", u32), ("external", u32), |
| 23 | ("internal", u32), ("uncompressed", u64), |
| 24 | ] |
| 25 | |
| 26 | |
| 27 | class BootTime(ctypes.Structure): |
| 28 | _fields_ = [("seconds", ctypes.c_long), ("microseconds", ctypes.c_int32)] |
| 29 | |
| 30 | |
| 31 | class SwapUsage(ctypes.Structure): |
| 32 | _fields_ = [("total", u64), ("available", u64), ("used", u64), |
| 33 | ("page_size", u32), ("encrypted", ctypes.c_int32)] |
| 34 | |
| 35 | |
| 36 | lib.sysctlbyname.argtypes = [ctypes.c_char_p, ctypes.c_void_p, |
| 37 | ctypes.POINTER(ctypes.c_size_t), ctypes.c_void_p, ctypes.c_size_t] |
| 38 | lib.sysctlbyname.restype = ctypes.c_int |
| 39 | lib.mach_host_self.argtypes = [] |
| 40 | lib.mach_host_self.restype = u32 |
| 41 | lib.host_statistics.argtypes = [u32, ctypes.c_int, ctypes.c_void_p, ctypes.POINTER(u32)] |
| 42 | lib.host_statistics.restype = ctypes.c_int |
| 43 | lib.host_statistics64.argtypes = lib.host_statistics.argtypes |
| 44 | lib.host_statistics64.restype = ctypes.c_int |
| 45 | |
| 46 | |
| 47 | def sysctl(name, kind): |
| 48 | value = kind() |
| 49 | size = ctypes.c_size_t(ctypes.sizeof(value)) |
| 50 | if lib.sysctlbyname(name.encode(), ctypes.byref(value), ctypes.byref(size), None, 0) != 0: |
| 51 | raise OSError(ctypes.get_errno(), "sysctl " + name) |
| 52 | if size.value != ctypes.sizeof(value): |
| 53 | raise ValueError("unexpected sysctl size for " + name) |
| 54 | return value |
| 55 | |
| 56 | |
| 57 | host = lib.mach_host_self() |
| 58 | vm = VMStatistics() |
| 59 | count = u32(ctypes.sizeof(vm) // ctypes.sizeof(u32)) |
| 60 | if lib.host_statistics64(host, 4, ctypes.byref(vm), ctypes.byref(count)) != 0 or count.value != 38: |
| 61 | raise OSError("host_statistics64 did not return complete VM counters") |
| 62 | ticks = (u32 * 4)() |
| 63 | count = u32(4) |
| 64 | if lib.host_statistics(host, 3, ctypes.byref(ticks), ctypes.byref(count)) != 0 or count.value != 4: |
| 65 | raise OSError("host_statistics did not return complete CPU counters") |
| 66 | boot = sysctl("kern.boottime", BootTime) |
| 67 | page_size = os.sysconf("SC_PAGE_SIZE") |
| 68 | |
| 69 | |
| 70 | result = { |
| 71 | "version": 1, |
| 72 | "total_bytes": sysctl("hw.memsize", u64).value, |
| 73 | "available_bytes": (vm.free + vm.inactive) * page_size, |
| 74 | "free_bytes": vm.free * page_size, |
| 75 | "inactive_bytes": vm.inactive * page_size, |
| 76 | "wired_bytes": vm.wired * page_size, |
| 77 | "compressed_bytes": vm.compressor * page_size, |
| 78 | "cpus": sysctl("hw.logicalcpu", u32).value, |
| 79 | "cpu_ticks": list(ticks), |
| 80 | "boot_seconds": boot.seconds, |
| 81 | } |
| 82 | try: |
| 83 | swap = sysctl("vm.swapusage", SwapUsage) |
| 84 | result.update(swap_total_bytes=swap.total, swap_free_bytes=swap.available) |
| 85 | except OSError: |
| 86 | pass |
| 87 | try: |
| 88 | result["pressure_level"] = sysctl("kern.memorystatus_vm_pressure_level", u32).value |
| 89 | except OSError: |
| 90 | pass |
| 91 | print(json.dumps(result, separators=(",", ":"))) |