博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vmtouch命令 -
阅读量:6912 次
发布时间:2019-06-27

本文共 1744 字,大约阅读时间需要 5 分钟。

前段时间在dbanotes上看到,文中提到了Instagram使用的一个小命令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中。

转载于:https://www.cnblogs.com/coldplayerest/archive/2012/02/28/2371881.html

你可能感兴趣的文章
[Head First设计模式]策略模式
查看>>
阿里云ECS服务器源配置
查看>>
github插件
查看>>
iOS重绘机制drawRect
查看>>
Elementary Methods in Number Theory Exercise 1.2.4
查看>>
Spring+Ehcache
查看>>
winform:界面加载时自定义选中Button
查看>>
第四周总结
查看>>
maven-surefire-plugin:jar:2.12.4 has not been downloaded from it before.
查看>>
PXE 自动安装物理机 (DHCP服务由路由提供, 不能再配置)
查看>>
Python中的单例模式
查看>>
Android开发基础知识
查看>>
Ubuntu的快捷键
查看>>
视频压缩编码问答--转载
查看>>
android ListView中使用notifyDataSetChanged()不刷新
查看>>
Sandcastle入门:创建C#帮助文档
查看>>
[bzoj 4036][HAOI2015]按位或
查看>>
Django的ModelForm
查看>>
C++对象指针—指向对象成员的指针
查看>>
supermap使用小结
查看>>