跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

macos_resources.py

91 行 · 3.5 KB历史下载
1"""Read macOS guest metrics through the public Mach and sysctl interfaces."""
2import ctypes
3import json
4import os
5
6
7lib = ctypes.CDLL("/usr/lib/libSystem.B.dylib", use_errno=True)
8u32 = ctypes.c_uint32
9u64 = ctypes.c_uint64
10
11
12class VMStatistics(ctypes.Structure):
13 # HOST_VM_INFO64_REV1_COUNT, available on every supported macOS guest.
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
27class BootTime(ctypes.Structure):
28 _fields_ = [("seconds", ctypes.c_long), ("microseconds", ctypes.c_int32)]
29
30
31class SwapUsage(ctypes.Structure):
32 _fields_ = [("total", u64), ("available", u64), ("used", u64),
33 ("page_size", u32), ("encrypted", ctypes.c_int32)]
34
35
36lib.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]
38lib.sysctlbyname.restype = ctypes.c_int
39lib.mach_host_self.argtypes = []
40lib.mach_host_self.restype = u32
41lib.host_statistics.argtypes = [u32, ctypes.c_int, ctypes.c_void_p, ctypes.POINTER(u32)]
42lib.host_statistics.restype = ctypes.c_int
43lib.host_statistics64.argtypes = lib.host_statistics.argtypes
44lib.host_statistics64.restype = ctypes.c_int
45
46
47def 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
57host = lib.mach_host_self()
58vm = VMStatistics()
59count = u32(ctypes.sizeof(vm) // ctypes.sizeof(u32))
60if 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")
62ticks = (u32 * 4)()
63count = u32(4)
64if 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")
66boot = sysctl("kern.boottime", BootTime)
67page_size = os.sysconf("SC_PAGE_SIZE")
68# Mach free_count already includes speculative_count. Inactive pages form an
69# availability estimate and are counted once, separately from compressed pages.
70result = {
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}
82try:
83 swap = sysctl("vm.swapusage", SwapUsage)
84 result.update(swap_total_bytes=swap.total, swap_free_bytes=swap.available)
85except OSError:
86 pass
87try:
88 result["pressure_level"] = sysctl("kern.memorystatus_vm_pressure_level", u32).value
89except OSError:
90 pass
91print(json.dumps(result, separators=(",", ":")))