簡體   English   中英

應用程序如何衡量CPU使用率(以%表示)?

[英]How do apps measure CPU usage (as a %)?

所以我正在嘗試編寫一個測量CPU使用率的應用程序(即CPU工作時間與不工作時間)。 我已經做了一些研究,但不幸的是,對於應該如何做,有很多不同的意見。

這些不同的解決方案包括但不限於: 在Android中獲取內存使用情況

http://juliano.info/en/Blog:Memory_Leak/Understanding_the_Linux_load_average

我自己嘗試過編寫一些代碼,但我可能會這樣做,因為上面的鏈接在核心關閉時沒有考慮(或者是嗎?)

    long[][] cpuUseVal = {{2147483647, 0} , {2147483647, 0} , {2147483647, 0} , 
        {2147483647, 0} , {2147483647, 0}};

    public float[] readCPUUsage(int coreNum) {
    int j=1;
    String[] entries;  //Array to hold entries in the /proc/stat file
    int cpu_work;
    float percents[] = new float[5];
    Calendar c = Calendar.getInstance();

    // Write the dataPackage
    long currentTime = c.getTime().getTime();

        for (int i = 0; i <= coreNum; i++){
    try {     
            //Point the app to the file where CPU values are located
            RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
            String load = reader.readLine();
            while (j <= i){
                load = reader.readLine();
                j++;
            }
            //Reset j for use later in the loop
            j=1; 

            entries = load.split("[ ]+");

            //Pull the CPU working time from the file
            cpu_work = Integer.parseInt(entries[1]) + Integer.parseInt(entries[2]) + Integer.parseInt(entries[3])
                      + Integer.parseInt(entries[6]) + Integer.parseInt(entries[6]) + Integer.parseInt(entries[7]);
            reader.close();

            percents[i] = (float)(cpu_work - cpuUseVal[i][1]) / (currentTime - cpuUseVal[i][0]);

            cpuUseVal[i][0] = currentTime;
            cpuUseVal[i][1] = cpu_work;

    //In case of an error, print a stack trace   
    } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
    //Return the array holding the usage values for the CPU, and all cores
    return percents;
}

所以這里是我寫的代碼的想法...我有一個全局數組,其中包含一些虛擬值,在第一次運行函數時應返回負百分比。 這些值存儲在一個數據庫中,所以我知道要忽略任何負面的東西。 無論如何,函數運行,獲取cpu執行某些操作的時間值,並將其與上次運行函數進行比較(在全局數組的幫助下)。 這些值除以函數運行之間經過的時間量(借助日歷)

我已經下載了一些現有的cpu使用情況監視器,並將它們與我從我的應用程序獲得的值進行了比較,而且我的內容從未接近他們所獲得的值。 誰能解釋我做錯了什么?

感謝一些幫助,我已將我的功能改為如下所示,希望這有助於其他有此問題的人

    // Function to read values from /proc/stat and do computations to compute CPU %
public float[] readCPUUsage(int coreNum) {
    int j = 1;
    String[] entries; 
    int cpu_total;
    int cpu_work;
    float percents[] = new float[5];

    for (int i = 0; i <= coreNum; i++) {
        try {
            // Point the app to the file where CPU values are located
            RandomAccessFile reader = new RandomAccessFile("/proc/stat","r");
            String load = reader.readLine();
            // Loop to read down to the line that corresponds to the core
            // whose values we are trying to read
            while (j <= i) {
                load = reader.readLine();
                j++;
            }
            // Reset j for use later in the loop
            j = 1;
            // Break the line into separate array elements. The end of each
            // element is determined by any number of spaces
            entries = load.split("[ ]+");

            // Pull the CPU total time on and "working time" from the file
            cpu_total = Integer.parseInt(entries[1])
                    + Integer.parseInt(entries[2])
                    + Integer.parseInt(entries[3])
                    + Integer.parseInt(entries[4])
                    + Integer.parseInt(entries[5])
                    + Integer.parseInt(entries[6])
                    + Integer.parseInt(entries[7]);
            cpu_work = Integer.parseInt(entries[1])
                    + Integer.parseInt(entries[2])
                    + Integer.parseInt(entries[3])
                    + Integer.parseInt(entries[6])
                    + Integer.parseInt(entries[7]);
            reader.close();

            //If it was off the whole time, say 0
            if ((cpu_total - cpuUseVal[i][0]) == 0)
                percents[i] = 0;
            //If it was on for any amount of time, compute the %
            else
                percents[i] = (float) (cpu_work - cpuUseVal[i][1])
                        / (cpu_total - cpuUseVal[i][0]);

            //Save the values measured for future comparison
            cpuUseVal[i][0] = cpu_total;
            cpuUseVal[i][1] = cpu_work;

            // In case of an error, print a stack trace
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    // Return the array holding the usage values for the CPU, and all cores
    return percents;
}

應用程序不測量CPU使用率,內核通過每秒中斷進程100次(或某些其他頻率,取決於內核的調整方式)並遞增計數器,該計數器與中斷時的操作相對應。

如果在進程中=>遞增用戶計數器。

如果在kernel =>中增加系統計數器

如果等待磁盤或網絡或設備=>增加等待IO

否則增加空閑計數器。

正常運行時間由運行隊列的衰減平均長度確定,即等待運行的線程數。 第一個數字是最后一分鍾的平均長度。 您可以通過JMX獲得平均負載。

暫無
暫無

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

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