I am trying to get overall CPU usage not of a single app. I found some of the resources but they are either written in C or outdated swift. Can anyone help me with this problem? I am trying to conver this https://github.com/beltex/SystemKit/blob/master/SystemKit/System.swift#L12 to swift.
Till now I am able to convert this much
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);
    }
But the thing is I am getting an error like this
Error  - /Users/administrator/Downloads/Documents/Swift/SystemInfo/RAMInformation.swift: hostCPULoadInfo() - kern_result_t = 5
Does anybody knows what's wrong in the above code? I thing I am doing wrong on conversion of host_statistics.
Can anybody help me?
There are three errors in your code:
host_statistics(), not 
host_info().HOST_CPU_LOAD_INFO, not HOST_BASIC_INFO.hostInfo contains the pointer to the allocated structure, 
so that value must be rebound, not the address of the variable.Putting it all together:
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
}
(I changed the return type to an optional so that nil can be returned
in the error case).
Alternatively, use a local variable instead of allocating and releasing
the host_cpu_load_info structure:
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
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With