Linux top 命令的前五行告诉你什么
top实用程序是显示系统性能信息的常用工具。它动态地向管理员显示哪些进程正在消耗处理器和内存资源。top 非常方便。
虽然大多数管理员可以快速理解 top 输出的下半部分,但上半部分却很难理解。因此,本文将解释 top 顶部显示的五行:
top
显示正常运行时间信息Tasks
显示进程状态信息%Cpu(s)
显示各种处理器值MiB Mem
显示物理内存利用率MiB Swap
显示虚拟内存利用率
正常运行时间
Top 的第一行top显示的信息与uptime
命令相同。第一个值是系统时间。第二个值表示系统已启动并运行了多长时间,而第三个值表示系统上的当前用户数。最后一个值是系统的平均负载。
平均负载分为三个时间增量。第一个增量显示最近一分钟的负载,第二个增量显示最近五分钟的负载,最后一个增量显示最近 15 分钟的负载。结果是 CPU 负载百分比,介于 0 到 1.0 之间。如果显示 1.0(或更高),则处理器可能超负荷工作。
top - 23:03:09 up 4 min, 1 user, load average: 0.75, 0.59, 0.25
任务
第二行是Tasks输出,它分为五种状态。这五种状态显示系统中进程的状态:
total
显示任意状态下的进程总和。running
显示有多少进程正在处理请求、正常执行并拥有 CPU 访问权。sleeping
表示等待资源的进程,这是正常状态。stopped
报告退出并释放资源的进程;这些进程向父进程发送终止消息。zombie
指的是一个进程等待其父进程释放它;如果父进程先退出,它可能会成为孤儿进程。
僵尸进程通常意味着应用程序或服务没有正常退出。长期运行的系统上出现一些僵尸进程通常不是什么问题。
Tasks: 220 total, 3 running, 217 sleeping, 0 stopped, 0 zombie
%Cpu
第三行显示与处理器利用率相关的值。它们可让您了解 CPU 的具体运行情况。
us
是运行用户进程所用时间的百分比。sy
is the percent of time spent running the kernel.ni
is the percent of time spent running processes with manually configured nice values.id
is the percent of time idle (if high, CPU may be overworked).wa
is the percent of wait time (if high, CPU is waiting for I/O access).hi
is the percent of time managing hardware interrupts.si
is the percent of time managing software interrupts.st
is the percent of virtual CPU time waiting for access to physical CPU.
Values such as id
, wa
, and st
help identify whether the system is overworked.
%Cpu(s): 19.3 us, 4.0 sy, 0.0 ni, 74.7 id, 0.0 wa, 0.3 hi, 1.7 si, 0.0 st
MiB Memory
The final two lines of top's output provide information on memory utilization. The first line—MiB Mem
—displays physical memory utilization. This value is based on the total amount of physical RAM installed on the system.
MiB Mem: 3898.5 total, 385.2 free, 1167.0 used, 2346.2 buff/cache
Note: The term mebibyte (and similar units, such as kibibytes and gibibytes) differs slightly from measurements such as megabytes. Mebibytes are based on 1024 units, and megabytes are based on 1000 units (decimal). Most users are familiar with the decimal measurement, but it is not as accurate as the binary form. The top utility reports memory consumption in decimal.
total
shows total installed memory.free
shows available memory.used
shows consumed memory.buff/cache
shows the amount of information buffered to be written.
MiB Swap
Linux can take advantage of virtual memory when physical memory space is consumed by borrowing storage space from storage disks. The process of swapping data back and forth between physical RAM and storage drives is time-consuming and uses system resources, so it's best to minimize the use of virtual memory.
MiB Swap: 3898.0 total, 3898.0 free, 0.0 used, 2433.1 avail Mem
total
shows total swap space.free
shows available swap space.used
shows consumed swap space.buff/cache
shows the amount of information cached for future reads.
In general, a high amount of swap utilization indicates the system does not have enough memory installed for its tasks. The solution is to either increase RAM or decrease the workload.
Wrap up
Glancing at the bottom 75% of top's output gives you a sense of what processes are consuming the most resources on the system. This information is often sufficient for many needs. However, the upper portion of top's output allows you to delve more deeply into exactly how the system is performing and whether CPU or RAM (or both) are utilized effectively.