您的位置:首页 > 运维架构 > Linux

Linux 命令 top 学习总结

2011-06-27 15:26 579 查看
本文简介
概要: 学习总结 Linux 下的 top 命令
版本: Debian 5(Lenny), top: procps version 3.2.7
日期: 2010-11-17
永久链接: http://sleepycat.org/linux/linuxcommand/top.html

I. 概述
学习总结 top 命令。主要学习自 man 手册。
Linux 下 top 命令:
# top
top - 00:34:31 up 169 days, 6:05, 16 users, load average: 0.01, 0.01, 0.00
Tasks: 97 total, 1 running, 96 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0%us, 0.2%sy, 0.0%ni, 99.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 126396k total, 123312k used, 3084k free, 40908k buffers
Swap: 369452k total, 5912k used, 363540k free, 18244k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1993 root 20 0 8668 2584 2468 S 1 2.0 131:31.81 nmbd
15774 hw 20 0 2388 1116 876 R 1 0.9 0:00.08 top
1 root 20 0 1980 604 520 S 0 0.5 5:49.12 init
2 root 15 -5 0 0 0 S 0 0.0 0:00.00 kthreadd
3 root RT -5 0 0 0 S 0 0.0 0:12.78 migration/0
4 root 15 -5 0 0 0 S 0 0.0 1:57.97 ksoftirqd/0
5 root RT -5 0 0 0 S 0 0.0 0:00.00 watchdog/0
6 root RT -5 0 0 0 S 0 0.0 0:04.78 migration/1
7 root 15 -5 0 0 0 S 0 0.0 2:29.45 ksoftirqd/1
8 root RT -5 0 0 0 S 0 0.0 0:00.10 watchdog/1
9 root 15 -5 0 0 0 S 0 0.0 11:29.26 events/0
10 root 15 -5 0 0 0 S 0 0.0 11:50.89 events/1
11 root 15 -5 0 0 0 S 0 0.0 0:00.02 khelper
44 root 15 -5 0 0 0 S 0 0.0 2:37.22 kblockd/0
45 root 15 -5 0 0 0 S 0 0.0 1:51.64 kblockd/1
以下是详细解释。

II. 第一行, 系统信息.
top - 01:33:08 up 169 days, 7:03, 16 users, load average: 0.07, 0.02, 0.00
- 01:33:08
当前时间 01:33:08
up 169 days, 7:03
系统总运行时间: 169天7小时3分钟
16 users
当前登陆的用户数: 16
load average
系统平均负载.
分别说明上一分钟、最后五分钟以及最后十五分钟的系统负载均值
在多处理器系统中,负载均值是基于内核的数量决定的。
以 100% 负载计算,1.00 表示单个处理器,而 2.00 则说明有两个双处理器,那么 4.00 就说明主机具有
四个处理器。
也就是说,对双核处理器,值为2.00则表示100%负载。
关于 load average 的详细解释的英文出处及中文翻译:
http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages
http://doc.chinaunix.net/linux/201004/507455.shtml

III. 第二行, CPU信息.
Cpu(s): 0.0%us, 0.2%sy, 0.0%ni, 99.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
点数字键1可以展开或合上多个CPU, 如下:
Cpu0 : 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 0.0%us, 0.3%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
各个参数值显示的都是百分比。
us -- User CPU time
CPU花在未调整优先级的(not niced)用户进程的时间。
The time the CPU has spent running users’ processes that are not niced.
注: nice 命令: 调整程序运行的优先级(范围 -20 到 19,越大,优先级越高)
sy -- System CPU time
CPU花在内核及其进程上的时间。
The time the CPU has spent running the kernel and its processes.
ni -- Nice CPU time CPU花在调整过优先级的(niced)用户进程的时间。
The time the CPU has spent running users’ proccess that have been niced.
wa -- iowait
等待I/O结束的CPU时间。
Amount of time the CPU has been waiting for I/O to complete.
hi -- Hardware IRQ
CPU花在硬件中断的时间。
The amount of time the CPU has been servicing hardware interrupts.
si -- Software Interrupts
CPU花在软件中断的时间。
The amount of time the CPU has been servicing software interrupts.
st -- Steal Time
The amount of CPU ’stolen’ from this virtual machine by the hypervisor for other tasks (such
as running another virtual machine).

