Page Cache Trace
非direct读取/写入文件时, 文件内容会缓存到Page Cache中. 用多个命令看cache统计:
~$ free -g total used free shared buff/cache available Mem: 15 7 1 1 5 5 Swap: 0 0 0 ~$ cat /proc/meminfo | grep Cached Cached: 3869192 kB SwapCached: 0 kB
mmap, tmpfs, file, shm 会占用Page Cache. 文件删除时, Page Cache自动回收. 内存紧张时, 同步回收(direct reclaim). 也可通过写入 /proc/sys/vm/drop_caches
手动回收.
mmap占用的Page Cache, 在munmap之前, 无法回收. tmpfs 文件占有的Page Cache, 文件删除之前, 无法回收.
Page Cache是系统级别的. cgroup内, 有自己的cache. 那当不同cgroup进程访问同一个文件, 是单个cgroup统计cache, 还是两边都统计?
pmap, smem用来分析内存使用情况:
~$ smem PID User Command Swap USS PSS RSS 7401 yanyg /home/yanyg/bin/emacsc -t R 0 96 114 1424 3112 yanyg /home/yanyg/bin/emacsc -t 0 96 118 1544 19712 yanyg cat 0 88 121 1756 19711 yanyg cat 0 88 126 1732 21062 yanyg sh start.sh 0 200 203 1436
1 page cache tools
1.1 vmtouch
Github: https://github.com/hoytech/vmtouch
Code: https://github.com/hoytech/vmtouch/blob/master/vmtouch.c
基于 mmap
和 mincore
实现.
mincore() returns a vector that indicates whether pages of the calling process's virtual memory are resident in core (RAM), and so will not cause a disk access (page fault) if referenced. The kernel returns residency information about the pages starting at the address addr, and continuing for length bytes.
example:
~$ git clone https://github.com/hoytech/vmtouch.git ~$ cd vmtouch ~$ make && sudo make install ~$ vmtouch test.img Files: 1 Directories: 0 Resident Pages: 0/320 0/1M 0% Elapsed: 0.000293 seconds ~$ cat test.img >/dev/null ~$ vmtouch test.img Files: 1 Directories: 0 Resident Pages: 320/320 1M/1M 100% Elapsed: 0.0001 seconds ~$ vmtouch /var/ 2>/dev/null Files: 15756 Directories: 686 Resident Pages: 1162109/1162110 4G/4G 100% Elapsed: 0.16369 seconds