linux host views CPU performance metrics through top.

Published: 2020-03-17


When the server becomes slow and the load is high, you need to first check whether the CPU load is too high. If the CPU load is high, then check which specific process is occupying CPU resources.

1. CPU load:
    Check with the top command: 

        image.png

        Mainly look at “load average”, which can be understood as representing the average load for the previous 1, 5, and 15 minutes respectively. Usually, a single-core CPU should not exceed 4; if it is a 4-core CPU, it should not exceed 16 (i.e., number of CPU cores * 4). If it exceeds this, it indicates that the load is high and there is an abnormality.

    Other parameters are described as follows:

        us (user time)  indicates the time the CPU executes user processes, including ni time. Usually we only look at this item.

        sy (system time)  indicates the CPU running time in the kernel, including IRQ and softirq time. A high system CPU usage indicates that some part of the system has a bottleneck. Usually, the lower the value, the better.

        wa (waiting time)  the time the CPU spends waiting for I/O operations to complete. The system should not spend a large amount of time waiting for I/O operations; otherwise, it indicates that there is a problem with I/O.

        id (idle time)  the system is in an idle period, waiting for processes to run.

        ni (nice time)  the time the system spends adjusting process priorities.

        hi (hard irq time)  the time the system spends handling hard interrupts.

        si (softirq time)  the time the system spends handling software interrupts.

        st (steal time)  the time forced to wait for a virtual CPU, when the hypervisor is serving another virtual processor.


2. Query processes occupying CPU

    Continue checking with the top command:

    image.png

    You can clearly see that the php-fpm process occupies a lot of resources. php-fpm, mysql, httpd, nginx, etc. are all site web processes. If there is a large amount of resource occupation, it usually indicates that website traffic is large. At this time, you can enable the website access log for analysis. Generally speaking, a website with a large access log also occupies more resources. You need to open the log for specific inspection and analysis to see whether it is caused by a large amount of traffic itself or by abnormal access (e.g., massive crawling by a certain type of spider, abnormal execution of a connection by a certain program, being subjected to cc attacks, etc.).