Discussing Linux: System Performance Monitoring and Tuning

In the practical introduction to Linux, the twelfth essential knowledge point is system performance monitoring and tuning, which is a core capability to ensure system stability and efficient operation. Here are the key points:

1. Core Monitoring Tools

  1. Real-time Resource Monitoring (<span>top</span>/<span>htop</span>)
  • <span>top</span>: Dynamically displays processes sorted by CPU and memory usage (default refresh every 3 seconds). Example: Press <span>P</span> to sort by CPU usage, press <span>M</span> to sort by memory usage.
  • <span>htop</span>: Enhanced version of <span>top</span>, supports mouse operations and color coding (requires installation: <span>apt install htop</span>).
  • Memory and Swap Space Analysis
    • <span>free -h</span>: Intuitively view physical memory (<span>Mem</span>) and swap space (<span>Swap</span>) usage (<span>-h</span> automatically converts units).
    • <span>vmstat 2 5</span>: Samples every 2 seconds, for a total of 5 times, outputting comprehensive metrics for memory, I/O, CPU, etc.
  • Disk I/O Monitoring
    • <span>iostat -dx 1</span>: Reports disk read/write rates (<span>r/s</span>, <span>w/s</span>) and utilization (<span>%util</span>) every second.
    • <span>iotop</span>: Similar to <span>top</span>, displays real-time process disk I/O usage (requires root privileges).

    2. Identifying Performance Bottlenecks

    1. CPU Bottleneck
    • <span>uptime</span> shows average load: if the load values for 1/5/15 minutes are consistently > the number of CPU cores, it indicates CPU saturation.
    • <span>pidstat -u 1</span>: Statistics CPU usage of processes every second to identify high consumption processes.
  • Memory Bottleneck
    • If the <span>available</span> value in <span>free</span> is close to 0 and <span>Swap</span> usage surges, memory optimization or expansion is needed.
  • I/O Bottleneck
    • <span>iostat</span> shows <span>%util > 80%</span> or <span>await</span> (average I/O wait time) is too high, indicating disk overload.

    3. Practical Tuning Techniques

    1. Kernel Parameter Optimization
    • Reduce swap tendency: <span>sysctl vm.swappiness=10</span> (default 60, lower values use Swap less).
    • Increase file handle limit: <span>sysctl fs.file-max=100000</span> and modify <span>/etc/security/limits.conf</span>.
  • Service-Level Optimization
    • Disable unnecessary services: <span>systemctl disable bluetooth.service</span> to reduce resource usage.
    • Adjust process priority: <span>nice -n -20 /path/to/program</span> to give critical processes higher priority.
  • Hardware-Level Optimization
    • Replace HDD with SSD: significantly reduce I/O latency.
    • Enable RAID 10: balancing read/write performance and data redundancy.

    Key Summary: Performance tuning should be based on monitoring data (<span>top</span>/<span>vmstat</span>/<span>iostat</span>) to identify bottlenecks, prioritizing kernel parameter adjustments (<span>sysctl</span>) and service configurations. Follow the “monitor → analyze → adjust → verify” cycle to avoid stability issues caused by over-optimization..

    Leave a Comment