Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Performance Troubleshooting in Linux

Introduction

Performance troubleshooting in Linux involves identifying, analyzing, and resolving issues that affect system performance. This tutorial will guide you through various techniques and tools used to troubleshoot performance issues on a Linux system.

Monitoring System Performance

Monitoring system performance is the first step in troubleshooting. Linux provides several tools to monitor CPU, memory, disk, and network usage.

Top Command

The top command provides a dynamic, real-time view of a running system. It displays system summary information and a list of tasks currently being managed by the Linux kernel.

To use the top command, simply type:

top
top - 15:44:10 up  1:36,  1 user,  load average: 0.00, 0.01, 0.05
Tasks:  87 total,   1 running,  86 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.4 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  2048000 total,  1583000 free,   307200 used,   153800 buff/cache
KiB Swap:  1024000 total,  1024000 free,        0 used.  1600000 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND     
 1234 root      20   0  123456   7890   5678 S   0.3  0.4   0:01.23 top         
                    

VMStat Command

The vmstat command reports information about processes, memory, paging, block IO, traps, and CPU activity. It provides an overview of system performance.

To use the vmstat command, type:

vmstat 5
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 1583000 153800  307200    0    0     1     1   50   60  0  0 100  0  0
 0  0      0 1583000 153800  307200    0    0     0     0  100  200  1  1  98  0  0
                    

Analyzing CPU Usage

High CPU usage can slow down the system. Identifying processes that consume a lot of CPU resources is crucial for performance troubleshooting.

PS Command

The ps command displays information about active processes. It can be used to find processes that are consuming high CPU resources.

To list processes sorted by CPU usage, use:

ps aux --sort=-%cpu
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1234  5.0  1.0 123456  7890 ?        R    14:30   0:30 /usr/bin/example
                    

Analyzing Memory Usage

Memory usage can significantly affect system performance. It is important to monitor and manage memory to ensure optimal performance.

Free Command

The free command displays the total amount of free and used physical and swap memory in the system, as well as the buffers and caches used by the kernel.

To display memory usage, use:

free -m
              total        used        free      shared  buff/cache   available
Mem:           2000         300        1500          10         200        1600
Swap:          1000           0        1000
                    

Analyzing Disk Usage

Disk I/O performance can impact overall system performance. Monitoring disk usage helps to identify disk-related bottlenecks.

iostat Command

The iostat command reports CPU and I/O statistics for devices and partitions. It helps to identify devices with high I/O activity.

To display I/O statistics, use:

iostat -x
Linux 5.4.0-42-generic (hostname)  10/12/2020  _x86_64_ (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.50    0.00    0.30    0.20    0.00   99.00

Device            r/s     w/s     rkB/s   wkB/s   rrqm/s  wrqm/s  %rrqm  %wrqm
sda               0.50    1.00    10.00   20.00    0.00    0.00    0.00   0.00
                    

Network Performance

Network performance issues can affect system performance. Monitoring network activity helps to identify network-related bottlenecks.

Netstat Command

The netstat command displays network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

To display network statistics, use:

netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500   12345      0      0 0      54321      0      0      0 BMRU
                    

Conclusion

Performance troubleshooting in Linux involves monitoring and analyzing various system metrics to identify and resolve performance issues. Using the tools and techniques described in this tutorial, you can effectively troubleshoot and optimize the performance of your Linux system.