What is “CPU time” (using Android's ps -x)?

孤人 提交于 2019-12-12 06:33:55

问题


So I'm trying to write a Java function that gathers CPU time for processes, and compares them to prior readings to determine the CPU time a process has demanded since the last sample was taken. I found out how to get the CPU time of processes from this site http://codeseekah.com/2012/10/21/android-shell-tricks-ps/

Basically, you can execute "ps -x" and add the values you see at the end; they look like this (u:15, s:854). The problem is that I seem to be getting higher values than expected. The way I understand this page here http://en.wikipedia.org/wiki/CPU_time#Total_CPU_time, is that the maximum CPU time in a given wall time interval is (number of cores)*(wall time interval). My testing device has 4 cores, and I am sampling every 3 seconds. I subtract previous from current values, and often see final values that are above 12 seconds. Is that possible? Am I misinterpreting the data? I'll post some code snippets below

if (currentNameAndTime.get(i).processName.equals(oldNameAndTime.get(j).processName)) {
    // If they match, subtract the CPU times, and store the
    // result in the time field
    obj.time = (currentNameAndTime.get(i).time - oldNameAndTime.get(j).time);
    // Add the object to the array that will be returned
    finalNameAndTime.add(obj);
    // Break the chain, as after a match is found, all other
    // name comparisons will fail
    break;
}
if (oldNameAndTime.size() == 0) {
        FgBgCPUInfo obj = new FgBgCPUInfo();
        obj.processName = "FIRST RUN";
        obj.time = 0;
        finalNameAndTime.add(obj);
    }
oldNameAndTime = new ArrayList<FgBgCPUInfo>(currentNameAndTime);

Thank you!


回答1:


The values are CPU and system time in clock ticks. The definition can be found in the man 5 proc text from a desktop system:

          utime %lu   Amount of time that this process has been  scheduled
                      in  user  mode,  measured  in clock ticks (divide by
                      sysconf(_SC_CLK_TCK).   This  includes  guest  time,
                      guest_time  (time  spent  running a virtual CPU, see
                      below), so that applications that are not  aware  of
                      the  guest  time  field  do  not lose that time from
                      their calculations.

          stime %lu   Amount of time that this process has been  scheduled
                      in  kernel  mode, measured in clock ticks (divide by
                      sysconf(_SC_CLK_TCK).

The values are tracked per-thread, so you should be using ps -t -x to see all threads in each process. Without -t you're just looking at the stats for the main thread, which may be why your numbers don't add up.



来源:https://stackoverflow.com/questions/21766996/what-is-cpu-time-using-androids-ps-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!