On Linux, CPU usage is broadly categorized into "user space" and "kernel space" (often called "system" time).
- User Space: This is where your applications, shells, browsers, databases, and most daemons run. When a program executes its own code, it's typically using user CPU.
- Kernel Space (System): This is where the Linux kernel operates. It handles system calls (like reading/writing files, network operations, process management), interrupts (hardware and software), and manages resources for user-space programs. When a user-space program makes a system call, the CPU time spent processing that call is accounted to kernel (system) time.
Here's how you can check this using various tools:
1. Overall System Summary
These tools give you a high-level view of CPU usage across all cores, broken down by state.
a. top or htop
These are real-time, interactive tools.
Command:
top
# or
htop
Interpretation:
Look at the %Cpu(s) line (or similar in htop):
%Cpu(s): 1.5 us, 0.7 sy, 0.0 ni, 97.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
us (user): CPU time spent executing user-space processes.
sy (system): CPU time spent executing kernel-space processes (system calls, kernel threads).
ni (nice): CPU time spent by user-space processes with a modified nice value (lower priority). This is still user space.
id (idle): CPU time spent doing nothing.
wa (iowait): CPU time spent waiting for I/O to complete. The CPU is idle, but waiting for disk or network.
hi (hardware interrupts): CPU time spent handling hardware interrupts. This is kernel space.
si (software interrupts): CPU time spent handling software interrupts (e.g., network processing, tasklets). This is kernel space.
st (steal): CPU time "stolen" by a hypervisor from a virtual machine (if you're in a VM).
To get total kernel CPU: Sum sy + hi + si.
To get total user CPU: Sum us + ni.
b. vmstat
vmstat reports statistics about processes, memory, paging, block IO, traps, and cpu activity.
Command:
vmstat 1
(reports every 1 second)
Interpretation:
Look at the cpu section:
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 1205160 15308 1715448 0 0 0 0 283 367 1 0 99 0 0
us: User CPU time.
sy: System (kernel) CPU time.
id: Idle CPU time.
wa: I/O wait CPU time.
st: Steal CPU time.
Again, sy is your primary kernel indicator here.
c. mpstat (from sysstat package)
mpstat reports processor-related statistics. It's great for multi-core systems. You might need to install sysstat (sudo apt-get install sysstat or sudo yum install sysstat).
Command:
mpstat -P ALL 1 5
(reports per-processor stats every 1 second, 5 times)
Interpretation:
You'll see output for each CPU (CPU column will be all for aggregate, then 0, 1, etc. for individual cores).
Linux 5.15.0-78-generic (your-hostname) 08/10/2023 _x86_64_ (4 CPU)
03:51:30 PM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle
03:51:31 PM all 1.00 0.00 0.50 0.00 0.00 0.00 0.00 0.00 0.00 98.50
03:51:31 PM 0 1.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 98.00
03:51:31 PM 1 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 99.00
03:51:31 PM 2 1.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 99.00
03:51:31 PM 3 1.00 0.00 1.00 0.00 0.00 0.00 0.00 0.00 0.00 98.00
%usr: User CPU.
%nice: Nice user CPU.
%sys: System (kernel) CPU.
%irq: Hardware interrupt CPU.
%soft: Software interrupt CPU.
Total kernel CPU: %sys + %irq + %soft.
d. sar (from sysstat package)
sar collects, reports, or saves system activity information. It's often used for historical data.
Command:
sar -u 1 5
(CPU utilization every 1 second, 5 times)
Interpretation:
Linux 5.15.0-78-generic (your-hostname) 08/10/2023 _x86_64_ (4 CPU)
03:52:43 PM CPU %user %nice %system %iowait %steal %idle
03:52:44 PM all 1.00 0.00 0.50 0.00 0.00 98.50
03:52:45 PM all 1.00 0.00 0.50 0.00 0.00 98.50
%user: User CPU.
%nice: Nice user CPU.
%system: System (kernel) CPU.
%iowait, %steal, %idle are as explained above.
2. Per-Process Breakdown
While top/htop show CPU usage per process, they usually show the total CPU for that process, not explicitly breaking it down into its user vs. system calls from that process's perspective. For that, pidstat is the best tool.
pidstat (from sysstat package)
pidstat reports statistics for Linux tasks (processes). This is very powerful for seeing how much of a specific program's CPU time is spent in user space versus making kernel calls.
Command:
pidstat -u 1
(reports CPU usage per process every 1 second)
Interpretation:
Linux 5.15.0-78-generic (your-hostname) 08/10/2023 _x86_64_ (4 CPU)
03:54:10 PM UID PID %usr %system %guest %CPU CPU Command
03:54:11 PM 1000 1234 98.00 2.00 0.00 100.00 0 my_cpu_hog_app
03:54:11 PM 0 5678 0.00 0.10 0.00 0.10 1 kworker/0:1
03:54:11 PM 1000 9012 0.50 0.00 0.00 0.50 2 bash
%usr: Percentage of CPU used by the task in user-space.
%system: Percentage of CPU used by the task in kernel-space (making system calls, etc.).
%CPU: Total CPU used by the task (%usr + %system + %guest).
This is exactly what you're looking for to differentiate CPU usage for individual programs.
3. Raw Data Source (/proc/stat)
All these tools get their data from /proc/stat. You can read it directly for scripting or deep dives.
Command:
cat /proc/stat
Interpretation:
The first line, starting with cpu, gives aggregate CPU times:
cpu 2135062 1085 711130 54109159 40386 0 16091 0 0 0
These are jiffies (typically 1/100th of a second on x86, check getconf CLK_TCK for your system) since boot for different states:
- user: Normal processes executing in user mode.
- nice: Niced processes executing in user mode.
- system: Processes executing in kernel mode.
- idle: The CPU is idle.
- iowait: CPU waiting for I/O to complete.
- irq: Servicing interrupts.
- softirq: Servicing softirqs.
- steal: Stolen from a virtual machine.
- guest: Running a virtual CPU for guest operating systems.
- guest_nice: Running a niced virtual CPU for guest operating systems.
To calculate percentages, you need to take two samples a short time apart and calculate the difference for each state, then divide by the total difference.
- Total User Jiffies:
user + nice
- Total Kernel Jiffies:
system + irq + softirq
Summary of CPU States:
| State | Description | Belongs to... |
|---|
us/%usr | User processes | User Space |
ni/%nice | User processes with lowered priority | User Space |
sy/%sys | Kernel processes, system calls from user processes, kernel threads | Kernel Space |
hi/%irq | Handling hardware interrupts | Kernel Space |
si/%soft | Handling software interrupts (e.g., network, tasklets) | Kernel Space |
id/%idle | CPU doing nothing | Neither |
wa/%iowait | CPU waiting for I/O completion | Neither |
st/%steal | CPU stolen by hypervisor (in VMs) | Neither |
guest/%guest | Running a virtual CPU for guest OS (in VMs, part of us normally) | User Space (VM) |
gnice/%gnice | Running a niced virtual CPU for guest OS (in VMs, part of ni normally) | User Space (VM) |
When to use which:
- Quick overview:
top or htop.
- Detailed per-core usage:
mpstat.
- General system activity over time:
vmstat, sar.
- Breakdown for individual processes (the best answer to your question for specific programs):
pidstat -u.
- Scripting or advanced analysis:
/proc/stat.
Remember to install the sysstat package (sudo apt-get install sysstat on Debian/Ubuntu, sudo yum install sysstat on RHEL/CentOS/Fedora) to get mpstat, pidstat, and sar.