Home > Linux, 调优 > Linux高速缓存使用率调查

Linux高速缓存使用率调查

June 1st, 2011

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

本文链接地址: Linux高速缓存使用率调查

Linux的高速缓存pagecache对性能的影响至关重要,但是实际系统中我们的利用率如何呢,特别是具体到每个设备的利用情况。

从下图我们可以很清楚的看到:

我们知道IO请求由vfs发起,经过pagecache缓存,挡不住的就落实到io设备去,那么统计这个利用率就很简单。 我们只要知道挡不住的IO的比例就好了。

我写了个systemtap脚本来解决这个问题:

$ uname -r
2.6.18-164.el5
 
$ sudo rpm -i kernel-debuginfo-common-2.6.18-164.el5.x86_64.rpm
$ sudo rpm -i kernel-debuginfo-2.6.18-164.el5.x86_64.rpm  

$ cat >pcmr.stp
global io_stat, blk_stat

global RD_CNT=2, WR_CNT=3

probe vfs.read.return {
  if ($return>0 && devname!="N/A") {
      io_stat[devname, 0] += $return
      io_stat[devname, RD_CNT] ++;
  }
}

probe ioblock.request {
  blk_stat[devname, rw] += size
  blk_stat[devname, rw+RD_CNT] ++;
} 

probe vfs.write.return {
  if ($return>0 && devname!="N/A") {
      io_stat[devname, 1] += $return
      io_stat[devname, WR_CNT] ++;
  }
}


global _io,_blk

probe timer.ms(5000) { 

foreach ([devname, action] in io_stat)
{
        _io[action] += io_stat[devname, action];
        _blk[action] += blk_stat[devname, action]
}

  if (_io[0] + _io[1]) {

    printf("\n%-25s, %s%db/%d, %s%db/%d, %s%db/%d, %s%db/%d, %s%d%%\n\n",
           ctime(gettimeofday_s()),
           "IO Read:", _io[0], _io[RD_CNT],
           "IO Write:", _io[1],_io[WR_CNT], 
           "Blk Read:", _blk[0], _blk[RD_CNT], 
           "Blk Write:", _blk[1],_blk[WR_CNT], 
           "PCMR:", (_blk[0]+_blk[1]) * 100 / (_io[0] + _io[1]))

        delete _io
        delete _blk
    
    /* print header */
    printf("%8s %2s %19s %19s %5s\n",
           "DEVICE","T", "IO", "BLK", "PCMR")
  }

  /* print top ten I/O */
  foreach ([devname, action] in io_stat- limit 10)
     if(action < RD_CNT) 
        printf("%8s %2s %11db/%6d %11db/%6d %4d%%\n",
           devname, action?"W":"R",
           io_stat[devname, action],io_stat[devname, action+RD_CNT],
           blk_stat[devname, action],blk_stat[devname, action+RD_CNT],
           (blk_stat[devname, action] * 100) / io_stat[devname, action]
           )

  /* clear data */
  delete io_stat
  delete blk_stat
}

probe begin
{
println("::");
}

CTRL+D

我们来演示下:

$ sudo stap pcmr.stp    
::
...
Wed Jun  1 09:16:16 2011 , IO Read:551320328b/124024, IO Write:0b/0, Blk Read:103219200b/17536, Blk Write:0b/0, PCMR:18%

  DEVICE  T                  IO                 BLK  PCMR
    fioa  R   493284566b/119460   103219200b/ 17536   20%
    sda2  R    41100816b/   624           0b/     0    0%
    sda5  R    16934946b/  3940           0b/     0    0%

Wed Jun  1 09:16:21 2011 , IO Read:554808170b/123568, IO Write:2295b/9, Blk Read:101883904b/17296, Blk Write:135168b/33, PCMR:18%

  DEVICE  T                  IO                 BLK  PCMR
    fioa  R   491003095b/118908   101543936b/ 17259   20%
    sda3  R    42119596b/   136           0b/     0    0%
    sda5  R    17730097b/  4061      163840b/    18    0%
    sda1  R     2399374b/    33           0b/     0    0%
    sda2  R     1543938b/   424      176128b/    19   11%
    sda6  R       12070b/     6           0b/     0    0%
    sda3  W        2295b/     9      135168b/    33 5889%
...

参数我们来解释下:
IO Read: vfs层面看到的IO读/次数
IO Write: vfs层面看到的IO写/次数
Blk Read: 块层面看到的IO读/次数
Blk Write: 块层面看到的IO写/次数
PCMR: pagecache miss rate =100 * blk / io, 由于文件读写是以page为单位,所以可能>100%
第一行是汇总统计,每隔5秒打印一次。
总结: PCMR越小越好,说明高速缓存利用率高。

玩得开心!

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

Categories: Linux, 调优 Tags: ,
  1. zhouyf
    June 2nd, 2011 at 12:29 | #1

    kernel-debuginfo-2.6.18-164.el5.x86_64.rpm
    这个包怎么要500多M,
    不好玩呀:)

    Yu Feng Reply:

    安装下又不麻烦。。。

  2. 刺猬
    June 2nd, 2011 at 15:22 | #2

    挡不住的就落实到io设备去
    这个用词太传神了

    Yu Feng Reply:

    呵呵,小学没学好,请原谅,:(

  3. lipingtababa
    June 15th, 2011 at 13:15 | #3

    systemtap这么神奇,但是只看到你一个用户,身边的同事都没人用呢, 这是怎么回事?

    Yu Feng Reply:

    车好,为什么还那么多人走路?信用卡好,为什么那么多人没有?用的人自然用,不用的人永远用不到。

  4. shusl
    June 28th, 2011 at 18:49 | #4

    如果在mysql数据库服务器上,PCMR很高,应该怎么改善呢?

    Yu Feng Reply:

    pagecache的内存搞大。

    shusl Reply:

    多谢霸神,回复的这么快。

  5. June 29th, 2011 at 15:54 | #5

    霸神的systemtap用的真实生龙活虎啊~!

    Yu Feng Reply:

    工具用起来才有价值!

  6. blackhat
    July 3rd, 2011 at 22:21 | #6

    分析的方法不错,学习了。

  7. luqi
    June 24th, 2012 at 11:29 | #7

    哥,问一下,那张图哪里搞来的?有原始链接没?

    web Reply:

    IBM redbook
    <>
    地址:
    http://www.redbooks.ibm.com/redpapers/pdfs/redp4285.pdf

  8. web
    May 5th, 2014 at 15:10 | #8

    t

Comments are closed.