有没有可能列出caching的文件?

这里是free -m的输出:

  total used free shared buffers cached Mem: 7188 6894 294 0 249 5945 -/+ buffers/cache: 698 6489 Swap: 0 0 0 

我可以看到7GB内存中有将近6GB (5945MB)的内存用于caching文件。 我知道如何冲洗caching。 我的问题是: 是否有可能看到哪些文件(或inode)被caching?

    那么,有一个简单的方法来看看内核的页面caching,如果你碰巧有ftools – “fincore”给你一些关于什么文件的页面是caching内容的摘要信息。

    您将需要提供一个文件名列表来检查它们在页面caching中的存在。 这是因为存储在内核页面caching表中的信息只包含数据块引用而不包含文件名。 fincore将通过inode数据parsing给定文件的数据块,并在页面caching表中search相应的条目。

    没有有效的search机制来做相反的事情 – 获取属于数据块的文件名需要读取文件系统上的所有inode和间接块。 如果您需要了解存储在页面caching中的每个文件块,则需要提供文件系统上所有文件的列表以进行fincore 。 但是这又可能会损害测量,因为大量的数据将被读取遍历目录,并获取所有的inode和间接块 – 将它们放入页面caching,并驱逐您试图检查的页面caching数据。

    您可以使用vmtouch实用程序查看指定的文件或目录是否在caching中。 您还可以使用该工具强制项目caching或将其locking到caching中。

     [root@xt ~]# vmtouch -v /usr/local/var/orca/procallator.cfg /usr/local/var/orca/procallator.cfg [ ] 0/5 Files: 1 Directories: 0 Resident Pages: 0/5 0/20K 0% Elapsed: 0.000215 seconds 

    现在我可以“触摸”到caching中。

     [root@xt ~]# vmtouch -vt /usr/local/var/orca/procallator.cfg /usr/local/var/orca/procallator.cfg [OOOOO] 5/5 Files: 1 Directories: 0 Touched Pages: 5 (20K) Elapsed: 0.005313 seconds 

    现在看看有多lesscaching…

     [root@xt ~]# vmtouch -v /usr/local/var/orca/procallator.cfg /usr/local/var/orca/procallator.cfg [OOOOO] 5/5 Files: 1 Directories: 0 Resident Pages: 5/5 20K/20K 100% Elapsed: 0.000241 seconds 

    我写了一个非常简单的shell脚本来使用linux-fincore来显示caching的文件。 由于caching是内存的一部分,因此我的代码是find进程的前10个RSZ使用情况,并使用lsof来查找打开进程的文件,最后使用linux-fincore查找这些文件是否被caching。

    请纠正我,如果我想错了。

     #!/bin/bash #Author: Shanker #Time: 2016/06/08 #set -e #set -u #you have to install linux-fincore if [ ! -f /usr/local/bin/linux-fincore ] then echo "You haven't installed linux-fincore yet" exit fi #find the top 10 processs' cache file ps -e -o pid,rss|sort -nk2 -r|head -10 |awk '{print $1}'>/tmp/cache.pids #find all the processs' cache file #ps -e -o pid>/tmp/cache.pids if [ -f /tmp/cache.files ] then echo "the cache.files is exist, removing now " rm -f /tmp/cache.files fi while read line do lsof -p $line 2>/dev/null|awk '{print $9}' >>/tmp/cache.files done</tmp/cache.pids if [ -f /tmp/cache.fincore ] then echo "the cache.fincore is exist, removing now" rm -f /tmp/cache.fincore fi for i in `cat /tmp/cache.files` do if [ -f $i ] then echo $i >>/tmp/cache.fincore fi done linux-fincore -s `cat /tmp/cache.fincore` rm -f /tmp/cache.{pids,files,fincore} 

    你也可以使用pcstat(Page Cache Stat) https://github.com/tobert/pcstat

    希望它可以帮助别人。