LinuxCommandLibrary

vmtouch

Force files into system memory (page cache)

TLDR

Print the cache status of a file

$ vmtouch [path/to/file]
copy

Load a file into cache
$ vmtouch -t [path/to/file]
copy

Evict a file from cache
$ vmtouch -e [path/to/file]
copy

Lock a file in cache to prevent eviction from memory
$ vmtouch -l [path/to/file]
copy

Lock a file and daemonize the program
$ vmtouch -ld [path/to/file]
copy

SYNOPSIS

vmtouch [options] FILE...

PARAMETERS

-t, --touch
    Load file(s) into the page cache. This is the default action if no other action option is specified.

-e, --evict
    Evict (remove) file(s) from the page cache, freeing up memory.

-d, --daemon
    Run vmtouch as a daemon process, continuously monitoring and keeping the specified files in cache. Less commonly used.

-v, --verbose
    Enable verbose output, showing statistics about cache hits, misses, and overall status.

-q, --quiet
    Suppress all output except for error messages.

-m, --max-threads
    Specify the maximum number of threads vmtouch can use for I/O operations, improving performance on multi-core systems.

-p, --percentage
    When used with -v, reports only the percentage of the file(s) currently in cache, rather than full statistics.

-h, --help
    Display a brief help message and exit.

-V, --version
    Display version information and exit.

DESCRIPTION

vmtouch is a command-line utility for interacting with the Linux kernel's file system page cache. Its primary function is to "touch" or load the entire contents of specified files or directories into memory, making subsequent reads from these files significantly faster as they are served directly from RAM rather than requiring slower disk I/O. This is achieved by reading the files efficiently without buffering them in user-space.

The utility is highly valuable for performance optimization, such as warming up caches for frequently accessed data in database systems, content delivery networks, or virtual machine images. It helps ensure critical data is resident in memory, especially after a system reboot or during periods of high memory pressure that might lead to cache eviction. Beyond loading, vmtouch can also report on the cache status of files (how much is currently cached) or explicitly evict files from the cache, providing administrators granular control over system memory utilization related to file caching. Its straightforward interface makes it a go-to tool for preloading data and improving application responsiveness by minimizing disk access latency.

CAVEATS

Root Privileges: vmtouch often requires root privileges (or the CAP_SYS_ADMIN capability) to effectively manipulate the page cache, especially for eviction operations (-e).

Temporary Cache: Loading files into the cache does not guarantee they will remain there indefinitely. The kernel's cache eviction policies will eventually remove less recently used or lower-priority pages if system memory pressure arises.

Memory Consumption: Using vmtouch to load large files or directories can consume significant amounts of RAM, potentially impacting other processes if the system is memory-constrained.

CACHING MECHANISM

vmtouch efficiently populates the page cache by internally utilizing system calls such as mincore(2) to check cache status, madvise(2) with flags like MADV_WILLNEED or MADV_DONTNEED for hinting, and reading data using read(2) into a temporary buffer to force it into memory. For eviction, it often leverages posix_fadvise(2) with POSIX_FADV_DONTNEED.

DIRECTORY HANDLING

When a directory is specified as an argument, vmtouch recursively processes all files and subdirectories within it. This feature is particularly convenient for warming up entire datasets or application installations with a single command.

HISTORY

vmtouch has been a valuable tool in the Linux ecosystem for system administrators and performance engineers since its inception. It was developed to provide a dedicated and efficient way to manage the page cache, distinguishing itself from general-purpose utilities like cat or dd that involve user-space buffering overhead. Its utility for warming up application data and optimizing I/O performance has made it a common inclusion in performance tuning guides and benchmark setups, particularly for database servers and high-throughput file systems.

SEE ALSO

sync(1), free(1), cat(1), dd(1), drop_caches (procfs mechanism), cachestat(8)

Copied to clipboard