【发布时间】:2014-02-13 22:33:13
【问题描述】:
所以我正在尝试编写一个 Java 函数来收集进程的 CPU 时间,并将它们与之前的读数进行比较,以确定自上次采样以来进程所需的 CPU 时间。我发现了如何从这个站点http://codeseekah.com/2012/10/21/android-shell-tricks-ps/获取进程的 CPU 时间
基本上,你可以执行“ps -x”并添加你在最后看到的值;它们看起来像这样 (u:15, s:854)。问题是我似乎得到了比预期更高的价值。我在这里理解这个页面http://en.wikipedia.org/wiki/CPU_time#Total_CPU_time 的方式是,给定的墙时间间隔内的最大 CPU 时间是(核心数)*(墙时间间隔)。我的测试设备有 4 个核心,我每 3 秒采样一次。我从当前值中减去以前的值,并且经常看到超过 12 秒的最终值。那可能吗?我是否误解了数据?我会在下面贴一些代码sn-ps
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);
谢谢!
【问题讨论】:
标签: android bash cpu-usage cpu-time