LinuxでI/Oと格闘していると、重要なファイルがどのタイミングでキャッシュに乗ってくるかは死活問題になります。
このファイルってどれくらいキャッシュに乗っているの?という時に便利な vmtouch というツールがあったのでご紹介。
導入方法
導入はいたって簡単。
$ git clone https://github.com/hoytech/vmtouch.git
$ cd vmtouch
$ make
$ sudo make install
ちょっと調査で使いたいだけなら make install
はやらなくてもOKです。インストールした場合は /usr/local/bin
に実行ファイルがインストールされてmanも利用できるようになります。
基本的な使い方
まずは動作の確認に使うファイルを作成します。90000行です。
$ for n in `seq 10000 99999`;do echo $n;done > file1 $ ls -l file1 -rw-rw-r-- 1 vagrant vagrant 540000 Jun 30 06:52 file1 $ wc -l file1 90000 file1
-v
コマンドでキャッシュ状況を確認できます。
キャッシュに乗っている部分割合が O
や o
で表されていて視覚的にもわかるようになっています。 このファイルは 132Page * 4K = 528K
で現時点では全部のページがキャッシュに乗っています。
$ vmtouch -v file1 file1 [OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO] 132/132 Files: 1 Directories: 0 Resident Pages: 132/132 528K/528K 100% Elapsed: 0.00016 seconds
では、キャッシュ落としてみます。
# echo 1 > /proc/sys/vm/drop_caches
全部がキャッシュから落ちました。
$ vmtouch -v file1 file1 [ ] 0/132 Files: 1 Directories: 0 Resident Pages: 0/132 0/528K 0% Elapsed: 0.000162 seconds
少し読んでみるとコマンドの結果も変わってきます。
$ head -n 20 file1 > /dev/null $ vmtouch -v file1 file1 [OOo ] 8/132 Files: 1 Directories: 0 Resident Pages: 8/132 32K/528K 6.06% Elapsed: 0.000112 seconds
vmtouchという名前がついているくらいなので -t
オプションをつけるとファイルにタッチしてキャッシュに乗っけてくれます。
$ vmtouch -tv file1 file1 [OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO] 132/132 Files: 1 Directories: 0 Touched Pages: 132 (528K) Elapsed: 0.006956 seconds
このように一括で確認もできてかなり便利。
$ vmtouch -v ./file* ./file1 [OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO] 132/132 ./file2 [OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO] 132/132 ./file3 [OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO] 132/132 ./file4 [OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO] 132/132 Files: 4 Directories: 0 Resident Pages: 528/528 2M/2M 100% Elapsed: 0.000431 seconds
man と オプション
他にも色々機能ありますが。manも準備されているのでそちらで詳細見られます。
$ man vmtouch
$ man ./vmtouch.8 # インストールしていない場合
オプションは以下の通り。
$ ./vmtouch ./vmtouch: no files or directories specified vmtouch v1.3.0 - the Virtual Memory Toucher by Doug Hoyte Portable file system cache diagnostics and control Usage: vmtouch [OPTIONS] ... FILES OR DIRECTORIES ... Options: -t touch pages into memory -e evict pages from memory -l lock pages in physical memory with mlock(2) -L lock pages in physical memory with 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 -w wait until all pages are locked (only useful together with -d) -v verbose -q quiet