*批处理模式下的CPU使用率

时间:2021-04-04 21:43:11

I have a problem that makes me mad. I am running top in batch mode with the following command,

我有一个让我发疯的问题。我使用以下命令在批处理模式下运行top,

top -b -n 1

顶部-b -n 1

The problem is I can run top in batch mode 100 times but the CPU usage never changes past the original value. The memory usage changes as expected but CPU stays the same. If I simultaneously run another top in a different window the CPU usage is changing for that top but not for the top in batch mode.

问题是我可以在批处理模式下运行100次,但CPU使用率永远不会超过原始值。内存使用情况按预期更改,但CPU保持不变。如果我在另一个窗口中同时运行另一个顶部,则CPU使用率会在该批量模式下更改为顶部,而不是顶部。

Basically CPU stats don't seem to change in batch mode and do in interactive mode. Does anyone know why? Try it yourself, run the above command a few times and observe the CPU usage staying the same then run top in interactive mode and observe the CPU usage constantly changing.

基本上,CPU统计数据似乎在批处理模式下不会改变,而在交互模式下也是如此。有谁知道为什么?自己尝试一下,运行上面的命令几次并观察CPU使用情况保持不变然后在交互模式下运行顶部并观察CPU使用情况不断变化。

2 个解决方案

#1


6  

On the first iteration, it is showing you the average CPU usage since system startup.

在第一次迭代中,它向您显示自系统启动以来的平均CPU使用率。

(Note that this is no longer the case for newer versions of top).

(请注意,较新版本的top不再是这种情况)。

#2


3  

Here's one liner that displays cumulative CPU usage over a longer period of time, 5 seconds in this case. You can adjust it with -d flag.

这是一个显示在较长时间内累计CPU使用率的衬垫,在这种情况下为5秒。您可以使用-d标志进行调整。

top -b -d 5 -n 2 | awk '$1 == "PID" {block_num++; next} block_num == 2 {sum += $9;} END {print sum}'

#1


6  

On the first iteration, it is showing you the average CPU usage since system startup.

在第一次迭代中,它向您显示自系统启动以来的平均CPU使用率。

(Note that this is no longer the case for newer versions of top).

(请注意,较新版本的top不再是这种情况)。

#2


3  

Here's one liner that displays cumulative CPU usage over a longer period of time, 5 seconds in this case. You can adjust it with -d flag.

这是一个显示在较长时间内累计CPU使用率的衬垫,在这种情况下为5秒。您可以使用-d标志进行调整。

top -b -d 5 -n 2 | awk '$1 == "PID" {block_num++; next} block_num == 2 {sum += $9;} END {print sum}'