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越小越好,說明高速緩存利用率高。