Memory caching in linux

Memory caching in linux

Buffer is a buffer, and Cache is a cache, both of which are temporary storage in data in memory. So, do you know what is the difference between these two «temporary storage»?

1. Source of Free data

man free buffers Memory used by kernel buffers (Buffers in /proc/meminfo) cache Memory used by the page cache and slabs (Cached and SReclaimable in /proc/meminfo) buff/cache Sum of buffers and cache 

From Free’s manual, you can see the instructions for Buffer and Cache.

  • Buffers It is the memory used by the kernel buffer, corresponding is /proc/meminfo The buffers value.
  • Cache It is the memory used by the kernel cache and the SLAB, corresponding is /proc/meminfo middle Cached and SReclaimable Sum.

2. PROC file system

/ PROC is a special file system provided by the Linux kernel, which is an interface to the user with kernel. For example, users can query the running status and configuration options of the kernel from / proc, query the running status, statistics, etc. of the process, of course, you can also modify the kernel through / proc.

The PROC file system is also the final data source of many performance tools. For example, what we just saw, is read by reading /proc/meminfo , Get the usage of memory.

implement man proc You can get a detailed documentation of the Proc file system
Search (such as searching MemInfo) so as quickly orientation to the memory section.

Buffers %lu Relatively temporary storage for raw disk blocks that shouldn't get tremendously large (20MB or so). Cached %lu In-memory cache for files read from the disk (the page cache). Doesn't include SwapCached. ... SReclaimable %lu (since Linux 2.6.19) Part of Slab, that might be reclaimed, such as caches. SUnreclaim %lu (since Linux 2.6.19) Part of Slab, that cannot be reclaimed on memory pressure. 

Through this document, we can see:

  • Buffers It is a temporary storage for the original disk block, that is, the data used to cache the disk, usually not particularly large (20MB
    about). In this way, the kernel can concentrate the dispersed write, uniformly optimize the write to the disk, such as the multiple small write to merge into a single large write, etc.
  • CachedIt is a page buffer read from the disk, which is used to cache data read from the file. In this way, when you access these file data next time, you can quickly get it directly from the memory, without having to access a slow disk again.
  • SReclaimableIt is part of the SLAB. SLAB includes two parts, which can be recovered with SRECLAIMABLE (not recycled, using SunReclaim).

The first question, Buffer’s document does not mention this is a disk read data or a write data, and Buffer will be mentioned in many network search results, which is just a cache that will be written to disk data. That contrary, will it also cach the data read from the disk?

Second question, in the document, cache is a cache to read data from the file, then it is also the data of write files?

Читайте также:  Pascal dos для linux

3. Case

Your preparation
Ubuntu 18.04
Machine configuration: 2 CPU, 8GB memory.
Pre-installed SysStat package, such as Apt Install Sysstat

Install sysstat because we have to use VMSTAT to observe the changes in Buffer and Cache. Although the same results can be read from / proc / meminfo, it is more intuitive.

In addition, these cases use DD to simulate I / O of disk and files, so we also need to observe changes in I / O

In the first terminal, run the following command.Clean up system cache

# , directory item, inodes, etc. $ echo 3 > /proc/sys/vm/drop_caches 

here /proc/sys/vm/drop_caches It is an example of modifying the kernel behavior through the Proc file system, and writes 3 to the cleaning file page, directory item, inodes and other caches.

3.1 Scene 1: Disk and file writing case

First, in the first terminal, run the following VMSTAT command:

# $ vmstat 1 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 0 7743608 1112 92168 0 0 0 0 52 152 0 1 100 0 0 0 0 0 7743608 1112 92168 0 0 0 0 36 92 0 0 100 0 0 

In the output interface, the BUFF and Cache of the memory section, and the BI and BO of the IO part are the focus we have to pay attention.

  • Buff and cache are the buffers and cache we have seen in front, and the unit is KB.
  • BI and BO respectively indicate the size of the block device read and write, in blocks per second. Because the size of the block in Linux is 1KB, this unit is equivalent to Kb / s.
    Second terminal execution dd Command, generate one by reading a random device 500MB Size file:
$ dd if=/dev/urandom of=/tmp/file bs=1M count=500 

Then return to the first terminal, observe the changes of Buffer and Cache:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 0 7499460 1344 230484 0 0 0 0 29 145 0 0 100 0 0 1 0 0 7338088 1752 390512 0 0 488 0 39 558 0 47 53 0 0 1 0 0 7158872 1752 568800 0 0 0 4 30 376 1 50 49 0 0 1 0 0 6980308 1752 747860 0 0 0 0 24 360 0 50 50 0 0 0 0 0 6977448 1752 752072 0 0 0 0 29 138 0 0 100 0 0 0 0 0 6977440 1760 752080 0 0 0 152 42 212 0 1 99 1 0 ... 0 1 0 6977216 1768 752104 0 0 4 122880 33 234 0 1 51 49 0 0 1 0 6977440 1768 752108 0 0 0 10240 38 196 0 0 50 50 0 

By observing the output of Vmstat, we found that Cache is constantly increasing when the DD command is running, while Buffer is basically unchanged. Further observe the case of I / O, you will see,

  • When Cache has just started growth, the block device I / O is very small, BI only has a 488 kb / s, and Bo is only once.
    4KB. After a period of time, a large number of blocks will be written, such as BO turning 122880.
  • When the DD command ends, cache will no longer grow, but the block device write will last for a while, and the results of multiple I / O writes are added to the 500m data written by DD.

To make a comparison with the definition of Cache we just learned, you may have a bit dizzy. Why do cache is a file read by a file read in front document, how can it be written now? This question, we are temporarily remember, then come back to another disk written case. After the end of the two cases, we will reunitely analyze.

Читайте также:  Linux создать консольное приложение

