Skip to content

Instantly share code, notes, and snippets.

@kkebo
Last active May 15, 2021 17:40
Show Gist options
  • Save kkebo/a9ba33fe7b1625e50678806a773ff77b to your computer and use it in GitHub Desktop.
Save kkebo/a9ba33fe7b1625e50678806a773ff77b to your computer and use it in GitHub Desktop.
vmstat of iPadOS
import Darwin
func getVMStatistics64() -> vm_statistics64? {
let hostPort = mach_host_self()
var hostSize = mach_msg_type_number_t(MemoryLayout<vm_statistics64_data_t>.size / MemoryLayout<integer_t>.size)
var vmstat = vm_statistics64()
let result = withUnsafeMutablePointer(to: &vmstat) {
$0.withMemoryRebound(to: integer_t.self, capacity: Int(hostSize)) {
host_statistics64(
hostPort,
HOST_VM_INFO64,
$0,
&hostSize
) == KERN_SUCCESS
}
}
return result ? vmstat : nil
}
func getPageSize() -> UInt64 {
var pageSize: vm_size_t = 0
host_page_size(mach_host_self(), &pageSize)
return UInt64(pageSize)
}
let vmstat = getVMStatistics64()!
let pageSize = getPageSize()
let pageIns = Double(vmstat.pageins * pageSize) / Double(1 << 30)
let pageOuts = Double(vmstat.pageouts * pageSize) / Double(1 << 30)
let swapIns = Double(vmstat.swapins * pageSize) / Double(1 << 30)
let swapOuts = Double(vmstat.swapouts * pageSize) / Double(1 << 30)
let faults = Double(vmstat.faults * pageSize) / Double(1 << 40)
print("page ins: \(pageIns) GiB")
print("page outs: \(pageOuts) GiB")
print("swap ins: \(swapIns) GiB")
print("swap outs: \(swapOuts) GiB")
print("faults: \(faults) TiB")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment