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越小越好,说明高速缓存利用率高。
原文转自:http://blogread.cn/it/article/3687?f=wb