linux 中 top 命令使用方法

环境说明

Centos7系统

top 命令是最直观最好用的查看节点负载的命令。可以实时动态的刷新服务器状态信息,并且支持交互命令自定义显示内容,功能算是很强大了。在终端中输入top,回车后会显示如下内容:

top - 21:51:04 up 25 min,  2 users,  load average: 1.22, 0.37, 0.15
Tasks: 186 total,   5 running, 181 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.0 us,  1.7 sy,  0.0 ni, 14.0 id,  0.1 wa,  0.0 hi, 83.2 si,  0.0 st
KiB Mem : 10221584 total,  8109544 free,  1408312 used,   703728 buff/cache
KiB Swap:  4194300 total,  4194300 free,        0 used.  8486220 avail Mem 

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                         
  2389 hui       20   0 5254692 481268  15748 S  43.1  4.7   0:37.65 java                                                                            
  1539 mysql     20   0 2058528 211396   7688 S  25.3  2.1   0:08.01 mysqld                                                                          
  2693 hui       20   0 6209116 185272  16008 S  18.9  1.8   0:12.75 java                                                                            
    19 root      20   0       0      0      0 R  17.5  0.0   0:01.86 ksoftirqd/2                                                                     
   499 root      20   0   40248   5912   5384 R  15.6  0.1   0:03.01 systemd-journal                                                                 
   787 root      20   0  320252   6664   5192 S  11.4  0.1   0:04.27 vmtoolsd                                                                        
    14 root      20   0       0      0      0 S   9.4  0.0   0:00.95 ksoftirqd/1                                                                     
  2764 root      20   0       0      0      0 S   8.1  0.0   0:00.51 kworker/2:1                                                                     
    24 root      20   0       0      0      0 S   6.9  0.0   0:00.98 ksoftirqd/3                                                                     
     9 root      20   0       0      0      0 S   6.7  0.0   0:01.12 rcu_sched                                                                       
   731 avahi     20   0   62236   2284   1824 S   6.1  0.0   0:01.28 avahi-daemon                                                                    
  1997 hui       20   0 4626924 132468  13272 S   5.8  1.3   0:06.88 java                                                                            
  1055 root      20   0  218504   5972   4484 S   5.6  0.1   0:01.13 rsyslogd                                                                        
   423 root      20   0       0      0      0 S   3.6  0.0   0:01.03 xfsaild/dm-0                                                                    
  2782 root      20   0       0      0      0 S   3.3  0.0   0:00.36 kworker/0:0                                                                     
    23 root      rt   0       0      0      0 S   2.2  0.0   0:00.58 migration/3                                                                     
   275 root      20   0       0      0      0 R   2.2  0.0   0:00.56 kworker/2:2                                                                     
     3 root      20   0       0      0      0 R   1.9  0.0   0:01.31 ksoftirqd/0                                                                     
    13 root      rt   0       0      0      0 S   1.9  0.0   0:00.77 migration/1                                                                     
  1866 hui       20   0  160884   2520   1136 S   1.4  0.0   0:00.53 sshd                                                                            
   734 root      20   0  396664   4420   3364 S   1.1  0.0   0:00.38 accounts-daemon                                                                 
  1049 root      20   0  573816  17176   6116 S   1.1  0.2   0:00.75 tuned                                                                           
  1696 gdm       20   0  731608  14092   9004 S   0.8  0.1   0:00.44 gsd-color                                                                       
     7 root      rt   0       0      0      0 S   0.6  0.0   0:00.55 migration/0                                                                     
   745 root      20   0  221312   4796   3376 S   0.6  0.0   0:00.18 abrt-watch-log                                                                  
  2820 root      20   0       0      0      0 S   0.6  0.0   0:00.02 kworker/u256:2                                                                  
    18 root      rt   0       0      0      0 S   0.3  0.0   0:01.35 migration/2                                                                     
   443 root      20   0       0      0      0 S   0.3  0.0   0:00.16 kworker/3:2                                                                     
   815 root       0 -20       0      0      0 S   0.3  0.0   0:00.03 kworker/1:1H                                                                    
  1560 gdm       20   0 3416160 132560  55020 S   0.3  1.3   0:06.15 gnome-shell                                                                     
  2625 hui       20   0  161980   2332   1584 R   0.3  0.0   0:03.64 top                                                                             
     1 root      20   0  193868   6916   4156 S   0.0  0.1   0:03.13 systemd                                                                         
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.05 kthreadd                                                                        
     5 root       0 -20       0      0      0 S   0.0  0.0   0:00.00 kworker/0:0H                                                                    
     8 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcu_bh  

1、系统信息统计

前五行是系统整体状态的统计信息展示区域。下面分别介绍每一行中的内容:

1.1、第一行显示服务器总体负载信息

如下所示,第一行列出了服务器运行了多长时间,当前有多少个用户登录,服务器的负荷情况等,使用uptime命令能获得同样的结果。 

平均负载的值越小代表系统压力越小,越大则代表系统压力越大。通常,我们会以最后一个数值,也就是15分钟内的平均负载作为参考来评估系统的负载情况。对于只有单核cpu的系统,1.0是该系统所能承受负荷的边界值,大于1.0则有处理需要等待。一个单核cpu的系统,平均负载的合适值是0.7以下。如果负载长期徘徊在1.0,则需要考虑马上处理了。超过1.0的负载,可能会带来非常严重的后果。当然,多核cpu的系统是在前述值的基础上乘以cpu内核的个数。如对于多核cpu的系统,有N个核则所能承受的边界值为N.0

