系统检测分析工具--free、vmstat、slabtop、top

free命令详细介绍

$ free -h
              total        used        free      shared  buff/cache   available
Mem:            15G        6.9G        201M        197M        8.5G        8.2G
Swap:          2.0G          0B        2.0G

free -h选项表示的是按照human方便查看的方式进行查看内存数据。buffer表示块设备块缓存,而cache表示的是文件页缓存,因此buffer和cache的缓存是可能会被系统回收的,因此上述数据有如下关系:available约等于free+buffer/cache的大小。这里是约等于的关系,因为有一些系统中特殊用途的buffer和cache是不允许回收的。Swap这一行表示系统中的交换分区使用情况,本例中的交换分区大小为2G,已使用0B,也就是说系统还没有到使用交换分区来回收内存的地步。

vmstat

vmstat也是一个用来查看系统内存使用情况的程序Virtual Meomory Statistics。

$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  4      0 205092 696852 8251540    0    0    16    29   30   39  2  3 91  3  0

上述输出表示系统中处于runing状态的进程为0个,blocked状态等待IO的进程有4个,swapd表示交换分区使用的大小(本例为0KB)这也和上面free命令的输出一致的,free/buff/cache表示物理内存使用情况。
swap信息:si表示每秒钟swap in的内存大小,so表示每秒钟swap out的内存大小
io信息:bi表示每秒钟从block读入内存的大小,bo表示每秒钟写入到block中的数据大小。
system信息:in表示interrupt数量,cs表示content switch进程切换数量。
cpu信息:us表示userspace CPU占用率,sy表示system内核CPU占用率,id表示CPU处于idel空闲状态时间占比,wa表示CPU等待IO输入输出时间占比。

vmstat可以通过传入参数来运行,比如:vmstat 2 5表示每2s更新一次,一共更新5次。

$ vmstat 2 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  4      0 278660 687488 8366684    0    0    16    29    8   16  2  3 93  2  0
 1  4      0 278156 687488 8366684    0    0     1    26 4796 14002  3 13 41 43  0
 1  4      0 278156 687488 8366684    0    0     0    38 4846 13866  3 13 43 41  0
 0  4      0 277020 687492 8366688    0    0     0    92 5100 14162  3 13 42 42  0
 2  4      0 275812 687492 8366688    0    0     4  1640 4930 14131  4 12 41 43  0

slabtop

该程序可以用来查看slab使用统计信息。该程序是从/proc/slabinfo来获取该信息的,并且以更加简明的方式展现出来。它的输出风格和top命令很类似,只不过输出信息是一些slab使用的统计情况。

 Active / Total Objects (% used)    : 2353272 / 2536158 (92.8%)
 Active / Total Slabs (% used)      : 69977 / 69977 (100.0%)
 Active / Total Caches (% used)     : 98 / 160 (61.2%)
 Active / Total Size (% used)       : 499035.78K / 550749.03K (90.6%)
 Minimum / Average / Maximum Object : 0.01K / 0.22K / 24.00K

  OBJS ACTIVE  USE OBJ SIZE  SLABS OBJ/SLAB CACHE SIZE NAME                   
1517958 1513179   0%    0.10K  38922       39    155688K buffer_head
161658  92302   0%    0.19K   7698       21     30792K dentry
140130 116166   0%    1.06K   4671       30    149472K ext4_inode_cache
100380 100304   0%    0.57K   3585       28     57360K radix_tree_node
 75174  24070   0%    0.04K    737      102      2948K ext4_extent_status
 66963  66234   0%    0.20K   1717       39     13736K vm_area_struct
 44285  42669   0%    0.05K    521       85      2084K ftrace_event_field
 44280  44280 100%    0.13K   1476       30      5904K kernfs_node_cache
 43136  41363   0%    0.06K    674       64      2696K pid
 33408  33349   0%    0.03K    261      128      1044K kmalloc-32
 30144  19171   0%    0.06K    471       64      1884K kmalloc-64
 24648  21349   0%    0.59K    948       26     15168K inode_cache
 23828  23828 100%    0.09K    518       46      2072K anon_vma
 21588  21588 100%    0.14K    771       28      3084K ext4_groupinfo_4k
 20992  19709   0%    0.25K    656       32      5248K filp
 20470  17651   0%    0.69K    890       23     14240K squashfs_inode_cache
 17664  17664 100%    0.02K     69      256       276K kmalloc-16
 11392  11392 100%    0.03K     89      128       356K fscrypt_info
 10752  10752 100%    0.01K     21      512        84K kmalloc-8
 10024  10024 100%    0.07K    179       56       716K Acpi-Operand
  9681   8646   0%    0.19K    461       21      1844K kmalloc-192
  9618   6734   0%    0.09K    229       42       916K kmalloc-96
  7944   3003   0%    0.66K    331       24      5296K proc_inode_cache
  7140   7140 100%    0.04K     70      102       280K Acpi-Namespace
  6944   6540   0%    0.12K    217       32       868K kmalloc-128
  5280   5176   0%    0.50K    165       32      2640K kmalloc-512
  4922   4896   0%    0.70K    214       23      3424K shmem_inode_cache
  4704   4141   0%    0.19K    224       21       896K cred_jar
  4244   4244 100%    8.00K   1061        4     33952K kmalloc-8192
  4088   4088 100%    0.05K     56       73       224K mbcache
  3200   3101   0%    0.12K    100       32       400K seq_file
  2784   2615   0%    2.00K    174       16      5568K kmalloc-2048

