Linux System Performance Analysis

From Server rental store
Jump to navigation Jump to search

This guide will walk you through essential Linux system performance analysis tools, helping you understand and diagnose potential bottlenecks on your server. By mastering `top`, `htop`, `vmstat`, `iostat`, and `sar`, you'll gain the ability to identify resource-hungry processes, memory pressure, disk I/O issues, and historical performance trends.

Prerequisites

Before you begin, ensure you have:

  1. A Linux server with root or `sudo` privileges.
  2. Basic understanding of the Linux command line.
  3. SSH access to your server.
  4. The `htop`, `vmstat`, `iostat`, and `sar` utilities installed. Most distributions include these by default. If not, you can typically install them using your distribution's package manager:
    • Debian/Ubuntu:
sudo apt update && sudo apt install htop sysstat
    • CentOS/RHEL/Fedora:
sudo yum install htop sysstat

Understanding `top`

`top` is a real-time process monitoring utility that displays a dynamic, sortable list of running processes. It provides a quick overview of system resource utilization.

Running `top`

1. Log in to your server via SSH. 2. Execute the `top` command:

top

Interpreting `top` Output

The output of `top` is divided into two main sections: the summary area and the process list.

Summary Area

  • **`top - HH:MM:SS up D days, HH:MM, U users, load average: X.XX, Y.YY, Z.ZZ`**:
   *   `HH:MM:SS`: Current system time.
   *   `up D days, HH:MM`: System uptime.
   *   `U users`: Number of currently logged-in users.
   *   `load average: X.XX, Y.YY, Z.ZZ`: The average number of processes waiting to run over the last 1, 5, and 15 minutes. High load averages (significantly exceeding the number of CPU cores) indicate potential CPU contention.
  • **`Tasks: T total, R running, S sleeping, s stopped, Z zombie`**:
   *   `T total`: Total number of processes.
   *   `R running`: Number of processes currently running or ready to run.
   *   `S sleeping`: Number of processes waiting for an event.
   *   `s stopped`: Number of processes that have been stopped.
   *   `Z zombie`: Processes that have terminated but whose parent process hasn't yet retrieved their exit status. A few zombie processes are normal, but a large number can indicate an issue.
  • **`%Cpu(s): us, sy, ni, id, wa, hi, si, st`**: CPU utilization breakdown:
   *   `us` (user): CPU time spent in user space.
   *   `sy` (system): CPU time spent in kernel space.
   *   `ni` (nice): CPU time spent on processes with a positive nice value (lower priority).
   *   `id` (idle): CPU time spent idle. This is the most important metric for assessing CPU availability.
   *   `wa` (I/O wait): CPU time spent waiting for I/O operations to complete. High `wa` indicates disk or network bottlenecks.
   *   `hi` (hardware interrupts): CPU time spent servicing hardware interrupts.
   *   `si` (software interrupts): CPU time spent servicing software interrupts.
   *   `st` (steal time): Time stolen from a virtual machine by the hypervisor (relevant in virtualized environments).
  • **`MiB Mem : T total, F free, U used, A buff/cache`**: Physical memory (RAM) utilization:
   *   `T total`: Total physical memory.
   *   `F free`: Amount of free memory.
   *   `U used`: Amount of used memory.
   *   `A buff/cache`: Memory used by the kernel for buffers and page cache. This memory can be reclaimed by applications if needed.
  • **`MiB Swap: T total, F free, U used, A avail Mem`**: Swap space utilization:
   *   `T total`: Total swap space.
   *   `F free`: Amount of free swap space.
   *   `U used`: Amount of used swap space.
   *   `A avail Mem`: Estimated available memory, including free memory and reclaimable cache/buffers.

Process List

