How do I find out Linux CPU utilization?

Linux Add comments

Following command will help you to identify CPU utilization, so that you can troubleshoot CPU related performance problems.

Finding CPU utilization is one of the important tasks. Linux comes with various utilities to report CPU utilization. With these commands, you will be able to find out:

* CPU utilization
* Display the utilization of each CPU individually (SMP cpu)
* Find out your system’s average CPU utilization since the last reboot etc
* Determine which process is eating the CPU(s)

Old good top command

The top program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of tasks currently being managed by the Linux kernel.
The top command monitors CPU utilization, process statistics, and memory utilization. The top section contains information related to overall system status - uptime, load average, process counts, CPU status, and utilization statistics for both memory and swap space.
Top command

Type the top command:

$ top

Find Linux CPU utilization using mpstat and other tools

Please note that you need to install special package called sysstat to take advantage of following commands. This package includes system performance tools for Linux (Red Hat Linux / RHEL includes these tools by default).

# apt-get install sysstat
Use up2date command if you are using RHEL:
# up2date sysstat

Display the utilization of each CPU individually using mpstat

If you are using SMP (Multiple CPU) system, use mpstat command to display the utilization of each CPU individually. It report processors related statistics. For example, type command:
# mpstat

# mpstat -P ALL
Linux 2.6.9-67.0.1.ELsmp (cochin.armia.com)     02/04/2008

01:10:02 AM  CPU   %user   %nice %system %iowait    %irq   %soft   %idle    intr/s
01:10:02 AM  all    4.69    0.00    0.26    0.09    0.02    0.00   94.94   1048.56
01:10:02 AM    0    5.57    0.00    0.24    0.03    0.00    0.00   94.16   1021.09
01:10:02 AM    1    3.82    0.00    0.29    0.14    0.03    0.00   95.72     27.48

The mpstat command display activities for each available processor, processor 0 being the first one. Global average activities among all processors are also reported. The mpstat command can be used both on SMP and UP machines, but in the latter, only global average activities will be printed.:

# mpstat -P ALL

Report CPU utilization using sar command

You can display today’s CPU activity, with sar command:
# sar

# sar
Linux 2.6.9-67.0.1.ELsmp (cochin.armia.com)     02/04/2008

12:00:01 AM       CPU     %user     %nice   %system   %iowait     %idle
12:10:01 AM       all      3.19      0.00      0.19      0.01     96.60
12:20:01 AM       all      8.46      0.00      0.73      0.09     90.73
12:30:01 AM       all      4.48      0.00      0.52      0.16     94.84
12:40:01 AM       all      5.91      0.00      0.66      0.07     93.36
12:50:01 AM       all      5.48      0.00      0.62      0.21     93.69
01:00:01 AM       all      6.35      0.00      0.76      0.03     92.87
01:10:01 AM       all      7.33      0.00      0.78      0.06     91.83
Average:          all      5.89      0.00      0.61      0.09     93.41

Comparison of CPU utilization

The sar command writes to standard output the contents of selected cumulative activity counters in the operating system. The accounting system, based on the values in the count and interval parameters. For example display comparison of CPU utilization; 2 seconds apart; 5 times, use:
# sar -u 2 5

Where,

* -u 12 5 : Report CPU utilization. The following values are displayed:
o %user: Percentage of CPU utilization that occurred while executing at the user level (application).
o %nice: Percentage of CPU utilization that occurred while executing at the user level with nice priority.
o %system: Percentage of CPU utilization that occurred while executing at the system level (kernel).
o %iowait: Percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.
o %idle: Percentage of time that the CPU or CPUs were idle and the system did not have an outstanding disk I/O request.

To get multiple samples and multiple reports set an output file for the sar command. Run the sar command as a background process using.
# sar -o output.file 12 8 >/dev/null 2>&1 &

Better use nohup command so that you can logout and check back report later on:
# nohup sar -o output.file 12 8 >/dev/null 2>&1 &

All data is captured in binary form and saved to a file (data.file). The data can then be selectively displayed ith the sar command using the -f option.
# sar -f data.file
Task: Find out who is monopolizing or eating the CPUs

Finally, you need to determine which process is monopolizing or eating the CPUs. Following command will displays the top 10 CPU users on the Linux system.
# ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10

OR
# ps -eo pcpu,pid,user,args | sort -r -k1 | less

iostat command

You can also use iostat command which report Central Processing Unit (CPU) statistics and input/output statistics for devices and partitions. It can be use to find out your system’s average CPU utilization since the last reboot.
# iostat

You may want to use following command, which gives you three outputs every 5 seconds (as previous command gives information since the last reboot):$ iostat -xtc 5 3

GUI tools for your laptops/desktops

Above tools/commands are quite useful on remote server. For local system with X GUI installed you can try out gnome-system-monitor. It allows you to view and control the processes running on your system. You can access detailed memory maps, send signals, and terminate the processes.
$ gnome-system-monitor

—————————–

top
top c

top process causing I/O
———————–
ps auxw|tail +2|sort -k 1.16,1.20nr|head

iostat -d -x
iostat -d -m -x
iostat -x -d 2

cd /sys/devices/system/cpu

Type ls command to see all cpus
ls

Output:
cpu0 cpu1 cpu2 cpu3 cpu4 cpu5 cpu6 cpu7

Inside this directory you will see entry for online or offline CPU

Use “netstat -anp | sort -u” to check for network problems.




Leave a Reply

Wordpress Themes by Natty WP. Web Hosting
Images by our golf tips desEXign.