IV. 内存信息
Mem: 126396k total, 123312k used, 3084k free, 40908k buffers
Swap: 369452k total, 5912k used, 363540k free, 18244k cached
显示物理内存与交换内存使用情况。
先详细了解下 free 命令:
tester@debian01:~$ free -m
total used free shared buffers cached
Mem: 60 56 3 0 3 35
-/+ buffers/cache: 17 42
Swap: 172 0 171

系统内存信息(单位为Mb)详解:

Mem(表示物理内存使用概况):
Mem: total = 60, 表示总共物理内存为 60M (其实是 64M 的内存, 单位转换造成误差) (total = used + free, 即: 60 = 56 + 3; 因为省略了小数位, 可能存在误差)
Mem: used = 56, 表示总共已经使用了 56M (这个数值是从内核的角度来看的. 也就是说, 所有 buffers 和 cached 所占用的内存也都被当作已经使用. 其实这些部分还是可以分给应用程序的)
Mem: free = 3, 表示目前空闲 3M. (同样, 这个数值是从内核的角度来看的. 也就是说, 所有 buffers 和 cached 所占用的内存也都被当作已经使用)
Mem: shared = 0, 表示应用程序的共享内存(这一块还不是很理解)
Mem: buffers = 2, 表示 buffers 缓冲使用了 2M (块设备的读写缓冲区)
Mem: cached = 35, 表示 cache 缓冲使用了 35M (文件系统的cache)

-/+ buffers/cache(表示如果将 buffers 和 cached 的内存看作可用的话, 内存使用情况):
-/+ buffers/cache: used = 17, 表示不考虑 buffers 和 cached 的内存, 系统实际使用了 17 M. (56 - 3 - 35, 误差来源于省略掉的小数位)
-/+ buffers/cache: free = 42, 表示不考虑 buffers 和 cached 的内存, 系统实际空闲了 42 M. (3 + 3 + 35, 误差来源于省略掉的小数位) (注: 这一组值很重要, 可以作为系统空闲情况的重要参考)

Swap(表示交换内存使用概况):
"total/used/free" 分别表示 "总共/已使用/空闲" 状况.

而 top 命令中的内存信息, 只是将 -/+ buffers/cache 省掉(其实我们可以自己算), 并将 cached 挪到了下一行而已.

V. 各列名称解释
PID
进程ID(Process Id)
USER
任务所有者(The effective user name of the task’s owner)
PR
优先级(Priority)
NI
优先值(Nice value)
任务的优先值。正值表示高优先级,负值表示低优先级(范围 -20 到 19)。值为零则表示不会调整任务分配
的优先级。
The nice value of the task. A negative nice value means higher pri‐ority, whereas a positive nice value means lower priority.
Zero in this field simply means priority will not be adjusted in determining a task’s dispatchability.
VIRT
虚拟内存, 单位: Kb. [Virtual Image (kb)]
任务所占用的虚拟内存。包含了所有的代码、数据、共享库及交换页面。
The total amount of virtual memory used by the task.
It includes all code, data and shared libraries plus pages that have been swapped out.
RES
物理内存, 单位: Kb. [Resident size (kb)]
任务所占用的非交换物理内存。
The non-swapped physical memory a task has used.
注: 这一组内存信息最重要, %MEM 所显示的百分比就是由此列的值得出.
SHR
共享内存, 单位: Kb. [Shared Mem size (kb)]
任务所占用的共享内存。它反映了可以与其他进程共享的内存的大小。
The amount of shared memory used by a task.
It simply reflects memory that could be potentially shared with other processes.

