
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
Measure CPU usage for a process on Linux?
Introduction
On modern multi-core CPUs, it is often useful to know the CPU usage of individual cores for a particular process. This can be helpful for identifying bottlenecks in the system, or for understanding the workload distribution across CPU cores. In this article, we will see how to measure separate CPU core usage for a process on Linux using various command line tools.
Measure CPU Core Usage using top Command
The top command is a commonly used tool for monitoring system resources, including CPU usage. By default, top displays the overall CPU usage for all cores, but it also provides a way to see the CPU usage for each core individually.
To do this, first, run the top command and press 1 to see the usage of each CPU core. This will display a breakdown of the CPU usage for each core, as well as the overall usage.
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
In the above output, the %CPU column shows the CPU usage for each core. For example, the first core is using 0.3% of the CPU, while the other cores are not being used at all.
Measure CPU Core Usage using mpstat Command
The mpstat command is another tool that can be used to measure CPU usage on a per-core basis. To use mpstat, first install the sysstat package using your distribution's package manager. On Ubuntu and other Debian-based systems, you can use the following command −
$ sudo apt-get install sysstat
Once the sysstat package is installed, you can use the mpstat command to see the CPU usage for each core. By default, mpstat displays the overall CPU usage, but you can use the -P ALL option to see the usage of each individual core.
$ 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
Measure CPU Core Usage using perf Command
A perf command is a powerful tool for performance analysis and profiling on Linux. It can be used to measure the CPU usage of a particular process or set of processes.
To use perf to measure CPU usage, you will need to first install the perf package using your distribution's package manager. On Ubuntu and other Debian-based systems, you can use the following command −
$ sudo apt-get install linux-tools-common
Once the perf package is installed, you can use the perf stat command to measure the CPU usage of a particular process. For example, to measure the CPU usage of the sleep process with PID 6565, you can use the following command −
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 perf command provides a wide range of options for measuring various performance metrics, including CPU usage, memory usage, and more.
Conclusion
Overall, measuring separate CPU core usage for a process on Linux can be useful for identifying performance issues and optimizing the workload distribution on a multi-core system. The top, mpstat, and perf commands are all useful tools for this purpose, and provide a range of options for measuring different performance metrics. Understanding the CPU usage of individual cores can help you identify bottlenecks and improve the overall performance of your system.
- Related Articles
- How to create a CPU spike with bash command on Linux?
- How to free Inode usage on Linux?
- Init process on UNIX and Linux systems
- Count the number of threads in a process on linux
- What Linux utility for sorting processes by network usage?
- Linux Process Monitoring
- Monitoring Network Usage in Linux
- How to Count the Number of Threads in a Process on Linux
- Redirecting the Output of an Already Running Process on Linux
- How to see top processes sorted by actual memory usage on Linux?
- Process Synchronization in Linux
- How to use the sub process module with pipes on Linux?
- How to create a process in Linux?
- How to Clean a Linux Zombie Process
- How to set a proxy for wget on Linux?