This section lists individual processes, sorted by CPU usage by default. Key columns include:

  • **`PID`**: Process ID.
  • **`USER`**: Owner of the process.
  • **`PR`**: Priority of the process.
  • **`NI`**: Nice value of the process.
  • **`VIRT`**: Virtual memory used by the process.
  • **`RES`**: Resident Set Size (physical memory used).
  • **`SHR`**: Shared memory used by the process.
  • **`S`**: Process status (R, S, D, Z, T).
  • **`%CPU`**: CPU utilization percentage.
  • **`%MEM`**: Memory utilization percentage.
  • **`TIME+`**: Total CPU time used by the process.
  • **`COMMAND`**: The command name or full command line.

Interactive Commands in `top`

  • **`k`**: Kill a process (you'll be prompted for PID and signal).
  • **`r`**: Renice a process (change its priority).
  • **`M`**: Sort by memory usage.
  • **`P`**: Sort by CPU usage (default).
  • **`T`**: Sort by time.
  • **`q`**: Quit `top`.

Understanding `htop`

`htop` is an enhanced, interactive process viewer that offers a more user-friendly and visually appealing interface than `top`. It provides color-coded output and easier navigation.

Running `htop`

1. Execute the `htop` command:

htop

Interpreting `htop` Output

`htop` presents similar information to `top` but with distinct visual elements:

  • **Header**: Displays CPU usage per core (graphical bars), memory and swap usage, tasks, load average, and uptime.
  • **Process List**: Similar to `top` but with color-coding for different process states and user privileges.

Interactive Commands in `htop`

  • **Arrow keys**: Navigate the process list.
  • **`F1` (Help)**: Display help.
  • **`F2` (Setup)**: Configure `htop`'s display.
  • **`F3` (Search)**: Search for a process.
  • **`F4` (Filter)**: Filter processes by name.
  • **`F5` (Tree)**: Display processes in a tree view.
  • **`F6` (SortBy)**: Select sorting column.
  • **`F7` (Nice -)**: Decrease process priority.
  • **`F8` (Nice +)**: Increase process priority.
  • **`F9` (Kill)**: Send a signal to a process.
  • **`F10` (Quit)**: Exit `htop`.

Understanding `vmstat`

`vmstat` (virtual memory statistics) reports information about processes, memory, paging, block IO, traps, and CPU activity. It's excellent for observing system behavior over time.

Running `vmstat`

To get a snapshot of current activity:

vmstat

To get updates every 2 seconds, 5 times:

vmstat 2 5

Interpreting `vmstat` Output

  • **`procs`**:
   *   `r`: The number of processes waiting for run time.
   *   `b`: The number of processes in uninterruptible sleep (waiting for I/O).
  • **`memory`**:
   *   `swpd`: The amount of virtual memory used.
   *   `free`: The amount of idle memory.
   *   `buff`: The amount of memory used as buffers.
   *   `cache`: The amount of memory used as cache.
  • **`swap`**:
   *   `si`: Amount of memory switched in from disk (pages per second).
   *   `so`: Amount of memory switched out to disk (pages per second). High `si`/`so` indicates heavy swapping, a sign of memory pressure.
  • **`io`**:
   *   `bi`: Blocks received from a block device (blocks/s).
   *   `bo`: Blocks sent to a block device (blocks/s). High `bi`/`bo` indicates significant disk activity.
  • **`system`**:
   *   `in`: The number of interrupts per second, including the clock.
   *   `cs`: The number of context switches per second. High context switching can indicate too many processes or frequent inter-process communication.
  • **`cpu`**:
   *   `us`: User time.
   *   `sy`: System time.
   *   `id`: Idle time.
   *   `wa`: I/O wait time.
   *   `st`: Steal time.

Understanding `iostat`

`iostat` reports statistics for CPU utilization and input/output statistics for devices and partitions. It's crucial for diagnosing disk I/O performance issues.

Running `iostat`

To display CPU statistics and statistics for all devices:

iostat

To display statistics every 2 seconds, 5 times, with extended device statistics:

iostat -xd 2 5

Interpreting `iostat` Output

  • **`%user`**: CPU utilization in user mode.
  • **`%nice`**: CPU utilization for processes with a positive nice value.
  • **`%system`**: CPU utilization in system mode.
  • **`%iowait`**: Time spent waiting for I/O to complete. High values here often point to disk bottlenecks.
  • **`%steal`**: Time stolen from a virtual machine.
  • **`%idle`**: Time spent idle.

For extended device statistics (`-x` flag):

  • **`r/s`**: Reads completed per second.
  • **`w/s`**: Writes completed per second.
  • **`rkB/s`**: Kilobytes read per second.
  • **`wkB/s`**: Kilobytes written per second.
  • **`await`**: The average I/O completion time, in milliseconds. This includes queue time and service time. High `await` values are a strong indicator of disk contention.
  • **`svctm`**: The average service time, in milliseconds, that the device driver was busy.
  • **`%util`**: Percentage of CPU time during which I/O requests were issued to the device (bandwidth utilization). When this approaches 100%, the disk is saturated.

Understanding `sar`

`sar` (System Activity Reporter) is a powerful tool that collects, reports, and saves system activity information. It's invaluable for historical analysis and trend spotting. The `sysstat` package provides `sar`.

Running `sar`

`sar` typically collects data in the background if `sysstat` is configured to run via cron. You can view historical data from previous days.

To view CPU utilization for the current day (if `sar` has been running):

sar

To view CPU utilization every 2 seconds, 5 times:

sar 2 5

To view memory utilization for the current day:

sar -r

To view disk I/O statistics for the current day:

sar -d

To view network statistics:

sar -n DEV

To view specific historical data (e.g., from yesterday):

sar -f /var/log/sysstat/saXX

(Replace `XX` with the day of the month, e.g., `sa25` for the 25th).

Interpreting `sar` Output

`sar`'s output is highly configurable and depends on the options used. Generally, it provides historical snapshots of various system metrics, allowing you to identify patterns and anomalies over time. For detailed interpretation, refer to the `man sar` page and the specific metrics discussed for `top`, `vmstat`, and `iostat`.

Troubleshooting Common Issues

  • **`top`/`htop` shows high CPU usage but no obvious culprit:**
   *   **Cause**: A process might be stuck in a loop, or a background service might be consuming resources.
   *   **Solution**: Use `htop`'s tree view (`F5`) to see parent-child process relationships. Sort by CPU (`P` or `F6`) and investigate processes with consistently high `%CPU`. Check system logs (`/var/log/syslog`, `/var/log/messages`) for errors related to the suspicious process.
  • **High `%iowait` in `top`/`vmstat`/`iostat`:**
   *   **Cause**: The disk subsystem is overloaded. This could be due to excessive reads/writes from applications, database operations, or log file activity.
   *   **Solution**: Use `iostat -xd 2 5` to identify which specific disk device is experiencing high utilization and `await` times. Use tools like `iotop` (install if needed: `sudo apt install iotop`) to see which processes are generating the most I/O. Optimize application I/O patterns, consider faster storage, or offload I/O-intensive tasks.
  • **System is slow, `top` shows high memory usage but low `free` memory:**
   *   **Cause**: The system is running out of physical RAM and is heavily using swap space. This is known as "thrashing" and severely degrades performance.
   *   **Solution**: Use `vmstat` or `sar -r` to observe swap in/out (`si`/`so`). If these values are consistently high, you need more RAM or need to reduce memory consumption. Identify memory-hungry processes using `htop` (sort by memory) or `top` (press `M`). Consider optimizing applications, increasing swap space (as a temporary measure), or upgrading hardware.
  • **`sar` command not found or no data available:**
   *   **Cause**: The `sysstat` package might not be installed, or the `sar` collection service might not be enabled or running.
   *   **Solution**: Install `sysstat` (`sudo apt install sysstat` or `sudo yum install sysstat`). Ensure the `sysstat` service is enabled and started:
sudo systemctl enable sysstat
        sudo systemctl start sysstat
       Check cron jobs or systemd timers for `sar` data collection schedules.

Related Articles