linux内存-buffer和cache

一、什么是Buffer/Cache?
buffer/cache就是cpu和磁盘之间的一层内存缓存,用于优化磁盘的读写性能,
1、从写数据的维度,可以将多次I/O操作合并后,变成单次IO操作,提高写入磁盘的效率。
2、从读数据的维度,将频繁访问的数据缓存到内存,加快访问数据速度,降低磁盘的I/O压力
 
二、Buffer和cache的区别
Buffer和cache都是缓存,那区别在哪里呢?
1、Buffer 是对磁盘数据的缓存,既会用在读请求中,也会用在写请求中。
2、Cache 是文件数据的缓存,既会用在读请求中,也会用在写请求中。

三、查看buffer和cache
1、free命令
# free
              total        used        free      shared  buff/cache   available
Mem:        8173556     1212644     2251324      419156     4709588     6235780
Swap:             0           0           0
  • buffers:是指内核缓存区用到的内存,原始磁盘块的临时存储,通常不会太大
  • cache:是由内核页缓存+slab中的SReclaimable
2、vmstat命令
# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 7960080   2068  84076    0    0   872    46  334  343  3  4 92  0  0
 0  0      0 7960080   2068  84076    0    0     0     0  170  257  0  0 100  0  0
 1  0      0 7959964   2068  84092    0    0     0     0   38   63  0  0 100  0  0
  • bi: Blocks received from a block device (blocks/s). 每秒从磁盘读取的块数量,linux的块大小为1K,所以单位也是KB/s
  • bo: Blocks sent to a block device (blocks/s). 每秒写入到磁盘的块数量

buffer和cache跟free命令一致

四、写文件,使用cache进行缓存数据
清理文件页、目录项、Inodes等各种缓存

echo 3 > /proc/sys/vm/drop_caches

通过读取随机设备,生成一个 500MB 大小的文件

# dd if=/dev/urandom of=/tmp/file bs=1M count=500

观察buffer、cache变化

# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  0  0      0 7822584      0 154576    0    0     0     0  365  854  0  1 99  0  0
 0  0      0 7822584      0 154576    0    0     0     0  347  855  0  1 99  0  0
 0  0      0 7822584      0 154576    0    0     0     0  334  843  0  1 99  0  0
 1  0      0 7756344      0 220420    0    0    85     0 1319 1173  0 15 85  0  0
 1  0      0 7646852      0 329780    0    0     0     0 1768 1302  0 25 74  0  0
 3  0      0 7533308      0 443264    0    0     0     0 1737 1299  0 26 74  0  0
 1  0      0 7416616      0 560068    0    0     0     0 1838 1364  0 25 75  0  0
 0  0      0 7296836      0 680340    0    0     0 266308 2931 1461  0 27 70  3  0
 0  0      0 7297264      0 680340    0    0     0     0  353  841  0  1 99  0  0
 0  0      0 7297264      0 680340    0    0     0     0  309  834  0  0 99  0  0
 0  0      0 7297264      0 680340    0    0     0     0  298  844  0  0 99  0  0
 0  0      0 7297152      0 680340    0    0     0     0  792 1401  2  2 97  0  0
 1  0      0 7296092      0 680340    0    0     0 245760 1110  878  0  2 96  2  0
 0  0      0 7296812      0 680340    0    0     0     0  419  807  0  1 99  0  0
 0  0      0 7296688      0 680340    0    0     0    27  320  847  0  1 99  0  0
 0  0      0 7296712      0 680340    0    0     0     0  320  826  0  1 99  0  0
 0  0      0 7296712      0 680340    0    0     0     0  324  819  0  1 99  0  0

在Cache刚开始增长时,块设备I/O很少,bi只出现了一次85KB/s,bo没有任何变化。
而过一段时间后,才会出现大量的块设备写,比如bo变成了266308
在此过程中buff没有变化
当 dd 命令结束后,Cache 不再增长,但块设备写还会持续一段时间,并且,多次 I/O 写的结果加起来,才是 dd 要写的 500M 的数据

五、写磁盘,使用buffer缓存数据
清理文件页、目录项、Inodes等各种缓存

echo 3 > /proc/sys/vm/drop_caches

通过读取随机设备,生成一个 500MB 大小的文件

# dd if=/dev/urandom of=/dev/vda1 bs=1M count=2048

观察buffer、cache变化

 # vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 7828444      0 151680    0    0     0     0  315  798  0  1 99  0  0
 0  0      0 7828444      0 151680    0    0     0     0  349  849  0  1 99  0  0
 0  0      0 7828444      0 151680    0    0     0     8  342  845  0  1 99  0  0
 1  0      0 7807608  17408 154724    0    0    76     0 1417 1577  2  6 93  0  0
 1  0      0 7664600 136192 179284    0    0     0     0 3168 1380  0 26 74  0  0
 1  0      0 7521160 254976 203616    0    0     0     0 2889 1390  0 26 74  0  0
 1  0      0 7391464 362496 225988    0    0     0     0 2496 1403  0 25 74  0  0
 1  0      0 7245052 482468 251000    0    0     0 53276 2918 1380  0 28 70  2  0
 1  0      0 7098744 604160 276352    0    0     0     0 2548 1367  0 26 74  0  0
 1  0      0 6952896 724992 300992    0    0     0     0 3041 1406  0 26 74  0  0
 1  0      0 6807908 844800 326252    0    0    16 53282 3362 1409  0 28 72  0  0
 1  0      0 6676668 953344 348560    0    0     0 106496 4649 1393  0 30 70  0  0
 2  0      0 6534180 1070080 372796    0    0     0 293196 6423 1400  0 38 61  0  0
 3  0      0 6389396 1188864 397324    0    0     0 539969 10492 1609  0 48 48  3  0
 2  0      0 6247256 1308760 422588    0    0     0 18803 3333 1353  0 27 73  0  0
 1  0      0 6105508 1426432 446796    0    0     0     0 3183 1326  0 26 74  0  0
 1  0      0 5964620 1543168 470900    0    0     0     0 3839 1408  0 26 74  0  0
 1  0      0 5821376 1661952 495804    0    0     0     0 3396 1397  0 26 74  0  0
 4  0      0 5675528 1782784 520544    0    0     0     0 3044 1400  0 26 74  0  0
 2  0      0 5531668 1899520 544824    0    0     0 510784 10551 1543  0 48 49  2  0
 1  0      0 5390060 2018304 569404    0    0     0 255168 6388 1519  0 38 60  3  0
 0  0      0 5294572 2097668 586040    0    0   700     0 2476 1276  0 17 83  0  0
 0  0      0 5295184 2097668 586132    0    0     0     0  333  804  0  1 99  0  0
 0  0      0 5295260 2097668 586132    0    0     0     0  338  847  0  1 99  0  0
 0  0      0 5295260 2097668 586132    0    0     0     0  344  831  0  1 99  0  0
 0  1      0 5291320 2097668 586132    0    0     0 155886 2083  970  0  8 89  3  0
 0  0      0 5294656 2097668 586132    0    0     0 110354 2381  876  0  6 94  0  0
 0  0      0 5295160 2097668 586132    0    0     0     0  301  808  0  1 99  0  0
 0  0      0 5295160 2097668 586132    0    0     0     0  297  806  0  1 99  0  0

Buffer 和 Cache 都在增长,但显然 Buffer 的增长快得多

七、读文件,使用cache缓存数据
清理文件页、目录项、Inodes等各种缓存

echo 3 > /proc/sys/vm/drop_caches

读取文件

dd if=/tmp/file of=/dev/null

观察buffer、cache变化

 # vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  0  0      0 7827972      0 151644    0    0     0     0  312  841  0  1 99  0  0
 0  0      0 7827972      0 151644    0    0     0     0  324  817  0  1 99  0  0
 0  0      0 7827944      0 151644    0    0     0     0  837 1382  1  2 97  0  0
 0  0      0 7828344      0 151644    0    0     0     0  324  846  0  1 99  0  0
 1  0      0 7653356      0 327816    0    0 176028     0 1906  865  9 16 75  0  0
 1  0      0 7464400      0 515952    0    0 188416     0 1927  824 10 16 74  0  0
 0  0      0 7316380      0 664412    0    0 147632     0 2121 1553  9 12 79  0  0
 0  0      0 7316404      0 664416    0    0     0     0  330  843  0  1 99  0  0
 0  0      0 7316404      0 664416    0    0     0     8  346  886  0  0 99  0  0

Buffer 保持不变,而 Cache 则在不停增长

八、读磁盘,使用buffer缓存数据
清理文件页、目录项、Inodes等各种缓存

echo 3 > /proc/sys/vm/drop_caches

读取磁盘

dd if=/dev/vda1 of=/dev/null bs=1M count=1024

观察buffer、cache变化

 # vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 7819980      0 152412    0    0     0     0  371  867  0  1 99  0  0
 0  0      0 7819980      0 152412    0    0     0     0  337  834  0  1 99  0  0
 1  0      0 7128772 690176 152912    0    0 690256     0 2771 1309  1  8 89  2  0
 0  0      0 6763520 1054720 153988    0    0 364544     0 1682 1050  0  5 94  1  0
 0  0      0 6763020 1054720 154316    0    0   220     0  498  913  1  2 98  0  0
 0  0      0 6762792 1054720 154484    0    0     0     0  337  853  0  1 99  0  0
0  0      0 6762916 1054720 154484    0    0     0     0  324  826  0  1 99  0  0
0  0      0 6762916 1054720 154484    0    0    16   140  353  857  0  1 99  0  0
1  0      0 6762916 1054720 154484    0    0     0     0  342  838  0  0 99  0  0
0  0      0 6762916 1054720 154488    0    0     0     0  700 1316  0  1 99  0  0
0  0      0 6762916 1054720 154492    0    0     0     0  455 1009  1  1 99  0  0

Buffer 和 Cache 都在增长,但显然 Buffer 的增长快很多

原文地址:https://www.cnblogs.com/guoxianqi2020/p/13885750.html