Let's start with the most common CPU metric.
Fire up top
, and let's start figuring out what the different CPU percentage values are.
%Cpu(s): 2.3 us, 0.6 sy, 0.0 ni, 96.7 id, 0.2 wa
For each of the following scripts (dummy1.sh
, dummy2.sh
, dummy3.sh
, dummy4.sh
) under the scripts/cpu/
directory:
- Run the script
- While the script is running, look at
top
on another terminal window - Without looking at the code, try to figure out what the script is doing (find the percentage fields description in
man 1 top
) - Stop the script (use
Ctrl+C
or wait 2 minutes for it to timeout) - Verify your answer by reading the script content
- Most tools use
/proc/stat
to fetch CPU percentage. Note that it displays amount of time and not percentage. - To get a percentage over a specific interval of time, you can use:
sar -P ALL -u <interval> <count>
-P
per-processor statistics-u
CPU utilization
- or
mpstat
(similar usage and output)
- What's the difference between %IO-wait and %idle?
- Is the entire CPU load created by a process accounted to that process?
You may have noticed the st
label. From man 1 top
:
st : time stolen from this vm by the hypervisor
Amazon EC2 uses the hypervisor to regulate the machine CPU usage (to match the instance type's EC2 Compute Units). If you see inconsistent stolen percentage over time, then you might be using Burstable Performance Instances.