Operations 10 min read

Master Linux ‘top’: Real‑Time Process Monitoring and Performance Tuning

This guide explains how to use the Linux top command to monitor real‑time process activity, interpret CPU, memory and swap statistics, customize displayed columns, apply command‑line options, and understand key metrics such as load average and steal time for effective system performance management.

Efficient Ops
Efficient Ops
Efficient Ops
Master Linux ‘top’: Real‑Time Process Monitoring and Performance Tuning

Overview

top is a dynamic real‑time process monitor for Linux, similar to Windows Task Manager.

Statistical Information Area

The first five lines display system‑wide statistics, including uptime, load average, task counts, CPU usage breakdown, and memory and swap usage.

<code>Cpu(s):
us   用户空间占用CPU百分比
sy   内核空间占用CPU百分比
ni   用户进程空间内改变过优先级的进程占用CPU百分比
id   空闲CPU百分比
wa   等待输入输出的CPU时间百分比
hi   硬中断(Hardware IRQ)占用CPU的百分比
si   软中断(Software Interrupts)占用CPU的百分比
st   (Steal time) 是当 hypervisor 服务另一个虚拟处理器的时候,虚拟 CPU 等待实际 CPU 的时间的百分比。</code>
<code>Mem:
total   物理内存总量
free   空闲内存总量
used   使用的物理内存总量
buff/cache   用作内核缓存的内存量</code>
<code>Swap:
total   交换区总量
free   空闲交换区总量
used   使用的交换区总量
avail Mem 缓冲的交换区总量</code>

Process Information Area

Below the statistics, each process is listed with columns such as PID, USER, PR, NI, VIRT, RES, %CPU, %MEM, TIME+, COMMAND, etc.

<code>PID 进程id
PPID 父进程id
RUSER Real user name
d UID 进程所有者的用户id
e USER 进程所有者的用户名
f GROUP 进程所有者的组名
g TTY 启动进程的终端名。不是从终端启动的进程则显示为 ?
h PR 优先级
i NI nice值。负值表示高优先级,正值表示低优先级
j P 最后使用的CPU,仅在多CPU环境下有意义
k %CPU 上次更新到现在的CPU时间占用百分比
l TIME 进程使用的CPU时间总计,单位秒
m TIME+ 进程使用的CPU时间总计,单位1/100秒
n %MEM 进程使用的物理内存百分比
o VIRT 进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES
p SWAP 进程使用的虚拟内存中,被换出的大小,单位kb。
q RES 进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA
r CODE 可执行代码占用的物理内存大小,单位kb
s DATA 可执行代码以外的部分(数据段+栈)占用的物理内存大小,单位kb
t SHR 共享内存大小,单位kb
u nFLT 页面错误次数
v nDRT 最后一次写入到现在,被修改过的页面数。
w S 进程状态。
D=不可中断的睡眠状态
R=运行
S=睡眠
T=跟踪/停止
Z=僵尸进程
x COMMAND 命令名/命令行
y WCHAN 若该进程在睡眠,则显示睡眠中的系统函数名
z Flags 任务标志,参考 sched.h</code>

By default only the most important columns are shown; press

f

to toggle columns,

o

to reorder them, and

F

to select the sort column.

top Command Options

<code>top [-] [d] [p] [q] [C] [S] [s] [n]
-d 后跟秒数,指定每两次屏幕信息刷新之间的时间间隔,表示进程界面更新时间(默认为5秒)。如:将top进程表在/tmp/top.txt中打印2次。
top -b -n 2 > /tmp/top.txt
-p 进程ID 如:查看进程号为1的进程。top -p 1
-q 该选项将使top没有任何延迟的进行刷新。如果调用程序有超级用户权限,那么top将以尽可能高的优先级运行。
-S 指定累计模式
-s 使top命令在安全模式中运行。这将去除交互命令所带来的潜在危险。
-i 使top不显示任何闲置或者僵死进程。
-c 显示整个命令行而不只是显示命令名</code>

Common Operations

Capture output every 2.5 seconds for five iterations:

top -b -d 2.5 -n 5 > performance.txt

Sort by %CPU, %MEM, or TIME+ using capital keys

P

,

M

,

T

.

Change sorting order to ascending with

R

.

Show/hide columns with

f

, reorder with

o

, and select sort column with

F

.

CPU Steal Time

Steal time (%st) indicates the fraction of CPU cycles taken by the hypervisor for other virtual machines; a high value signals severe performance degradation in virtualized environments.

operationslinuxcpumemorysystem performancetop commandProcess Monitoring
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.