1

如何初略判断一个进程是计算密集型还是IO密集型

 2 years ago
source link: https://www.lujun9972.win/blog/2019/09/27/%E5%A6%82%E4%BD%95%E5%88%9D%E7%95%A5%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E8%BF%9B%E7%A8%8B%E6%98%AF%E8%AE%A1%E7%AE%97%E5%AF%86%E9%9B%86%E5%9E%8B%E8%BF%98%E6%98%AFio%E5%AF%86%E9%9B%86%E5%9E%8B/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

如何初略判断一个进程是计算密集型还是IO密集型

也是从 https://blogs.oracle.com/linux/solving-problems-with-proc-v2 中学到的.

一般来说计算密集型的进程会有较多的非自愿上下文切换(nonvoluntary context switch),而IO密集型的进程会有较多的自愿上下文切换(voluntary context switch).

那么如何查看某个进程遭遇到的上下文切换数量呢? 原来在 /proc/$PID/status 中有 voluntary_ctxt_switchesnonvoluntary_ctxt_switches 两个标签分别标示了自愿上下文切换和非自愿上下文切换的次数.

比如我们来看看Emacs是计算密集型还是IO密集型呢?

grep ctxt_switches /proc/$(pgrep emacs)/status
voluntary_ctxt_switches:        74840
nonvoluntary_ctxt_switches:     21080

可以看到自愿上下文切换次数比较多,因此可以 初步 推测为IO密集型.

再比如我们自己写一个计算密集型的程序测试一下

int main() {
  while (1) {
  }
}

可以看到如下测试结果

lujun9972:UO/ $ /tmp/loop &
[1] 13247
lujun9972:UO/ $ cat /proc/13247/status |grep ctxt_switches
voluntary_ctxt_switches:        0
nonvoluntary_ctxt_switches:     401
lujun9972:UO/ $ cat /proc/13247/status |grep ctxt_switches
voluntary_ctxt_switches:        0
nonvoluntary_ctxt_switches:     470
lujun9972:UO/ $ cat /proc/13247/status |grep ctxt_switches
voluntary_ctxt_switches:        0
nonvoluntary_ctxt_switches:     519
lujun9972:UO/ $ cat /proc/13247/status |grep ctxt_switches
voluntary_ctxt_switches:        0
nonvoluntary_ctxt_switches:     1930

可以看到全都是非自愿上下文切换,符合预想的结果


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK