top 和 ps 没有显示相同的 cpu 结果

The*_*dor 60 cpu top ps

这与这个问题有关。

当我运行时,top我得到以下结果:

在此处输入图片说明

pid3038使用 18% cpu,但是在运行时

在此处输入图片说明

结果是 5.5%。而且这个数字似乎没有随时间变化(即稍后运行相同的命令时)......

ps命令是否以某种方式平均 CPU 使用率?

Run*_*ium 101

man psNOTES部分。

   CPU usage is currently expressed as the percentage of time spent running
   during the entire lifetime of a process.  This is not ideal, and it does not
   conform to the standards that ps otherwise conforms to.  CPU usage is
   unlikely to add up to exactly 100%.
Run Code Online (Sandbox Code Playgroud)

而且,猜你知道,但你也可以这样做:

top -p <PID>
Run Code Online (Sandbox Code Playgroud)

编辑:至于您对其他答案的评论;

嗯,是的,我想知道如何从 ps 中获得(即时 CPU 百分比)

简短的回答:你不能。

为什么会这样?

这就像要求某人根据图片计算汽车的速度。

whiletop是一个监控工具,ps是一个快照工具。可以这样想:在任何给定时刻,进程要么使用 CPU,要么不使用 CPU。因此,您在那个确切时刻有 0% 或 100% 的负载。

给予:如果ps应该给予即时 CPU 使用率,它将是 0% 或 100%。

top 另一方面,保持轮询数字并随着时间的推移计算负载。

ps可以给出当前的使用情况——但这需要它多次读取数据并在每次读取之间休眠。它没有。

ps %cpu 的计算

ps 以下列方式计算 CPU 使用率:

正常运行时间 = 系统运行的总时间。
ps_time = 从启动开始以秒为单位测量的进程启动时间。
pu_time = 进程已使用 CPU 的总时间。

;; 秒进程一直在运行:
秒 = 正常运行时间 - ps_time
;; 用法:
cpu_usage = pu_time * 1000 / 秒

打印:cpu_usage / 10 "." cpu_usage % 10


例子: 正常运行时间 = 344,545 ps_time = 322,462 pu_time = 3,383 秒 = 344,545 - 322,462 = 22,083 cpu_usage = 3,383 * 1,000 / 22,083 = 153 打印:153 / 10“。” 153 % 10 => 15.3

所以打印的数字是:进程在其生命周期内使用 CPU 的时间。如上例所示。它在其生命周期的 15.3% 中做到了这一点。在 84.7% 的时间里,它没有在 CPU 上出现故障。

数据检索

ps,以及top,使用来自存储在/proc/进程信息伪文件系统下的文件的数据

您的根目录中有一些文件,/proc/其中包含有关系统整体状态的各种信息。此外,每个进程都有自己的子文件夹/proc/<PID>/,其中存储了进程特定的数据。因此,例如,您问题中的过程在/proc/3038/.

ps计算 CPU 使用率时,它使用两个文件:

/proc/uptime 系统的正常运行时间(秒),以及空闲进程花费的时间(秒)。
/proc/[PID]/stat 有关进程的状态信息。
  • uptime它使用第一个值(uptime)。
  • [PID]/stat它使用以下内容:
 # 名称 描述
14 utime CPU 花费在用户代码上的时间,以 jiffies 为单位
15 stime CPU 花费在内核代码上的时间,以 jiffies 为单位
16 个可爱的 CPU 时间花在用户代码上,包括孩子的时间
17 cstime CPU 花费在内核代码上的时间,包括孩子的时间 
22 starttime 进程开始的时间,以jiffies为单位

一个jiffie是时钟周期。因此,除此之外,它还使用各种方法sysconf(_SC_CLK_TCK)来获取系统的赫兹(每秒滴答数)——最终在用尽其他选项后使用 100 作为后备。

所以如果utime是 1234 并且赫兹是 100 那么:

seconds = utime / Hertz = 1234 / 100 = 12.34
Run Code Online (Sandbox Code Playgroud)

实际计算如下:

total_time = utime + stime

IF include_dead_children
    total_time = total_time + cutime + cstime
ENDIF

seconds = uptime - starttime / Hertz

