统计Linux中各个文件占用cache的情况,使用工具fincore。
可以从GitHub上获取到源码:https://github.com/david415/linux-ftools.git
下载后,./configure; make; make install。在 /usr/local/bin 目录下生成3个工具:fadvise fallocate fincore。最后一个就是查询文件cache所用到的工具。fincore的基本原理是根据文件名找到inode,再对比kernel中的page cache table,得到结果。
工具有3个可选参数,并需要指定被查询的文件:
ubuntu:/usr/local/bin$ fincore
fincore version 1.0.0
fincore [options] files...
--pages=false Don't print pages
--summarize When comparing multiple files, print a summary report
--only-cached Only print stats for files that are actually in cache.
对某个文件查询可以使用如下命令:
ubuntu:~/test/dir1$ fincore --pages=false --summarize --only-cached f11
filename size total pages cached pages cached size cached percentage
f11 12 1 1 4096 100.000000
---
total cached size: 4096
fincore在查询cache的时候需要指定文件名,想要查一下系统中哪些文件占用的cache比较大,可以使用下面的脚本。
基本思路是:find 递归查找,将结果作为fincore的输入,再根据占用的cache大小排序,作为最终输出。这个方法有一个缺点是如果文件间存在硬链接,那么这个文件会被统计多次。
# !/bin/bash
if [ -e ./log ]; then
rm -f log;
fi
sum=0
count_file_cache()
{
for name in `find $1`; do
res=`fincore --pages=false --summarize --only-cached $name 2>/dev/null | grep "total cached size" | awk '{print int($4)}'`;
if [[ $res -ne 1 && $res -ne 0 ]]; then
echo $res $name >> log;
sum=`expr $sum + $res`
fi
done
}
count_file_cache $1
sum=`expr $sum / 1024 / 1024`
echo "sum =" $sum "MB" > log2
echo "-------------------------------" >> log2
cat log | sort -k1 -g -r >> log2
mv log2 log
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)