【问题标题】:What is "CPU time" (using Android's ps -x)?什么是“CPU 时间”(使用 Android 的 ps -x)?
【发布时间】: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


    【解决方案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,您只是在查看主线程的统计信息,这可能就是您的数字不加起来的原因。

    【讨论】:

    • 这不是桌面系统重要吗?我在想它可能是毫秒,而不是时钟滴答声
    • 桌面与移动无关。我尝试了一个简单的测试,该程序旋转调用System.nanoTime(),直到两秒钟过去,然后进入睡眠状态。 ps -x -t 中的结果是 dalvikvm (u:3, s:204),这大致是您对 100 滴答/秒的预期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 2011-07-26
    • 1970-01-01
    相关资源
    最近更新 更多