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

时间:2014-02-03 18:57:12

标签: java android cpu-usage

所以我正在尝试编写一个测量CPU使用率的应用程序(即CPU工作时间与不工作时间)。我已经做了一些研究,但不幸的是,对于应该如何完成有很多不同的意见。

这些不同的解决方案包括但不限于: Get Memory Usage in 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;
}

1 个答案:

答案 0 :(得分:2)

应用程序不测量CPU使用率,内核通过每秒中断进程100次(或某些其他频率,具体取决于内核的调整方式)并递增计数器,该计数器与中断时的操作相对应。

如果在此过程中=&gt;增加用户计数器。

如果在kernel =&gt;增加系统计数器

如果等待磁盘或网络或设备=&gt;增加等待IO

否则增加空闲计数器。

正常运行时间由运行队列的衰减平均长度确定,即等待运行的线程数。第一个数字是最后一分钟的平均长度。您可以通过JMX获得平均负载。