什么是" CPU时间" (使用Android的PS -x)? [英] What is "CPU time" (using Android's ps -x)?
问题描述
所以我想写收集CPU时间的进程Java函数,并将它们与之前的读数来确定CPU时间的进程已经要求自上次取样。我发现了如何从本网站的 HTTP://$c$cseekah.com/2012/10/21/android-shell-tricks-ps/
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/
基本上,你可以执行PS -x,并添加你在最后看到的值;它们看起来像这样(U:15,S:854)。的问题是,我似乎比预期的要获得更高的值。我理解这里这个页面的方式 http://en.wikipedia.org/wiki/CPU_time#Total_CPU_time < /一>是,在给定的壁的时间间隔最大CPU时间(核数)*(壁时间间隔)。我的测试设备具有4个核心,而我每3秒采样。我从电流值减去previous,而且经常看到高于12秒最终值。那可能吗?我是misinter preting的数据?我会后下面
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);
感谢您!
推荐答案
的值是CPU和时钟滴答系统时间。该定义可以在从桌面系统中的 5人PROC
文本中找到:
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).
该值每线程跟踪,所以你应该使用 PS -t -x
来查看每个进程的所有线程。如果没有 -t
你只是在看统计信息为主线,这可能是为什么你的号码对不上号。
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.
这篇关于什么是&QUOT; CPU时间&QUOT; (使用Android的PS -x)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!