我试图获得整体CPU使用率而不是单个应用程序。我找到了一些资源,但它们要么是用C语言编写的,要么是过时的swift。任何人都可以帮我解决这个问题吗?我试图转换这个https://github.com/beltex/SystemKit/blob/master/SystemKit/System.swift#L12 迅速。
直到现在我能够转换这么多
fileprivate func hostCPULoadInfo() -> host_cpu_load_info{
let HOST_CPU_LOAD_INFO_COUNT = MemoryLayout<host_cpu_load_info>.stride / MemoryLayout<integer_t>.stride;
var size = mach_msg_type_number_t(HOST_CPU_LOAD_INFO_COUNT);
var hostInfo = host_cpu_load_info_t.allocate(capacity: 1);
let result = withUnsafeMutablePointer(to: &hostInfo) {$0.withMemoryRebound(to: integer_t.self, capacity: Int(size)){
host_info(mach_host_self(), Int32(HOST_BASIC_INFO), $0, &size)
}
}
let data = hostInfo.move()
hostInfo.deallocate(capacity: 1)
#if DEBUG
if result != KERN_SUCCESS{
print("Error - \(#file): \(#function) - kern_result_t = \(result)");
}
#endif
return data;
}
public func cpuUsage() -> (system: Double, user: Double, idle : Double, nice: Double){
let load = hostCPULoadInfo();
let usrDiff: Double = Double(load.cpu_ticks.0 - loadPrevious.cpu_ticks.0);
let systDiff = Double(load.cpu_ticks.1 - loadPrevious.cpu_ticks.1);
let idleDiff = Double(load.cpu_ticks.2 - loadPrevious.cpu_ticks.2);
let niceDiff = Double(load.cpu_ticks.3 - loadPrevious.cpu_ticks.3);
let totalTicks = usrDiff + systDiff + idleDiff + niceDiff
print("Total ticks is ", totalTicks);
let sys = systDiff / totalTicks * 100.0
let usr = usrDiff / totalTicks * 100.0
let idle = idleDiff / totalTicks * 100.0
let nice = niceDiff / totalTicks * 100.0
return (sys, usr, idle, nice);
}
但问题是我收到了这样的错误
Error - /Users/administrator/Downloads/Documents/Swift/SystemInfo/RAMInformation.swift: hostCPULoadInfo() - kern_result_t = 5
有人知道上面的代码有什么问题吗?我在转换host_statistics时做错了。
有人能帮助我吗?
答案 0 :(得分:4)
您的代码中有三个错误:
host_statistics()
获取CPU统计信息,而不是
host_info()
。HOST_CPU_LOAD_INFO
,而不是HOST_BASIC_INFO
。hostInfo
包含指向已分配结构的指针,
所以必须反弹价值,而不是变量的地址。全部放在一起:
func hostCPULoadInfo() -> host_cpu_load_info? {
let HOST_CPU_LOAD_INFO_COUNT = MemoryLayout<host_cpu_load_info>.stride / MemoryLayout<integer_t>.stride
var size = mach_msg_type_number_t(HOST_CPU_LOAD_INFO_COUNT)
let hostInfo = host_cpu_load_info_t.allocate(capacity: 1)
let result = hostInfo.withMemoryRebound(to: integer_t.self, capacity: HOST_CPU_LOAD_INFO_COUNT) {
host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, $0, &size)
}
if result != KERN_SUCCESS{
print("Error - \(#file): \(#function) - kern_result_t = \(result)")
return nil
}
let data = hostInfo.move()
hostInfo.deallocate(capacity: 1)
return data
}
(我将返回类型更改为可选,以便可以返回nil
在错误的情况下)。
或者,使用局部变量而不是分配和释放
host_cpu_load_info
结构:
func hostCPULoadInfo() -> host_cpu_load_info? {
let HOST_CPU_LOAD_INFO_COUNT = MemoryLayout<host_cpu_load_info>.stride/MemoryLayout<integer_t>.stride
var size = mach_msg_type_number_t(HOST_CPU_LOAD_INFO_COUNT)
var cpuLoadInfo = host_cpu_load_info()
let result = withUnsafeMutablePointer(to: &cpuLoadInfo) {
$0.withMemoryRebound(to: integer_t.self, capacity: HOST_CPU_LOAD_INFO_COUNT) {
host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, $0, &size)
}
}
if result != KERN_SUCCESS{
print("Error - \(#file): \(#function) - kern_result_t = \(result)")
return nil
}
return cpuLoadInfo
}