该命令同样支持显示排序:

The following are valid sort criteria:
 a: sort by number of active objects
 b: sort by objects per slab
 c: sort by cache size
 l: sort by number of slabs
 v: sort by number of active slabs
 n: sort by name
 o: sort by number of objects (the default)
 p: sort by pages per slab
 s: sort by object size
 u: sort by cache utilization

top命令详解:

使用top命令的输出内如如下所示:

top - 11:17:20 up 1 day,  2:18,  1 user,  load average: 1.66, 1.70, 1.55
Tasks: 343 total,   1 running, 262 sleeping,   0 stopped,   1 zombie
%Cpu(s):  4.2 us, 11.4 sy,  0.0 ni, 79.6 id,  2.4 wa,  0.0 hi,  2.4 si,  0.0 st
KiB Mem : 16349520 total,  1246220 free,  6641700 used,  8461600 buff/cache
KiB Swap:  2047436 total,  2047436 free,        0 used.  9204820 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND         
30928 xiehaoc+  20   0 8232608 339324 256640 S  95.7  2.1  32:49.40 VirtualBox      
29249 xiehaoc+  20   0 1435028 115072  85516 S  11.6  0.7   2:17.37 Xorg            
29394 xiehaoc+  20   0 4798876 256716 110140 S   9.6  1.6   3:37.71 gnome-shell     
31819 xiehaoc+  20   0 6680104 224220 177456 S   7.6  1.4   3:44.03 netease-cloud-m 
29437 xiehaoc+   9 -11 3007976  15000  11092 S   6.0  0.1   1:19.90 pulseaudio      
29993 xiehaoc+  20   0  699716  48132  32844 S   4.3  0.3   0:11.66 gnome-terminal- 
31476 xiehaoc+  20   0 1476932 333392 145984 S   3.6  2.0   2:24.98 dingtalk        
31561 xiehaoc+  20   0 4758996 213144 131112 S   1.0  1.3   0:26.47 AppRun          
 2241 xiehaoc+  20   0   51452   4404   3672 R   0.7  0.0   0:00.07 top             
    1 root      20   0  225912   9748   6768 S   0.3  0.1   0:50.25 systemd         
   10 root      20   0       0      0      0 I   0.3  0.0   1:01.05 rcu_sched       
  242 root      20   0       0      0      0 S   0.3  0.0   0:54.60 usb-storage     
 1922 root       0 -20       0      0      0 I   0.3  0.0   0:00.22 kworker/2:2H-ra 
