Thursday, August 9, 2012

How To Find out LINUX CPU Utilization


Whenever a Linux system CPU is occupied by a process, it is unavailable for processing other requests. Rest of pending requests must wait till CPU is free. This becomes a bottleneck in the system. 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 to find out Linux cpu load

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 to find out Linux cpu usage

Type the top command:

$ top
top - 08:10:22 up 2 min,  3 users,  load average: 1.13, 0.85, 0.35
Tasks: 159 total,   1 running, 158 sleeping,   0 stopped,   0 zombie
Cpu(s): 10.2%us, 24.8%sy,  0.0%ni, 54.1%id,  9.9%wa,  0.6%hi,  0.4%si,  0.0%st
Mem:    511100k total,   338112k used,   172988k free,    25436k buffers
Swap:  1048568k total,        0k used,  1048568k free,   158028k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 2454 sshd      20   0  9852 1332  784 S  7.5  0.3   0:00.05 sshd
 2453 root      20   0  9852 2576 2052 S  3.0  0.5   0:00.02 sshd
 2148 root      20   0 53188  12m 9920 S  1.5  2.5   0:00.71 gnome-panel
    1 root      20   0  2880 1436 1212 S  0.0  0.3   0:01.31 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0
    4 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0
    6 root      RT   0     0    0    0 S  0.0  0.0   0:00.01 watchdog/0

You can see Linux CPU utilization under CPU stats. The task’s share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time. In a true SMP environment (multiple CPUS), top will operate in number of CPUs. Please note that you need to type q key to exit the top command display.

The top command produces a frequently-updated list of processes. By default, the processes are ordered by percentage of CPU usage, with only the "top" CPU consumers shown. The top command shows how much processing power and memory are being used, as well as other information about the running processes.

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 4:

# 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:

[root@server ~]# mpstat
Linux 2.6.32-279.el6.i686 (server.example.com)  08/10/2012      _i686_  (1 CPU)

08:12:22 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guempsst   %idle
08:12:22 AM  all    5.69    0.00   13.62    7.29    0.35    0.22    0.00    0.00   72.83

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.:

[root@server ~]# mpstat -P ALL
Linux 2.6.32-279.el6.i686 (server.example.com)  08/10/2012      _i686_  (1 CPU)

08:13:14 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
08:13:14 AM  all    4.86    0.00   11.53    6.28    0.29    0.19    0.00    0.00   76.85
08:13:14 AM    0    4.86    0.00   11.53    6.28    0.29    0.19    0.00    0.00   76.85

Report CPU utilization using sar command

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

[root@server ~]# sar
Linux 2.6.32-279.el6.i686 (server.example.com)  08/10/2012      _i686_  (1 CPU)
12:00:02 AM       CPU     %user     %nice   %system   %iowait     %idle
12:10:01 AM       all      1.05      0.00      0.28      0.04     98.64
12:20:01 AM       all      0.74      0.00      0.34      0.38     98.54
12:30:02 AM       all      1.09      0.00      0.28      0.10     98.53
12:40:01 AM       all      0.76      0.00      0.21      0.03     99.00
12:50:01 AM       all      1.25      0.00      0.32      0.03     98.40
01:00:01 AM       all      0.80      0.00      0.24      0.03     98.92

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:

[root@server ~]# sar -u 2 5


Linux 2.6.32-279.el6.i686 (server.example.com)  08/10/2012      _i686_  (1 CPU)

08:15:20 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
08:15:22 AM     all      0.00      0.00      0.50      0.00      0.00     99.50
08:15:24 AM     all      0.00      0.00      0.50      0.00      0.00     99.50
08:15:26 AM     all      0.00      0.00      0.51      0.00      0.00     99.49
08:15:28 AM     all      0.00      0.00      1.00      0.00      0.00     99.00
08:15:30 AM     all      0.00      0.00      0.50      0.00      0.00     99.50
Average:        all      0.00      0.00      0.60      0.00      0.00     99.40

Where,

-u 2 5 : Report CPU utilization. The following values are displayed:

%user: Percentage of CPU utilization that occurred while executing at the user level (application).

%nice: Percentage of CPU utilization that occurred while executing at the user level with nice priority.

%system: Percentage of CPU utilization that occurred while executing at the system level (kernel).

%iowait: Percentage of time that the CPU or CPUs were idle during which the system had an outstanding disk I/O request.

%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 &
[1] 2503

 Better use nohup command so that you can logout and check back report later on:

[root@server ~]# nohup sar -o output.file 12 8 >/dev/null 2>&1 &
[2] 2505

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

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.

[root@server ~]# ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10
%CPU   PID USER     COMMAND
 0.6  1863 root     /usr/bin/Xorg :0 -nr -verbose -audit 4 -auth /var/run/gdm/auth-for-gdm-UHauwp/database -nolisten tcp vt1
 0.5  2151 root     nautilus
 0.2     1 root     /sbin/init
 0.1     7 root     [events/0]
 0.1  2422 root     sshd: root@pts/1
 0.1  2204 root     gnome-screensaver
 0.1  2148 root     gnome-panel
 0.1  1775 root     crond
 0.1  1247 dbus     dbus-daemon --system
[1]-  Done                    sar -o output.file 12 8 > /dev/null 2>&1
[2]+  Done                    nohup sar -o output.file 12 8 > /dev/null 2>&1

OR

[root@server ~]# ps -eo pcpu,pid,user,args | sort -r -k1 | less
%CPU   PID USER     COMMAND
 96  2520 root     /usr/sbin/packagekitd
 0.5  1863 root     /usr/bin/Xorg :0 -nr -verbose -audit 4 -auth /var/run/gdm/auth-for-gdm-UHauwp/database -nolisten tcp vt1
 0.4  2151 root     nautilus
 0.1     7 root     [events/0]
 0.1  2422 root     sshd: root@pts/1
 0.1  2204 root     gnome-screensaver
 0.1  2148 root     gnome-panel
 0.1     1 root     /sbin/init
 0.1  1247 dbus     dbus-daemon --system
 0.0     9 root     [khelper]
 0.0   918 root     [kauditd]
 0.0     8 root     [cgroup]
 0.0   872 root     [ext4-dio-unwrit]
 0.0   871 root     [jbd2/sda1-8]
 0.0     6 root     [watchdog/0]
 0.0    69 root     [kstriped]
 0.0     5 root     [migration/0]
 0.0     4 root     [ksoftirqd/0]
 0.0     3 root     [migration/0]
 0.0    39 root     [usbhid_resumer]

Now you know packagekitd process is eating up lots of CPU power. ps command displays every process (-e) with a user-defined format (-o pcpu). First field is pcpu (cpu utilization). It is sorted in reverse order to display top 10 CPU eating process.

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.

[root@server ~]# iostat
Linux 2.6.32-279.el6.i686 (server.example.com)  08/10/2012      _i686_  (1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           1.87    0.00    4.47    2.24    0.00   91.43

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
scd0              0.07         0.49         0.00        424          0
sda               9.20       559.49        21.79     484636      18874
                                    
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

2 comments:

  1. Are you frustrated with Skype and searching for alternatives? One of these fantastic video chat and calling apps available on almost all platforms can help you make the switch. you can look here

    ReplyDelete