前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >How Linux CPU Usage Time and Percentage is calculated

How Linux CPU Usage Time and Percentage is calculated

作者头像
用户9732312
发布2022-05-13 18:44:41
9910
发布2022-05-13 18:44:41
举报
文章被收录于专栏:ADAS性能优化

Overview

CPU time is allocated in discrete time slices (ticks). For a certain number of time slices, the cpu is busy, other times it is not (which is represented by the idle process). In the picture below the CPU is busy for 6 of the 10 CPU slices. 6/10 = .60 = 60% of busy time (and there would therefore be 40% idle time).

Note: A tick(cycle) is the time it takes to send a single pulse. A pulse consists of a high voltage followed by a low voltage. There can be billions of ticks per second depending on the frequency(GHz) of the CPU clock.

You can get the number of CPU ticks since boot from /proc/stat

代码语言:javascript
复制
 cat /proc/stat 
user nice system idle iowait  irq  softirq steal guest guest_nice
cpu4705 356  584    3699   23    23     0       0     0          0
  • user: normal processes executing in user mode
  • nice: niced processes executing in user mode
  • system: processes executing in kernel mode
  • idle: twiddling thumbs
  • iowait: waiting for I/O to complete
  • irq: servicing interrupts
  • softirq: servicing softirqs
  • steal: involuntary wait
  • guest: running a normal guest
  • guest_nice: running a niced guest

Formula

To calculate Linux CPU usage time subtract the idle CPU time from the total CPU time as follows:

Total CPU time since boot = user+nice+system+idle+iowait+irq+softirq+steal

Total CPU Idle time since boot = idle + iowait

Total CPU usage time since boot = Total CPU time since boot - Total CPU Idle time since boot

Total CPU percentage = Total CPU usage time since boot/Total CPU time since boot X 100

If you use the formula on the example data above you should get a Linux CPU usage Percentage of 60%.

Note: Guest and Guest_nice are already accounted in user and nice, hence they are not included in the total calculation

For real time CPU usage, you will need to calculate the time between two intervals.

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2016-11-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Android性能优化 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Overview
  • Formula
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档