跳到主要内容

1 篇博文 含有标签「performance」

查看所有标签

Best Practices of Linux System Performance Analysis and Tuning with CLI

· 阅读需 5 分钟

Introduction​

Monitoring and tuning Linux system performance is crucial for maintaining the health and efficiency of your infrastructure. Command line tools like top, htop, iotop, sar, ps, free, vmstat, and netstat provide powerful capabilities for analyzing and optimizing system performance. This article provides a comprehensive guide to the best practices of using these tools, covering various scenarios such as checking CPU and memory usage, monitoring processes, and troubleshooting real production issues.


Basic System Monitoring​

Check CPU Usage​

To check CPU usage, use the top command:

top

This command displays real-time CPU and memory usage, along with a list of running processes.

Check Memory Usage​

To check memory usage, use the free command:

free -h

This command displays the total, used, and free memory in a human-readable format.

Check Disk I/O​

To check disk I/O, use the iostat command:

iostat -x 1

This command displays detailed I/O statistics, including read and write rates.

Check Network Traffic​

To check network traffic, use the iftop command:

iftop -i eth0

This command displays real-time network traffic on the eth0 interface.


Process Monitoring​

List Running Processes​

To list all running processes, use the ps command:

ps aux

This command displays a detailed list of all running processes.

Check Process Status​

To check the status of a specific process, use the ps command with the process ID (PID):

ps -p <PID> -o pid,ppid,cmd,%cpu,%mem,state

This command displays the status of the process with the specified PID.

Kill a Process​

To kill a process, use the kill command with the process ID (PID):

kill <PID>

This command sends a termination signal to the process with the specified PID.


Advanced Performance Tuning​

Top​

To monitor system performance in real-time, use the top command:

top

This command provides a dynamic view of system resources and running processes.

Htop​

To monitor system performance with a more user-friendly interface, use htop:

htop

This command provides a colorful and interactive view of system resources and running processes.

Iotop​

To monitor disk I/O in real-time, use the iotop command:

iotop

This command displays real-time disk I/O statistics for running processes.

Sar​

To collect and report system activity, use the sar command:

sar -u 1 5

This command collects CPU usage statistics every second for 5 seconds.


Troubleshooting Real Production Issues​

High CPU Usage​

To identify processes causing high CPU usage, use top or htop:

top

Sort processes by CPU usage by pressing P.

High Memory Usage​

To identify processes causing high memory usage, use top or htop:

top

Sort processes by memory usage by pressing M.

High Disk I/O​

To identify processes causing high disk I/O, use iotop:

iotop

This command displays real-time disk I/O statistics for running processes.

Network Latency​

To diagnose network latency, use ping and traceroute:

ping google.com
traceroute google.com

These commands help identify network latency and routing issues.


Conclusion​

Monitoring and tuning Linux system performance is essential for maintaining the health and efficiency of your infrastructure. By mastering the various options and scenarios covered in this article, you can effectively use command line tools like top, htop, iotop, sar, ps, free, vmstat, and netstat to analyze and optimize system performance.


References​

  1. top Man Page
  2. htop Man Page
  3. iotop Man Page
  4. sar Man Page
  5. ps Man Page
  6. free Man Page
  7. iostat Man Page
  8. iftop Man Page
  9. ping Man Page
  10. traceroute Man Page

By following these best practices, you can leverage these system performance tools to their full potential and efficiently manage your monitoring and tuning tasks.