26796 root       0 -20       0      0      0 I   0.3  0.0   0:04.37 kworker/5:2H-ra 
29532 xiehaoc+  20   0  606888  35732  29520 S   0.3  0.2   0:00.74 sogou-qimpanel- 
    2 root      20   0       0      0      0 S   0.0  0.0   0:00.09 kthreadd        
    3 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_gp          
    4 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 rcu_par_gp      
    8 root       0 -20       0      0      0 I   0.0  0.0   0:00.00 mm_percpu_wq    
    9 root      20   0       0      0      0 S   0.0  0.0   0:04.67 ksoftirqd/0     
   11 root      20   0       0      0      0 I   0.0  0.0   0:00.00 rcu_bh          
   12 root      rt   0       0      0      0 S   0.0  0.0   0:00.03 migration/0     
   13 root      rt   0       0      0      0 S   0.0  0.0   0:00.21 watchdog/0      
   14 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/0         
   15 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/1         
   16 root      rt   0       0      0      0 S   0.0  0.0   0:00.18 watchdog/1      
   17 root      rt   0       0      0      0 S   0.0  0.0   0:00.03 migration/1     
   18 root      20   0       0      0      0 S   0.0  0.0   0:03.67 ksoftirqd/1     
   21 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/2         
   22 root      rt   0       0      0      0 S   0.0  0.0   0:00.19 watchdog/2      
   23 root      rt   0       0      0      0 S   0.0  0.0   0:00.03 migration/2     
   24 root      20   0       0      0      0 S   0.0  0.0   0:03.21 ksoftirqd/2     
   27 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/3         
   28 root      rt   0       0      0      0 S   0.0  0.0   0:00.20 watchdog/3      
   29 root      rt   0       0      0      0 S   0.0  0.0   0:00.03 migration/3     
   30 root      20   0       0      0      0 S   0.0  0.0   0:02.60 ksoftirqd/3     
   33 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/4         
   34 root      rt   0       0      0      0 S   0.0  0.0   0:00.20 watchdog/4      
   35 root      rt   0       0      0      0 S   0.0  0.0   0:00.03 migration/4     
   36 root      20   0       0      0      0 S   0.0  0.0   0:02.70 ksoftirqd/4     
   39 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/5         
   40 root      rt   0       0      0      0 S   0.0  0.0   0:00.20 watchdog/5      
   41 root      rt   0       0      0      0 S   0.0  0.0   0:00.03 migration/5     
   42 root      20   0       0      0      0 S   0.0  0.0   0:02.38 ksoftirqd/5     
   45 root      20   0       0      0      0 S   0.0  0.0   0:00.00 cpuhp/6         
   46 root      rt   0       0      0      0 S   0.0  0.0   0:00.19 watchdog/6      
 

首先来介绍一些top命令的默认输出内容含义:

top - 11:17:20 up 1 day,  2:18,  1 user,  load average: 1.66, 1.70, 1.55
Tasks: 343 total,   1 running, 262 sleeping,   0 stopped,   1 zombie
%Cpu(s):  4.2 us, 11.4 sy,  0.0 ni, 79.6 id,  2.4 wa,  0.0 hi,  2.4 si,  0.0 st
KiB Mem : 16349520 total,  1246220 free,  6641700 used,  8461600 buff/cache
KiB Swap:  2047436 total,  2047436 free,        0 used.  9204820 avail Mem 

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

第一行:

11:17:20为当前时间
1 day, 2:18是系统开机之后持续运行的时间
1 user表示一个用户在使用
load average: 1.66, 1.70, 1.55分别是系统1分钟,5分钟,15分钟的平均负载值

第二行表示task情况:

343 total:一共有343个任务
1 running:1个处于running状态
262 sleeping:262个处于sleeping状态
0 stopped:0个处于stop状态
1 zombie:1个处于僵尸状态

第三行表示Cpu使用占比,后面的几个项目相加正好等于100%:

4.2 us:用户空间CPU占比
11.4 sy:内核空间CPU占比
0.0 ni:用户空间改变nice的进程CPU占比
79.6 id:空闲CPU占比
2.4 wa:CPU等待IO输入输出时间占比
0.0 hi:硬件中断CPU占比
2.4 si:软件中断CPU占比

第四行第五行分别就是系统的内存使用情况,和free一样,不再介绍。
最后显示的就是以进程为单位的内存和CPU资源占用情况了:

PID:进程id
USER:用户
PR:优先级
NI:nice值
VIRT:虚拟内存大小
RES:没有被swap的物理内存大小
SHR:共享内存大小
S:进程状态(R、S、Z、D、T)
%CPU:CPU占比(一个CPU是100%,多个CPU是n×100%)
%MEM:内存占比
TIME+:运行时间
COMMAND:运行命令

在该界面下可以使用如下的命令操作top输出选项:

M:按照内存使用大小排序
P:按照CPU占用率大小排序
T:按照运行时间大小排序
f:配置显示的列条目
m:显示系统内存情况柱状图
1:显示各个CPU的使用情况
发布了234 篇原创文章 · 获赞 78 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/rikeyone/article/details/90297850