top 使用技巧

top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器。下面跟着笔者一起top一下吧:

# top
top - 10:52:40 up 3 days, 52 min,  1 user,  load average: 57.28, 112.40, 123.60
Tasks:  99 total,   1 running,  98 sleeping,   0 stopped,   0 zombie
Cpu(s): 19.5%us, 11.4%sy,  0.0%ni,  0.0%id, 65.7%wa,  0.0%hi,  3.4%si,  0.0%st
Mem:  16435896k total, 16232468k used,   203428k free,    58004k buffers
Swap:  1044476k total,   713552k used,   330924k free, 10052032k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
  ... ...

统计信息区前五行是系统整体的统计信息。第一行是任务队列信息,同uptime命令的执行结果。其内容如下:

10:52:40当前时间
up 3 days, 52 min系统运行时间
1 users当前登录用户数
load average: 57.28, 112.40, 123.60系统负载,即任务队列平均长度。
分别为1、5、15min前到现在平均值。

第二、三行为进程和CPU的信息。当有多个CPU时,这些内容可能会超过两行。内容如下:

Tasks:99 total进程总数[键入H可查看线程数]
1 running, 98 sleeping, 0 stopped正在运行的进程、睡眠进程、停止的进程
0 zombie僵尸进程数
Cpu(s): 19.5%us, 11.4%sy,
0.0%ni, 0.0%id,
65.7%wa, 0.0%hi, 3.4%si, 0.0%st
用户空间占用CPU百分比、内核空间占用CPU百分比
用户进程空间内改变进程优先级占用CPU、空闲CPU百分比
等待IO的CPU时间百分比,最后三个是中断请求相关

倒数第2、3行为内存相关信息:

Mem: 16435896k total, 16232468k used,
203428k free, 58004k buffers
分别是物理内存总量、使用物理内存总量
空闲内存总量、用作内核缓存内存量
Swap: 1044476k total, 713552k used,
330924k free, 10052032k cached
分别是交换分区量、使用交换分区总量
空闲交换区总量、缓存交换区总量

buffer Difference between buffer and cache?

A data area, shared by hardware devices or program a process is called buffer. They are operated at different speeds or with different sets of priorities. The buffer allows each device or process to operate without holding up by the other. In order to a buffer to be effective, the size of the buffer needs to be considered by the buffer designer. Like a cache, a buffer is a “midpoint holding place” but does not exist so much to accelerate the speed of an activity as for supporting the coordination of separate activities.

This term is used not only in programming but in hardware as well. In programming, buffering sometimes needs to screen data from its final intended place so that it can be edited or processed before moving to a regular file or database.

cached

Cache memory is type of random access memory (RAM). Cache Memory can be accessed more quickly by the computer microprocessor than it can be accessed by regular RAM. Like microprocessor processes data, it looks first in the cache memory and if there, it finds the data from a previous reading of data, it does not need to do the more time consuming reading of data from larger memory.

Sometimes Cache memory is described in levels of closeness and convenience to the microprocessor. An L1 cache is on the same chip like the microprocessors.

In addition to cache memory, RAM itself is a cache memory for hard disk storage since all of RAM’s contents come up to the hard disk initially when you turn on your computer and load the operating system that you are loading it into RAM and later when you start new applications and access new data. RAM also contains a special area called a disk cache that consists of the data most recently read in from the hard disk.

最后1行则是进程相关的资源占用信息:

  • PID:进程的ID
  • USER:进程所有者
  • PR:进程的优先级别,越小越优先被执行
  • NI:nice值。负值表示高优先级,正值表示低优先级
  • VIRT:进程占用的虚拟内存
  • RES:进程占用的物理内存
  • SHR:进程使用的共享内存
  • S:进程的状态。S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值为负数
  • %CPU:进程占用CPU的使用率
  • %MEM:进程使用的物理内存和总内存的百分比
  • TIME+:该进程启动后占用的总的CPU时间,即占用CPU使用时间的累加值。
  • COMMAND:进程启动命令名称

终端执行top命令之后【也可后接一些选项,比如top -p 1只监控init进程,top -u root只显示root运行进程等等】,可以敲击如下按键,实现不同功能:

  • 1(数字1):列出所有的单个CPU负载情况
  • z:top显示颜色
  • P[大写]:按CPU占用高低顺序列出程序
  • M[大写]:按内存占用高低顺序列出程序
  • x:类似高亮显示,在z显示模式下效果才会更明显

如下效果图:

geek
  • top显示Swap利用率,使用top命令后按f键,然后按p键,回车之后即可看到Swap状态
geek
  • d:top默认的刷新时间是3s,使用d键可以自定义刷新时间
geek

top类似上下翻页的方法:

  • shift < 下翻页
  • shift > 上翻页

top默认按cpu占用排序,这也是可以修改的,按F(大写)即可选择相应排序,之后任意键退出即可

geek
  • c:显示进程命令的全路径与参数
  • f:可以指定top显示的内容,如ppid、swap等都可以选择显示
  • k:输入k之后可以kill掉指定的进程
  • A:分类显示各种系统资源高的进程。可用于快速识别系统上的性能要求极高的任务,推荐使用
  • h:获取top的命令帮助
  • H:显示线程,默认只显示进程
  • W[大写]:将当前设置写入~/.toprc文件中。这是写top配置文件的推荐方法



请参考

Top基本介绍

Top实践小技巧

Linux查看某个进程的线程的3种方法



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值