tldr top
tldr topDisplay dynamic real-time information about running processes. - Start top: top - Do not show any idle or zombie processes: top -i - Show only processes owned by given user: top -u user_name - Show only the processes with the given PID(s), passed as a comma-separated list. (Normally you would not know PIDs off hand. This example picks the PIDs from the process name): top -p $(pgrep -d ',' process_name) - Show java processes top -p $(pgrep -d ',' java) - Show threads of specified java process top -Hp 15961 - Get help about interactive commands: ? |
top 默认值
*开头的参数可以在top命令行参数中进行覆盖,其他的参数可以通过~/.toprc文件自定义配置。
Global_defaults 'A' - Alt display Off (full-screen) * 'd' - Delay time 3.0 seconds 'I' - Irix mode On (no, 'solaris' smp) * 'p' - PID monitoring Off * 's' - Secure mode Off (unsecured) 'B' - Bold disable OffSummary_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 On (thus 1 line if smp)Task_Area_defaults 'b' - Bold hilite On (not 'reverse') * 'c' - Command line Off (name, not cmdline) * 'H' - Threads Off (show all threads) * 'i' - Idle tasks On (show all tasks) 'R' - Reverse sort On (pids high-to-low) * 'S' - Cumulative time Off (no, dead children) 'x' - Column hilite Off (no, sort field) 'y' - Row hilite On (yes, running tasks) 'z' - color/mono Off (no, colors) |
top 命令常用选项
-b:批量模式,一般用来导出 top 命令结果到一个文件中-c:显示完整的命令路径信息-d:屏幕刷新间隔时间-u:指定用户名-p:指定进程-n:循环显示的次数
top 帮助文档
h: 显示帮助画面。k: 终止一个进程。i: 忽略闲置和僵死进程。q: 退出程序。r: 重新安排一个进程的优先级别。S: 切换到累计模式。s: 改变两次刷新之间的延迟时间。f: 从当前显示中添加或者删除项目。o: 改变显示项目的顺序。l: 切换显示平均负载和启动时间信息。m: 切换显示内存信息。t: 切换显示进程和CPU状态信息。c: 切换显示命令名称和完整命令行。M: 根据驻留内存大小进行排序。P: 根据CPU使用百分比大小进行排序。T: 根据时间/累计时间进行排序。W: 将当前设置写入~/.toprc文件中。
Example of linux top
toptop - 15:17:03 up 32 days, 22:35, 1 user, load average: 0.06, 0.04, 0.00Tasks: 101 total, 1 running, 100 sleeping, 0 stopped, 0 zombieCpu(s): 11.3%us, 3.4%sy, 0.0%ni, 84.3%id, 0.1%wa, 0.0%hi, 0.9%si, 0.0%stMem: 3924684k total, 3817324k used, 107360k free, 8628k buffersSwap: 0k total, 0k used, 0k free, 414188k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND27221 tomcat 20 0 5221m 2.7g 17m S 59.3 73.0 591:09.49 java26833 nginx 20 0 76096 29m 1340 S 2.0 0.8 25:51.72 nginx 1 root 20 0 21448 1104 804 S 0.0 0.0 0:00.77 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root RT 0 0 0 0 S 0.0 0.0 0:13.43 migration/0 4 root 20 0 0 0 0 S 0.0 0.0 3:08.30 ksoftirqd/0 5 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 |
Linux load average 简单说明
- load average: 0.00, 0.00, 0.00 — 后面的三个数分别是
1分钟、5分钟、15分钟的系统负载情况。 - load average 的值是从
/proc/loadavg文件中读取的。 - 如果这个数除以 CPU 的内核数量,结果高于
1.0的时候就要注意了。 - Linux 中因为1993年一个内核补丁,这些数值可以理解为系统的整体负载,而 UNIX 中则是 CPU 的负载。
- 关于 Linux 的
load balance算法,可参考这篇文章。 引用 linux 源码 kernel/sched/loadavg.c 中的注释如下:
This file contains the magic bits required to compute the global loadavg
figure. Its a silly number but people think its important. We go through
great pains to make it work on big machines and tickless kernels.
CPU 状态信息简单说明
0.3% us— 用户空间占用CPU的百分比0.0% sy— 内核空间占用CPU的百分比0.0% ni— 改变过优先级的进程占用CPU的百分比99.7% id— 空闲CPU百分比0.0% wa— IO等待占用CPU的百分比0.0% hi— 硬中断(Hardware IRQ)占用CPU的百分比0.0% si— 软中断(Software Interrupts)占用CPU的百分比0.0% st— 这个虚拟机被Hypervisor占用的CPU时间
这里显示数据是所有CPU的平均值,如果想看每一个CPU的处理情况,按数字1即可;折叠,再次按1。
CPU 数量查看命令
lscpu nproc grep 'model name' /proc/cpuinfo | wc -l |
top 内存信息简单说明
Mem: 3924684k total, 3817324k used, 107360k free, 8628k buffersSwap: 0k total, 0k used, 0k free, 414188k cached |
total— 物理内存总量used— 使用中的内存总量free— 空闲内存总量buffers— 缓存的内存量cached— 缓冲的交换区总量
系统实际可用内存
当前系统可用内存可以用top命令中这3个字段加起来free + buffers + cached进行估计。
如上显示的信息,可以估计系统大概还有 530M 内存可用,与下面free命令显示的结果差不多:
107360 + 8628 + 414188 = 530176
free 显示 CentOS 6.5 内存使用情况
free total used free shared buffers cachedMem: 3924684 3806404 118280 0 6476 404972-/+ buffers/cache: 3394956 529728Swap: 0 0 0 |
118280 + 6476 + 404972 = 529728
free 显示 Ubuntu 18.04 内存使用情况
在 Ubuntu 中显示top和free的信息字段名与 CentOS 中显示的字段名略有差异。
free total used free shared buff/cache availableMem: 492952 102008 22276 644 368668 374128Swap: 0 0 0 |
top 进程信息字段说明
PID: 进程的IDUSER: 进程所有者PR: 进程的优先级别,越小越优先被执行NI: 进程的nice值VIRT: 进程使用的虚拟内存总量(VIRT = SWAP + RES),单位kbRES: 进程使用的、未被换出的物理内存大小(RES = CODE + DATA),单位kbSHR: 进程使用的共享内存,单位kbS: 进程的状态,S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值为负数%CPU: 进程占用CPU的使用率%MEM: 进程使用的物理内存和总内存的百分比TIME+: 该进程启动后占用的总的CPU时间,即占用CPU使用时间的累加值COMMAND: 进程启动命令名称
以上是默认显示的字段说明,更多的字段可以使用man top,或者是top运行中按o键查看并使用字段对应的字母大小写排序,按f键控制字段显示。
字段排序和高亮显示
在top命令运行时,按下z和x,可以看到%CPU列被高亮显示了。默认的排序列是%CPU,通过>或<键可以向右或左改变排序列。
~/.toprc 文件
在top命令运行时,使用f和o键进行显示的设置,然后返回到top应用运行界面中,按大写W键可以保存当前的配置文件.toprc到个人主目录下,内容如下:
RCfile for "top with windows" # shameless braggin'Id:a, Mode_altscr=0, Mode_irixps=1, Delay_time=3.000, Curwin=0Def fieldscur=AEHIOQTWKNMbcdfgjplrsuvyzX winflags=65465, sortindx=10, maxtasks=0 summclr=1, msgsclr=1, headclr=3, taskclr=1Job fieldscur=ABcefgjlrstuvyzMKNHIWOPQDX winflags=62777, sortindx=0, maxtasks=0 summclr=6, msgsclr=6, headclr=7, taskclr=6Mem fieldscur=ANOPQRSTUVbcdefgjlmyzWHIKX winflags=62777, sortindx=13, maxtasks=0 summclr=5, msgsclr=5, headclr=4, taskclr=5Usr fieldscur=ABDECGfhijlopqrstuvyzMKNWX winflags=62777, sortindx=4, maxtasks=0 summclr=3, msgsclr=3, headclr=2, taskclr=3 |
ps 命令显示 CPU 使用最高的进程
在 CentOS 中运行以下ps命令,可以查看进程的 CPU 使用情况,效果类似上面的top命令。
ps -e -o pid,user,nice,size,rss,pcpu,pmem,etime,comm --sort=-pcpu | head PID USER SZ RSS %CPU %MEM ELAPSED COMMAND27221 tomcat 5184880 2856904 22.3 72.7 2-02:01:01 java 2043 root 284664 59192 1.0 1.5 33-06:02:15 ilogtail26832 nginx 28500 30488 0.6 0.7 3-12:44:49 nginx 997 root 2351344 67248 0.5 1.7 33-06:13:11 java26833 nginx 28936 30264 0.5 0.7 3-12:44:49 nginx 708 root 3465836 231636 0.2 5.9 33-06:13:19 java 1 root 412 1104 0.0 0.0 33-06:13:22 init 2 root 0 0 0.0 0.0 33-06:13:22 kthreadd 6 root 0 0 0.0 0.0 33-06:13:22 watchdog/0 |
Show java processes
top -p $(pgrep -d ',' java)top - 14:36:53 up 32 days, 21:55, 1 user, load average: 0.00, 0.00, 0.00Tasks: 3 total, 0 running, 3 sleeping, 0 stopped, 0 zombieCpu(s): 11.3%us, 3.4%sy, 0.0%ni, 84.3%id, 0.1%wa, 0.0%hi, 0.9%si, 0.0%stMem: 3924684k total, 3801792k used, 122892k free, 10756k buffersSwap: 0k total, 0k used, 0k free, 401836k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND27221 tomcat 20 0 5172m 2.7g 7632 S 45.9 72.6 572:22.10 java 708 root 20 0 3539m 226m 6468 S 0.0 5.9 105:34.78 java 997 root 20 0 2447m 65m 2872 S 0.0 1.7 259:02.85 java |
Show threads of tomcat process
显示 tomcat 进程中的所有线程,可以查看哪个线程实际使用的 CPU 资源最高。
top -Hp 27221top - 14:32:07 up 32 days, 21:50, 1 user, load average: 0.01, 0.02, 0.00Tasks: 268 total, 0 running, 268 sleeping, 0 stopped, 0 zombieCpu(s): 21.1%us, 6.4%sy, 0.0%ni, 70.3%id, 0.0%wa, 0.0%hi, 2.2%si, 0.0%stMem: 3924684k total, 3788740k used, 135944k free, 10524k buffersSwap: 0k total, 0k used, 0k free, 389656k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND27602 tomcat 20 0 5172m 2.7g 7624 S 3.7 72.6 1:18.00 java27229 tomcat 20 0 5172m 2.7g 7624 S 3.3 72.6 19:16.14 java27253 tomcat 20 0 5172m 2.7g 7624 S 2.3 72.6 25:36.15 java27595 tomcat 20 0 5172m 2.7g 7624 S 2.3 72.6 1:17.06 java 5629 tomcat 20 0 5172m 2.7g 7624 S 2.0 72.6 7:16.77 java29973 tomcat 20 0 5172m 2.7g 7624 S 1.7 72.6 10:17.02 java31240 tomcat 20 0 5172m 2.7g 7624 S 1.7 72.6 9:16.38 java27588 tomcat 20 0 5172m 2.7g 7624 S 1.7 72.6 1:16.14 java27267 tomcat 20 0 5172m 2.7g 7624 S 1.3 72.6 13:47.18 java 4076 tomcat 20 0 5172m 2.7g 7624 S 1.3 72.6 13:46.08 java27590 tomcat 20 0 5172m 2.7g 7624 S 1.3 72.6 1:16.81 java27603 tomcat 20 0 5172m 2.7g 7624 S 1.3 72.6 1:15.60 java27609 tomcat 20 0 5172m 2.7g 7624 S 1.3 72.6 1:16.83 java27863 tomcat 20 0 5172m 2.7g 7624 S 1.0 72.6 12:43.72 java29829 tomcat 20 0 5172m 2.7g 7624 S 1.0 72.6 3:45.81 java |
一次性导出 top 运行的结果
top -bc -n 1top - 15:11:59 up 32 days, 22:30, 1 user, load average: 0.02, 0.02, 0.00Tasks: 101 total, 1 running, 100 sleeping, 0 stopped, 0 zombieCpu(s): 11.3%us, 3.4%sy, 0.0%ni, 84.3%id, 0.1%wa, 0.0%hi, 0.9%si, 0.0%stMem: 3924684k total, 3817316k used, 107368k free, 8784k buffersSwap: 0k total, 0k used, 0k free, 414308k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND27221 tomcat 20 0 5216m 2.7g 12m S 39.6 72.9 588:44.48 /opt/java/jdk/bin/java -Djava.util.logging.config.file=/opt/tomcat/conf/loggin26832 nginx 20 0 75920 29m 1340 S 4.0 0.8 32:11.93 nginx: worker process 1 root 20 0 21448 1104 804 S 0.0 0.0 0:00.77 /sbin/init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 [kthreadd] |
其他的类似工具
- htop
- glances