Home > Linux, 工具介绍, 调优 > systemtap观察page_cache的使用情况

systemtap观察page_cache的使用情况

原创文章,转载请注明: 转载自系统技术非业余研究

本文链接地址: systemtap观察page_cache的使用情况

在规划服务器的内存使用的时候经常需要知道应用在理想情况下会使用多少的pagecache, 我们好预先把这个内存预留出来.

这个值操作系统没有提供可查看的管道,我们只能自己写个脚本来实现.

下面的systemtap脚本每隔N秒显示下当前os下头10个文件占用多少的pagecache, 降序排列.


$ cat > pagecache.stp
global __filenames
global pagecache

probe vfs.add_to_page_cache
{
  pagecache[ino]++;
}

probe vfs.remove_from_page_cache
{
  pagecache[ino]--;
}

probe generic.fop.open
{
__filenames[ino]=filename
}


function find_filename(ino)
{
  if (ino in __filenames) 
    return __filenames[ino];
  else return sprintf("N/A ino:%d", ino);
}

probe timer.s($1)
{
  ansi_clear_screen();
  printf ("%50s %10s\n", "FILENAME", "COUNT")
 foreach( pages = ino in pagecache- limit 10)
 {
   if(pages)
   printf("%50s %10d\n", find_filename(ino), pages);
 }

}

CTRL+D
$ sudo sysctl vm.drop_caches=3

$ sudo stap pagecache.stp  1
                                          FILENAME      COUNT
                                   librpmdb-4.4.so        173
                               libpython2.4.so.1.0        153
                                 libxml2.so.2.6.26        107
                                  N/A ino:68781310        100
                                     __m2crypto.so         91
                           libglib-2.0.so.0.1200.3         64
                                        libperl.so         53
                                     librpm-4.4.so         52
                                        pyexpat.so         45
                                libreadline.so.5.1         38

#拷贝个文件看看pagecache的变化
$ dd if=/dev/zero of=test.dat count=1024 bs=4096

玩得开心!

Post Footer automatically generated by wp-posturl plugin for wordpress.

  1. asterisk622
    June 8th, 2011 at 12:07 | #1

    你好,我发现if else后面必须有{},我是ubuntu10.04 2.6.32-32-generic #62
    还有就是下面的错误,请教如何解决啊

    [asterisk@ systemtap]$sudo stap –vp 1 pagecache.stp 1
    parse error: expected ‘in’
    saw: operator ‘=’ at pagecache.stp:33:18
    source: foreach( pages = ino in pagecache- limit 10) {
    ^
    parse error: expected ‘probe’, ‘global’, ‘function’, or ‘%{‘
    saw: operator ‘}’ at pagecache.stp:37:3
    source: }
    ^
    parse error: expected ‘probe’, ‘global’, ‘function’, or ‘%{‘
    saw: operator ‘}’ at pagecache.stp:38:1
    source: }
    ^
    3 parse error(s).
    Pass 1: parsed user script and 59 library script(s) in 70usr/20sys/100real ms.
    Pass 1: parse failed. Try again with another ‘–vp 1’ option.

    [Reply]

    Yu Feng Reply:

    我的stap是1.4版本的。。。

    [Reply]

    asterisk622 Reply:

    [zhangtao@ systemtap]$stap -V
    SystemTap translator/driver (version 1.0/0.143 Debian version 1.0-2)
    我用apt-get装的版本太低了,自己编译一个看看

    [Reply]

    Yu Feng Reply:

    linux 2.6.32+systemtap 1.4会很稳定。

  2. huangbt
    December 20th, 2013 at 17:41 | #2

    您好!
    请问下,__filenames[ino]=filename;这个ino是什么东西啊,为什么 可以直接这样子用呢?

    [Reply]

    Yu Feng Reply:

    vfs.* 是个别名的stap检查点,system带的stp已经对参数进行格式化了。
    probe vfs.add_to_page_cache =
    kernel.function(“add_to_page_cache_locked”) !,
    kernel.function(“add_to_page_cache”)
    {
    dev = $mapping->host->i_sb->s_dev
    devname = __find_bdevname(dev, $mapping->host->i_sb->s_bdev)
    ino = $mapping->host->i_ino

    index = $offset
    nrpages = $mapping->nrpages
    size = $mapping->nrpages
    units = “pages”

    name = “vfs.add_to_page_cache”
    argstr = sprintf(“%d, %d”, ino, $offset)
    }

    [Reply]

  1. September 27th, 2013 at 15:15 | #1