如何初略判断一个进程是计算密集型还是 IO 密集型
一般来说计算密集型的进程会有较多的非自愿上下文切换(nonvoluntary context switch),而 IO 密集型的进程会有较多的自愿上下文切换(voluntary context switch)。
那么如何查看某个进程遭遇到的上下文切换数量呢?原来在 /proc/$PID/status
中有 voluntary_ctxt_switches
和 nonvoluntary_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
可以看到全都是非自愿上下文切换,符合预想的结果
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论