Rate This Document
Findability
Accuracy
Completeness
Readability

top

Introduction

top is one of the most commonly used Linux performance monitoring tools. You can use the top tool to monitor the process and system performance.

Command examples:

Command

Description

top

Checks the overall CPU and memory usage of the system.

Run the top command and enter 1.

Checks the usage of each CPU core.

Run the top command, enter F, and select P.

Checks whether other CPU cores are scheduled during thread execution.

top -p $PID -H

Checks the CPU usage of all threads in a process.

Installation Method

It is built in the system and requires no installation.

How to Use

  1. Run the top command to collect statistics on the overall CPU and memory usage.

    • CPU: Displays the total CPU time usage distribution.
      • us indicates the percentage of CPU time occupied by user-mode programs.
      • sy indicates the percentage of CPU time occupied by kernel-mode programs.
      • wa indicates the percentage of the CPU time used for waiting for I/Os.
      • hi indicates the percentage of CPU time occupied by hardware interrupts.
      • si indicates the percentage of CPU time occupied by software interrupts.

        Based on these parameters, you can analyze the CPU time distribution and check whether there is much I/O waiting time. After the tuning is complete, you can compare the CPU times before and after the tuning. If the CPU time decreases when the same program and service are running, the performance is improved.

    • KiB Mem indicates the size of the server's total memory and its usage.
    • KiB Swap indicates the size of the swap space in use. The swap space is used to virtualize some drive space into memory when the memory is insufficient. If the swap space in use is greater than 0, you can optimize the memory usage of applications or increase the physical memory.
  2. After running the top command, press 1 to view the usage of each CPU core.

    You can run this command to check the usage of a single CPU core. If the CPU usage is concentrated on several CPU cores, you can analyze the cause based on services to find the tuning method.

  3. Select the P option of the top command to view the CPU cores on which the threads run.

    After running the top command, press F to go to the top command management page. Press the up or down arrow keys to move the cursor to the P option, press the space bar to select P, and press Esc to exit. The CPU cores on which the threads are displayed. Observe for a period of time. If the service threads run on CPU cores of different NUMA nodes, a large number of cross-NUMA accesses exist. In this case, you can optimize the performance by binding NUMA cores.

  4. Run the top -p $PID -H command to check the CPU usage of each thread in the process.

    The parameter following -p is the ID of the process to be observed. You can run this command to find the threads that consume a large number of resources. Then, you can analyze the hotspot functions and invoking process in a thread based on the thread ID.