有没有任何方法或工具来转储内存缓存和缓冲区?

有没有任何方法或工具来转储内存缓存和缓冲区?

我想检查缓冲区中加载了哪些文件。很清楚,我指的缓冲区和缓存是运行free -m命令时显示的内容:

$ free -m
             total       used       free     shared    buffers     cached
Mem:          7800       7671        128          0        291        724
-/+ buffers/cache:       6655       1144
Swap:         5823        613       5210

答案1

看一眼linux-ftools。这套工具专门用于分析缓冲区和缓存。它包括以下工具:

芬科雷

$ fincore [options] files...

  --pages=false      Do not print pages
  --summarize        When comparing multiple files, print a summary report
  --only-cached      Only print stats for files that are actually in cache.

root@xxxxxx:/var/lib/mysql/blogindex# fincore --pages=false --summarize --only-cached * 
stats for CLUSTER_LOG_2010_05_21.MYI: file size=93840384 , total pages=22910 , cached pages=1 , cached size=4096, cached perc=0.004365 
stats for CLUSTER_LOG_2010_05_22.MYI: file size=417792 , total pages=102 , cached pages=1 , cached size=4096, cached perc=0.980392 
stats for CLUSTER_LOG_2010_05_23.MYI: file size=826368 , total pages=201 , cached pages=1 , cached size=4096, cached perc=0.497512 
stats for CLUSTER_LOG_2010_05_24.MYI: file size=192512 , total pages=47 , cached pages=1 , cached size=4096, cached perc=2.127660 
...

笔记:在上面的示例输出中,显示目录 中/var/lib/mysql/blogindex正在缓存的所有文件。在本例中,有多个名为CLUSTER_LOG_*.MYI.

法德维塞

SYNTAX: filename mode [offset] [,length]
Where mode can be:

  POSIX_FADV_NORMAL       No further special treatment.  
  POSIX_FADV_RANDOM       Expect random page references.  
  POSIX_FADV_SEQUENTIAL   Expect sequential page references.  
  POSIX_FADV_WILLNEED     Will need these pages.  
  POSIX_FADV_DONTNEED     Dont need these pages.  
  POSIX_FADV_NOREUSE      Data will be accessed once.  

Allows an application to to tell the kernel how it expects to use a file handle,
so that the kernel can choose appropriate read-ahead and caching techniques for
access to the corresponding file. This is similar to the POSIX version of the
madvise system call, but for file access instead of memory access. The
sys_fadvise64() function is obsolete and corresponds to a broken glibc API,
sys_fadvise64_64() is the fixed version. The following are the values for the
advice parameter:

FADV_NORMAL

No special treatment.

FADV_RANDOM

Expect page references in random order.

FADV_SEQUENTIAL

Expect page references in sequential order.

FADV_WILLNEED

Expect access in the near future.

FADV_DONTNEED

Do not expect access in the near future. Subsequent access of pages in this
range will succeed, but will result either in reloading of the memory contents
from the underlying mapped file or zero-fill-in-demand pages for mappings
without an underlying file.

FADV_NOREUSE

Access data only once.

错误定位

SYNTAX: fallocate file length

fallocate() allows the caller to directly manipulate the allocated disk space
for the file referred to by fd for the byte range starting at offset and
continuing for len bytes.

The mode argument determines the operation to be performed on the given
range. Currently only one flag is supported for mode:

FALLOC_FL_KEEP_SIZE

This flag allocates and initializes to zero the disk space within the range
specified by offset and len. After a successful call, subsequent writes into
this range are guaranteed not to fail because of lack of disk
space. Preallocating zeroed blocks beyond the end of the file is useful for
optimizing append workloads. Preallocating blocks does not change the file size
(as reported by stat(2)) even if it is less than offset+len.

If FALLOC_FL_KEEP_SIZE flag is not specified in mode, the default behavior is
almost same as when this flag is specified. The only difference is that on
success, the file size will be changed if offset + len is greater than the file
size. This default behavior closely resembles the behavior of the
posix_fallocate(3) library function, and is intended as a method of optimally
implementing that function.

Because allocation is done in block size chunks, fallocate() may allocate a
larger range than that which was specified.

清空缓冲区缓存

如果您想清空它,可以使用此命令链。

$ free && sync && echo 3 > /proc/sys/vm/drop_caches && free

             total       used       free     shared    buffers     cached
Mem:       1018916     980832      38084          0      46924     355764
-/+ buffers/cache:     578144     440772
Swap:      2064376        128    2064248
             total       used       free     shared    buffers     cached
Mem:       1018916     685008     333908          0        224     108252
-/+ buffers/cache:     576532     442384
Swap:      2064376        128    2064248

您可以通过更改上述命令的数字参数来通知 Linux 内核删除缓存项目的各个方面。

笔记:清理内存中不必要的东西(Kernerl 2.6.16 或更高版本)。始终确保首先运行同步以将有用的内容刷新到磁盘!

  • 释放页面缓存:

    $ echo 1 > /proc/sys/vm/drop_caches
    
  • 释放 dentry 和 inode:

    $ echo 2 > /proc/sys/vm/drop_caches
    
  • 释放页面缓存、目录项和索引节点:

    $ echo 3 > /proc/sys/vm/drop_caches
    

以上内容旨在以 root 身份运行。如果您尝试使用它们来完成它们sudo,那么您需要将语法稍微更改为如下所示:

$ sudo sh -c 'echo 1 >/proc/sys/vm/drop_caches'
$ sudo sh -c 'echo 2 >/proc/sys/vm/drop_caches'
$ sudo sh -c 'echo 3 >/proc/sys/vm/drop_caches'

上述方法的替代方法:

# alternative #1
$ sudo tee /proc/sys/vm/drop_caches <<<1
# alternative #2
$ echo "echo 1 > /proc/sys/vm/drop_caches" | sudo sh

为什么要改变语法?由于 原因,该/bin/echo程序以 root 身份运行sudo,但将 echo 的输出重定向到仅 root 文件的 shell 仍在以您的身份运行。您当前的 shell 执行重定向 sudo开始。

参考

答案2

你也可以尝试虚拟触摸

vmtouch opens every file provided on the command line and maps it into virtual memory        
with mmap(2). The mappings are opened read-only. It recursively crawls any directories 
and does the same to all files it finds within them.

With no options, vmtouch will not read from (touch) any memory pages. It will only use 
mincore(2) to determine how many pages of each file are actually resident in memory. 
Before exiting, it will print a summary of the total pages encountered and how many were 
resident.

使用它,您可以打印出内存中存在的内容,类似于 fincore。此外,您还可以逐出文件、将文件永久映射到缓存等。

答案3

echo 2 | sudo tee -a /proc/sys/vm/drop_caches

完美指挥

相关内容