## 执行命令
cat /proc/meminfo
## 输出结果
## 服务器总内存
MemTotal: 132030344 kB
## 服务器空闲内存
MemFree: 1396884 kB
## Buffer使用的内存
Buffers: 409812 kB
## Cache使用的内存
Cached: 53136072 kB
## 使用Slab分配的内存。
Slab: 16681824 kB
## 使用Slab分配且可回收内存。
SReclaimable: 16592540 kB
## 使用Slab分配但不可以回收内存。
SUnreclaim: 89284 kB
可以使用/proc/slabinfo文件来查看Slab的内存分配情况,如:
## 执行命令
cat /proc/slabinfo |head -n 10
## 输出结果
slabinfo - version: 2.1
# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab> : tunables <limit> <batchcount> <sharedfactor> : slabdata <active_slabs> <num_slabs> <sharedavail>
fib6_nodes 19 59 64 59 1 : tunables 120 60 8 : slabdata 1 1 0
ip6_dst_cache 13 20 384 10 1 : tunables 54 27 8 : slabdata 2 2 0
ndisc_cache 0 0 256 15 1 : tunables 120 60 8 : slabdata 0 0 0
ip6_mrt_cache 0 0 128 30 1 : tunables 120 60 8 : slabdata 0 0 0
RAWv6 27 28 1088 7 2 : tunables 24 12 8 : slabdata 4 4 0
UDPLITEv6 0 0 1024 4 1 : tunables 54 27 8 : slabdata 0 0 0
UDPv6 2 8 1024 4 1 : tunables 54 27 8 : slabdata 2 2 0
tw_sock_TCPv6 0 0 320 12 1 : tunables 54 27 8 : slabdata 0 0 0
但上述结果需要自行计算各模块使用的内存情况,可以使用slabtop来查看主要的内存使用情况:
## 执行命令
slabtop --sort=c -o |head -n 17
## 输出结果
Active / Total Objects (% used) : 87179810 / 89818826 (97.1%)
Active / Total Slabs (% used) : 4166722 / 4166821 (100.0%)
Active / Total Caches (% used) : 105 / 184 (57.1%)
Active / Total Size (% used) : 15364477.91K / 15662436.88K (98.1%)
Minimum / Average / Maximum Object : 0.02K / 0.17K / 4096.00K
OBJS ACTIVE USE OBJ SIZE SLABS OBJ/SLAB CACHE SIZE NAME
73443720 73407245 99% 0.19K 3672186 20 14688744K dentry
15713530 13234322 84% 0.10K 424690 37 1698760K buffer_head
303044 244340 80% 0.55K 43292 7 173168K radix_tree_node
41660 41411 99% 0.98K 10415 4 41660K ext4_inode_cache
1053 1053 100% 16.00K 1053 1 16848K size-16384
183 183 100% 32.12K 183 1 11712K kmem_cache
1129 1079 95% 8.00K 1129 1 9032K size-8192
118177 74869 63% 0.06K 2003 59 8012K size-64
28260 28104 99% 0.19K 1413 20 5652K size-192
7452 7259 97% 0.64K 1242 6 4968K proc_inode_cache
slabtop displays detailed kernel slab cache information in real time. It displays a listing of the top caches sorted by one of the listed sort criteria. It also displays a statistics header filled with slab layer information.
slabtop命令可实时显示内核slab缓存信息,linux系统透过/proc/slabinfo来向用户暴露slab的使用情况。
PS:请勿在进程较多或内存较大的服务器如Redis服务器上运行slabtop命令。
dentry的使用状态可以使用/proc/sys/fs/dentry-state
来获取:
## 执行命令
cat /proc/sys/fs/dentry-state
## 输出信息
nr_dentry nr_unused age_limit want_pages nr_negative dummy
73563262 73554846 45 0 0 0
通过脚本来监控slab可以回收内存
和dentry使用内存
描述情况:
while true;
do
date_str=`date "+%Y-%m-%d %H:%M:%S"`;
memory_info=`cat /proc/meminfo |grep SReclaimable`;
dentry_info=`cat /proc/slabinfo |grep dentry`;
echo "${date_str} ${memory_info}";
echo "${date_str} ${dentry_info}";
sleep 1;
done
通过监控可发现dentry内存持续增长,可以通过监控dentry内存分配(d_alloc)和内存释放(d_free)来定位,创建dentry_chek.stp文件:
## dentry.stp
## 监控kernel上dentry内存分配和内存释放的进程信息
probe kernel.function("d_alloc")
{
printf("%s[%ld] %s %s\n", execname(), pid(), pp(), probefunc())
}
probe kernel.function("d_free")
{
printf("%s[%ld] %s %s\n", execname(), pid(), pp(), probefunc())
}
probe timer.s(5)
{
exit()
}
执行脚本并分析结果:
## 执行脚本并输入到日志文件
stap dentry_chek.stp 1>/tmp/dentry_chek.log 2>&1
## 分析日志文件命令
cat /tmp/dentry_chek.log | awk '{print $1"-->"$3}' | sort | uniq -c | sort -k1 -n -r| head -n 50
## 分析日志文件输出结果
15049 BackgrProcPool[13267]-->d_alloc
14707 BackgrProcPool[13267]-->d_free
8367 yum[8606]-->d_alloc
265 agent_daemon.py[5830]-->d_free
262 ZooKeeperRecv[13267]-->d_free
262 ZooKeeperRecv[13267]-->d_alloc
254 agent[7269]-->d_alloc
166 clickhouse-serv[13267]-->d_alloc
162 clickhouse-serv[13267]-->d_free
131 ZooKeeperSend[13267]-->d_free
131 ZooKeeperSend[13267]-->d_alloc
122 agent_daemon.py[5830]-->d_alloc
114 mysqld[2822]-->d_alloc
113 mysqld[2822]-->d_free
107 rdk:b/source[13267]-->d_free
107 rdk:b/source[13267]-->d_alloc
105 bash[27747]-->d_free
89 netstat[8586]-->d_alloc
62 TCPHandler[13267]-->d_free
60 irqbalance[3611]-->d_free
60 irqbalance[3611]-->d_alloc
58 TCPHandler[13267]-->d_alloc
56 sh[8578]-->d_free
55 SystemLogFlush[13267]-->d_alloc
50 bash[27747]-->d_alloc
47 snmpd[3679]-->d_free
47 snmpd[3679]-->d_alloc
42 netstat[8586]-->d_free
38 ParalInputsProc[13267]-->d_alloc
36 ParalInputsProc[13267]-->d_free
从上面的日志分析输出结果可以发现yum进程执行d_alloc的次数远远大于执行d_free的次数。
PS:BackgrProcPool进程的d_alloc与d_free延长是由于脚本执行时间点导致的。
安装stap工具
## 安装stap工具
yum install systemtap
stap运行依赖于kernel-devel和kernel-debuginfo,推荐使用stap-prep来安装。
如果通过yum来安装kernel-debuginfo,需要打开debug的yum源,并且严格匹配kernel版本,否则很容易报错或不兼容。
## 下载包:kernel-debuginfo-common-x86_64-xxx.rpm
## 下载包:kernel-debuginfo-xxx.rpm
## centos 7的下载路径
http://debuginfo.centos.org/7/x86_64/
## ## centos 6的下载路径
http://debuginfo.centos.org/6/x86_64/
对于循环执行命令,可以通过watch来监控:
watch -n 1 -d 'ps -ef|grep yum'
联系客服