-------- 关于 VIRT/RES/SHR 的详解区别, 请参考如下地址 -------------------
http://blog.csdn.net/dingpeng1978/article/details/6559232
VIRT:virtual memory usage
1、进程“需要的”虚拟内存大小,包括进程使用的库、代码、数据等
2、假如进程申请100m的内存,但实际只使用了10m,那么它会增长100m,而不是实际的使用量
RES:resident memory usage 常驻内存
1、进程当前使用的内存大小,但不包括swap out
2、包含其他进程的共享
3、如果申请100m的内存,实际使用10m,它只增长10m,与VIRT相反
4、关于库占用内存的情况,它只统计加载的库文件所占内存大小
SHR:shared memory
1、除了自身进程的共享内存,也包括其他进程的共享内存
2、虽然进程只使用了几个共享库的函数,但它包含了整个共享库的大小
3、计算某个进程所占的物理内存大小公式:RES – SHR
4、swap out后,它将会降下来
DATA
1、数据占用的内存。如果top没有显示,按f键可以显示出来。
2、真正的该程序要求的数据空间,是真正在运行中要使用的。
-----------------------------------------------------------------------

S
进程状态 [Process Status]
状态值所对应的意义如下:
D = 不可中断的休眠 [uninterruptible sleep]
R = 运行 [running]
S = 休眠 [sleeping]
T = 回溯或停止 [traced or stopped]
Z = 僵尸 [zombie]
%CPU
CPU 使用率 [CPU usage]
%MEM
物理内存使用率 [Memory usage (RES)], 以 RES 列的值为标准.
TIME+
CPU Time, hundredths (精确到1/100秒,如11:29.46=11分钟, 29.46秒)
任务开启后所占用的总的CPU时间。
COMMAND
任务名称

VI. 常用参数:
top -d 1
每隔一秒刷新一次。(默认为3秒)
在 top 运行过程中, 也可以通过输入 d 或 s 重新设置刷新值。
Delay time interval as: -d ss.tt (seconds.tenths)

top -c
对于COMMAND列,显示包含路径的命令行,而不只是程序名称。
如: 不使用 -c 显示为 smbd, 加 -c 则为 /usr/sbin/smbd

在 top 运行过程中可以通过输入 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
visa versa.
See the ’c’ interactive command for additional information.

top -S
累计模式
累计模式开启,则每个进程的CPU时间(TIME+),表示它及它的关闭的子进程所用的时间。
在top命令运行时,可以通过输入 S 切换累计模式状态。
Cumulative time mode toggle
Starts top with the last remembered ’S’ state reversed.
When’Cumulative 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.

VII. 交互命令
top 运行时的交互命令:
M
按 %MEM 由高到低排列
N
按 PID 由高到低排列
P
按 %CPU 由高到低排列
T
按 TIME+ 由高到低排列

s
更改刷新频率,输入秒数。
d
同 s 一样。
S
开/关累计模式
c
显示/不显示程序全路径 [参看 top -c]

i
忽略闲置、僵死的进程
u
输入用户名,只显示此用户的进程
H
显示/不显示线程

>
向后翻页
<
向前翻页
q
退出

f
选择要显示的列。直接输入小写索引字母即可。点击任意其他键返回(如回车)。
选中的会改为大写并加*号。如下:
* A: PID = Process Id
未选中的(即不显示的), 状态为小写,如下:
a: PID = Process Id

o
改变显示列的顺序,用法与上相同。
输入大写索引字母,则会往前移动。
输入小写索引字母,则会往后移动。
F
选择排序的列。
top 以此处选择的列为准排序。
O
与 F 功能完全相同。

VIII. 应用
1. 如何用 top 只监视某个进程的状态?
目前只发现用 top -p PID 的方式, 监控某个PID的进程。
所以,先用pstree -p 取得进程PID, 然后再监控。如:
# pstree -p | grep apache2
# top -p 2038,13734,13735
2. 如何通过 shell 脚本监控 top 命令并抓取某几个进程的信息?

在 shell 脚本中用如下一行即可.

top -b -n 2 | grep -E "mysql| apache2" >> log
参数 -b 用于输出 top 的信息, -n 2 表示两次刷新. 可以加 -d 10 更改刷新间隔时间. 也可以去掉 -n 2, 表示一直不停取结果.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: