Performance Tuning-Analyzing Hardware Bottlenecks-CPU
Tags: opengauss
Published: 2023-04-24 08:02:50

You can run the top command to check the CPU usage of each node in openGauss and analyze whether performance bottleneck caused by heavy CPU load exists. The **top** command is used to monitor the Linux OS status. It is a common performance analysis tool and can display the resource usage of each process in the system in real time.

Description

**·** **-d**: number of seconds, indicating the interval for updating the page displayed by running the **top** command. The default value is 5 seconds.
**·** **-b**: executes the **top** command in batches.
**·** **-n**: This parameter is used together with **-b** to indicate the number of times that the **top** command is executed.
**·** **-p**: specifies a PID for observation.

**Checking CPU Usage**
You can query the CPU usage of the server in the following ways:

On each storage node, run the **top** command to check the CPU usage. Then, press **1** to view the usage of each CPU core.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yl9aodvkc2jnjht5qvhi.png)

In the command output, focus on the CPU usage occupied by each process.

**us** indicates the CPU percentage occupied by the user space, **sy** indicates the CPU percentage occupied by the kernel space, and **id** indicates the idle CPU percentage. If **id** is less than 10%, the CPU load is high. In this case, you can reduce the CPU load by reducing the number of tasks on nodes.

**Analyzing Performance Parameters**
1. Run the top-H command to check the CPU usage. The following is displayed:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ks3k7upcijsoq7kpuwh.png)

2. In the query result for Cpu(s), check whether the system CPU (sy) or user CPU (us) usage is high.
2.1 If the system CPU usage is too high, you need to identify the abnormal system processes and handle them.
2.2 If the CPU usage of the openGauss process whose **USER** is **omm** is too high, optimize the service-related SQL statements based on the running services queries. Based on the features of the currently running service, perform the following operations to check whether this process containing infinite loop logics.
2.1.1 Run the **top -H -p pid **command to identify the threads that use much CPU in the process.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/937epd1ki6d1i6e24ysv.png)

The threads causing high CPU usage are displayed in the **top** column of the command output. In this section, thread **54775** is used as an example for analyzing the causes of the high CPU usage.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3q8e78rwiucviwhi81r.png)

2.2.2 Run the following command to view the function invocation stack for each thread in the process. Check the thread number for the ID of the thread that occupies high CPU usage in the last step.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1u9glsd1gcn46v43xlz.png)

The query result is as follows. The thread number for the thread ID **54775** is **10**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rsohst1me5ex2f6z5hmp.png)