Linux系统内存使用率该怎么计算

Linux系统内存使用率该怎么计算

郝朝阳 DevOps视角

前言

由于公司逐步使用自己研发的基于docker的云平台,所以监控系统逐步由open-falcon迁移到Prometheus。我写了自动分析各个业务线一个月内服务器性能,并在每天早上10点30分自动发送给相关业务线负责人。数据通过Prometheus的API获取的。其中内存使用率存在很大的分歧。两个平台使用的计算方式很不一样,并且业务方也经常问这个问题。
open-falcon的内存使用率计算方式为:(memtotal-(free+buffer+cache))/memtotal
Prometheus的内存使用率计算方式为:(memtotal-available)/memtotal
下面是同一台机器,两个平台按照各自的算法一天内的内存使用曲线图
Linux系统内存使用率该怎么计算
open-falcon平台显示的内存使用率
Linux系统内存使用率该怎么计算
Prometheus平台按照(memtotal-available)/memtotal计算方法得出的曲线图
Linux系统内存使用率该怎么计算
Prometheus平台按照(memtotal-(free+buffer+cache))/memtotal计算方法得出的曲线图,和open-falcon平台的曲线图能够保持一致。

理解内存指标

要想知道哪种计算方式更适合,就需要对内存的指标有正确的理解。

free

free手册对free的定义如下:


free   Unused memory (MemFree and SwapFree in /proc/meminfo)

free是未使用的内存,对应的/proc/meminfo的MemFree。
proc手册对MemFree的定义为:


MemFree %lu
   The sum of LowFree+HighFree.

buffer和cache

要想知道到底使用哪个公式计算比较合适,这就要先理解内存的各项指标的含义。首先来理解buffer和cache。
通过free的手册可以找到buffer和cache的介绍:


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是内核页缓存和Slab用到的内存,对应的是/proc/meminfo的Cached与Slab之和
    这里只告诉我们通过free获取的值都来自/proc/meminfo,单更具体的Buffers、Cached和Slab的含义,还是没有说详细。怎么知道更详细的定义呢?我们可以看下proc文件系统手册。

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.
SReclaimable %lu (since Linux 2.6.19)
    Part of Slab, that might be reclaimed, such as caches.
SUnreclaim %lu (since Linux 2.6.19)
    Part of Slab, that cannot be reclaimed on memory pressure.     

通过proc文件系统的文档我们可以知道:

  • Buffers是对原始磁盘块的临时存储,也就是用来缓存磁盘的数据的,通常不会太大(20M左右)。
  • Cached是从磁盘读取文件的页缓存,也就是用来缓存从文件读取的数据。并且不包括 SwapCached。
  • slab包括SReclaimable和SUnreclaim两部分。可回收的用SReclaimable不可回收的是SUnreclaim记录。

对于buffer和cache进行总结的话,可以理解为buffer是对磁盘数据的缓存,cache是文件数据的缓存。

available

free手册对available的介绍:


available    Estimation of how much memory is available for starting new applications, without swapping. Unlike the  data  provided  by the  cache or free fields, this field takes into account page cache and also that not all reclaimable memory slabs will be reclaimed due to items being in use (MemAvailable in  /proc/meminfo,  available  on  kernels  3.14,  emulated  on  kernels 2.6.27+, otherwise the same as free)

可以知道,available是在不swap的情况下,评估要启动一个新的应用时有多少内存可以使用。并也把页缓存和正在使用的可以回收的内存部分考虑进去了。对应的是/proc/meminfo的MemAvailable的值。此时可以得出一个结论:available是包括系统中虽然已经使用但是可以回收的内存。比如cache、buffer和slab中都有一部分可以回收。

MemAvailable的定义如下:


An estimate of how much memory is available for starting new applications, without swapping. Calculated from MemFree, SReclaimable, the size of the file LRU lists, and the low watermarks in each zone.  The estimate takes into account that the system needs some page cache to function well, and that not all reclaimable slab will be reclaimable, due to items being in use. The impact of those factors will vary from system to system.

可以得到这样的一个结论:MemAvailable约等于MemFree+Buffers+Cached,但两者并不相等。MemAvailable相对较小。

结论

从上面的各个内存项目的介绍来说,在应用层面来评估可以内存使用MemAvailable更合适。因此,内存使用率采用(memtotal-available)/memtotal的计算方式是最适合评估系统能否再启动一个新应用的。

猜你喜欢

转载自blog.51cto.com/15127511/2657921