做为一个经常来去linux 系统之上的管理员来说,top 再熟悉不过了,可是每项都代表了什么?
相信大多数人都犯了个懒,只看了自己关心的那个,当然我也不例外,今天写了这个笔记也只是写了部分经常用到选项,欢迎大家吐槽:
先看一下top显示了什么
显示当前包括负载、进程数目、CPU 和 内存使用量等,并列出了cpu使用率,以下是我觉得比较好用的几个常用参数
-c 显示完整的进程全路径命令
-u<用户名> 指定查看某一个用户启动的进程
-p<进程号> 指定进程号查看
top 交互模式下命令参数
? 或 h - 显示说明画面
q - 离开 top
M - 进程依内存使用率 (%MEM) 由大至小排列
T - 进程依总 CPU 时间 (TIME+) 由大至小排列
k - 并输入pid号,终止某个进程
r - 改变进程 nice 值,影响进程的优先序。
Space或Enter - 更新并重新绘划整个画面。
其他man的合作方法就得麻烦大家自行man top 或google了,以下给出一部分:
Global-defaults
A - Alt display Off (full-screen)
* d - Delay time 1.5 seconds
* H - Threads mode Off (summarize as tasks)
I - Irix mode On (no, `solaris' smp)
* p - PID monitoring Off (show all processes)
* s - Secure mode Off (unsecured)
B - Bold enable On (yes, bold globally)
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 Off (thus multiple cpus)
Task-Area-defaults
b - Bold hilite Off (use `reverse')
* c - Command line Off (name, not cmdline)
* i - Idle tasks On (show all tasks)
J - Num align right On (not left justify)
j - Str align right Off (not right justify)
R - Reverse sort On (pids high-to-low)
* S - Cumulative time Off (no, dead children)
* u - User filter Off (show euid only)
* U - User filter Off (show any uid)
V - Forest view On (show as branches)
x - Column hilite Off (no, sort field)
y - Row hilite On (yes, running tasks)
z - color/mono On (show colors)
The command-line syntax for top consists of:
-hv|-bcEHiOSs1 -d secs -n max -u|U user -p pid -o fld -w [cols]
The typically mandatory switch (`-') and even whitespace are
completely optional.
-h | -v :Help/Version
Show library version and the usage prompt, then quit.
-b :Batch-mode operation
Starts top in Batch mode, which could be useful for sending
output from top to other programs or to a file. In this mode,
top will not accept input and runs until the iterations limit
you've set with the `-n' command-line option or until killed.
-c :Command-line/Program-name toggle
Starts top with the last remembered `c' state reversed. Thus,
if top was displaying command lines, now that field will show
program names, and vice versa. See the `c' interactive command
for additional information.
-d :Delay-time interval as: -d ss.t (secs.tenths)
Specifies the delay between screen updates, and overrides the
corresponding value in one's personal configuration file or the
startup default. Later this can be changed with the `d' or `s'
interactive commands.
Fractional seconds are honored, but a negative number is not
allowed. In all cases, however, such changes are prohibited if
top is running in Secure mode, except for root (unless the `s'
command-line option was used). For additional information on
Secure mode see topic 6a. SYSTEM Configuration File.
-H :Threads-mode operation
Instructs top to display individual threads. Without this
command-line option a summation of all threads in each process
is shown. Later this can be changed with the `H' interactive
command.
-i :Idle-process toggle
Starts top with the last remembered `i' state reversed. When
this toggle is Off, tasks that have not used any CPU since the
last update will not be displayed. For additional information
regarding this toggle see topic 4c. TASK AREA Commands, SIZE.
-E :Extend-Memory-Scaling as: -E k | m | g | t | p | e
Instructs top to force summary area memory to be scaled as:
k - kibibytes
m - mebibytes
g - gibibytes
t - tebibytes
p - pebibytes
e - exbibytes
Later this can be changed with the `E' command toggle.
-n :Number-of-iterations limit as: -n number
Specifies the maximum number of iterations, or frames, top
should produce before ending.
-o :Override-sort-field as: -o fieldname
Specifies the name of the field on which tasks will be sorted,
independent of what is reflected in the configuration file. You
can prepend a `+' or `-' to the field name to also override the
sort direction. A leading `+' will force sorting high to low,
whereas a `-' will ensure a low to high ordering.
This option exists primarily to support automated/scripted batch
mode operation.
-O :Output-field-names
This option acts as a form of help for the above -o option. It
will cause top to print each of the available field names on a
separate line, then quit. Such names are subject to nls
translation.
-p :Monitor-PIDs mode as: -pN1 -pN2 ... or -pN1,N2,N3 ...
Monitor only processes with specified process IDs. This option
can be given up to 20 times, or you can provide a comma
delimited list with up to 20 pids. Co-mingling both approaches
is permitted.
A pid value of zero will be treated as the process id of the top
program itself once it is running.
This is a command-line option only and should you wish to return
to normal operation, it is not necessary to quit and restart top
-- just issue any of these interactive commands: `=', `u' or
`U'.
The `p', `u' and `U' command-line options are mutually
exclusive.
-s :Secure-mode operation
Starts top with secure mode forced, even for root. This mode is
far better controlled through the system configuration file (see
topic 6. FILES).
-S :Cumulative-time toggle
Starts top with the last remembered `S' state reversed. When
Cumulative time mode is On, each process is listed with the cpu
time that it and its dead children have used. See the `S'
interactive command for additional information regarding this
mode.
-u | -U :User-filter-mode as: -u | -U number or name
Display only processes with a user id or user name matching that
given. The `-u' option matches on effective user whereas the
`-U' option matches on any user (real, effective, saved, or
filesystem).
Prepending an exclamation point (`!') to the user id or name
instructs top to display only processes with users not matching
the one provided.
The `p', `u' and `U' command-line options are mutually
exclusive.
-w :Output-width-override as: -w [ number ]
In Batch mode, when used without an argument top will format
output using the COLUMNS= and LINES= environment variables, if
set. Otherwise, width will be fixed at the maximum 512 columns.
With an argument, output width can be decreased or increased (up
to 512) but the number of rows is considered unlimited.
In normal display mode, when used without an argument top will
attempt to format output using the COLUMNS= and LINES=
environment variables, if set. With an argument, output width
can only be decreased, not increased. Whether using environment
variables or an argument with -w, when not in Batch mode actual
terminal dimensions can never be exceeded.
Note: Without the use of this command-line option, output width
is always based on the terminal at which top was invoked whether
or not in Batch mode.
-1 :Single/Separate-Cpu-States toggle
Starts top with the last remembered Cpu States portion of the
summary area reversed. Either all cpu information will be
displayed in a single line or each cpu will be displayed
separately, depending on the state of the NUMA Node command
toggle ('2').
See the `1' and '2' interactive commands for additional
information.
下面说一下top 显示的几个区域:概要区,讯息/提示列,任务区。
概要区:系统运作时间及平均负载
显示现在时间、系统运行时间 (up time)、使用者数目 (users) 和平均负载 (load average)。可以按 【l】 键切换是否显示。
平均负载的三个数值分别为在平均过去 1 分钟、5 分钟和 15 分钟在可运行或不可中断状态的进程数目(也可通过 cat /proc/loadavg 取得平均负载)。平均负载为 1.0 表示一个 CPU 被占用所有时间。[2]如果计算机有多个 CPU,平均负载的参考值亦会倍数增长。例如一个双处理器双核的计算机,所有 CPU 所有时间被完全占用时的平均负载应该为 1.0 x 2 x 2 = 4.0。详情可参阅《显示系统平均负载》。
任务资讯:
显示任务 (task) 资讯,包括任务总数、运行中 (running)、睡眠中 (sleeping)、停止了 (stopped) 和不能运作 (zombie) 的进程数目。可以按 【t】 键切换是否和下一列 CPU 状态列一同显示。
CPU使用率:
us (user) : time running un-niced user processes; 使用者空间 (user space) 占用 CPU 百分比。此数值主要受较少 I/O 等系统呼叫的进程所影响。
sy (system): time running kernel processes; 核心空间 (kernel space) 占用 CPU 百分比。一般在系统要处理大量 IRQ(中断) 或任务排程时,此值会较高。
ni (nice) : time running niced user processes; nice 值比一般值 0 大 (优先序较低) 的进程占用 CPU 百分比
id (idle) : time spent in the kernel idle handler; CPU 空闲时间百分比
wa (iowait): time waiting for I/O completion; CPU 等待输入/输出的百分比。当此值过高 (如超过 30%),表示系统的储存或网络 I/O 效能有问题。
hi (H/W Interrupt): time spent servicing hardware interrupts; CPU 处理硬件中断时间的百分比。除光驱不断检查是否有光盘外,一般此值不会太高。
si (S/W Interrupt): time spent servicing software interrupts; CPU 处理软件中断时间的百分比,此值很少机会会太高。
st: (Steal): time stolen from this vm by the hypervisor; 在如 Xen 等虚拟环境下 CPU 运作虚拟机器时间的百分比。太高表示可能需要停止一些虚拟机器
可以按t键切换是否和上一列任务资讯一同显示。在多处理器 (multi-processor) 或多核 (multi-core) 系统上,您亦可以按 1 键切换使用一列显示整体 CPU 使用状态还是每个 CPU 使用一列显示其状态。
内存和置换空间使用率:
Mem : 物理内存总量(total)、使用中的内存总量(used)、空闲的内存总量(free)、缓存的内存量(buffers)
Swap: swap交互总量(swap)、使用中的交换区总量(used)、空闲交换区总量(free)、缓冲的交换区总量(cached)
buffers (缓冲区) 表示被使用作磁盘写入快取 (write cache) 的内存大小。当有磁盘控制卡有时间或系统内存不够时,自然会把 buffers 上的资料清出 (flush) 至磁盘并释放有关内存。过大 buffers (超过几百 MB) 一般表示储存子系统有问题,令 buffers 上的资料无法清出。
cache (快取) 则表示被使用作档案读取快取 (read cache) 的内存大小。同样在统内存不够时,部份 cache 使用的内存会被释放。当系统正进行大量读取工作,此值效高是很自然和良好的现像。但如果此值在有大量读取工作时过低 (少于总内存大小的四成),这表示系统到了需要增加内存的时候。
对大部份服务器工作,置换空间 (swap) 被使用一般都会拖慢整个系统。除 Oracle 等本身会尽用置换空间的服务外,一般服务器在开始使用超过几 MB 的置换空间时都表示需要进行一些微调工作。
那么buffer 与 cache 有什么区别呢?
buffers是用来给块设备做的缓冲大小,他只记录文件系统的metadata以及 tracking in-flight pages。
cached用来给文件做缓冲。
即:buffers是用来存储目录里面有什么内容,权限等等,而cached用来记忆我们打开的文件
Cache:高速缓存,是位于CPU与主内存间的一种容量较小但速度很高的存储器。由于CPU的速度远高于主内存,CPU直接从内存中存取数据要等待一定时间周期,Cache中保存着CPU刚用过或循环使用的一部分数据,当CPU再次使用该部分数据时可从Cache中直接调用,这样就减少了CPU的等待时间,提高了系统的效率。Cache又分为一级Cache(L1 Cache)和二级Cache(L2 Cache),L1 Cache集成在CPU内部,L2 Cache早期一般是焊在主板上,现在也都集成在CPU内部,常见的容量有256KB或512KB L2 Cache。
Buffer:缓冲区,一个用于存储速度不同步的设备或优先级不同的设备之间传输数据的区域。通过缓冲区,可以使进程之间的相互等待变少,从而使从速度慢的设备读入数据时,速度快的设备的操作进程不发生间断。
Free中的buffer和cache:(它们都是占用内存):
buffer : 作为buffer cache的内存,是块设备的读写缓冲区
cache: 作为page cache的内存, 文件系统的cache
如果 cache 的值很大,说明cache住的文件数很多。如果频繁访问到的文件都能被cache住,那么磁盘的读IO bi会非常小。
下面说一下任务区:
PID (Process ID) - 进程的识别码
USER (Effective user name) - 进程的有效使用者名称,决定了进程的使用权限,一般是执行进程的使用者,但 suid 进程则会变成程序档案的拥有者。
PR (priority) - 进程的优选值,数值越小表示进程的优先序越高。
NI (nice) - 进程的 nice 值,会影响进程的优选值 (priority),可以为 20 至 -19 任何一个整数,数值越小会令进程的优先序越高。
VIRT (Virtual memory) - 进程使用的总虚拟内存 (virtual memory) 大小,包括进程的程序码、资料和共享程序库再加上被置换 (swap out) 的空间,单位为 KiB。VIRT = SWAP + RES.
RES (Resident size) - 进程非被置换的实体内存大小,单位为 KiB。RES = CODE + DATA
SHR (Shared Mem size) - 进程使用的共享内存大小,即可以和其他进程共享的内存空间,单位为 KiB。
S (Stats) - 进程的状态,可以为:
D (uninterruptible sleep) - 在不可中断的休眠中 (一般为正在进行输入/输出)
R (running) - 在运行中或可以被运行 (即在运行序列 run queue)
S (sleeping) - 在可以被中断的休眠中 (一般是正在等待某事件完结)
T (traced or stopped) - 已被停止。因工作控制讯号 (job control signal) 或进程在被追踪中。
Z (Zombie) - 不能运作的进程,即所谓 zombie 进程。一般因为已终止但未能被其母进程成功接收的进程。
%CPU (CPU usage) - 由上次 top 画面更新资料后进程的 CPU 使用率。为进程占用的 CPU 时间除以实际时间 (CPU 时间 / 实际时间),以百分比显示。显示小数点后一个位。可以按 I 键切换 Irix 模式 (一般计算方法) 和 Solaris 模式 (一般的 CPU 使用率再除系统的处理器数目) 改变此栏显示的数值。
%MEM (Memory usage) - 进程占用实体内存大小对系统总实体内存大小的比例,以百分比显示。
TIME+ (CPU Time) - 由进程被启动至今的总 CPU 时间。可以按 S 键切换是否开启累计模式 (Cumulative mode) 改变此栏显示的数值。
COMMAND (Command line or Program name) - 启动进程的命令,可以按 c 键切换只显示程序名称或整个连参数的命令行。