本项目提供一个 Swift 库来监控系统性能。
此软件包使用 Swift Package Manager 构建,并且是 Perfect 项目的一部分,但也可以作为一个独立的模块使用。
请确保您已安装并激活最新的 Swift 4.0 工具链。
将 Perfect SysInfo 库添加到您的 Package.swift 文件中
.package(url: "https://github.com/PerfectlySoft/Perfect-SysInfo.git", from: "3.0.0")
/// target section
.target(
// name: "your app's name",
dependencies: ["PerfectSysInfo"]),
将库头文件添加到您的源代码中
import PerfectSysInfo
现在 SysInfo 类可以被调用了。
调用静态变量 SysInfo.CPU
将返回一个字典 [String: [String: Int]]
,其中包含所有 CPU 的使用率,例如
print(SysInfo.CPU)
//here is a typical return of single CPU (from Linux):
[
"cpu0":
["nice": 1201, "system": 3598, "user": 8432, "idle": 8657606],
"cpu":
["nice": 1201, "system": 3598, "user": 8432, "idle": 8657606]
]
// and the following is another example with 8 cores (from Mac):
[
"cpu3":
["user": 18095, "idle": 9708265, "nice": 0, "system": 16177],
"cpu5":
["user": 18032, "idle": 9708329, "nice": 0, "system": 16079],
"cpu7":
["user": 18186, "idle": 9707892, "nice": 0, "system": 16285],
"cpu":
["user": 344301, "idle": 9201762, "nice": 0, "system": 196763],
"cpu0":
["user": 730263, "idle": 8387000, "nice": 0, "system": 626684],
"cpu2":
["user": 648287, "idle": 8799969, "nice": 0, "system": 294749],
"cpu1":
["user": 17708, "idle": 9708996, "nice": 0, "system": 15950],
"cpu4":
["user": 647701, "idle": 8800643, "nice": 0, "system": 294544],
"cpu6": ["user": 656136, "idle": 8793002, "nice": 0, "system": 293640]
]
该记录是一个包含 N+1
个条目的结构,其中 N
是 CPU 的数量,1
是摘要,因此每个记录都将标记为 "cpu0" ... "cpuN-1",标签 "cpu" 代表总体平均值。每个条目将包含 idle
、user
、system
和 nice
,以表示 CPU 使用时间。通常来说,idle
值应该尽可能大,以表明 CPU 不繁忙。
调用静态属性 SysInfo.Memory
将返回一个字典 [String: Int]
,其中包含以 ** MB ** 为单位的内存指标
print(SysInfo.Memory)
** 注意 ** 由于系统信息受操作系统类型的影响,因此在读取系统指标之前,请使用指令 #if os(Linux) #else #endif
来确定操作系统类型;每个计数器的定义超出了本文档的范围,请参阅操作系统手册了解详情。
典型的 Linux 内存信息如下所示 (总内存 1G,可用内存约 599MB)
[
"Inactive": 283, "MemTotal": 992, "CmaFree": 0,
"VmallocTotal": 33554431, "CmaTotal": 0, "Mapped": 74,
"SUnreclaim": 14, "Writeback": 0, "Active(anon)": 98,
"Shmem": 26, "PageTables": 7, "VmallocUsed": 0,
"MemFree": 98, "Inactive(file)": 179, "SwapCached": 0,
"HugePages_Total": 0, "Inactive(anon)": 104, "HugePages_Rsvd": 0,
"Buffers": 21, "SReclaimable": 39, "Cached": 613,
"Mlocked": 3, "SwapTotal": 1021, "NFS_Unstable": 0,
"CommitLimit": 1518, "Hugepagesize": 2, "SwapFree": 1016,
"WritebackTmp": 0, "Committed_AS": 1410, "AnonHugePages": 130,
"DirectMap2M": 966, "Unevictable": 3, "HugePages_Surp": 0,
"Dirty": 3, "HugePages_Free": 0, "MemAvailable": 599,
"Active(file)": 426, "Slab": 54, "Active": 525,
"KernelStack": 2, "VmallocChunk": 0, "AnonPages": 177,
"Bounce": 0, "HardwareCorrupted": 0, "DirectMap4k": 57
]
这是典型的 macOS X 内存摘要,表明有大约 4.5GB 的可用内存
[
"hits": 0, "faults": 3154324, "cow": 31476,
"wired": 3576, "reactivations": 366, "zero_filled": 2296248,
"pageins": 13983, "lookups": 1021, "pageouts": 0,
"active": 6967, "free": 4455, "inactive": 1008
]
调用静态属性 SysInfo.Net
将返回来自所有接口的总流量摘要,形式为一个字典 [String: [String: Int]]
,其中键代表网络接口名称,值是一个详细的字典,包含两个键值对 - i
代表接收,o
代表发送,单位均为 KB
if let net = SysInfo.Net {
print(net)
}
如果成功,它将打印类似于这些 mac / linux 输出的内容
// typical mac os x network summary, where the only physical network
// adapter "en0" has 1MB incoming data totally.
[
"p2p0": ["o": 0, "i": 0],
"stf0": ["o": 0, "i": 0],
"vboxnet0": ["o": 0, "i": 1],
"gif0": ["o": 0, "i": 0],
"lo0": ["o": 0, "i": 887],
"bridge0": ["o": 0, "i": 0],
"utun0": ["o": 0, "i": 0],
"awdl0": ["o": 0, "i": 318],
"en1": ["o": 0, "i": 0],
"en0": ["o": 0, "i": 1063],
"en2": ["o": 0, "i": 0]
]
// typical linux network summary, where the only physical network
// adapter "enp0s3" has received 0.6MB data and sent out 506KB in the same time.
[
"virbr0": ["o": 0, "i": 0],
"enp0s8": ["o": 506, "i": 614],
"virbr0-nic": ["o": 0, "i": 0],
"lo": ["o": 1804, "i": 1804],
"enp0s3": ["o": 158, "i": 7594]
]
调用静态方法 SysInfo.Disk
可以实时检查磁盘 i/o 活动统计信息。它将返回一个 [String:[String: UInt64]]
字典,其中包含如下示例的指标。有关这些计数器的更多信息,请参阅操作系统手册。
print(SysInfo.Disk)
// here is a sample output from Linux:
[
"sda":
[
"io_ms": 7516, "reads_merged": 17, "reads_completed": 14993,
"writing_ms": 9772, "io_in_progress": 0, "writes_completed": 4921,
"sectors_read": 1292762, "reading_ms": 11952, "writes_merged": 5738,
"sectors_written": 969480, "weighte_io_ms": 21636
],
"sda2":
[
"io_ms": 0, "reads_merged": 0, "reads_completed": 4,
"writing_ms": 0, "io_in_progress": 0, "writes_completed": 0,
"sectors_read": 8, "reading_ms": 0, "writes_merged": 0,
"sectors_written": 0, "weighte_io_ms": 0
],
"sda1":
[
"io_ms": 7252, "reads_merged": 7, "reads_completed": 14817,
"writing_ms": 9520, "io_in_progress": 0, "writes_completed": 3971,
"sectors_read": 1281954, "reading_ms": 11908, "writes_merged": 5426,
"sectors_written": 966696, "weighte_io_ms": 21340
]
]
请注意,如果在循环中使用 SysInfo.Disk
,则强烈建议使用 autoreleasepool{ }
以避免在此类对象上进行不必要的内存缓存
autoreleasepool(invoking: {
let io = SysInfo.Disk
print(io)
})
// here is the sample output from macOS:
[
"disk0":
[
"operations_read": 501077, "latency_time_read": 0,
"bytes_written": 21265645056, "bytes_read": 25022815232,
"operations_written": 360598, "latency_time_written": 0
]
]
有关 Perfect 项目的更多信息,请访问 perfect.org。