buffer和cache的区别,如何查看缓存命中率

10 篇文章 1 订阅

查看buffer和cache

在linux环境中,我们常常使用free命令来查看系统内存的使用情况:

$ free
              total        used        free      shared  buff/cache   available
Mem:        7868592     3002332     1642244      735956     3224016     3761396
Swap:             0           0           0

显示的信息包括总的内存、已用内存、缓存、可用内存等,两行信息分别是物理内存和交换分区的使用情况。可以通过man free命令来查看字段的解释,其中buff/cache是我们关心的字段:

$ man free
......
DESCRIPTION
       ......
       buffers
              Memory used by kernel buffers (Buffers in /proc/meminfo)

       cache  Memory used by the page cache and slabs (Cached and Slab in /proc/meminfo)
       ......

从上面描述可以知道,Buffers 是内核缓冲区使用的内存,记录在 /proc/meminfo 中的 Buffers 中,而Cache 是页缓存和slabs 使用的内存,记录在 /proc/meminfo 中的 Cached 与 Slab 中。那具体的内核缓冲区和页缓存及slabs具体是什么含义并没有说明清楚。

buffer和cache的含义

这时我们通过man来查看proc虚拟文件系统的对应字段的解释:

$ man proc
......
       /proc/meminfo
              This file reports statistics about memory usage on the system.  It is used by free(1) to report the amount of free  and  used
              memory  (both physical and swap) on the system as well as the shared memory and buffers used by the kernel.  Each line of the
              file consists of a parameter name, followed by a colon, the value of the parameter, and an option unit of measurement  (e.g.,
              "kB").   The  list  below describes the parameter names and the format specifier required to read the field value.  Except as
              noted below, all of the fields have been present since at least Linux 2.6.0.  Some fields are displayed only  if  the  kernel
              was configured with various options; those dependencies are noted in the list.

              ......

              Buffers %lu
                     Relatively temporary storage for raw disk blocks that shouldn't get tremendously large (20MB or so).

              Cached %lu
                     In-memory cache for files read from the disk (the page cache).  Doesn't include SwapCached.
              ......
              Slab %lu
                     In-kernel data structures cache

从上面的解释可以看到,Buffers是主要是用于对原始磁盘块的数据缓存,一般不会特别大,20MB左右;Cache是用于缓存从磁盘读取的数据的页缓存,不包括SwapCached部分。

上面的解释其实还是比较清楚的,当应用需要将数据写入磁盘时,而每次写入的数据不是特别大时,内核会将多次的需要写入数据集成起来,放到Buffers,最后在一起写入磁盘;而Cached主要缓存从磁盘读取的文件,当cpu需要再次访问相同数据时,可以直接从Cached中直接获取,而不是从较慢的磁盘中读取。

Buffer和Cache主要是为了提供系统的I/O性能,不仅可以缓存需要写入的数据,也将需要经常读取的数据缓存起来,弥补可慢速的磁盘和高速cpu之间访问速度的差异。其中buffer主要提高了内存和磁盘的数据交换的速度,而cache主要是提高了cpu和内存之间的交换速度。

缓存命中率

缓存命中率,即直接通过缓存获取数据的请求次数,占所有数据请求次数的百分比。当可以直接通过缓存获取到需要的数据,则命中缓存;否则需要从磁盘等地方读取获取数据。缓存命中率越高,表示直接从缓存获取数据的次数越多,程序执行效率越高。
使用cachestat 可以查看整个个操作系统缓存的读写命中情况:
cachestat安装方式:

sudo apt install perf-tools-unstable

下面以1秒间隔输出三组缓存信息:

$ sudo cachestat 1
Counting cache functions... Output every 1 seconds.
    HITS   MISSES  DIRTIES    RATIO   BUFFERS_MB   CACHE_MB
    1989        0       13   100.0%          501       2600
   12969        0     1412   100.0%          501       2600
   16798        0     2803   100.0%          501       2600

从结果可以看到,HITS 是缓存命中的次数;MISSES是缓存未命中的次数;DIRTIES是表示新增到缓存中的脏页数;BUFFERS_MB 表示 Buffers 的大小,单位为MB;CACHED_MB表示Cache 的大小,单位为MB。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值