1. Caches in Memory
When we talk about memory caches, we mainly refer to buffers and caches, which can be viewed using the free command.
# Note that the output of free may vary between different versions
$ free
total used free shared buff/cache available
Mem: 8169348 263524 6875352 668 1030472 7611064
Swap: 0 0 0
Why are caches divided into buffers and caches? In fact, the data cached by these two is different.
- A buffer is used by programs to directly access raw disks; disks are block devices, and data is cached when reading and writing disk data.
- A cache requires interaction with the filesystem, which is responsible for communicating with the disk. The raw disk must be formatted (e.g., nfs, ext, xfs, etc.) and then mounted to a file directory before data can be read and written to the cache area.
2. Cache Metrics
The effective utilization of caches is primarily determined by the cache hit rate. A higher hit rate indicates better cache utilization and higher application efficiency.Buffers and caches significantly impact system performance, greatly enhancing disk I/O performance and acting as a bridge. Disk read and write operations are generally slow, while memory is very fast. During program execution, data is typically first read into the cache, then swapped, and finally written back to the disk.To release the cache, you can use the following command:
# Clear cache
$ echo 3 > /proc/sys/vm/drop_caches
3. Tools for Analyzing Caches
1) cachestat
root@test:~# cachestat 1 3
HITS MISSES DIRTIES HITRATIO BUFFERS_MB CACHED_MB
14076 0 0 100.00% 1084 484807
0 0 0 0.00% 1084 484807
0 0 0 0.00% 1084 484807
cachestat is a tool used to monitor the page cache hit rate in Linux systems. The parameters 1 3 indicate that samples are taken every 1 second, for a total of 3 samples.
- HITS indicates the number of cache hits;
- MISSES indicates the number of cache misses;
- DIRTIES indicates the number of dirty pages added to the cache;
- HITRATIO indicates the hit rate;
- BUFFERS_MB indicates the size of Buffers in MB;
- CACHED_MB indicates the size of Cache in MB.
2) cachetop
17:56:37 Buffers MB: 1084 / Cached MB: 484808 / Sort: HITS / Order: ascending
PID UID CMD HITS MISSES DIRTIES READ_HIT% WRITE_HIT%
12210 root pg_watcher_serv 15 0 0 100.0% 0.0%
14835 root cachetop 31 0 0 100.0% 0.0%
14880 root ping 359 0 0 100.0% 0.0%
14879 root ping 376 0 0 100.0% 0.0%
14878 root ping 418 0 0 100.0% 0.0%
14881 root nc 481 0 0 100.0% 0.0%
cachetop displays the cache hit status for each process.PID refers to the process ID.HITS, MISSES, and DIRTIES have the same meanings as in cachestat.READ_HIT and WRITE_HIT indicate the cache hit rates for reads and writes, respectively.
Both of these commands need to be installed:
sudo apt install -y bcc-tools