pcpu = (total_time * 1000 / Hertz) / seconds

print: "%CPU" pcpu / 10 "." pcpu % 10
Run Code Online (Sandbox Code Playgroud)

示例(来自自定义 Bash 脚本的输出):

$ ./psw2 30894
System information
           uptime : 353,512 seconds
             idle : 0
Process information
              PID : 30894
         filename : plugin-containe
            utime : 421,951 jiffies 4,219 seconds
            stime : 63,334 jiffies 633 seconds
           cutime : 0 jiffies 0 seconds
           cstime : 1 jiffies 0 seconds
        starttime : 32,246,240 jiffies 322,462 seconds

Process run time  : 31,050
Process CPU time  : 485,286 jiffies 4,852 seconds
CPU usage since birth: 15.6%
Run Code Online (Sandbox Code Playgroud)

用 ps计算“当前”负载

这是一个(有点?)阴暗的努力,但还可以。让我们试一试。

可以使用提供的时间ps并从中计算 CPU 使用率。当考虑它时,它实际上可能相当有用,但有一些限制。

这对于计算较长时期内的 CPU 使用率可能很有用。也就是说,您想plugin-container在执行一些与 Firefox 相关的任务时监视Firefox 中的平均 CPU 负载。

通过使用以下输出:

$ ps -p -o cputime,etimes

CODE    HEADER   DESCRIPTION
cputime TIME     cumulative CPU time, "[DD-]hh:mm:ss" format.  (alias time).
etime   ELAPSED  elapsed time since the process was started, [DD-]hh:]mm:ss.
etimes  ELAPSED  elapsed time since the process was started, in seconds.
Run Code Online (Sandbox Code Playgroud)

我在这个示例中使用etime了 overetimes进行计算,只是为了更清楚一点。我还为“乐趣”添加了 %cpu。在 ie 一个 bash 脚本中,一个人显然会使用etimes- 或者更好地读取/proc/<PID>/等等。

Start:
$ ps -p 30894 -o %cpu,cputime,etime,etimes
%CPU     TIME     ELAPSED ELAPSED
 5.9 00:13:55    03:53:56   14036

End:
%CPU     TIME     ELAPSED ELAPSED
 6.2 00:14:45    03:56:07   14167

Calculate times:
            13 * 60 + 55 =    835   (cputime this far)
3 * 3,600 + 53 * 60 + 56 = 14,036   (time running this far)

            14 * 60 + 45 =    885   (cputime at end)
3 * 3,600 + 56 * 60 +  7 = 14,167   (time running at end)

Calculate percent load:
((885 - 835) / (14,167 - 14,036)) * 100 = 38
Run Code Online (Sandbox Code Playgroud)

在此期间,进程使用 CPU 的时间为 38%。

看代码

如果你想知道ps它是怎么做的,并且知道一点 C,那么做(看起来你运行 Gnome Debain deriavnt) - 代码中关于注释等的态度很好:

apt-get source procps
cd procps*/ps
vim HACKING
Run Code Online (Sandbox Code Playgroud)

  • 真的很棒的答案! (4认同)

Rah*_*til 5

man top

%CPU  --  CPU usage
The  task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.  In a true SMP environment, if 'Irix
mode' is Off, top will operate in 'Solaris mode' where a task's cpu usage will be divided by the total number of CPUs.  You toggle  'Irix/Solaris'  modes
with the 'I' interactive command.


man ps 
%cpu       %CPU    cpu utilization of the process in "##.#" format. Currently, it is the CPU time used divided by the time the process has been running                       (cputime/realtime ratio), expressed as a percentage. It will not add up to 100% unless you are lucky. (alias pcpu).
Run Code Online (Sandbox Code Playgroud)

  • 您可以从顶级示例中提取`top -p 3343 -n1 | awk '/ R /{print $10}'` (3认同)
  • 谢谢“top -p”很好,我从来没有注意到这个选项!但是我不喜欢搜索PID,如果在空闲内存部分偶然出现相同的数字怎么办。在我的系统上,信息位于第 8 行和第 9 列,因此我这样做:`top -p $PID -n1 | awk '{if (NR ==8) 打印 $9 }' ` (2认同)