vmtouch 使用方法

2021/7/5

近来在循环跑自己写的程序的时候,发现读取文件占了不少的时间,想利用系统(Linux)提前将文件读入内存,以提高加载速度,在循环中提前加载也变相等于异步的曲线救国把,于是找到了 vmtouch (opens new window)

# 安装

$ git clone https://github.com/hoytech/vmtouch
$ cd vmtouch
$ make
$ sudo make install
1
2
3
4

# 优点

  • 发现您的操作系统正在缓存哪些文件
  • 告诉操作系统缓存或在缓存中清理某些文件或文件区域
  • 将文件锁定到内存中,以便操作系统不会清理它们
  • 故障转移服务器时保留虚拟内存配置文件
  • 保持“热备”文件服务器
  • 随时间绘制文件系统缓存使用情况
  • 维护缓存使用的“软配额”
  • 加速批处理/cron 作业
  • 等等

# 用法

vmtouch v1.3.1 - the Virtual Memory Toucher by Doug Hoyte
Portable file system cache diagnostics and control

用法:vmtouch [OPTIONS] ... 文件或目录 ...

选项:
Options:
  -t touch pages into memory                        将文件载入内存
  -e evict pages from memory                        将文件从内存中清除
  -l lock pages in physical memory with mlock(2)    用 mlock(2) 锁定物理内存中的页面
  -L lock pages in physical memory with mlockall(2) 用 mlockall(2) 锁定物理内存中的页面
  -d daemon mode                                    守护进程模式
  -m <size> max file size to touch                  要载入的文件最大大小
  -p <range> use the specified portion instead of the entire file
                                                    使用指定的部分而不是整个文件
  -f follow symbolic links                          跟随符号链接
  -F don't crawl different filesystems              不抓取不同的文件系统
  -h also count hardlinked copies                   也计算硬链接副本
  -i <pattern> ignores files and directories that match this pattern
                                                    忽略匹配此模式的文件和目录
  -I <pattern> only process files that match this pattern
                                                    只处理匹配此模式的文件
  -b <list file> get files or directories from the list file
                                                    从列表文件中获取文件或目录
  -0 in batch mode (-b) separate paths with NUL byte instead of newline
                                                    批处理模式 (-b) 使用 NUL 字节而不是换行符分隔路径
  -w wait until all pages are locked (only useful together with -d)
                                                    等待所有页面被锁定(仅与 -d 一起使用)
  -P <pidfile> write a pidfile (only useful together with -l or -L)
                                                    写一个pidfile(只与-l 或-L 一起使用)
  -v verbose                                        详细输出
  -q quiet                                          简略输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 使用示例

参见 官网示例 (opens new window)

这里我主要在 python 中利用多进程一边运行,一边完成文件的提前缓存

Last Updated: 2023-10-29T08:26:04.000Z