top命令是linux下常用的性能分析工具,也是一个强大的系统检测工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器。本次学习主要参考man top
语法:top -hv | -bcisSH -d delay -n iterations [-u user | -U user] -p pid [,pid ...]
启动默认参数值:(带*的参数是可以修改的)
Global_defaults
'A' - Alt display Off (full-screen)
* 'd' - Delay time 3.0 seconds
'I' - Irix mode On (no, 'solaris' smp)
* 'p' - PID monitoring Off
* 's' - Secure mode Off (unsecured)
'B' - Bold enable Off
Summary_Area_defaults
'l' - Load Avg/Uptime On (thus program name)
't' - Task/Cpu states On (1+1 lines, see '1')
'm' - Mem/Swap usage On (2 lines worth)
'1' - Single Cpu On (thus 1 line if smp)
Task_Area_defaults
'b' - Bold hilite On (not 'reverse')
* 'c' - Command line Off (name, not cmdline)
* 'H' - Threads Off (show all threads)
* 'i' - Idle tasks On (show all tasks)
'R' - Reverse sort On (pids high-to-low)
* 'S' - Cumulative time Off (no, dead children)
'x' - Column hilite Off (no, sort field)
'y' - Row hilite On (yes, running tasks)
'z' - color/mono Off (no, colors)
参数说明:
-b : Batch mode operation批处理模式操作;使用批处理模式启动top,可以从top中发送有用的文件到其他程序或文件中,在这种模式下,top命令将不接受任何输入与运行,直到该命令终止运行;
-c : Command line/Program name toggle显示进程完成的路径与名称
-d : Delay time interval as: -d ss.tt (seconds.tenths)指定更新的间隔,以秒计算
-h : Help
-H : Threads toggle是否显示线程,为on时将显示所有线程
-i : Idle Processes toggle是否显示任何闲置的或无用的进程,默认为显示
-n : Number of iterations limit as: -n number显示更新的次数
-u : Monitor by user as: -u somebody监视某有效uid用户的进程
-U : Monitor by user as: -U somebody监视匹配real, effective, saved, and filesystem UID的进程
-p : Monitor PIDs as: -pN1 -pN2 ... or -pN1, N2 [,...]监视指定PID的进程,可以最多指定20个进程;当要恢复显示所有的进程时,只需要执行交互式命令等号=就可以
-s : Secure mode operation安全模式操作
-S : Cumulative time mode toggle累积模式,会将已完成或消失的子进程的cpu时间累积起来
-v : Version显示版本
top命令显示的描述:
第一行显示的依次为当前时间、系统启动时件、当前系统登录用户数目、平均负载;
第二行显示的是所有启动的进程、目前运行的、挂起的和无用的进程;
第三行显示的是目前CPU的使用情况,包括系统占用的比例、用户使用比例、闲置比例;具体表现为:us用户进程使用CPU的时间;sy系统进程使用CPU的时间;ni好的进程使用CPU的时间;id闲置进程占用cpu的时间;wa等待输入输出结束的CPU时间;hi服务于硬件中断的CPU时间;si使用于软件中断的CPU时间;st运行于虚拟机等的CPU时间;
第四行显示的是物理内存的使用情况,包括总的可以使用的内存、易用内存、空闲内存、缓冲区占用的内存;
第五行显示的是交换分区使用情况,包括总的交换分区、使用的、空闲的和用于高速缓存的大小;
第六行显示的项目最多,以下是详细解释:
a: PID -- Process Id进程标识号
b: PPID -- Parent Process Pid父进程标识号
c: RUSER -- Real User Name进程的真实所有者的用户名
d: UID -- User Id用户标识号
e: USER -- User Name进程的有效所有者的用户名
f: GROUP -- Group Name
The effective group name of the task's owner.
g: TTY -- Controlling Tty
The name of the controlling terminal. This is usually the device (serial port, pty, etc.) from which the process was started, and
which it uses for input or output. However, a task need not be associated with a terminal, in which case you'll see '?' isplayed.
h: PR -- Priority进程的优先级别
i: NI -- Nice value进程的优先级别数值
j: P -- Last used CPU (SMP)
A number representing the last used processor. In a true SMP environment this will likely change frequently since the kernel intentionally uses weak affinity. Also, the very act of running top may break this weak affinity and cause more processes to change CPUs more often (because of the extra demand for cpu time).
k: %CPU -- CPU usage进程占用的CPU使用率
The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time. In a true SMP environment, if 'Irix mode' is Off, top will operate in 'Solaris mode' where a task's cpu usage will be divided by the total number of CPUs. You toggle 'Irix/Solaris' modes with the 'I' interactive command.
l: TIME -- CPU Time
Total CPU time the task has used since it started. When 'Cumulative mode' is On, each process is listed with the cpu time that it and its dead children has used. You toggle 'Cumulative mode' with 'S', which is a command-line option and an interactive command.
See the 'S' interactive command for additional information regarding this mode.
m: TIME+ -- CPU Time, hundredths该进程启动后占用的总得CPU时间
n: %MEM -- Memory usage (RES)该进程占用的物理内存和总内存的百分比
o: VIRT -- Virtual Image (kb)进程占用的虚拟内存值
VIRT = SWAP + RES.
p: SWAP -- Swapped size (kb)
The swapped out portion of a task's total virtual memory image.
q: RES -- Resident size (kb)
The non-swapped physical memory a task has used.
r: CODE -- Code size (kb)
The amount of virtual memory devoted to executable code, also known as the 'text resident set' size or TRS.
s: DATA -- Data+Stack size (kb)
The amount of virtual memory devoted to other than executable code, also known as the 'data resident set' size or DRS.
t: SHR -- Shared Mem size (kb)
The amount of shared memory used by a task. It simply reflects mem- ory that could be potentially shared with other processes.
u: nFLT -- Page Fault count
The number of major page faults that have occurred for a task. A page fault occurs when a process attempts to read from or write to a virtual page that is not currently present in its address space. A major page fault is when backing storage access (such as a disk) is involved in making that page available.
v: nDRT -- Dirty Pages count
The number of pages that have been modified since they were last written to disk. Dirty pages must be written to disk before the corresponding physical memory location can be used for some other virtual page.
w: S -- Process Status进程状态
The status of the task which can be one of:
'D' = uninterruptible sleep
'R' = running正在运行
'S' = sleeping休眠
'T' = traced or stopped
'Z' = zombie僵死状态
x: Command -- Command line or Program name进程启动的启动命令名称,如果这一行显示不下,进程会有一个完成的命令行
y: WCHAN -- Sleeping in Function
Depending on the availability of the kernel link map ('System.map'), this field will show the name or the address of the kernel function in which the task is currently sleeping. Running tasks will display a dash ('-') in this column.
Note: By displaying this field, top's own working set will be increased by over 700Kb. Your only means of reducing that
overhead will be to stop and restart top.
z: Flags -- Task Flags
This column represents the task's current scheduling flags which are expressed in hexadecimal notation and with zeros suppressed. These flags are officially documented in . Less formal documentation can also be found on the 'Fields select' and 'Order fields' screens.
top运行中可以通过top的内部命令对进程的显示方式进行控制:
s - 改变画面更新频率;
l - 关闭或开启第一部分第一行 top 信息的表示;
t - 关闭或开启第一部分第二行 Tasks 和第三行 Cpus 信息的表示;
m - 关闭或开启第一部分第四行 Mem 和 第五行 Swap 信息的表示;
N - 以 PID 的大小的顺序排列表示进程列表(第三部分后述);
P - 以 CPU 占用率大小的顺序排列进程列表
M - 以内存占用率大小的顺序排列进程列表 (第三部分后述);
h - 显示帮助;
n - 设置在进程列表所显示进程的数量;
q - 退出 top;
s - 改变画面更新周期
T - 根据时间、累积时间排序
c- 切换显示命令名称和完成命令行
W- 将当前设置写入~/.toprc文件中,这是写top配置文件的推荐方式