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!