欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

linux - top与ps间的区别

程序员文章站 2022-04-29 13:02:05
背景 在linux系统中提供了2个查询系统负荷值的命令,一个是 ps o THREAD 一个是 top ,这两个命令都能够查询当前进程的CPU使用率情况,但是所代表的含义确实不一样的, ps o THREAD 偏向与整个进程周期所占的CPU使用率,而 top 更偏向于某个固定时间窗口的CPU使用率也 ......

背景

  在linux系统中提供了2个查询系统负荷值的命令,一个是 ps -o thread 一个是 top ,这两个命令都能够查询当前进程的cpu使用率情况,但是所代表的含义确实不一样的,ps -o thread 偏向与整个进程周期所占的cpu使用率,而 top 更偏向于某个固定时间窗口的cpu使用率也就是说更倾向于查询实时cpu使用率。

具体

notes
       this ps works by reading the virtual files in /proc.  this ps does not need to be setuid kmem or have any privileges to run.  do not give this ps any special permissions.

       this ps needs access to namelist data for proper wchan display.  for kernels prior to 2.6, the system.map file must be installed.

       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%.

  从第三行 cpu usage is currently expressed as the percentage of time spent running during the entire lifetime of a process 可知 cpu使用率表示为进程整个生命周期中 与 运行在cpu上所花费的时间 百分比

1. %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 a process is multi-threaded and top is not operating in threads mode, amounts greater than 100% may be reported.  you toggle threads mode
           with the `h' interactive command.

  根据 cpu usage the task's share of the elapsed cpu time since the last screen update, expressed as a percentage of total cpu time.知道cpu使用率是在一个单位时间窗口下该进程占用cpu时间与该单位时间的百分比值。

总结

  1. ps -o thread 命令更偏向于整个进程生命中占用cpu时间的比率
  2. top 命令更偏向于某个时间点占用cpu时间的比率