首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当psutil.cpu_percent被调用时,iowait时间算数吗?

当psutil.cpu_percent被调用时,iowait时间算数吗?
EN

Stack Overflow用户
提问于 2022-02-07 07:25:41
回答 1查看 82关注 0票数 2

我想知道iowait时间是否在psutil.cpu_percent()中计算,所以编写如下代码来测试

代码语言:javascript
复制
#cat test.py
import os
import psutil
import time


p = psutil.Process(os.getpid())


start = time.time()
times_before_workload = p.cpu_times()
# this function return cpu_percent between two call. so we have to call it once before workload
percent_before_workload = p.cpu_percent()  


# I call f open/close many times and read from the file 
# hoping cpu will spent time on both user system and iowait

c = 1000
while c:
    f = open('/tmp/big_text')
    content = f.read(10240)
    f.close()
    c -= 1

end = time.time()
percent_after_workload = p.cpu_percent()


times_after_workload = p.cpu_times()
user_seconds = times_after_workload.user - times_before_workload.user
system_seconds = times_after_workload.system - times_before_workload.system
iowait_seconds = times_after_workload.iowait - times_before_workload.iowait

# if cpu percent == user_percent + system_percent + iowait_percent then it means yes. iowait are counted

print 'user_percent %s' % round(user_seconds / (end - start) * 100, 2)
print 'system_percent %s' % round(system_seconds / (end - start) * 100, 2)
print 'iowait_percent %s' % round(iowait_seconds / (end - start) * 100, 2)
print 'percent %s' % percent_after_workload

这是输出

代码语言:javascript
复制
#python test.py
user_percent 67.06
system_percent 67.06
iowait_percent 0.0
percent 134.8

iowait是0,所以仍然无法验证。所以问题是

  1. iowait计算cpu百分比吗?
  2. 怎样才能做到这一点?(没有零)
EN

回答 1

Stack Overflow用户

发布于 2022-02-17 15:42:37

  1. 不,不算cpu百分比。
  2. 运行vmtouch,在循环的每次迭代开始时从内存中删除页面。

您可能需要安装vmtouch:

代码语言:javascript
复制
apt update
apt install vmtouch

在Python中运行vmtouch:

代码语言:javascript
复制
import subprocess

subprocess.run(['vmtouch', '-e', '/tmp/big_text'], stdout=subprocess.DEVNULL)

参考资料:为什么读取文件是零?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71014792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档