簡體   English   中英

獲取CPU使用率IOS Swift

[英]Get CPU usage IOS Swift

我正在嘗試獲取單個應用程序的總體CPU使用率。 我找到了一些資源,但是它們要么是用C編寫的,要么是過時的。 誰能幫助我解決這個問題? 我正在嘗試將這個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的轉換做錯了。

有誰能夠幫助我?

您的代碼中存在三個錯誤:

  • 通過調用host_statistics()而不是host_info()獲得CPU統計信息。
  • “ flavor”參數必須是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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM