Fork me on GitHub

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, 用户空间占用CPU百分比、内核空间占用CPU百分比
0.0%ni, 0.0%id, 用户进程空间内改变进程优先级占用CPU、空闲CPU百分比
65.7%wa, 0.0%hi, 3.4%si, 0.0%st 等待IO的CPU时间百分比,最后三个是中断请求相关

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

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

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.

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行则是进程相关的资源占用信息:

二、top技巧

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

三、参考