How is CPU usage calculated?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In understanding the performance of a computer system, one crucial metric to examine is CPU usage. This article explores how CPU usage is calculated, providing both a technical explanation and practical examples to present a comprehensive guide.
Understanding CPU Usage
CPU usage refers to the percentage of the CPU's capacity that is currently being utilized by running processes. It acts as a critical metric for system performance, helping identify bottlenecks, gauge system efficiency, and decide scaling needs.
Breakdown of CPU Activities
A CPU can be in one of several states: • User Mode: Time spent executing application code. • Kernel Mode: Time spent executing system calls, i.e., core system operations. • Idle Mode: Time when the CPU is idle. • I/O Wait: Time spent waiting for input/output operations to complete.
Calculating CPU Usage Intuitively
To calculate CPU usage for a process or the system in general, it's essential to tally the time spent in each state over a given period. Here's a simple formula:
\text{CPU Usage (%)} = \left( \frac{\text{CPU Time Used}}{\text{Total CPU Time}} \right) \times 100
CPU Time Used: This refers to the time the CPU spends executing tasks in user or kernel mode.
Total CPU Time: This includes CPU Time Used and Idle or I/O Wait states.
Technical Explanation and Examples
Consider the `/proc/stat` file in a Unix-based system, which gives a snapshot of CPU usage. A typical entry might look like:
• The first number (13942) is time spent in user mode. • The second number (34) is time spent with low-priority processes in user mode (nice). • The third number (2026) is time spent in system mode. • The fourth number (22423) is idle time. • Additional numbers may represent time in other specific states.
• User time delta = 3000 - 2000 = 1000 • Nice time delta = 150 - 100 = 50 • System time delta = 700 - 500 = 200 • Idle time delta = 8000 - 7000 = 1000 • top: Provides live, real-time CPU usage statistics. • ps: Offers CPU usage statistics for processes. • mpstat: Displays CPU usage for each processor. • htop: An interactive process viewer with enhanced features over `top`.
Related reading
- How is Docker different from a virtual machine?
- How is Docker Swarm different than Kubernetes?
- How is rancher different from Kubernetes
- How load balancer works in RabbitMQ
- How is dynamic programming different from greedy algorithms?
- How is Greedy Technique different from Exhaustive Search?
- how many consumer groups can a kafka topic handle?
- How multiplayer games like MMO's handle massive requests

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.