vmtouch命令 -

前段时间在dbanotes上看到一篇讲Instagram后台架构的文章,文中提到了Instagram使用的一个小命令vmtouch,觉得挺有意思的,特此推荐一下。

先看一下vmtouch作者给的介绍:

vmtouch is a tool for learning about and controlling the file system cache of unix and unix-like systems.

从介绍可以看出vmtouch是一个管理和控制Unix和类Unix文件系统缓存的工具。

vmtouch的主要功能如下:

  • 查看一个文件(或者目录)哪些部分在内存中;
  • 把文件调入内存;
  • 把文件清除出内存;
  • 把文件锁住在内存中而不被换出到磁盘上;

由于vmtouch是个比较小众的命令,很多Linux发行版不带这个命令,所以如果要使用这个命令要自己先编译一个。

抄了几个vmtouch的用法:

1. 看看/bin目录有多少内容在内存中

$ vmtouch /bin/
Files: 92
Directories: 1
Resident Pages: 348/1307 1M/5M 26.6%
Elapsed: 0.003426 seconds

2. 看看某文件(big-dataset.txt)有多少在内存中

How much of big-dataset.txt is currently in memory?

$ vmtouch -v big-dataset.txt
big-dataset.txt
[ ] 0/42116

Files: 1
Directories: 0
Resident Pages: 0/42116 0/164M 0%
Elapsed: 0.005182 seconds

然后读入部分文件,

$ tail -n 10000 big-dataset.txt > /dev/null

然后在用vmtouch查看一下:

$ vmtouch -v big-dataset.txt
big-dataset.txt
[ oOOOOOOO] 4950/42116

Files: 1
Directories: 0
Resident Pages: 4950/42116 19M/164M 11.8%
Elapsed: 0.006706 seconds

我们可以看出big-datset.txt开始时没有数据在内存中,在用tail命令读入了部分数据后,有19MB的数据进入了内存。

3. 把文件(a.txt)清除出内存

$ vmtouch -ve a.txt
Evicting a.txt

Files: 1
Directories: 0
Evicted Pages: 42116 (164M)
Elapsed: 0.076824 seconds

vmtouch主要作用是做数据的warmup,即对于将要用到的数据,通过vmtouch把它们事先读入内存,而不是在需要时再从硬盘上读入,这样可以提高系统效率。

 

对于vmtouch的实现,一个核心的函数是mincore(), 看一下它的manual:

mincore - determine whether pages are resident in memory

       #include <unistd.h>
       #include <sys/mman.h>

       int mincore(void *addr, size_t length, unsigned char *vec)

mincore()需要调用者传入文件的地址(通常由mmap()返回),它会把文件在内存中的情况写在vec中。

posted @ 2012-02-28 17:37  coldplayerest  阅读(8185)  评论(0编辑  收藏  举报