Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Measure CPU usage for a process on Linux?
On modern multi-core CPUs, monitoring CPU usage of individual cores for specific processes is essential for performance analysis and identifying system bottlenecks. This article demonstrates how to measure CPU core usage for processes on Linux using various command-line tools.
Monitoring CPU Usage with top Command
The top command is the most common tool for monitoring system resources. By default, it shows aggregate CPU usage, but you can view per-core statistics.
To display individual CPU core usage, run top and press 1 to toggle per-core view:
$ top
Then press 1 to see individual core statistics:
top - 10:38:00 up 45 min, 3 users, load average: 0.26, 0.14, 0.15 Tasks: 23 total, 1 running, 22 sleeping, 0 stopped, 0 zombie %Cpu0 : 5.0 us, 4.0 sy, 0.0 ni, 91.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st %Cpu1 : 4.7 us, 4.0 sy, 0.0 ni, 89.3 id, 2.0 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem: 7957.6 total, 6407.9 free, 541.9 used, 1007.8 buff/cache MiB Swap: 0.0 total, 0.0 free, 0.0 used, 7185.1 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 1 root 20 0 3900 3016 2728 S 0.0 0.0 0:00.02 bash
The %CPU column shows per-process CPU usage, while the top section displays individual core utilization. The values represent: us (user), sy (system), ni (nice), id (idle), and wa (I/O wait).
Using mpstat for Detailed Core Statistics
The mpstat command provides comprehensive per-core CPU statistics. First, install the sysstat package:
$ sudo apt-get install sysstat
Use -P ALL to display statistics for all CPU cores:
$ mpstat -P ALL
Linux 5.10.147+ (cs) 12/19/2022 _x86_64_ (2 CPU) 10:43:33 AM CPU %usr %nice %sys %iowait %irq %soft %steal %guest %gnice %idle 10:43:33 AM all 4.67 0.00 3.53 0.64 0.00 0.06 0.04 0.00 0.00 91.06 10:43:33 AM 0 4.63 0.00 3.36 0.05 0.00 0.04 0.04 0.00 0.00 91.87 10:43:33 AM 1 4.70 0.00 3.70 1.24 0.00 0.07 0.04 0.00 0.00 90.24
For continuous monitoring, use interval and count parameters:
$ mpstat -P ALL 2 5
This displays statistics every 2 seconds for 5 iterations.
Process-Specific Monitoring with perf
The perf command provides detailed performance profiling capabilities. Install it using:
$ sudo apt-get install linux-tools-common
To monitor a specific process by PID, use perf stat:
$ perf stat -p 6565
Performance counter stats for process id '6565':
1.07 msec task-clock # 0.003 CPUs utilized
569 context-switches # 0.529 K/sec
0 CPU-migrations # 0.000 K/sec
26 page-faults # 0.024 K/sec
22,547,697 cycles # 2.108 GHz
8,262,105 instructions # 0.37 insn per cycle
1,981,482 branches # 1846.695 M/sec
16,532 branch-misses # 0.83% of all branches
1.477977927 seconds time elapsed
The CPUs utilized field shows the fraction of available CPU cores used by the process. For per-core analysis, combine perf with taskset to bind processes to specific cores.
Comparison of Tools
| Tool | Real-time | Per-core View | Process-specific | Best For |
|---|---|---|---|---|
| top | Yes | Yes | Yes | Interactive monitoring |
| mpstat | Yes | Yes | No | System-wide core analysis |
| perf | No | Limited | Yes | Detailed profiling |
Conclusion
Measuring CPU core usage helps identify performance bottlenecks and optimize workload distribution across multi-core systems. The top command provides real-time interactive monitoring, mpstat offers comprehensive system-wide statistics, and perf delivers detailed process-specific profiling capabilities.