可以使用如下命令来查看每个处理器的信息:

cat /proc/cpuinfo

虚拟机执行后结果如下

processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 154
model name      : 12th Gen Intel(R) Core(TM) i7-12700H
stepping        : 3
microcode       : 0xffffffff
cpu MHz         : 2688.001
cache size      : 24576 KB
physical id     : 0
siblings        : 2
core id         : 0
cpu cores       : 2
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 32
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 ibpb ibrs stibp arat spec_ctrl intel_stibp arch_capabilities
bogomips        : 5376.00
clflush size    : 64
cache_alignment : 64
address sizes   : 45 bits physical, 48 bits virtual
power management:

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 154
model name      : 12th Gen Intel(R) Core(TM) i7-12700H
stepping        : 3
microcode       : 0xffffffff
cpu MHz         : 2688.001
cache size      : 24576 KB
physical id     : 0
siblings        : 2
core id         : 1
cpu cores       : 2
apicid          : 1
initial apicid  : 1
fpu             : yes
fpu_exception   : yes
cpuid level     : 32
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 ibpb ibrs stibp arat spec_ctrl intel_stibp arch_capabilities
bogomips        : 5376.00
clflush size    : 64
cache_alignment : 64
address sizes   : 45 bits physical, 48 bits virtual
power management:

processor       : 2
vendor_id       : GenuineIntel
cpu family      : 6
model           : 154
model name      : 12th Gen Intel(R) Core(TM) i7-12700H
stepping        : 3
microcode       : 0xffffffff
cpu MHz         : 2688.001
cache size      : 24576 KB
physical id     : 1
siblings        : 2
core id         : 0
cpu cores       : 2
apicid          : 2
initial apicid  : 2
fpu             : yes
fpu_exception   : yes
cpuid level     : 32
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 ibpb ibrs stibp arat spec_ctrl intel_stibp arch_capabilities
bogomips        : 5376.00
clflush size    : 64
cache_alignment : 64
address sizes   : 45 bits physical, 48 bits virtual
power management:

processor       : 3
vendor_id       : GenuineIntel
cpu family      : 6
model           : 154
model name      : 12th Gen Intel(R) Core(TM) i7-12700H
stepping        : 3
microcode       : 0xffffffff
cpu MHz         : 2688.001
cache size      : 24576 KB
physical id     : 1
siblings        : 2
core id         : 1
cpu cores       : 2
apicid          : 3
initial apicid  : 3
fpu             : yes
fpu_exception   : yes
cpuid level     : 32
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 ibpb ibrs stibp arat spec_ctrl intel_stibp arch_capabilities
bogomips        : 5376.00
clflush size    : 64
cache_alignment : 64
address sizes   : 45 bits physical, 48 bits virtual
power management
View Code

如果只想计算有多少个cpu内核,可以使用如下命令:

[hui@hadoop104 ~]$ cat /proc/cpuinfo |grep 'model name' 
model name      : 12th Gen Intel(R) Core(TM) i7-12700H
model name      : 12th Gen Intel(R) Core(TM) i7-12700H
model name      : 12th Gen Intel(R) Core(TM) i7-12700H
model name      : 12th Gen Intel(R) Core(TM) i7-12700H
[hui@hadoop104 ~]$ cat /proc/cpuinfo |grep 'model name' |wc -l
4

1.2、进程数信息

Tasks: 182 total,   2 running, 180 sleeping,   0 stopped,   0 zombie

1.3、CPU使用信息

%Cpu(s):  
0.0 us --> 用户空间CPU占比
1.3 sy --> 系统空间CPU占比 
0.0 ni --> 用户进程空间改过优先级的进程CPU占比   
0.0 id --> 空闲CPU占比    
0.0 wa --> 待输入输出的CPU占比
0.0 hi --> 硬中断(Hardware IRQ)CPU占比  
98.7 si --> 软中断(Software Interrupts)CPU占比  
0.0 st -->  用于有虚拟cpu的情况,用来指示被虚拟机偷掉的cpu时间

1.4、内存使用信息

 

1.5、swap 交换分区使用信息

  

2、进程状态信息

第七行及以下显示了各进程(任务)的状态监控。各列所代表的含义如下:

PID --> 进程id
USER --> 进程所有者
PR  --> 优先级
NI --> nice值。负值表示高优先级,正值表示低优先级
VIRT --> 进程使用的虚拟内存总量,单位kb。VIRT=SWAP+RES
RES --> 进程使用的、未被换出的物理内存大小,单位kb。RES=CODE+DATA
SHR --> 共享内存大小,单位kb
S --> 进程状态。D=不可中断的睡眠状态 R=运行 S=睡眠 T=跟踪/停止 Z=僵尸进程
%CPU --> 上次更新到现在的CPU时间占用百分比
%MEM --> 进程使用的物理内存百分比
TIME+ --> 进程使用的CPU时间总计,单位1/100秒
TIME --> 进程使用的CPU时间总计,单位秒
COMMAND --> 命令名/命令行

3、top 交互命令

  • 按键b打开或关闭 运行中进程的高亮效果
  • 按键x打开或关闭 排序列的高亮效果
  • shift + > 或 shift + < 可以向右或左改变排序列
  • f键,可以进入编辑要显示字段的视图,有 * 号的字段会显示,无 * 号不显示,可根据页面提示选择或取消字段。
posted @ 2022-10-23 15:33  晓枫的春天  阅读(400)  评论(0编辑  收藏  举报