所以我正在尝试编写一个Java函数来为进程收集CPU时间,并将它们与先前的读数进行比较,以确定自上次采样以来进程所需的CPU时间。我发现如何从此站点获取进程的CPU时间http://codeseekah.com/2012/10/21/android-shell-tricks-ps/
基本上,你可以执行“ps -x”并添加你在最后看到的值;它们看起来像这样(你:15,s:854)。问题是我的价值似乎高于预期。我在这里http://en.wikipedia.org/wiki/CPU_time#Total_CPU_time理解这个页面的方式是,在给定的墙上时间间隔内的最大CPU时间是(核心数)*(墙上时间间隔)。我的测试设备有4个核心,我每3秒钟采样一次。我从当前值中减去前一个值,并且经常看到超过12秒的最终值。那可能吗?我是否误解了数据?我将在下面发布一些代码片段
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);
谢谢!
答案 0 :(得分:1)
值是CPU和系统时间,以时钟周期为单位。该定义可以在桌面系统的man 5 proc
文本中找到:
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).
每个线程都会跟踪这些值,因此您应该使用ps -t -x
来查看每个进程中的所有线程。没有-t
你只是查看主线程的统计数据,这可能就是你的数字没有加起来的原因。