Linux中各文件占用的Cache统计

        统计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

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值