The record is a structure of N+1 entries, where N is the number of CPU and 1 is the summary, so the each record will be tagged with “cpu0” … “cpuN-1” and the tag “cpu” represents the average of overall. Each entries will contain idle, user, system and nice to represent the cpu usage time. In a common sense, idle shall be as large as possible to indicate the CPU is not busy.
Memory Usage
Call static property SysInfo.Memory will return a dictionary [String: Int] with memory metrics in ** MB **:
print(SysInfo.Memory)
** Note ** Since system information is subject to operating system type, so please use directive #if os(Linux) #else #endif determine OS type before reading system metrics; The definition of each counter is out of the scope of this document, please see OS manual for detail.
Typical Linux memory looks like this ( 1G total memory with about 599MB available):
Call static property SysInfo.Net will return total traffic summary from all interfaces as a dictionary [String: [String: Int]] where the key represents the network interface name, and the value is a detailed dictionary with two key-value pairs - i stands for receiving and o for transmitting, both in KB:
if let net = SysInfo.Net {
print(net)
}
If success, it will print something like these mac / linux outputs:
// 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]
]
Disk IO
Call static method SysInfo.Disk may inspect disk i/o activity statistics in real time. It will return a [String:[String: UInt64]] dictionary with metrics as sample below. For more information of these counters, please refer to the operating system manual.
Please note that if using SysInfo.Disk in a loop, then an autoreleasepool{ } is strongly recommend to avoid unnecessary memory caching on such objects:
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
]
]
Further Information
For more information on the Perfect project, please visit perfect.org.
Perfect SysInfo 简体中文
This project provides a Swift library to monitor system performance.
This package builds with Swift Package Manager and is part of the Perfect project but can also be used as an independent module.
Ensure you have installed and activated the latest Swift 4.0 tool chain.
Quick Start
Add Perfect SysInfo library to your Package.swift:
Add library header to your source code:
Now SysInfo class is available to call.
CPU Usage
Call static variable
SysInfo.CPU
will return a dictionary[String: [String: Int]]
of all CPU usage, for example:The record is a structure of
N+1
entries, whereN
is the number of CPU and1
is the summary, so the each record will be tagged with “cpu0” … “cpuN-1” and the tag “cpu” represents the average of overall. Each entries will containidle
,user
,system
andnice
to represent the cpu usage time. In a common sense,idle
shall be as large as possible to indicate the CPU is not busy.Memory Usage
Call static property
SysInfo.Memory
will return a dictionary[String: Int]
with memory metrics in ** MB **:** Note ** Since system information is subject to operating system type, so please use directive
#if os(Linux) #else #endif
determine OS type before reading system metrics; The definition of each counter is out of the scope of this document, please see OS manual for detail.Typical Linux memory looks like this ( 1G total memory with about 599MB available):
And here is a typical mac OS X memory summary, which indicates that there is about 4.5GB free memory:
Network Traffic
Call static property
SysInfo.Net
will return total traffic summary from all interfaces as a dictionary[String: [String: Int]]
where the key represents the network interface name, and the value is a detailed dictionary with two key-value pairs -i
stands for receiving ando
for transmitting, both in KB:If success, it will print something like these mac / linux outputs:
Disk IO
Call static method
SysInfo.Disk
may inspect disk i/o activity statistics in real time. It will return a[String:[String: UInt64]]
dictionary with metrics as sample below. For more information of these counters, please refer to the operating system manual.Linux Release Notes
Mac OS X Release Notes
Please note that if using
SysInfo.Disk
in a loop, then anautoreleasepool{ }
is strongly recommend to avoid unnecessary memory caching on such objects:Further Information
For more information on the Perfect project, please visit perfect.org.
Now WeChat Subscription is Available (Chinese)