栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Linux中以百分比给出的CPU使用率的准确计算?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Linux中以百分比给出的CPU使用率的准确计算?

根据撰写本文时的htop源代码,我的假设看起来是正确的:

(请参阅ProcessList.c中的

voidProcessList_scan(ProcessList*this)
函数)

// Guest time is already accounted in usertimeusertime = usertime - guest;          # As you see here, it subtracts guest from user timenicetime = nicetime - guestnice;      # and guest_nice from nice time// Fields existing on kernels >= 2.6// (and RHEL's patched kernel 2.4...)idlealltime = idletime + ioWait;      # ioWait is added in the idleTimesystemalltime = systemtime + irq + softIrq;virtalltime = guest + guestnice;totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime;

因此,从

/proc/stat
:第一行中列出的字段中(请参阅文档中的
1.8节)

     user    nice   system  idle      iowait irq   softirq  steal  guest  guest_nicecpu  74608   2520   24433   1117073   6176   4054  0        0      0      0

通过算法,我们可以计算出CPU使用率百分比,如下所示:

PrevIdle = previdle + previowaitIdle = idle + iowaitPrevNonIdle = prevuser + prevnice + prevsystem + previrq + prevsoftirq + prevstealNonIdle = user + nice + system + irq + softirq + stealPrevTotal = PrevIdle + PrevNonIdleTotal = Idle + NonIdle# differentiate: actual value minus the previous onetotald = Total - PrevTotalidled = Idle - PrevIdleCPU_Percentage = (totald - idled)/totald


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/379321.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号