However, for the next case, I must emphasize a little: The following command is high, you need your system to configure multiple disks, and disk partition /dev/sdb1 It is also necessary to use unused state. If you only have a disk, don’t try, otherwise it will cause damage to your disk partition.

# First, clean the cache $ echo 3 > /proc/sys/vm/drop_caches # Then run the DD command to write 2G data to disk partition / dev / sdb1 $ dd if=/dev/urandom of=/dev/sdb1 bs=1M count=2048 

Then, go back to the terminal 1. Observe the changes in memory and I / O:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 7584780 153592 97436 0 0 684 0 31 423 1 48 50 2 0 1 0 0 7418580 315384 101668 0 0 0 0 32 144 0 50 50 0 0 1 0 0 7253664 475844 106208 0 0 0 0 20 137 0 50 50 0 0 1 0 0 7093352 631800 110520 0 0 0 0 23 223 0 50 50 0 0 1 1 0 6930056 790520 114980 0 0 0 12804 23 168 0 50 42 9 0 1 0 0 6757204 949240 119396 0 0 0 183804 24 191 0 53 26 21 0 1 1 0 6591516 1107960 123840 0 0 0 77316 22 232 0 52 16 33 0 

From here you will see, although it is written data, the phenomenon of writing disk keeps files is still different. When writing disks (that is, BO greater than 0), Buffer and Cache are growing, but it is clear that Buffer grows much faster. This shows that the write disk uses a lot of buffer, which is the same as the definition we find in the document.

Compare two cases, we found that Cache cache data is used when writing files, while writing the disk will be used to cache data. So, returning to the question, although the document is only mentioned, cache is a cache read by the file, but in fact, Cache also caches the data when writing files.

3.2 Scene 2: Disk and file read cases

Understand the situation written by disk and files, what is the time when we think, disk and file read? Let’s go back to the second terminal and run the following command. After cleaning the cache, from the file /tmp/file In the middle, read the data write in an empty device:

# First, clean the cache $ echo 3 > /proc/sys/vm/drop_caches # Run the DD command to read file data $ dd if=/tmp/file of=/dev/null 

Then, go back to the terminal 1. Observe the changes in memory and I / O:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 1 0 7724164 2380 110844 0 0 16576 0 62 360 2 2 76 21 0 0 1 0 7691544 2380 143472 0 0 32640 0 46 439 1 3 50 46 0 0 1 0 7658736 2380 176204 0 0 32640 0 54 407 1 4 50 46 0 0 1 0 7626052 2380 208908 0 0 32640 40 44 422 2 2 50 46 0 

Observe the output of VMSTAT, you will find that when reading the file (that is, Bi is greater than 0), the buffer remains unchanged, and Cache is constantly growing. This is consistent with the definition of our finding «Cache is a page that reads the file». So what is the disk reading? Let’s run the second case to see.

So what is the disk reading? Let’s run the second case to see. First, return to the second terminal, run the following command. After cleaning the cache, read data from disk partition / dev / sda1, write an empty device:

# First, clean the cache $ echo 3 > /proc/sys/vm/drop_caches # d Read the file $ dd if=/dev/sda1 of=/dev/null bs=1M count=1024 

Then, go back to the terminal 1. Observe the changes in memory and I / O:

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 0 7225880 2716 608184 0 0 0 0 48 159 0 0 100 0 0 0 1 0 7199420 28644 608228 0 0 25928 0 60 252 0 1 65 35 0 0 1 0 7167092 60900 608312 0 0 32256 0 54 269 0 1 50 49 0 0 1 0 7134416 93572 608376 0 0 32672 0 53 253 0 0 51 49 0 0 1 0 7101484 126320 608480 0 0 32748 0 80 414 0 1 50 49 0 

Observe the output of VMSTAT, you will find that when you read the disk (that is, Bi is greater than 0), Buffer and Cache are growing, but it is clear that the buffer grows much faster. This illustrates that the data is cached in the buffer when reading the disk.

Читайте также:  Linux all command line arguments

Of course, I think, after the analysis of two cases in the last scene, you can also compare this conclusion:Data can be cached in Cache when reading files, and the data is cacked in the buffer when reading the disk.

Here you should find it, although documentation provides instructions for Buffer and Cache, but still cannot overwrite all details. For example, today we have learned these two points:

  • Buffer can be used as «a cache that will be written to disk data, or it can be used as a» cache read data from disk «.
  • Cache can be used as «read data from files», or it can be used as a «write file page cache».

simply put,Buffer is a cache for disk data, and cache is a cache of file data, which will be used both in a read request.

4. Summary

We have explored the detailed meaning of Buffer and Cache in memory performance. Buffer and Cache cache read and write data for disk and file systems, respectively.

  • From the perspective of writing, not only optimize the write to the disk and files, but also the application is also good, the application can return to do other work before the data is really launched.
  • From a read perspective, it is possible to accelerate the data that requires frequent access, also reduces the pressure of frequent I / O on the disk.

Regarding the difference between disks and documents, I thought everyone understood, so there is no preference. Disk is a block device that can be divided into different partitions; create a file system over the partition, mount to a directory, and then read and write files in this directory.

In fact, «all documents» in Linux, and «file» mentioned in the article is a normal file, the disk is a block device file, which can perform «LS -L » to see the differences (if not the meaning of the output Please ask MAN LS to query).

When reading and writing ordinary documents, the file system is passed, and the file system is responsible for interacting with disk; and when reading and writing a disk or partition, it will skip the file system, which is the so-called «naked I / O». The caching used by these two read and write modes is different, which is the Cache and Buffer differences mentioned in the article.

With regard to the principle of file system, disk, I / O, don’t worry, the back I / O module will also speak.

Источник

Оцените статью
Adblock
detector